[S0020] Create a parts set by specifying the part names

 

A “set” is a function that treats multiple parts, faces, edges, or vertices as a single group, regardless of differences in material properties.
By creating a set in advance and defining a name, it can be easy and reliable to set analysis conditions in a script.
This script creates a set for the specified parts.

Preconditions

  • One or more models have been created.
    This script runs for the active model in the project tree.
  • The title of parts to create set is known.

Script Function

  • Get a set with the specified name. If it does not exist, create it.
  • Associate the parts named “Coil2” and “Coil4” to the set.
  • Set type to “From Selection”, turn off Consider parent-child relation.
# Copyright (c) 2026 JSOL CORPORATION
#
# This script is released under the MIT License.
# See the full license text at:
# https://www.jmag-international.com/scriptlibrary/jmag_script_library_mit/


def getOrCreatePartSet(model, setNm):
    """Gets or creates the set passed as an argument"""
    partSet = model.GetSetList().GetSet(setNm)
    partSet = partSet if partSet.IsValid() else model.GetSetList().CreatePartSet(setNm)
    return partSet


def setParts(partSet, partsNmArray):
    """Sets the parts passed as arguments to the set"""
    partSet.SetMatcherType(u"Selection")
    partSet.SetUpdateByRelation(False)
    partSet.ClearParts()
    sel = partSet.GetSelection()
    for partsNm in partsNmArray:
      sel.SelectPart(partsNm)
    partSet.AddSelected(sel)


app = designer.GetApplication()
model = app.GetCurrentModel()
setNm = u"coilSet"
partSet = getOrCreatePartSet(model, setNm)
partsNmArray = [u"Coil2", u"Coil4"]
setParts(partSet, partsNmArray)

Download Python source code

How to use script file

Use the JMAG Script Library after reading and agreeing to the following terms of use.

Search Filter

  • All Categories

An engineer's diary
JMAG-Express Online