IronPython Samples

Change property

The following snippets show how a change property command (in this case, to change the color of a cone) looks in VBScript and its two possible IronPython variants.

oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab",_

Array("NAME:PropServers", "Cone1"), _

Array("NAME:ChangedProps", _

Array("NAME:Color", "R:=", 255, "G:=", 255, "B:=", 0))))

Sample Script 13: ChangeProperty command to change color of a cone in VBScript

oEditor.ChangeProperty(

["NAME:AllTabs",

["NAME:Geometry3DAttributeTab",

["NAME:PropServers", "Cone1"],

["NAME:ChangedProps",

["NAME:Color", "R:=", 0, "G:=", 0, "B:=", 64]

]

]

])

Sample Script 14: ChangeProperty command to change color of cone using Python arrays

Any time there are named arrays composed purely of key-value pairs, they can always be represented using a Python dictionary, irrespective of the nesting of said named array.

oEditor.ChangeProperty(

["NAME:AllTabs",

["NAME:Geometry3DAttributeTab",

["NAME:PropServers", "Cone1"],

["NAME:ChangedProps",

{

"NAME":"Color",

"R" : 0,

"G" : 64,

"B" : 0

}]]

])

Sample Script 15: ChangeProperty command to change the color of a cone using Python arrays and dictionaries

Create a Cone using IronPython

Most scripting tasks using IronPython are expected to be formatted as the following example. One starts with the predefined oDesktop object and drills down to the design, editors, modules etc and issues any required commands on the object while formatting the script command arguments in natural python syntax.

oProject = oDesktop.GetActiveProject()

oDesign = oProject.InsertDesign("HFSS","Random","DrivenModal","")

oEditor = oDesign.SetActiveEditor("3D Modeler")

oEditor.CreateCone(

{

"NAME" : "ConeParameters",

"XCenter" : "0mm",

"YCenter" : "0mm",

"ZCenter" : "0mm",

"WhichAxis" : "Z",

"Height" : "2mm",

"BottomRadius" : "1.56204993518133mm",

"TopRadius" : "0mm"

},

{

"NAME" : "Attributes",

"Name" : "Cone1",

"Flags" : "",

"Color" : "(132 132 193)",

"Transparency" : 0,

"PartCoordinateSystem": "Global",

"UDMId" : "",

"MaterialValue" : "\"vacuum\"",

"SolveInside" : True

}

)

Sample Script 16: IronPython script to create a cone

Create geometry and then create a grid from it using copy/paste/move

The following script demonstrates slightly more advanced use of scripting and the use of return values from script methods. It creates a 5x5 grid of cones and also demonstrates the adding of information messages to the application's message window.

oProject = oDesktop.GetActiveProject()

oDesign = oProject.InsertDesign("HFSS","Hersheys Kisses","DrivenModal","")

oEditor = oDesign.SetActiveEditor("3D Modeler")

 

# create the first cone

AddInfoMessage("Creating first cone")

firstConeName = "firstCone"

coneBotRad = "1.5mm"

oEditor.CreateCone(

{

"NAME" : "ConeParameters",

"XCenter" : "0mm",

"YCenter" : "0mm",

"ZCenter" : "0mm",

"WhichAxis" : "Z",

"Height" : "2mm",

"BottomRadius": coneBotRad,

"TopRadius" : "0mm"

},

{

"NAME" : "Attributes",

"Name" : firstConeName,

"Flags" : "",

"Color" : "(132 132 193)",

"Transparency" : 0,

"PartCoordinateSystem": "Global",

"UDMId" : "",

"MaterialValue" : "\"vacuum\"",

"SolveInside" : True

}

)

 

# Now replicate this a few times and create an array out of it

AddInfoMessage("Replicating it 24 times")

for x in range(5):

for y in range(5):

# leave the first one alone in it's created

# position

if x == 0 and y == 0:

continue

 

# all other grid positions, replicate from the

# first one

 

# copy first

oEditor.Copy(

{

"NAME" : "Selections",

"Selections" : firstConeName

}

)

 

# paste it and capture the pasted name

# the pasted names come in an array as we could

# be pasting a selection cmposed of multiple objects

pasteName = oEditor.Paste()[0]

 

# now move the pasted item to it's final position

oEditor.Move(

{

"NAME" : "Selections",

"Selections" : pasteName

},

{

"NAME" : "TransalateParameters",

"CoordinateSystemID" : -1,

"TranslateVectorX" : "%d * 3 * %s" % (x, coneBotRad),

"TranslateVectorY" : "%d * 3 * %s" % (y, coneBotRad),

"TranslateVectorZ" : "0mm"

}

)

 

# Now fit the display to the created grid

oEditor.FitAll()

Sample Script 17: Sample script to create a cone and then use copy/paste/move to replicate it.

Related Topics 

Introduction to IronPython

IronPython Mini-cookbook

Translating Script commands from VBScript to IronPython

Scripting Using Iron Python: Putting it all Together