# 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)