The purpose of this chapter is to share the limited knowledge I have in plugin creation. I've only managed to get it going in Visual Basic so far. Please help me with Delphi if you can.
In many ways a Visual Basic plugin is the same as a VBScript. Here's a summary of my discoveries so far:
Can you remember why you wanted to write a plugin? To me the most obvious reason is the ease of input into the object creation routines. For example in the Swarm script there are at least four inputs required to describe the swarm. These can be entered by querying the user with InputBox statements, or as I did using separate subroutines with some commented out when they are not used.
Using a plugin they can all be entered on one form. This is how it looks in a Visual Basic form:
Call the form frmSwarmData. Typically one of the opening statements in the plugin would call that form. The user would enter the data they require, and then object creation would begin.
The main trick is getting the data between forms when you can't use global variables. This code shows how it is done for the SwarmRadius value. I've added a property called SwarmRadius to the SwarmData form. You need to set the data using both a Property Get and Property Let subroutines.
Private mintSwarmRadius As Single Private Sub Command1_Click() SwarmRadius = frmSwarmData.Text1.Text '*******the value in a box on the form is given to SwarmRadius when Unload Me ' the Go for It button is clicked End Sub Public Property Get SwarmRadius() As Single SwarmRadius = mintSwarmRadius End Property Public Property Let SwarmRadius(ByVal NewValue As Single) mintSwarmRadius = NewValue End Property
Back in the main Class module where the work is done, you can access the SwarmData from the form by using this:
frmSwarmData.Show vbModal S.SwarmRadius = frmSwarmData.SwarmRadius
One of the sample plugins with 3D Canvas is called NameIt. You have to compile this yourself, and when you do you'll find that it opens a window in the 3D Canvas screen from which you can select an object. The NameIt plugin just allows you to rename the selected object.
In the Swarm plugin I've used this to allow selection of a pallet object. The plugin calls the NameIt form, then the code calls Object.GetID for the selected object. This ID number is passed back to the main code. There is a trick, however.
The Object's ID is not the same as the Object's Index in the Object Database. The ID is an integer assigned when the object is created. If objects previously created are then deleted, the ID of the object remains the same. However, the objects position will be changed in the database so there are no empty entries in the list of 3DCanvas objects.
So if the ID of the object as returned by the Object.GetID is IDofMaterialObject, and you want to find the Index in the database (variable called IndexofMaterialObject), do this:
IndexofMaterialObject = 0
TempID = 1
While TempID <> IDofMaterialObject
Set TempObject = Scene.GetObject(IndexofMaterialObject)
TempID = TempObject.GetID
'msgbox ("TempID" & TempID & "IndexMat" & IndexofMaterialObject)
If TempID <> IDofMaterialObject Then IndexofMaterialObject = IndexofMaterialObject + 1
Wend
That's all I've got to say on this for the moment. You can study my example by downloading this archive. It contains all the Visual Basic files necessary for the project, and the compiled dll. The compiler creates three files: swarm.dll, swarm.lib, and swarm.exp. I think that only the swarm.dll file is necessary in the plugins directory. What are the others for?