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

augustbrandberg
augustbrandberg Member Posts: 8
First Anniversary Name Dropper First Comment
**

Hello!

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: 183
    100 Comments 5 Answers First Anniversary Ansys Employee
    admin
    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.

Answers

  • augustbrandberg
    augustbrandberg Member Posts: 8
    First Anniversary 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):
            file.write('new_ns.GenerationCriteria.Add(None)\n')
            preStrToConstruc = "new_ns.GenerationCriteria[" + str(aLoop) + "]."
    
            if gc.Action != None:
                file.write('{:s}{:s}=SelectionActionType.{:s}\n'.format(preStrToConstruc,'Action',str(gc.Action)))
            if gc.EntityType != None:
                file.write('{:s}{:s}=SelectionType.{:s}\n'.format(preStrToConstruc,'EntityType',str(gc.EntityType)))
            if gc.Criterion != SelectionCriterionType.Unknown:
                file.write('{:s}{:s}=SelectionCriterionType.{:s}\n'.format(preStrToConstruc,'Criterion',str(gc.Criterion)))
            if gc.Operator != SelectionOperatorType.Unknown:
                file.write('{:s}{:s}=SelectionOperatorType.{:s}\n'.format(preStrToConstruc,'Operator',str(gc.Operator)))
            if gc.Value != None:
                if isinstance(gc.Value, str):
                    file.write('{:s}{:s}=\"{:s}\"\n'.format(preStrToConstruc,'Value',str(gc.Value)))
                elif isinstance(gc.Value , Ansys.ACT.Automation.Mechanical.NamedSelection):
                    get_cm_name = gc.Value.Name
                    file.write('{:s}{:s}=DataModel.GetObjectsByName(\"{:s}\")[0]\n'.format(preStrToConstruc,'Value',str(get_cm_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) ))
                    else:
                        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
    file.close()
    

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

    exec(open("test.txt").read())
    

    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?