API example: Creating multiple drops
New entities can be created through the Application Programming Interface (API). This example creates entities for a hot runner system gated to selected nodes, and then meshes the new entities. The numbers used in properties (five-digit constants in the example) can be found in the tcodes.dat and tcodeset.dat files in the data/dat directory under the product installation directory.
Note: The tcodeset reference can help you understand how to use properties in scripts.
'%RunPerInstance
'@
'@ DESCRIPTION
'@ This command will run all Models in the current directory
'@
'@
'@ SYNTAX
'@ CreateDrops
'@
'@ PARAMETERS
'@ none
'@
'@ DEPENDENCIES/LIMITATIONS
'@
'@ History
'@ Created DRA 9/8/2006
'@@
Option Explicit
SetLocale("en-us")
Dim SynergyGetter, Synergy
On Error Resume Next
Set SynergyGetter = GetObject(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%SAInstance%"))
On Error GoTo 0
If (Not IsEmpty(SynergyGetter)) Then
Set Synergy = SynergyGetter.GetSASynergy
Else
Set Synergy = CreateObject("synergy.Synergy")
End If
Synergy.SetUnits "METRIC"
Dim StudyDoc, PropEd, Modeler, MeshGenerator, Viewer
Dim SelectList, Prop, DVec, DVec1, HotGateCount, HotRunCount
Dim I, nodeCoord, BaseX, BaseY, BaseZ, Vector, Vector_1, EntList
Set StudyDoc = Synergy.StudyDoc()
If StudyDoc is Nothing Then
MsgBox "No Active Study",,"Error"
WScript.Quit
End If
Set SelectList = StudyDoc.Selection
If SelectList.Size = 0 Then
MsgBox "No Entities have been selected",,"Error"
WScript.Quit
End If
Set PropEd = Synergy.PropertyEditor()
Set Modeler = Synergy.Modeler()
' Create Properties for Gate
<em class="strong">HotGateCount = GetLastTsetID(40434) + 1
Set Prop = PropEd.CreateProperty(40434, HotGateCount, True)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 2
Prop.FieldValues 30212, DVec
Set DVec1 = Synergy.CreateDoubleArray()
DVec1.AddDouble 1.5
DVec1.AddDouble 5
Prop.FieldValues 30262, DVec1
PropEd.CommitChanges ""</em>
' Create Properties for Drop
<em class="strong">HotRunCount = GetLastTsetID(40430) + 1
Set Prop = PropEd.CreateProperty(40430, HotRunCount, True)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 10
Prop.FieldValues 30260, DVec
PropEd.CommitChanges ""</em>
' Loop Through Slected Nodes and create Gates and Drops
For I = 0 To SelectList.Size()-1
Dim Ent
Set Ent = SelectList.Entity(I)
Dim EntName
EntName = Ent.ConvertToString
<em class="strong">If ( Left(EntName, 1) = "N") Then
Set nodeCoord = StudyDoc.GetNodeCoord(Ent)
BaseX = nodeCoord.X
BaseY = nodeCoord.Y
BaseZ = nodeCoord.Z
Set Vector = Synergy.CreateVector()
Set Vector_1 = Synergy.CreateVector()
Set Modeler = Synergy.Modeler()
' Create Gate
Set Prop = Modeler.FindProperty(40434, HotGateCount)
Vector.SetXYZ BaseX, BaseY, BaseZ
Vector_1.SetXYZ 0, 0, 5
Set EntList = Modeler.CreateLine(Vector, Vector_1, True, Prop, True)
PropEd.SetProperty EntList, Prop
' Create Drop
Set Prop = Modeler.FindProperty(40430, HotRunCount)
Vector.SetXYZ BaseX, BaseY, BaseZ+5
Vector_1.SetXYZ 0, 0, 100
Set EntList = Modeler.CreateLine(Vector, Vector_1, True, Prop, True)
PropEd.SetProperty EntList, Prop
End If</em>
Next
' Save All Proerty Data
PropEd.CommitChanges "Assign"
' Mesh the new drops
<em class="strong">Set MeshGenerator = Synergy.MeshGenerator()
MeshGenerator.Generate</em>
' Display Updated Model
<em class="strong">Set Viewer = Synergy.Viewer()
Viewer.Fit</em>
' Exist Script
MsgBox "Script Complete"
WScript.Quit
' Function to Find Last Instance of a Tset in the Study File
Private Function GetLastTsetID(Tset)
GetLastTsetID = 0
Dim Project, PropEd
Set Project = Synergy.Project()
Set PropEd = Synergy.PropertyEditor()
Dim TestPropertyID
Set TestPropertyID = PropEd.GetFirstProperty(Tset)
While Not TestPropertyID Is Nothing
GetLastTsetID = TestPropertyID.ID
if TestPropertyID.ID > GetLastTsetID Then
GetLastTsetID = TestPropertyID.ID
End if
Set TestPropertyID = PropEd.GetNextPropertyOfType(TestPropertyID)
Wend
End Function