Skip to main content
 首页 » 编程设计

pythonOCC 将默认单位设置为英寸

2024年12月31日10mq0036

请参阅简短版本的编辑

为此一直在 pythonOCC 文档中寻找。

我有以英寸为单位的 .step 文件。以下是 .step 文件中用于确认的行:

#50 =  ( CONVERSION_BASED_UNIT( 'INCH', #122 )LENGTH_UNIT(  )NAMED_UNIT( #125 ) ); 
#51 =  ( NAMED_UNIT( #127 )PLANE_ANGLE_UNIT(  )SI_UNIT( $, .RADIAN. ) ); 
#52 =  ( NAMED_UNIT( #127 )SI_UNIT( $, .STERADIAN. )SOLID_ANGLE_UNIT(  ) ); 
~~~ 
#122 = LENGTH_MEASURE_WITH_UNIT( LENGTH_MEASURE( 25.4000000000000 ), #267 ); 

文件读取并显示在窗口中:

当我使用手动坐标制作边界框时,我发现我的框偏离了方向:

位置关闭,因为 STEP 模型不在 0,0,0。

事实证明 pythonOCC 会自动将所有内容转换为 MM。当我以英寸为单位手动输入盒子尺寸时,它会将它们读取为毫米。我也尝试通过手动转换所有内容(英寸 * 25.4)来处理,但这是有问题且丑陋的。

我知道 pythonOCC 使用 STEP 文件行 #122 作为转换率,因为我已将其从上面更改为:

#122 = LENGTH_MEASURE_WITH_UNIT( LENGTH_MEASURE( 1.0 ), #267 ); 

当我这样做时,我的边界框和步进模型完美对齐......但我仍然知道 PythonOCC 认为它正在转换为 MM。

有没有人有过更改 pythonocc 的默认单位的经验? 我试图在以下 occ 包中找到: OCC.STEPControl、OCC.Display、OCC.AIS 和许多其他人。

编辑:

当我像这样使用自己的坐标绘制框时:

minPoint = gp_Pnt(minCoords) 
maxPoint = gp_Pnt(maxCoords) 
my_box = AIS_Shape(BRepPrimAPI_MakeBox(minPoint, maxPoint).Shape()) 
display.Context.Display(my_box.GetHandle()) 

我的坐标以英寸为单位,但 pythonOCC 将它们读取为 MM。如果我可以让我自己的坐标以英寸为单位读取,这将得到解决。在 OCC.Display 中找不到任何关于我的坐标是如何解释的。像“OCC.Display.inputUnitsAre("INCHES")”之类的东西?

编辑 2:

近距离观察这里:

https://dev.opencascade.org/doc/refman/html/class_units_a_p_i.html

在 UnitsAPI_SystemUnits 和 SetCurrentUnit 下...虽然我不确定如何在 python 中实现尚未测试。正在努力。

请您参考如下方法:

您会找到 units here 的文档

看看OCC.Extended.DataExchange 模块,您会看到以下函数:

def write_step_file(a_shape, filename, application_protocol="AP203"): 
    """ exports a shape to a STEP file 
    a_shape: the topods_shape to export (a compound, a solid etc.) 
    filename: the filename 
    application protocol: "AP203" or "AP214" 
    """ 
    # a few checks 
    assert not a_shape.IsNull() 
    assert application_protocol in ["AP203", "AP214IS"] 
    if os.path.isfile(filename): 
        print("Warning: %s file already exists and will be replaced" % filename) 
    # creates and initialise the step exporter 
    step_writer = STEPControl_Writer() 
    Interface_Static_SetCVal("write.step.schema", "AP203") 
 
    # transfer shapes and write file 
    step_writer.Transfer(a_shape, STEPControl_AsIs) 
    status = step_writer.Write(filename) 
 
    assert status == IFSelect_RetDone 
    assert os.path.isfile(filename) 

默认情况下,OCC 以毫米为单位写入单位,所以我很好奇使用什么函数/方法导出您的STEP 文件。

Interface_Static_SetCVal("Interface_Static_SetCVal("write.step.unit","MM") 

虽然文档声明此方法 定义了一个单元,STEP 文件应该写入该单元。如果设置为 MM 以外的单位,模型将在翻译过程中转换为这些单位。,因此必须明确设置此单位是意外的。