Skip to content

A constructive solid geometry python library for OpenCASCADE that can read and write STEP files

Notifications You must be signed in to change notification settings

roseengineering/csgstep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csgstep

A constructive solid geometry python library for OpenCASCADE. The API is based on the OpenSCAD and SolidPython API. The library can read and write STEP files. It can also export STL files.

Examples

Create a cube that has a sphere subtracted from it:

from csgstep import cube, sphere
solid = cube(center=True) - sphere(.65)
solid.write_stl('cube.stl')    
solid.write_step('cube.stp')    

Create a helix:

from csgstep import circle
pitch = .3
solid = circle(.1)
solid = solid.helix_extrude(r=8, h=3.1 * pitch, pitch=pitch, center=True)
solid.write_stl('helix.stl')    
solid.write_step('helix.stp')    

Create a pipe:

from csgstep import circle
solid = circle(.2).spline_extrude([(0, 0, 0),(0, 1, 2),(0, 2, 3)])
solid.write_stl('pipe.stl')    
solid.write_step('pipe.stp')    

Dependencies

The library depends on pythonocc-core and numpy. To install pythonocc-core, I used anaconda and ran "conda install -c conda-forge pythonocc-core".

To install csgstep run "pip install ." in this directory, or the equivalent. It will bring in numpy.

Notes

All method functions (not properties) of Solid return a new Solid object. So remember to always assign the result of a method or method chain, otherwise it will be lost.

The rotate method here is different from the OpenSCAD rotate method. The first argument is the angle to rotate and the second argument is the vector to rotate around.

I also added a new extrude method called spline_extrude. It takes a list of points as its only argument. These points are converted into a cubic spline which is then used to extrude a solid. An example of spline_extrude is the helix_extrude method which creates a helix from a solid.

csgstep API

csgstep.load_step(filename)
Load the given STEP File.
filename the path of the STEP file
returns a Solid object

csgstep.sphere(r=1)
Create a sphere of the given radius centered at the origin.
r the radius of the sphere
returns a Solid object

csgstep.cube(s=1, center=False)
Create a cube of the given size.
s the length of the sides of the cube as a real or 3D vector
center if true center the cube at the origin, otherwise the lowest edge of the cube is at the origin
returns a Solid object

csgstep.wedge(s=1, xmin=0, zmin=0, xmax=0, zmax=0)
Create a wedge of the given size and given the face at dy.
s the length of the sides of the wedge as a real or 3D vector
xmin the minimum value of x at dy
zmin the minimum value of z at dy
xmax the maximum value of x at dy
zmax the maximum value of z at dy
returns a Solid object

csgstep.cylinder(r=1, h=1, center=False)
Create a cylinder along the Z axis of the given radius and height
r the radius of the cylinder
h the height of the cylinder
center if true center the cylinder on the Z axis, otherwise the base is at the origin
returns a Solid object

csgstep.cone(r1=1, r2=0, h=1, center=False)
Create a cone along the Z axis of the given base radius, top radius, and height
r1 the bottom radius of the cone
r2 the top radius of the cone
h the height of the cone
center if true center the cone on the Z axis, otherwise the base is at the origin
returns a Solid object

csgstep.circle(r=1)
Create a circle for the given radius centered at the origin in the XY plane.
r the radius of the circle
returns a (2D) Solid object

csgstep.ellipse(rx=1, ry=1)
Create a ellipse for the given X radius and Y radius centered at the origin in the XY plane.
rx the radius of the ellipse in the X axis direction
ry the radius of the ellipse in the Y axis direction
returns a (2D) Solid object

csgstep.square(s=1, center=False)
Create a square for the given size in the XY plane.
s the length of the sides of the square as a real or 2D vector
center if true center the square at the origin, otherwise one edge is at the origin
returns a (2D) Solid object

csgstep.polygon(points)
Create a polygon from 2D points in the XY plane.
points the points of the polygon in path order
returns a (2D) Solid object

class csgstep.Solid(self, shape=None, name=None)
Instantiate Solid class with a TopoDS object.
shape the TopoDS object to wrap the instantiated class around
name the name of the shape

Instances of the csgstep.Solid class have the following properties and methods:

Solid.name The name property of the solid. Use to get or set the name of the solid. (not implemented)

Solid.write_step(self, filename, schema='AP203')
Write this solid to a STEP file.
filename name of STEP output file
schema name of STEP output schema, defaults to AP203

Solid.write_stl(self, filename, mode='ascii', linear_deflection=0.5, angular_deflection=0.25)
Write this solid to a STL file.
filename name of STL output file
mode mode of STL file, whether ascii or binary
linear_deflection linear deflection value
angular_deflection angular deflection value

