Upload
doanthuan
View
265
Download
6
Embed Size (px)
Citation preview
© 2011 ANSYS, Inc. May 26, 2014 1
Lecture 2 Advanced Scripting of DesignModeler
© 2011 ANSYS, Inc. May 26, 2014 2
Contents
• Supported API’s of DesignModeler
• Attaching Debugger to DesignModeler
• Advanced scripting API’s of DesignModeler
– Handlers
• Tree, Graphics, File, Event (Feature)
– List View details
• Finding command of DM example (File >> Import)
• Summary
© 2011 ANSYS, Inc. May 26, 2014 3
Documented Scripting API’s in DM
• Scripting language used – Jscript
• Also support xml and html for wizard creations
• Jscript can be read from File Run Script in DM
• Basic API’s are useful for
– Recording and Editing of sketch functions...
– Creating new Planes and Sketches
– Using features like extrude/ revolve to create geometries
• Above functionality is not sufficient to work with complicated models
– Ex: Using CAD import, clean the geometry and modify it for meshing in automated way?
© 2011 ANSYS, Inc. May 26, 2014 4
Use of Debugger?
Documented API’s are useful to create geometries which are simple in creation methodology. But for complicated geometries we need further commands to work with...
Is there any why we can get these commands to use them in scripts...?
• Yes, by attaching debugger to DesignModeler application – We can find internal commands used for different operations
• What operations can be done here – Geometry creation
– Modifications and Decomposition
– Geometry clean-up
© 2011 ANSYS, Inc. May 26, 2014 5
Attach Debugger to DesignModeler
• To see the script in action, attach debugger to the DesignModeler Process – Using Visual Studio Professional
• Approach: – Open DesignModeler – Start Visual Studio (VS) – Go to Tools Attach to Process... – Select the suitable
"AnsysWBU.exe” • Note that the "Script" option under
"Type" (column 4 in the window) is available
• If it is not picked up by default, try to use the "Select" option (button on the right of "Attach to" in the window)
Note: If other session of DesignModeler is already open, there may be multiple AnsysWBU.exe processes !
© 2011 ANSYS, Inc. May 26, 2014 6
Attach Debugger to DesignModeler (2)
• Press the "Attach" button
• In the VS window, select Debug Break All
• Go back to DesignModeler application window and click at any location
– This will pop up the VS window with the JScript in it
© 2011 ANSYS, Inc. May 26, 2014 7
Attach Debugger to DesignModeler (3)
• All events fired from DM are handled through “agEventHandler” function
• Search for agEventHandler function and attach a “Break Point”
• Press “Continue (F5) ”
– This will not do anything (for now)
• In DM session, create a Box primitive (as usual)
• This takes the control back to VS, where you have attached the break point
© 2011 ANSYS, Inc. May 26, 2014 8
Attach Debugger to DesignModeler (4)
• “Step Into (F11)” the function
– This will take to the function: agEventHandler (eventName, eventID)
• Add variables to Watch window to check
– eventName
– eventID
• Go line by line “Step Over (F10)” till you reach the function that uses the specific eventID
• “Step Into (F11)” the function
• Look for the specific case using this eventID
– In the case: agFeatureHandler (eventName, eventID)
© 2011 ANSYS, Inc. May 26, 2014 9
Attach Debugger to DesignModeler (5)
• “Step Into (F11)” the function
• Go line by line “Step Over (F10)” till you reach the specific case using this eventID
• Go line by line “Step Over (F10)” to see what more is happening inside the function – You will notice few more function calls used
You may like to add “feature” to the Watch window to see what it contains (just after you step over it)!
!
© 2011 ANSYS, Inc. May 26, 2014 10
Attach Debugger to DesignModeler (6)
• You may “Step Into (F11)” the functions to see what is happening there
• Add a Watch for ag.tree.SelectedItem
• Attributes of the selected Item can be accessed – Example:
• Text (Name of the Object)
• Visible state etc.
• Continue press “Step Over (F10)” till control goes back to DM session
Note: Item values are non editable using the above route. Feature manger “ag.fm” can be used to edit item values such as Name etc.. !
© 2011 ANSYS, Inc. May 26, 2014 11
Attach Debugger to DesignModeler (7)
• Select “Operation” in the list view item
– This will take control back to VS
• Add a Watch for ag.listview.SelectedItem
• Attributes of the selected Item can be accessed
– Example:
• Value
• When done, you can use “Continue (F5)”
• And detach the debugger
– Debug Detach All
Note: Item values are non editable using the above route. Slide-12 and 13 shows the way to edit these values.
!
© 2011 ANSYS, Inc. May 26, 2014 12
List view and Tree view
Functions for selecting list view items can be located in agListview.js file in AGPages/scripts Functions for selecting tree view items can be located in agTreeview.js file in AGPages/scripts
List view Items
• Items in list view provides the details of created features/nodes
Example:
• List view for primitives
• Changing the operation, changing the Base plane etc…
• Items in tree are features/object’s and these are referred to as nodes
Example:
• Select a plane and create a sketch
• Select Solid bodies for Boolean operation
• Collapse or expand tree nodes
Tree view Items
© 2011 ANSYS, Inc. May 26, 2014 13
List view (Editing)
Items Value
Note: ItemValue takes a string argument (even for number!) !
Each Item has a Value – Example:
• “Direction” is item and values could be “Normal” or “Reversed” or …
Sample Task: – Create an Extrusion operation and change the operation to “Add
Frozen”
– Steps:
• Create Extrude object
• Select Extrude object from tree
• Activate the item “Operation” in the list view – ag.listview.ActivateItem("Operation");
• Changing the item value – ag.listview.ItemValue = "Add Frozen";
• Create a JS macro file (e.g. listview_selection.js)
– Run the Macro
© 2011 ANSYS, Inc. May 26, 2014 14
List view (Editing)
Example:
ag.gui.CreateExtrusion(); ag.listview.ActivateItem( "Extrude" ); ag.listview.ItemValue = "Extrude_temp";
ag.listview.ActivateItem( "Geometry" ); select_Node("Sketch1"); //This is explained in later slides ag.listview.ItemValue = "Apply";
ag.listview.ActivateItem( "Operation" ); ag.listview.ItemValue = "Add Material";
ag.listview.ActivateItem( "Direction Vector" ); ag.listview.ItemValue = "Apply";
ag.listview.ActivateItem( "Direction" ); ag.listview.ItemValue = "Normal";
ag.listview.ActivateItem( "Extent Type" ); ag.listview.ItemValue = "Fixed";
ag.listview.ActivateItem( "FD1, Depth (>0)" ); ag.listview.ItemValue = 40;
ag.listview.ActivateItem( "As Thin/Surface?" ); ag.listview.ItemValue = "No";
ag.listview.ActivateItem( "Merge Topology?" ); ag.listview.ItemValue = "Yes"; agb.Regen();
© 2011 ANSYS, Inc. May 26, 2014 15
Recipe for Finding scripts for all GUI events
All GUI events can be located in agEventHandler.js file in in AGPages/scripts
GUI events are categorized with specific handlers
• agGraphicsHandler
• agFeatureHandler
• agFileHandler
• agTreeHandler
All functions in the script needs to be refereed using the “ag.*” route
• Examples:
• ag.gui.CreatePrimitive(4)
• ag.gui.ZoomFit()
• Alternatively, functions can be also called using the “ag.agApplet” route
– ag.agApplet.GuiModeler.CreatePrimitive(4)
– ag.agApplet.GuiModeler.ZoomFit()
© 2011 ANSYS, Inc. May 26, 2014 16
agGraphicsHandler
• All graphics related operations can be located in “agGraphicsHandler” function
• All methods related to graphics handling can be also referred using ag.agApplet.GuiModeler identifier
• Operations
– Rotate, Pan, Zoom, Boxzoom
– Fit, Undo view, Redo view
– Reset
Sample Task • Zoom fit the DM graphics window.
– Search for “fit” in “agGraphicsHandler” function
– Locate the function
• Create a JS macro file (e.g. zoomfit.js)
– Add a line:
• ag.gui.ZoomFit();
• OR: ag.agApplet.GuiModeler.ZoomFit();
• Save the file
• Run the JS macro file
– File Run Macro
© 2011 ANSYS, Inc. May 26, 2014 17
agFeatureHandler
• All operations related to creation of objects and creation of other features can be located in “agFeatureHandler” function
• Operations
– Creation of Primitive objects
– Body operations
– Boolean operations etc..
Sample Task • Create a Cylinder from primitive objects.
– Search for “cylinder” in “agFeatureHandler“ function
– Locate the relevant function
• Create a JS macro file (e.g. createCylinder.js)
– Add a line:
• ag.gui.CreatePrimitive(4) ;
• OR: ag.agApplet.GuiModeler.CreatePrimitive(4);
• Save the file
• Run the JS macro file
– File Run Macro
© 2011 ANSYS, Inc. May 26, 2014 18
agFeatureHandler (2)
Features can be Accessed/Modified using ag.agApplet.FeatureMgr identifier
• ag.agApplet.FeatureMgr.BodyCount
– Returns the number of bodies in the model
• ag.agApplet.FeatureMgr.FeatureCount
– Returns number of features in model
Example:
Change the Name of the Box Primitive
• var box = ag.agApplet.FeatureMgr.feature(i);
• box.Name = “Box1”;
Changing name of first body
• var body = ag.agApplet.FeatureMgr.Body(i);
• body.Name = “solid_plate”;
© 2011 ANSYS, Inc. May 26, 2014 19
Feature object/node Accessed using ag.agApplet.FeatureMgr
– Some useful methods…
box.isDeleteable(); Returns 1 if Feature is deleteable else 0
box.isEditable(); Returns 1 if Feature is editable else 0
box.IsGenerated(); Returns 1 if Feature is generated else 0
box.isSuppressed(); Returns 1 if Feature is suppressed else 0
box.isSuppressible(); Returns 1 if Feature is suppressible else 0
box.SetParameter(...); Command used to set the WB parameter
agFeatureHandler (3)
© 2011 ANSYS, Inc. May 26, 2014 20
Body Object Accessed using ag.agApplet.FeatureMgr
– Some useful methods…
body.getFrozenStatus(); Returns 0 if body is frozen
body.getMaterialName(); Returns material name assigned to body
body.getSuppression(); Returns 1 if body is suppressed
body.getVisibility(); Returns 1 if body is visible in graphics window
body.getCentroid(0); To get X, Y and Z centroid values pass 0, 1 and 2
body.setSuppression(true); To suppress body
body.setSuppression(false); To un-suppress body
body.setVisibility(true); To show body in graphics
body.setVisibility(false); To hide body in graphics
agFeatureHandler (4)
© 2011 ANSYS, Inc. May 26, 2014 21
agFileHandler
• All operations related to file menu can be located in “agFileHandler” function
• Operations
– File Save/Open
– Project Save/Open
Sample Task
• Save the workbench project.
– Search for “saveproject” in “agFileHandler” function
– Locate the relevant function, it is agInteropSaveProject()
© 2011 ANSYS, Inc. May 26, 2014 22
agFileHandler (2)
• All functions which have “interop” in their name can be located in agInterop.js file in in AGPages/scripts
Open agInterop.js search with string “agInteropSaveProject”
Create a JS macro file (e.g. saveproject.js)
• Add a line:
– ag.m.Interop.SaveProject();
– OR: ag.agApplet.Modeler.Interop.SaveProject()
– Save the file
• Run the JS macro file
– File Run Macro
© 2011 ANSYS, Inc. May 26, 2014 23
agTreeHandler
• All operations related to selection of objects or items in the DesignModeler tree
• Selection of plane
• Selection of bodies
• Adding new items to Tree
• Clearing all the items in the Tree
• Refreshing tree items for new additions
• Example: agTreeFastRefresh(), agTreeNodesClear(), agTreeUpdateNode()
Task: Select the “XYPlane” in the tree • Tree items are referred as “nodes” in the script
• Search for “selectnode” in “agTreeview.js”
• Locate the function “agTree_SelectNode”
• agTree_SelectNode function requires two arguments
– Node
– Multiflag (optional, default = false)
Note: Refreshing tree items is important when you add new items using the scripts !
© 2011 ANSYS, Inc. May 26, 2014 24
agTreeHandler (2)
To be able to select a specific node based on its name, a function can be written using the agTree_SelectNode function.
The “XYPlane” in the tree can be selected now as: selectNode(“XYPlane”)
Target = Name of the tree item for selection
Loop over all the nodes to find the target
!
If the name is not unique, it will select the 1st matching node in the tree
© 2011 ANSYS, Inc. May 26, 2014 25
SelectionMgr object in DM
ag.m.SelectionMgr
– Pointer to access selection manager
– Ex: • SM.ClearSelection() //Clears selection
• SM.IsSelectedFace() //Returns “1” if face is selected else “0”
• SM.IsSelectedFeature() //Returns “1” if feature is selected else “0”
• SM.IsSelectedSolidBody() //Returns “1” if solid body is selected else “0”
© 2011 ANSYS, Inc. May 26, 2014 26
Useful methods of ag.m
ag.m.BodiesExist(); //Returns “1” if bodies exist in active session
ag.m.GetBodySelListSize(); //Returns number of selected bodies
ag.m.GetBodySelListIndex(i); //Returns indexed Body object
ag.m.GetFaceSelListSize(); //Returns number of selected faces
ag.m.GetFaceSelListIndex(i); //Returns indexed Face object
ag.m.GetEdge3dSelListSize(); //Returns number of selected edges
ag.m.GetEdge3dSelListIndex(i); //Returns indexed Edge object
ag.m.HiddenBodiesExist(); //Returns “1” if any of the body is hidden
ag.m.HiddenFacesExist(); //Returns “1” if any of the face is hidden
© 2011 ANSYS, Inc. May 26, 2014 27
Useful methods of ag.m
ag.m.HideAllOtherBodies(); //Hides bodies which are not selected
ag.m.ShowAllBodies(); //Show all unsuppressed bodies
ag.m.HideAllOtherFaces(); //Hides faces which are not selected
ag.m.ShowAllFaces(); //Show all faces
ag.m.HideBody(); //Hides selected bodies
ag.m.ShowBody(); //Show selected body
ag.m.HideFaces(); //Hides selected faces
© 2011 ANSYS, Inc. May 26, 2014 28
Example: Importing External Geometry File
Using the debugger we will trace the function for importing external geometry
The main steps are:
• Attach the debugger to DM process
• Attach a Break Point at agEventHandler
• In DM, click on FileImport External Geometry
• Watch the “eventID” and proceed to the relevant functions
– In this case “eventID” is 1005
– The function call for this event is: agFileHanlder()
© 2011 ANSYS, Inc. May 26, 2014 29
Importing External Geometry (2)
• Enter into this function
– Locate the case for this eventID
– The function call for this event is: agMenuFileImport()
• Enter into this function
– You will notice that if the fileName is undefined, it pops up a file browser
© 2011 ANSYS, Inc. May 26, 2014 30
Importing External Geometry (3)
– Once a file is selected, it moves on to CreateImport(fName)
– Add watch to find out “fName” • “fName” is filename of the geometry file with absolute path
– This is the function we were looking for!
– Exit debugger
Create a java script file “cadimport.js” • Type the command ag.gui.CreateImport(path)
– OR, ag.agApplet.Script.agMenuFileImport(path) • Replace “path” with the filename and path
– Example: “D:\\Temp\\box.stp”
Run the java script in DM (File Run Script) • You will notice that a “Generate” call is required to complete the process
Automatic generation can be achieved by adding a line at the end of your file • agb.Regen()
All the functions available in AGPages/scripts folder can be called this way!
!
© 2011 ANSYS, Inc. May 26, 2014 31
Example: Accessing existing body and tracing its feature till edges
Using the feature manager we will trace attributes of existing body
• Attributes of faces
• Attributes of edges
The main steps are:
• Accessing the existing body
• Finding its label
• Selecting all faces of body
• Accessing individual attributes of faces
• Accessing information related to individual edges of faces
Note: When multiple bodies are present in the model, individual bodies or entities can be selected using the “Label” of the entities using ag.m.FindEntity(0, 0, Label, 0); Last argument is for multiple selection. Making it “1” will add to previous selection.
!
© 2011 ANSYS, Inc. May 26, 2014 32
Step-1 Accessing body and Finding its “Label”
Start with existing Model which has one body
Enquire number of bodies
• ag.agApplet.FeatureMgr.BodyCount
Access the existing body
• Use ag.agApplet.FeatureMgr.Body(0) to find all attributes of the body – Ex: ag.agApplet.FeatureMgr.Body(0).Label returns the label of body
– Ex: ag.agApplet.FeatureMgr.Body(0).faces returns the number of faces
• Store the Label – Bodies can be traced out using the “Label”
Alternatively refer the code below
Note: You can select and access body by it’s index when multiple bodies are present in the model
!
© 2011 ANSYS, Inc. May 26, 2014 33
Step-2 Accessing the faces/edges of selected body
Access the faces of the body • In order to access the faces of the body, we need to find the
“Label” of the body first
Steps the access faces • Clear existing selections • Selection the body with the help of stored label
– ag.m.FindEntity(0, 0, Label, 0); • To select all faces of selected body use following command
– ag.m.SelectionMgr.ShowTopologicalConnections(0);
Access the number of faces selected and store value using variable • var FC = ag.m.GetFaceSelListSize() • FC returns number of faces in selection manager
Access attributes of each face • var FC1 = ag.m.GetFaceSelListIndex(0) • FC1 returns attributes of 1st face • FC1.Label returns the Label of the face • FC1.Edges.Item(1) returns the attributes of 1st edge
Note: Index starts at 0 for ag.m.GetFaceSelListIndex() Index starts at 1 for Edges.Item() !
© 2011 ANSYS, Inc. May 26, 2014 34
Step-3 Accessing Edges from Selected face
Clear all selections and Select the face using it’s Label stored previously and hide all other faces
• To Selection the face with the help of stored label
– ag.m.FindEntity(0, 0, Label, 0);
• To select all edges of selected face use following command
– ag.m.SelectionMgr.ShowTopologicalConnections(0);
Access the Edge length and Label
• Length of edge can be access using ag.m.GetEdge3dSelListIndex(0).Length
• Edge Label can be accessed from ag.m.GetEdge3dSelListIndex(0).Label
© 2011 ANSYS, Inc. May 26, 2014 35
Summary
DesignModeler supports JScript for scripting/customization • All script files can be run from File Run Script
API’s that are documented can be located in the documentation: “//DesignModeler // Scripting API”
Other functions can be located in the “Aisol\AGP\AGPages\scripts”
Functions for GUI operations can be traced by attaching a debugger to the DM process
Almost all manual DesignModeler processes can be customized