import_cad_file
Import STEP or STL files as named 3D objects, returning volume, topology, and bounding box for geometry queries and comparison.
Instructions
Import a STEP (.step/.stp) or STL (.stl) file as a named object in the session. path: absolute or relative path to the file. name: name to register the shape under (defaults to the filename stem). The shape becomes both the named object and the current_shape. Returns volume, topology, and bounding box of the imported shape. After importing, use render_view() to visualise the shape, measure() for geometry queries, or shape_compare() to diff against a show() object. Note: STL imports produce a shell (volume=0) rather than a solid — render_view and measure still work, but interference() and boolean operations require a solid. If you have both the original built shape and an imported copy in session.objects, render the imported one by name (e.g. objects='mypart') to avoid Z-fighting artifacts from two co-located shapes.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| name | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- Core implementation: imports a STEP/STL file, loads geometry via build123d, registers it in session.objects, sets it as current_shape, and returns JSON with volume, topology, and bounding box info.
def import_cad_file(session, path: str, name: str = "") -> str: resolved = os.path.realpath(path) if not os.path.isfile(resolved): raise ValueError(f"File not found: '{path}'") ext = os.path.splitext(resolved)[1].lower() if ext not in _ALLOWED_EXTS: raise ValueError(f"Expected a .step, .stp, or .stl file, got '{ext}'") obj_name = name or os.path.splitext(os.path.basename(resolved))[0] if ext in _STEP_EXTS: shape = _load_step(resolved) fmt = "step" else: shape = _load_stl(resolved) fmt = "stl" session.objects[obj_name] = shape session.current_shape = shape bb = shape.bounding_box() result = { "imported": obj_name, "format": fmt, "path": resolved, "volume": round(shape.volume, 4), "faces": len(shape.faces()), "edges": len(shape.edges()), "vertices": len(shape.vertices()), "bbox": { "xsize": round(bb.size.X, 4), "ysize": round(bb.size.Y, 4), "zsize": round(bb.size.Z, 4), }, } return json.dumps(result, indent=2) - Helper: _load_step loads a STEP file, handling multi-body shapes by fusing them together.
def _load_step(resolved: str): from build123d import import_step as _import_step imported = _import_step(resolved) # Multi-body STEP returns an iterable without a .wrapped attribute if hasattr(imported, "__iter__") and not hasattr(imported, "wrapped"): shapes = list(imported) if not shapes: raise ValueError("STEP file contains no geometry") shape = shapes[0] for s in shapes[1:]: shape = shape + s # type: ignore[assignment] return shape return imported - Helper: _load_stl loads an STL file using build123d's import_stl.
def _load_stl(resolved: str): from build123d import import_stl return import_stl(resolved) - src/build123d_mcp/server.py:179-182 (handler)MCP tool registration: defines the import_cad_file tool with FastMCP @mcp.tool() decorator, delegates to _session.import_cad_file().
@mcp.tool() def import_cad_file(path: str, name: str = "") -> str: """Import a STEP (.step/.stp) or STL (.stl) file as a named object in the session. path: absolute or relative path to the file. name: name to register the shape under (defaults to the filename stem). The shape becomes both the named object and the current_shape. Returns volume, topology, and bounding box of the imported shape. After importing, use render_view() to visualise the shape, measure() for geometry queries, or shape_compare() to diff against a show() object. Note: STL imports produce a shell (volume=0) rather than a solid — render_view and measure still work, but interference() and boolean operations require a solid. If you have both the original built shape and an imported copy in session.objects, render the imported one by name (e.g. objects='mypart') to avoid Z-fighting artifacts from two co-located shapes.""" return _session.import_cad_file(path, name) - src/build123d_mcp/worker.py:150-152 (registration)Worker dispatch: routes the 'import_cad_file' operation string to the handler function from import_step.py.
if op == "import_cad_file": from build123d_mcp.tools.import_step import import_cad_file return import_cad_file(session, args["path"], args.get("name", ""))