Mesh All Bodies Example

Description

This demonstrates the ability to mesh all surfaces in the model with user defined mesh size and type.

Python Script Files

  • mesh_all_bodies_ui.py
  • mesh_all_bodies.py

Significant Script Features

mesh_all_bodies_ui.py:

# Set pre-defined properties of ToolPropertyContainer
def getUIContent():

   my_toolProperty = apex_sdk.ToolPropertyContainer()
  
   # Provide an icon and a name for the tool property panel
   my_toolProperty.TitleImageUriString = os.path.join(os.path.dirname(current_file_path), r"Icons\script.png")
   my_toolProperty.TitleText = "Mesh All Bodies"

   # Define UI
   my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

   # Handle apply button (green) click event
   my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))
  
   return my_toolProperty

# Apply button handler (Green check mark)
# This function is called each time the Apply button is clicked
@apex_sdk.errorhandler
def HandleApplyButton():
   
   # Create a Dictionary to store the user defined tool data  
   dictionary = {}
 
   # Populate the Dictionary with the user defined tool data you need
   dictionary["MeshSize"] = meshSizeTextBox.Text
   dictionary["MeshType"] = meshTypeComboBox.Text
   
   # Call the Apex API script using apex_sdk.runScriptFunction()
   # argument 1 = the fully qualified path name of your API script
   # argument 2 = the name of the function in your API script that will receive the Dictionary
   # argument 3 = the Dictionary containing the UI data
   apex_sdk.runScriptFunction(os.path.join(current_file_path, r"mesh_all_bodies.py"),
                              "mesh_all_bodies",
                              dictionary)

mesh_all_bodies.py:

def mesh_all_bodies(dictionary={}):

    import apex

    # Disable showing output dialog at the end
    apex.disableShowOutput()

    # Get the UI data from the dictionary
    mesh_size = float(dictionary["MeshSize"])
    mesh_type = dictionary["MeshType"]
    
    # Convert the mesh type to the enum required for the meshing function
    mesh_types = {
        "Mixed" : apex.mesh.SurfaceMeshElementShape.Mixed,
        "Quadrilateral" : apex.mesh.SurfaceMeshElementShape.Quadrilateral,
        "Triangle" : apex.mesh.SurfaceMeshElementShape.Triangle
        }
    
    mesh_type = mesh_types[mesh_type] 
    
    # Get all bodies in the model   
    meshable_bodies = apex.EntityCollection()
    meshable_bodies.appendList([body for part in apex.currentModel().getParts(recursive=True) for body in part.geometryBodies])
    
    # Start Meshing
    apex.session.displayStatusMessage("Meshing started ...")

    result = apex.mesh.createSurfaceMesh(
        name = "",
        target = meshable_bodies,
        meshSize = mesh_size,
        meshType = mesh_type,
        meshMethod = apex.mesh.SurfaceMeshMethod.Auto,
        mappedMeshDominanceLevel = 2,
        elementOrder = apex.mesh.ElementOrder.Linear,
        meshUsingPrincipalAxes = False,
        refineMeshUsingCurvature = False,
        elementGeometryDeviationRatio = 0.10,
        elementMinEdgeLengthRatio = 0.20,
        createFeatureMeshOnFillets = False,
        createFeatureMeshOnChamfers = False,
        createFeatureMeshOnWashers = False,
        createFeatureMeshOnQuadFaces = False)

    apex.session.displayStatusMessage("Meshing completed.")

Notes

  • The AppliedCommand is set to register the function handle the Apply button click
  • The mesh_all_bodies() function defined in the Apex API script receives a single Dictionary argument which meshes all of the bodies using the dictionary's definitions of mesh_size and mesh_type