MCPcopy
hub / github.com/CadQuery/cadquery / toFusedCAF

Function toFusedCAF

cadquery/occ_impl/assembly.py:754–855  ·  view source on GitHub ↗

Converts the assembly to a fused compound and saves that within the document to be exported in a way that preserves the face colors. Because of the use of boolean operations in this method, performance may be slow in some cases. :param assy: Assembly that is being converted to a fu

(
    assy: AssemblyProtocol, glue: bool = False, tol: Optional[float] = None,
)

Source from the content-addressed store, hash-verified

752
753
754def toFusedCAF(
755 assy: AssemblyProtocol, glue: bool = False, tol: Optional[float] = None,
756) -> Tuple[TDF_Label, TDocStd_Document]:
757 """
758 Converts the assembly to a fused compound and saves that within the document
759 to be exported in a way that preserves the face colors. Because of the use of
760 boolean operations in this method, performance may be slow in some cases.
761
762 :param assy: Assembly that is being converted to a fused compound for the document.
763 """
764
765 # Prepare the document
766 app = XCAFApp_Application.GetApplication_s()
767 doc = TDocStd_Document(TCollection_ExtendedString("XmlOcaf"))
768 app.InitDocument(doc)
769
770 # Shape and color tools
771 shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main())
772 color_tool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
773
774 # To fuse the parts of the assembly together
775 fuse_op = BRepAlgoAPI_Fuse()
776 args = TopTools_ListOfShape()
777 tools = TopTools_ListOfShape()
778
779 # If there is only one solid, there is no reason to fuse, and it will likely cause problems anyway
780 top_level_shape = None
781
782 # Walk the entire assembly, collecting the located shapes and colors
783 shapes: List[Shape] = []
784 colors = []
785
786 for shape, _, loc, color in assy:
787 shapes.append(shape.moved(loc).copy())
788 colors.append(color)
789
790 # Initialize with a dummy value for mypy
791 top_level_shape = cast(TopoDS_Shape, None)
792
793 # If the tools are empty, it means we only had a single shape and do not need to fuse
794 if not shapes:
795 raise Exception(f"Error: Assembly {assy.name} has no shapes.")
796 elif len(shapes) == 1:
797 # There is only one shape and we only need to make sure it is a Compound
798 # This seems to be needed to be able to add subshapes (i.e. faces) correctly
799 sh = shapes[0]
800 if sh.ShapeType() != "Compound":
801 top_level_shape = Compound.makeCompound((sh,)).wrapped
802 elif sh.ShapeType() == "Compound":
803 sh = sh.fuse(glue=glue, tol=tol)
804 top_level_shape = Compound.makeCompound((sh,)).wrapped
805 shapes = [sh]
806 else:
807 # Set the shape lists up so that the fuse operation can be performed
808 args.Append(shapes[0].wrapped)
809
810 for shape in shapes[1:]:
811 tools.Append(shape.wrapped)

Callers 2

test_colors_fused_assyFunction · 0.90
exportAssemblyFunction · 0.85

Calls 7

appendMethod · 0.80
makeCompoundMethod · 0.80
copyMethod · 0.45
movedMethod · 0.45
ShapeTypeMethod · 0.45
fuseMethod · 0.45
FacesMethod · 0.45

Tested by 1

test_colors_fused_assyFunction · 0.72