#
# Copyright © 2020 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#
# MSC Apex - User Defined Tools using Python Scripts
# Mesh Selected Bodies script to be called by Mesh Selected Bodies UI Example
# 
def mesh_selected_bodies(dictionary={}):

    import apex
    import time

    # 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 selected entities   
    meshable_bodies = apex.selection.getCurrentSelection()


    # 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 ...")
        
