如何将PythonOCC默认单位设置为英寸适配STEP文件与自定义坐标?
I totally get the frustration here—PythonOCC’s default millimeter conversion throws off your manual bounding box coordinates when working with inch-based STEP files. Let’s fix this using the UnitsAPI you mentioned, which is exactly the right tool for the job.
First, Understand the Root Issue
OpenCASCADE (the underlying library powering PythonOCC) defaults to millimeters as the base length unit. When you load an inch-based STEP file, it automatically scales all geometry by 25.4 to convert to mm. But when you manually input coordinates (thinking in inches), those values are treated as mm directly—hence the massive mismatch between your model and custom bounding box.
Step-by-Step Solution
The key is to reconfigure PythonOCC’s unit system before loading your STEP file or performing any geometry operations. Here’s how to do it:
Import the Required UnitsAPI Module
Start by importing the functions needed to adjust the unit system:from OCC.Core.UnitsAPI import UnitsAPI_SetSystem, UnitsAPI_SI, UnitsAPI_INCHSet the Default Unit to Inches
CallUnitsAPI_SetSystemto switch the base length unit to inches. We useUnitsAPI_SIas the system (since it’s flexible enough to work with inch-based lengths) andUnitsAPI_INCHas the length reference:# Set default unit system to inches UnitsAPI_SetSystem(UnitsAPI_SI, UnitsAPI_INCH)Important: Run this line before loading your STEP file or initializing the display. This ensures all subsequent operations use inches as the base unit.
Load Your STEP File
Now load your inch-based STEP file—PythonOCC will no longer convert its geometry to millimeters:from OCC.Core.STEPControl import STEPControl_Reader reader = STEPControl_Reader() reader.ReadFile("your_inch_part.step") reader.TransferRoots() part_shape = reader.OneShape()Create Bounding Boxes with Inch Coordinates
Any manual coordinates you input now will be interpreted as inches, so your custom bounding box will match the model perfectly:from OCC.Core.Bnd import Bnd_Box from OCC.Core.BRepBndLib import brepbndlib_Add # Get the model's auto-generated bounding box (in inches) model_bbox = Bnd_Box() brepbndlib_Add(part_shape, model_bbox) print("Model BBox (inches):", model_bbox.Get()) # Create a custom bounding box using inch coordinates custom_bbox = Bnd_Box() custom_bbox.Add(0, 0, 0) # Min corner (x,y,z in inches) custom_bbox.Add(12, 8, 4) # Max corner (x,y,z in inches) print("Custom BBox (inches):", custom_bbox.Get())Update the Display (If Needed)
If you’re using PythonOCC’s display tools, initialize the display after setting the unit system to ensure the coordinate labels reflect inches:from OCC.Display.SimpleGui import init_display # Initialize display with inch units display, start_display, _, _ = init_display() display.DisplayShape(part_shape, update=True) start_display()
Key Notes
- Always set the unit system first: This ensures all geometry loading, creation, and display operations use inches consistently.
- Avoid mixing manual unit conversions: While you could multiply your inch coordinates by 25.4 to match mm, using
UnitsAPIis far more reliable and avoids human error.
内容的提问来源于stack exchange,提问作者Richard