Solid.__add__(self, solid)
Redirects call to the compound method.

Solid.__mul__(self, solid)
Redirects call to the intersection method.

Solid.__sub__(self, solid)
Redirects call to the difference method.

Solid.mirrorX(self)
Mirror this solid about the X axis.
returns a new Solid object

Solid.mirrorY(self)
Mirror this solid about the Y axis.
returns a new Solid object

Solid.mirrorZ(self)
Mirror this solid about the Z axis.
returns a new Solid object

Solid.rotateX(self, a)
Rotate this solid around the X axis by the given angle.
a the angle in radians to rotate by
returns a new Solid object

Solid.rotateY(self, a)
Rotate this solid around the Y axis by the given angle.
a the angle in radians to rotate by
returns a new Solid object

Solid.rotateZ(self, a)
Rotate this solid around the Z axis by the given angle.
a the angle in radians to rotate by
returns a new Solid object

Solid.translateX(self, v)
Translate this solid in the X direction by the given amount.
v the amount to translate object by
returns a new Solid object

Solid.translateY(self, v)
Translate this solid in the Y direction by the given amount.
v the amount to translate object by
returns a new Solid object

Solid.translateZ(self, v)
Translate this solid in the Z direction by the given amount.
v the amount to translate object by
returns a new Solid object

Solid.intersection(self, solid)
Intersect this solid with the given Solid object.
solid the Solid object to intersect with
returns a new Solid object

Solid.difference(self, solid)
Cut the given Solid object from this solid.
solid the Solid object to cut with
returns a new Solid object

Solid.fuse(self, solid)
Fuse this solid with the given Solid object. The openCASCADE BRepAlgoAPI_Fuse function is used to perform the fusion.
solid the Solid object to merge with
returns a new Solid object

Solid.union(self, *solids)
Union this solid with the given Solid objects. More than one Solid object can be passed as arguments for unioning. The openCASCADE BOPAlgo_MakerVolume function is used to perform the union.
*solids the Solid objects to merge with
returns a new Solid object

Solid.compound(self, *solids)
Create a compound shape with this solid and the given Solid objects. More than one Solid object can be passed as arguments for compounding. The method creates a openCASCADE TopoDS_Compound shape from the shapes.
*solids the Solid objects to compound with
returns a new Solid object with the TopoDS_Compound shape

Solid.mirror(self, v)
Mirror this solid about the given axis.
v the 3D vector to mirror object about
returns a new Solid object

Solid.translate(self, v)
Translate this solid by the given 3D vector.
v the 3D vector to translate object with
returns a new Solid object

Solid.rotate(self, a, v)
Rotate this solid around the given 3D vector by the given angle.
a the angle in radians to rotate object by
v the 3D vector to rotate object around
returns a new Solid object

Solid.scale(self, v)
Scale this solid by the given factor.
v the factor to scale, given as a real or 3D vector
returns a new Solid object

Solid.fillet(self, r)
Fillet all edges of this solid by the given radius.
radius the radius to fillet edges by
returns a new Solid object

Solid.chamfer(self, d)
Chamfer all edges of this solid by the given distance.
d the distance to chamfer edges by
returns a new Solid object

Solid.draft(self, a)
Apply a draft angle to all vertical faces of this solid. The vertical direction is used to measure the draft angle. The neutral plane is the XY plane at the origin.
a the draft angle to apply
returns a new Solid object

Solid.linear_extrude(self, v)
Linear extrude this (2D) solid in the Z direction by the given amount.
v the amount to linear extrude by
returns a new Solid object

Solid.rotate_extrude(self, a=None)
Rotate extrude this (2D) solid around the Z axis by the given angle. The object will be rotated around the X axis by 90 degrees before being extruded.
a the angle in radians to rotate extrude by, defaults to 2 * pi.
returns a new Solid object

Solid.spline_extrude(self, points)
Spline extrude this (2D) solid along a cubic spline given by 3D points.
points the 3D points to create the cubic spline from
returns a new Solid object

Solid.helix_extrude(self, r, h, pitch, center=False)
Helix extrude this (2D) solid by the given radius, height and pitch. The object will be rotated around the X axis by the slope of the helix before being extruded.
radius the radius of the helix
height the height of the helix
pitch the pitch of the helix
center if true center the helix on the Z axis, otherwise base is at the origin
returns a new Solid object

About

A constructive solid geometry python library for OpenCASCADE that can read and write STEP files

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published