Exporting Named Selection selection logic to file for import into other Mechanical models

augustbrandberg Member Posts: 8
Name Dropper First Comment


In Ansys Mechanical, I usually create named selections manually using the Worksheet functionality.

It would be convenient to be able to export my named selections to a file.

This functionality already exists for node and element named selections via the CDB file functionality. However, most of my named selections are scoped to bodies and faces.

Is there a way to export such named selections to a format that would allow me to import them into a new Mechanical model?

Best regards!

Best Answer

  • Chris Harrold
    Chris Harrold Member, Administrator, Employee Posts: 169
    First Answer First Comment First Anniversary Ansys Employee
    Answer ✓

    @augustbrandberg - this forum is for code and scripting questions related to developers. Product questions will not be answered here, but you can post this question to the support forum (forum.ansys.com) and they will assist.


  • augustbrandberg
    augustbrandberg Member Posts: 8
    Name Dropper First Comment

    Hi Chris,

    This is a scripting question since it is not part of the product. For example, below is a very rough approach to solving the above problem for some types of Named Selections.

    The below code will go through all named selections, writing the python code required to recreate it to a file.

    all_ns = DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection)
    file_to_w = "test.txt"
    file = open( file_to_w,"w")
    for ns in all_ns:
        file.write('{:s}\n'.format("new_ns = Model.AddNamedSelection()"))
        file.write('new_ns.Name = \"{:s}\"\n'.format(ns.Name))
        file.write('new_ns.ScopingMethod = {:s}\n'.format("GeometryDefineByType.Worksheet"))
        for aLoop, gc in enumerate(ns.GenerationCriteria):
            preStrToConstruc = "new_ns.GenerationCriteria[" + str(aLoop) + "]."
            if gc.Action != None:
            if gc.EntityType != None:
            if gc.Criterion != SelectionCriterionType.Unknown:
            if gc.Operator != SelectionOperatorType.Unknown:
            if gc.Value != None:
                if isinstance(gc.Value, str):
                elif isinstance(gc.Value , Ansys.ACT.Automation.Mechanical.NamedSelection):
                    get_cm_name = gc.Value.Name
                elif isinstance(gc.Value, Quantity):
                    if gc.Operator == SelectionOperatorType.Equal or gc.Operator == SelectionOperatorType.GreaterThan or gc.Operator == SelectionOperatorType.GreaterThanOrEqual or gc.Operator == SelectionOperatorType.LessThanOrEqual or gc.Operator == SelectionOperatorType.LessThan:
                        file.write('{:s}{:s}=Quantity({:s},\"{:s}\")\n'.format(preStrToConstruc,'Value',str(gc.Value.Value) , str(gc.Value.Unit) ))
                        file.write('{:s}{:s}=Quantity({:s},\"{:s}\")\n'.format(preStrToConstruc,'LowerBound',str(gc.LowerBound.Value) , str(gc.LowerBound.Unit) ))
                        file.write('{:s}{:s}=Quantity({:s},\"{:s}\")\n'.format(preStrToConstruc,'UpperBound',str(gc.UpperBound.Value) , str(gc.UpperBound.Unit) ))
    # Close the file again

    The file written can then be read back in using the scripting interface using


    This method is clearly not ideal as it is very verbose and prone to bugs.

    However, someone more well versed in Python and/or Mechanical scripting might have a more compact format to accomplish the task?