FoundationPartsOperationsImport STEP

Import STEP

ImportStep brings a solid from a STEP file (.step / .stp, ISO 10303-21: AP203, AP214 or AP242) into a part as an ordinary feature. It is fused with the part like an extrusion, and it can be the input of Fillet, Chamfer, Shell, BooleanOperation or MoveBody.

motor = ImportStep("nema17.step")         # a path, like open("nema17.step")
self.add_operation(motor)
 
self.add_operation(MoveBody(ImportStep("bracket.step"), Point3D(0, 0, 40)))
self.add_operation(ImportStep("pocket.step", cut=True))   # subtract it
 
with open("coupling.step", "rb") as f:    # any open file object works too
    coupling = ImportStep(f)
 
boss = ImportStep.from_text(step_text, "boss.step")       # text already in memory
 
shaft = Part.from_step("shaft.step")      # a Part whose only operation is the import
assy.add_component(shaft)

Reading the file

ImportStep reads a file like open() does: a path (str or pathlib.Path) is resolved from the current directory, and an open file object (text or binary) is read as is. Getting the file onto disk is up to you: a download, an upload or the project’s files. There is no URL or storage logic in ImportStep.

The file’s text is stored in the part itself, with its SHA-256 and its file name. The part is self-contained: the kernel never goes back to the file. The example above uses ImportStep.from_text with a small inline STEP file, so it runs anywhere.

Before anything is built, the text is checked: it must start with ISO-10303-21; and have HEADER and DATA sections. Otherwise you get a StepFileError that names the file.

Geometry rules

  • Units: lengths are converted from the unit the file declares (mm, m, inch…) to mm. A 1 in cube comes in as 25.4 mm.
  • Several solids: they are fused into one body.
  • Surfaces only (no closed solid), or a file with no geometry: the build fails with an error that names the file. The result is never an empty solid.
  • Placement: the solid keeps the coordinates in the file. Use MoveBody to move or rotate it.
  • Bounding box: check_interference can’t bound an imported solid without the kernel. Declare its extents with part.set_extents(...) if you need them.
from cadbuildr.foundation import *

# A tiny STEP file (ISO 10303-21): a Ø20 x 30 mm boss, units in mm.
# In a project you would write ImportStep("boss.step") instead: ImportStep
# reads a file like open() does (path relative to the current directory, or
# an open file object). Here the text is inline so the example runs anywhere.
BOSS_STEP = """ISO-10303-21;
HEADER;
FILE_DESCRIPTION(('boss: cylinder r=10 h=30'),'2;1');
FILE_NAME('boss.step','',(''),(''),'','','');
FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'));
ENDSEC;
DATA;
#1=APPLICATION_CONTEXT('automotive design');
#2=PRODUCT_CONTEXT('',#1,'mechanical');
#3=PRODUCT('boss','boss','',(#2));
#4=PRODUCT_DEFINITION_FORMATION('','',#3);
#5=PRODUCT_DEFINITION_CONTEXT('part definition',#1,'design');
#6=PRODUCT_DEFINITION('design','',#4,#5);
#7=PRODUCT_DEFINITION_SHAPE('','',#6);
#8=SHAPE_DEFINITION_REPRESENTATION(#7,#9);
#9=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#10),#60);
#10=MANIFOLD_SOLID_BREP('',#11);
#11=CLOSED_SHELL('',(#12,#13,#14));
#12=ADVANCED_FACE('',(#15),#20,.T.);
#13=ADVANCED_FACE('',(#16),#21,.T.);
#14=ADVANCED_FACE('',(#17),#22,.F.);
#15=FACE_BOUND('',#18,.T.);
#16=FACE_BOUND('',#19,.T.);
#17=FACE_BOUND('',#23,.T.);
#18=EDGE_LOOP('',(#24,#25,#26,#27));
#19=EDGE_LOOP('',(#28));
#23=EDGE_LOOP('',(#29));
#24=ORIENTED_EDGE('',*,*,#46,.F.);
#25=ORIENTED_EDGE('',*,*,#47,.F.);
#26=ORIENTED_EDGE('',*,*,#45,.T.);
#27=ORIENTED_EDGE('',*,*,#47,.T.);
#28=ORIENTED_EDGE('',*,*,#46,.T.);
#29=ORIENTED_EDGE('',*,*,#45,.F.);
#20=CYLINDRICAL_SURFACE('',#34,10.);
#21=PLANE('',#35);
#22=PLANE('',#34);
#30=CARTESIAN_POINT('',(0.,0.,0.));
#31=CARTESIAN_POINT('',(0.,0.,30.));
#32=DIRECTION('',(0.,0.,1.));
#33=DIRECTION('',(1.,0.,0.));
#34=AXIS2_PLACEMENT_3D('',#30,#32,#33);
#35=AXIS2_PLACEMENT_3D('',#31,#32,#33);
#36=CARTESIAN_POINT('',(10.,0.,0.));
#37=CARTESIAN_POINT('',(10.,0.,30.));
#38=VERTEX_POINT('',#36);
#39=VERTEX_POINT('',#37);
#41=CIRCLE('',#34,10.);
#42=CIRCLE('',#35,10.);
#43=LINE('',#36,#44);
#44=VECTOR('',#32,1.);
#45=EDGE_CURVE('',#38,#38,#41,.T.);
#46=EDGE_CURVE('',#39,#39,#42,.T.);
#47=EDGE_CURVE('',#38,#39,#43,.T.);
#60=(GEOMETRIC_REPRESENTATION_CONTEXT(3)
GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#64))
GLOBAL_UNIT_ASSIGNED_CONTEXT((#61,#62,#63))
REPRESENTATION_CONTEXT('',''));
#61=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
#62=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));
#63=(NAMED_UNIT(*)SI_UNIT($,.STERADIAN.)SOLID_ANGLE_UNIT());
#64=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E-07),#61,'distance_accuracy_value','');
ENDSEC;
END-ISO-10303-21;
"""


class BossPlate(Part):
    """ImportStep brings a STEP solid in as a feature: fused with the plate,
    drilled, then rounded with fillet_edges like any other body."""

    def __init__(self):
        s = Sketch(self.xy())
        self.extrude(Rectangle.from_center_and_sides(s.origin, 60, 40), 6)
        boss = ImportStep.from_text(BOSS_STEP, "boss.step")
        self.add_operation(boss)  # fused like any feature
        self.cut_extrude(Circle(s.origin, 4), 30)  # a bore through boss and plate
        self.fillet_edges(1.0)
        self.paint("medium_azure")


show(BossPlate())