[S0044] Create a sphere solid in the Geometry Editor

To create a 3D shape in the JMAG-Designer Geometry Editor, first create a 2D sketch on the specified plane.
The cross-sectional shape and plane are determined by considering the construction steps—such as which cross-section of the intended 3D shape to extrude or which parts to cut away later.
This script creates a sphere by revolving a semicircular region.

Preconditions

  • The center coordinates and radius of the sphere are set using the equation.

Script Function

  • Register variables for the sphere's center coordinates and radius as equation parameters.
  • Create a reference plane offset from the XY plane along the Z-axis and create a sketch on this plane.
    Set the variable representing the Z-coordinate of the center to the Z-axis offset distance.
  • Create a semicircle and a line to be the revolve axis based on the specified center position and apply constraints.
    Constrain the center point by distances from the X-axis and Y-axis, assigning the corresponding center coordinate variables to these distances.
    Apply a radius constraint to the semicircle and assign a variable to the radius value.
  • Create a region defined by the semicircle and the line.
  • Revolve the semicircle region 360 degrees around the line to create a spherical shape.
# 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()

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