# 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() geomApp = app.CreateGeometryEditor() geomDoc = geomApp.GetDocument() # Dimensional parameters SphereCenterX = 0 SphereCenterY = 0 SphereCenterZ = 0 SphereRadius = 100 # Equation names EqCenterX = u"center_x" EqCenterY = u"center_y" EqCenterZ = u"center_z" EqSphereRadius = u"radius" def createNewPart(): """Create a new Part and open it in edit mode.""" assm = geomDoc.GetAssembly() part = assm.CreatePart() part.OpenPart() return part def setupEquation(equation, expression): """Configure the basic settings for equations in DesignTable.""" # The argument of SetType # 0: Value # 1: Expression equation.SetType(0) equation.SetExpression(str(expression)) def createSphereEquations(): """Register the center coordinates and radius of the sphere as equations.""" designTable = geomDoc.GetDesignTable() equationDataList = [ (EqCenterX, SphereCenterX), (EqCenterY, SphereCenterY), (EqCenterZ, SphereCenterZ), (EqSphereRadius, SphereRadius), ] designTable.EditStart() for equationName, expression in equationDataList: designTable.AddEquation(equationName) equation = designTable.GetEquation(equationName) setupEquation(equation, expression) designTable.EditEnd() def createOffsetXYPlane(part): """Create an XY plane offset by center_z.""" refXYPlane = part.GetPlaneXY() offsetPlane = part.CreateReferencePlane() offsetPlane.SetTypeByString(u"Distance") offsetPlane.SetPropertyByReference(u"Plane", refXYPlane) offsetPlane.SetProperty(u"Distance", EqCenterZ) return offsetPlane def createSketchOnSpherePlane(part): """Create a sketch on the XY plane offset by center_z.""" offsetPlane = createOffsetXYPlane(part) offsetPlaneRef = geomDoc.CreateReferenceFromItem(offsetPlane) sketch = part.CreateSketch(offsetPlaneRef) return sketch def createRegionFromItems(sketch, itemList): """Create a region on the sketch from all items in the argument's itemList.""" selection = geomDoc.GetSelection() selection.Clear() for item in itemList: selection.Add(item) sketch.CreateRegions() selection.Clear() def setConstraintExpression(constraint, propertyName, equationName): """Set an equation name to the specified constraint property.""" constraint.SetProperty(propertyName, equationName) def getDistanceExpressionFromSignedValue(equationName, value): """Return a distance expression for a signed coordinate value.""" if value < 0: return u"-" + equationName return equationName def drawBaseShapeForSphere(sketch, radius): """Create a semicircular cross-section and a revolve axis for the sphere.""" sketch.OpenSketch() # Center point centerVertex = sketch.CreateVertex(SphereCenterX, SphereCenterY) centerRef = geomDoc.CreateReferenceFromItem(centerVertex) # Semicircular arc used as the cross-section of the sphere arc = sketch.CreateArc( SphereCenterX, SphereCenterY, SphereCenterX, SphereCenterY - radius, SphereCenterX, SphereCenterY + radius ) arcRef = geomDoc.CreateReferenceFromItem(arc) # Constrain the X position of the center point using an equation. centerXConstraint = sketch.CreateMonoConstraint( u"distancefromyaxis", centerRef ) centerXDistanceExpression = getDistanceExpressionFromSignedValue( EqCenterX, SphereCenterX ) setConstraintExpression(centerXConstraint, u"Distance", centerXDistanceExpression) # Constrain the Y position of the center point using an equation. centerYConstraint = sketch.CreateMonoConstraint( u"distancefromxaxis", centerRef ) centerYDistanceExpression = getDistanceExpressionFromSignedValue( EqCenterY, SphereCenterY ) setConstraintExpression(centerYConstraint, u"Distance", centerYDistanceExpression) # Constrain the radius of the arc using an equation. radiusConstraint = sketch.CreateMonoConstraint( u"radius", arcRef ) setConstraintExpression(radiusConstraint, u"Radius", EqSphereRadius) # Line segment used as the revolve axis axisLine = sketch.CreateLine( SphereCenterX, SphereCenterY - radius, SphereCenterX, SphereCenterY + radius ) axisRef = geomDoc.CreateReferenceFromItem(axisLine) # Constrain the axis line to be vertical. sketch.CreateMonoConstraint(u"verticality", axisRef) # Constrain the axis line to pass through the center point. sketch.CreateBiConstraint(u"coincident", axisRef, centerRef) # Create a closed region from the semicircular arc and the axis line. createRegionFromItems(sketch, [arc, axisLine]) sketch.CloseSketch() return axisLine def createSphereByRevolve(part, sketch, axisLine): """Create a sphere by revolving the semicircular region 360 degrees around the axis line.""" revolve = part.CreateRevolveSolid(sketch) revolve.SetTypeByName(u"Outside") revolve.SetProperty(u"AxisType", u"SelectEntity") axisRef = geomDoc.CreateReferenceFromItem(axisLine) revolve.SetAxis(axisRef) revolve.SetAngle(360.0) return revolve def createSpherePart(part): """Create a sphere with equation in the specified Part.""" sketch = createSketchOnSpherePlane(part) axisLine = drawBaseShapeForSphere(sketch, SphereRadius) createSphereByRevolve(part, sketch, axisLine) def main(): """Create a Part containing a sphere with equations.""" createSphereEquations() part = createNewPart() createSpherePart(part) part.ClosePart() main()