[S0046] Group the selected parts by specifying a box area

When configuring analysis settings —such as grouping parts or applying conditions— it often needs to select specific targets like parts, faces, or edges.
While the intended targets can be selected by using IDs or names, another method involves specifying a region in model space to select all targets contained within that area.
With range selection, only targets that lie entirely within the specified range are selected.
This script specifies a rectangular box region and creates a new part group containing all the selected parts.
Please refer to the diagram for the meaning of the parameters used to define the rectangular box region.
A box defined by two points

Preconditions

  • One or more models have been created.
    This script runs on the active model in the project tree.
  • JMAG-Designer is not running in no-window mode. Selecting a region by specifying coordinates cannot be performed in no-window mode.

Script Function

  • Set the coordinates of two diagonally opposite points defining the box area to select.
  • Select the parts within the box area.
  • Group all selected parts into a single new part group.
# 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 getPartsInBoxArea(model, diagonalPoint1, diagonalPoint2):
    """Select parts located within the box defined by a diagonal connecting the two points specified as arguments with sides parallel to the x, y, or z-axes. """
    result = model.CreateSelection()
    result.Clear()
    result.Attach()

    # The 3rd argumet of SelectPartByRange
    # False: Select all parts in the range
    # True: Select only visible parts in the range
    result.SelectPartByRange(diagonalPoint1, diagonalPoint2, False)

    return result

app = designer.GetApplication()
model = app.GetCurrentModel()

point1 = app.CreatePoint(-40, -40, 0)
point2 = app.CreatePoint(42, 42, 60)
selParts = getPartsInBoxArea(model, point1, point2)
model.GetGroupList().CreateGroupBySelected(selParts, "newGroup")

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