This is a simple function that obtains the volume of a part with a specified name. For part groups, it can also obtain the total for all grouped parts. In 2D, it also obtains the area, so branching is required for general scripting. This script searches for whether the specified name is a part group or a part, then checks whether it is 2D or 3D, and obtains the value accordingly.
Preconditions
- One or more models must have been created.
This script is run for the active model in the project tree. - The name of the part or part group to be obtained must be known.
Script Function
- Obtains the length unit set for the model to display the obtained area or volume.
- Obtains the model’s dimensions, and branches to obtain the area if 2D, or the volume if 3D.
- First, it checks whether the specified name “Magnet” exists in the part group name.
If it does not exist, an error message will be displayed, but this is a tolerable error and is cleared immediately afterwards. - If a part group with specified name exists, it searches for the part group; if not, it searches for the part, and obtains the value of the matched.
- The obtained value is the volume for a 3D model, or the area for a 2D model. When getting the volume from a 2D model or the area from a 3D model, the value returned is 0.
# 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/
app = designer.GetApplication()
def hasNamedPartsGroup(groupList, name):
"""Check the existence of the parts group with the specified name"""
ret = False
group = groupList.GetGroup(name)
# If the group with the specified name exists, the group object should contain valid data
if group.IsValid():
ret = True
else:
app.ClearError() # Suppress error messages from JMAG.
return ret
def getNamedPartsAreaFor2D(targetModel, name):
"""Get the area of the part with the specified name in the 2D model"""
area = 0.0
groupList = targetModel.GetGroupList()
if hasNamedPartsGroup(groupList, name):
group = groupList.GetGroup(name)
area = group.Area()
else:
part = targetModel.GetPart(name)
if part.IsValid():
area = part.Area()
else:
print(u"The part with the specified name doesn't exist")
return area
def getNamedPartsVolumeFor3D(targetModel, name):
"""Get the volume of the part with the specified name in the 3D model"""
volume = 0.0
groupList = targetModel.GetGroupList()
if hasNamedPartsGroup(groupList, name):
group = groupList.GetGroup(name)
volume = group.Volume()
else:
part = targetModel.GetPart(name)
if part.IsValid():
volume = part.Volume()
else:
print(u"The part with the specified name doesn't exist")
return volume
def getVolumeOrArea(targetModel):
"""Wrapper function for checking the model dimension"""
dim = targetModel.GetDimension()
unitString = targetModel.GetCurrentUnit(u"Length")
vol = 0.0
targetPartName = u"Magnet"
preDescriptionString = u""
if dim == 2:
vol = getNamedPartsAreaFor2D(targetModel, targetPartName)
preDescriptionString = u"The area of"
unitString += u"^2"
elif dim == 3:
vol = getNamedPartsVolumeFor3D(targetModel, targetPartName)
preDescriptionString = u"The volume of"
unitString += u"^3"
print(preDescriptionString, targetPartName, u"is", vol, unitString)
model = app.GetCurrentModel()
getVolumeOrArea(model)


