How to have ACT toolbar button create both parent and children objects?
In the Mechanical application, when the "Model" object is selected in the tree, the model ribbon tab allows the user to select "Named Selection". If the parent Named Selections object does not already exist in the tree, clicking this button will create the parent object and a child named slection object. If the parent object already exists, then this button just creates a named selection object. There are other examples of this in Mechanical, such as cross sections, and contacts (depending on what is currently selected in the tree).
How can we replicate this behaviour in an ACT extension toolbar?
I understand we can use to define functions for any toolbar button.
<callbacks> <onclick>createChildObject</onclick> </callbacks>
What would be the python code that checks for the presence of the parent object, and creates it if it doesn't exist?
Answers
-
I was able to figure this out as per the code below. Interested to see if there is a cleaner or more robust implementation.
MyParent=None def CanAdd_Parent(ParentObj,ObjName): return (MyParent==None) def CanDuplicate_Parent(Entity,Parent): return False class Parent: def __init__(self,ExtAPI,AnsysObj): self.AnsysObj = AnsysObj self.GeoModel = None return def oninit(self,Me): global MyParent; MyParent=Me def onremove(self,Me): global MyParent; MyParent=None def onshow(self,Me): pass class Child: def __init__(self,ExtAPI,AnsysObj): self.AnsysObj = AnsysObj self.ChildData= {} self.Graphics = [] # Function to create both objects at once if no parent exists. # Creates child object if parent already exists in the tree. def CreateParentChild(obj): results = [] if MyParent== None: ExtAPI.Log.WriteMessage("Create ParentName Object...") ParentObj = DataModel.CreateChild("ParentName", ExtAPI.ExtensionManager.CurrentExtension, DataModel) for child in ExtAPI.DataModel.GetUserObjects(ExtAPI.ExtensionManager.CurrentExtension): if child.ToString() == "ParentName@ExtensionName": results.append(child) ParentObj = results[0] ExtAPI.Log.WriteMessage("Create ChildName Object...") ChildObj = ParentObj.CreateChild("ChildName")
0