Custom Tools
Provides a mechanism for Python scripts to access user input
- Keyboard data
- Choose from a list of options provided by your script
- Objects picked from the graphics view or model browser
Designed to match the standard Apex Tool behavior
- Tool palettes & Icon Buttons
- Tool Property Panels
- Apply/Cancel buttons
- Workflow Instructions
- Auto/Manual Tool Execution Mode
- Tool side pick filters
Accessing Custom Tools
Activate the checkbox for specific custom tool palettes from the Custom Tools page of Application Settings.

.jpg)
Example Custom Tool Files
Example custom tool scripts are provided and referred to below to illustrate key concepts in creating custom tools. You will notice hyperlinks to these examples from the appropriate sections below, and a complete list of these examples at the bottom of this page. For these examples, yellow highlighting is used to differentiate the key portions of the script that are relevant to the example being discussed as opposed to code that is required but has been discussed previously or is not directly relevant to the example.
Script for Custom Tools with UI
- One script creates the custom tool UI
- The other script contains the Apex business logic
UI Script
- Is a Python script
- Does not load or invoke the regular Apex scripting API
- Does load Microsoft modules that expose WPF
- Windows Presentation Foundation
- This is exactly the same code that the entire Apex UI uses
- The entire scope of the Microsoft WPF is immediately available for use…
What does this script look like?
- Required Module imports
Basic module imports - always include these:

- Required Function
def getUIContent(): #Configures built-in properties of the User Defined tool
- Optional functions (but required for most UI tools)
def getCustomToolPropertyContent(): #Builds the user defined UI content def setPickFilterTools(): #Configures the tool side pick filter
Configuring the Basic Tool Behavior
- The basic behaviors of a user defined tool are implemented in the apex_sdk module
-
Specifically in the apex_sdk.ToolPropertyContainer class
- Use the getUIContent()function in your UI script to configure the basic behaviors of your user defined tool
- Tool name
- Icon
- Enable/Disable auto-execute
- Enable/Disable tool side pick filter
- And load the “Custom” content
- Property panel UI
- Workflow instructions
- Tool side pick filter configuration

- No user defined content is provided - it's an empty tool
- To see what it does use the "empty_tool_ui.py" script in the following: Empty Tool Example
User defined contents of the property panel
- The user defined content and behavior of the user defined tool are defined using the getCustomToolPropertyContent() function
- This function usually contains pure WPF code
- Create labels, text boxes, check boxes, combo boxes and buttons
- Datagrids, trees, display images or movies – endless possibilities…..
- To define this region requires some understanding of the WPF object model and API
- Be warned – the WPF API is huge!!!!!
- The API is fully documented by Microsoft
- The documentation is for C# (not Python)
Basic UI Principles
- More complex tools usually require a way to layout the different UI Controls (aka Widgets)
- WPF provides a Grid class to help layout
- Grids can be configured with any number of rows and columns
- Add RowDefinition and ColumnDefinition to the Grid
- One for each row or column you want your Grid to have
- Controls are positioned within the grid using the row, column indices
- The row & column indices are set on the control
- The controls are then added to the Grid
- Easily align controls horizontally or vertically
- Controls can be configured to span multiple rows or columns
- These concepts are demonstrated in the following example: UI Layout Example
Tool name, tool icon, and pick filters
- Tool name and associated icon are defined using the TitleText and TitleImageUriString properties of the ToolPropertyContainer class
- Icons should be .png files, 32 x 32 pixels in size
- Tool side pick filters can be configured with any combination of pickable Apex object types
- Assemblies, Parts, Surfaces, Bodies, Nodes, Elements Connectors, etc.
- Also supports “Visible entity picking” and “Inclusive/Exclusive” box picking controls
- All pickable object types are defined in an apex_sdk.PickFilterTypes enum
- Enable the pick filter on the ToolPropertyContainer
- Constrict a List of PickFilterTypes you want to enable
- Register the List of Pick filters with the ToolPropertyContainer.PickFilterList property
- See the following example for how to access the picked objects in your script: Tool Name, Tool Icon, and Pick Filter Example
Accessing tool property data from your script
- The script that implements your Apex logic must be in a separate file
- It cannot use Classes or Methods in your UI file
- The most commonly used pattern for accessing data from your User Defined Tool is when the “Apply” button is clicked
- The ToolPropertyContainer class provides an AppliedCommand property that can be used to register a function that will be executed when the “Apply” button is clicked
- Use the following pattern to send data from the UI to your Apex API script
- Create a function in your UI script and associate it with the AppliedCommand property of the ToolPropertyContainer
- This function will be called when the Apply button is clicked
- Use this function to gather data from the user defined tool property panel controls
- Package the data into a Dictionary
- Call a function in your Apex API script
- This function must implement a method that accepts the Dictionary as input
- Create a function in your UI script and associate it with the AppliedCommand property of the ToolPropertyContainer
- This is demonstrated in the following example: Mesh All Bodies Example
Accessing selected entities from your script
- Previous examples showed how to configure tool side pick filters to control picking
- To access the selected entities from your script, use the apex.selection.getCurrentSelection() method in your Apex API script
- This method returns all of the currently selected entities in an EntityCollection
- This is demonstrated in the following example: Mesh Selected Bodies Example
Accessing files from a user defined tool
- User defined tools can configure and display the familiar windows “File” dialog
- Easily configurable
- Default folder location
- File type filters
- Open/Close/Save
- Single/Multiple file selection
Using the File Dialog in User Defined Tool
- Construct a instance of the OpenFileDialog class
- This class is the Microsoft.Win32 namespace so this has to be imported first
- Configure the dialog properties
- Single/Multi Select
- File Type Filter
- Default folder
- Display the dialog
- If a file/files is/are selected then file name(s) can be retrieved from the dialog
- This is demonstrated in the following example: Accessing files from User Defined Tool Example
Populating UI controls with content from your Apex API script
- Sometimes it’s useful to populate the User Defined Tool property panel dynamically
- Using model data for example
- The API supports transfer of data from the Apex API script to the UI script using the same Dictionary approach used to transfer data from the Tool Property panel to your script
- Use the following approach to populate User Defined Tool property controls using your Apex API script
- Add a “callback” argument to the apex_sdk.runScriptFunction(). This is the function in the UI script that calls your Apex API script. The callback argument defines the name of a function in your UI script
- Make sure that the Apex API script that is executed by runScriptFunction()returns a dictionary containing the data you want to display in the User Defined Tool property panel
- In the UI function associated with the callback argument
- Extract the dictionary using the function apex_sdk.getScriptFunctionReturnData()
- Populate the User Defined Tool Property Controls with the data in the Dictionary
- This is demonstrated in the following example: Display Node and Element Counts for Selected Assemblies and Parts Example
Tips and Tricks
- Starting MSC Apex 2023.3, Apex now outputs the error message of the custom tool GUI in the MSC_Apex.log log file
- Use apex.session.disableShowOutput() in your Apex API script to prevent the script execution summary dialog from being displayed when the script completes
- Provides a more pleasing user experience (No need to dismiss the dialog every time)
- If the script throws an exception – the dialog will still be displayed
- Use apex.session.displayStatusMessage() to display text in the Apex status bar region
- Log in to post comments

