[S0032] Create a rectangular prism in the Geometry Editor

 

The JMAG-Designer Geometry Editor provides the functions to create 2D and 3D shapes.
To create a 3D shape, first create a part and specify a plane to create a 2D sketch.
Draw basic shapes in the sketch and define a closed region using a collection of basic shapes.
Then, define an extrusion of the sketch in the part to create a solid; this is the basic creation procedure.
This script creates a rectangular region and then defines an extrusion to create a rectangular prism solid.

Preconditions

None

Script Function

  • Create a part and set it to editable status.
  • Create a sketch on the XY plane and set it to editable status.
  • Set the starting point to X=10, Y=10, and create a line with the specified width in the + direction.
  • Apply a constraint(fixation) to the vertex of the starting point.
  • Apply a constraint(horizontality) to the line in the X direction and a constraint(verticality) to the line in the Y direction.
  • Apply a constraint(distance) between two parallel lines.
  • Select the four created lines, create a region, and finish editing the sketch.
  • Define an extrusion targeting the created sketch.
  • The extrusion type is set to both sides, and the extrusion height is set to half of the specified height.

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


# Horizontal(X) Width of the Rectangular
HW = 20
# Vertical(Y) Width of the Rectangular
VW = 10
# Height(Z) of a Rectangular Prism
H = 10


def createRectangleRegion(geomDoc, sketch):
    """Create a region by drawing a rectangle with a point, height and width in the sketch. And apply distance constraints to the height and width"""
    sketch.OpenSketch()
    
    leftBottomX = 10
    leftBottomY = 10
    rightTopX = leftBottomX + HW
    reghtTopY = leftBottomY + VW
    bottomLine = sketch.CreateLineByLengthAngle(leftBottomX, leftBottomY, HW, 0)
    startVertex = bottomLine.GetStartVertex()
    refStartVertex = geomDoc.CreateReferenceFromItem(startVertex)
    # fixture: Constraint(Fixation)
    sketch.CreateMonoConstraint(u"fixture", refStartVertex)
    refBottomLine = geomDoc.CreateReferenceFromItem(bottomLine)
    
    leftLine = sketch.CreateLineByLengthAngle(leftBottomX, leftBottomY, VW, 90)
    refLeftLine = geomDoc.CreateReferenceFromItem(leftLine)
    topLine = sketch.CreateLineByLengthAngle(leftBottomX, reghtTopY, HW, 0)
    refTopLine = geomDoc.CreateReferenceFromItem(topLine)
    rightLine = sketch.CreateLineByLengthAngle(rightTopX, leftBottomY, VW, 90)
    refRightLine = geomDoc.CreateReferenceFromItem(rightLine)

    # horizontality: Constraint(Horizontality)
    # verticality: Constraint(Verticality)
    # distance: Constraint(Distance)
    sketch.CreateMonoConstraint(u"horizontality", refBottomLine)
    sketch.CreateMonoConstraint(u"horizontality", refTopLine)
    sketch.CreateMonoConstraint(u"verticality", refLeftLine)
    sketch.CreateMonoConstraint(u"verticality", refRightLine)
    VWDistanceConstraint = sketch.CreateBiConstraint("distance", refBottomLine, refTopLine)
    VWDistanceConstraint.SetName(u"VW")
    HWDistanceConstraint = sketch.CreateBiConstraint("distance", refLeftLine, refRightLine)
    HWDistanceConstraint.SetName(u"HW")

    selection = geomDoc.GetSelection()
    selection.Clear()
    selection.Add(bottomLine)
    selection.Add(leftLine)
    selection.Add(topLine)
    selection.Add(rightLine)
    sketch.CreateRegions()
    selection.Clear()
    sketch.CloseSketch()
    
    
def createRectangularPrismPart(geomDoc):
    """As a part, create a rectangular region on the XY plane and create a rectangular prism by extrusion."""
    assembly = geomDoc.GetAssembly()
    rectPart = assembly.CreatePart()
    rectPart.SetName(u"RectangularPrism")
    rectPart.SetProperty(u"Color", u"navy")
    rectPart.OpenPart()
    
    refPlane = rectPart.GetPlaneXY()
    # Create a sketch on the XY Plane
    sketchOnXYPlane = rectPart.CreateSketch(refPlane)
    createRectangleRegion(geomDoc, sketchOnXYPlane)
    
    extrude = rectPart.CreateExtrudeSolid(sketchOnXYPlane, H/2)
    # Set extrude type to [Both Sides]
    extrude.SetTypeByName(u"BothSides")
    rectPart.ClosePart()


app = designer.GetApplication()
geomApp = app.CreateGeometryEditor()
geomDoc = geomApp.GetDocument()
createRectangularPrismPart(geomDoc)

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