| 444 | |
| 445 | |
| 446 | def toCAF( |
| 447 | assy: AssemblyProtocol, |
| 448 | coloredSTEP: bool = False, |
| 449 | mesh: bool = False, |
| 450 | tolerance: float = 1e-3, |
| 451 | angularTolerance: float = 0.1, |
| 452 | binary: bool = True, |
| 453 | ) -> Tuple[TDF_Label, TDocStd_Document]: |
| 454 | |
| 455 | # prepare a doc |
| 456 | app = XCAFApp_Application.GetApplication_s() |
| 457 | |
| 458 | if binary: |
| 459 | BinXCAFDrivers.DefineFormat_s(app) |
| 460 | doc = TDocStd_Document(TCollection_ExtendedString("BinXCAF")) |
| 461 | else: |
| 462 | XmlXCAFDrivers.DefineFormat_s(app) |
| 463 | doc = TDocStd_Document(TCollection_ExtendedString("XmlXCAF")) |
| 464 | |
| 465 | app.InitDocument(doc) |
| 466 | |
| 467 | tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main()) |
| 468 | tool.SetAutoNaming_s(False) |
| 469 | ctool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main()) |
| 470 | ltool = XCAFDoc_DocumentTool.LayerTool_s(doc.Main()) |
| 471 | mtool = XCAFDoc_DocumentTool.MaterialTool_s(doc.Main()) |
| 472 | |
| 473 | # used to store labels with unique part-color combinations |
| 474 | unique_objs: Dict[Tuple[Color | None, AssemblyObjects], TDF_Label] = {} |
| 475 | # used to cache unique, possibly meshed, compounds; allows to avoid redundant meshing operations if same object is referenced multiple times in an assy |
| 476 | compounds: Dict[AssemblyObjects, Compound] = {} |
| 477 | |
| 478 | def _toCAF(el: AssemblyProtocol, ancestor: TDF_Label | None) -> TDF_Label: |
| 479 | |
| 480 | # create a subassy if needed |
| 481 | if el.children: |
| 482 | subassy = tool.NewShape() |
| 483 | setName(subassy, el.name, tool) |
| 484 | |
| 485 | # define the current color |
| 486 | current_color = el.color if el.color else None |
| 487 | |
| 488 | # define the current material |
| 489 | current_material = el.material if el.material else None |
| 490 | |
| 491 | # add a leaf with the actual part if needed |
| 492 | if el.obj: |
| 493 | # get/register unique parts referenced in the assy |
| 494 | key0 = (current_color, el.obj) # (color, shape) |
| 495 | key1 = el.obj # shape |
| 496 | |
| 497 | if key0 in unique_objs: |
| 498 | lab = unique_objs[key0] |
| 499 | else: |
| 500 | lab = tool.NewShape() |
| 501 | if key1 in compounds: |
| 502 | compound = compounds[key1].copy(mesh) |
| 503 | else: |