結果
最小構成 / Minimum Example
from random import random from OCC.Core.TDocStd import TDocStd_Document from OCC.Core.XCAFApp import XCAFApp_Application from OCC.Core.XCAFDoc import ( XCAFDoc_DocumentTool_ShapeTool, XCAFDoc_DocumentTool_ColorTool, XCAFDoc_ColorGen, ) from OCC.Core.TCollection import TCollection_ExtendedString from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB from OCC.Core.STEPCAFControl import STEPCAFControl_Writer # https://techoverflow.net/2019/06/14/how-to-export-colored-step-files-in-opencascade/ def test(): app = XCAFApp_Application.GetApplication() doc = TDocStd_Document(TCollection_ExtendedString("")) app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"), doc) shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) color_tool = XCAFDoc_DocumentTool_ColorTool(doc.Main()) # Create a box box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() for face in TopologyExplorer(box).faces(): label = shape_tool.AddShape(face) color = Quantity_Color(random(), random(), random(), Quantity_TOC_RGB) color_tool.SetColor(label, color, XCAFDoc_ColorGen) # Export to STEP step_writer = STEPCAFControl_Writer() step_writer.SetColorMode(True) step_writer.Perform(doc, "hogehoge.stp") if __name__ == "__main__": test()
クラス化 / Utility Class
from OCC.Core.TDocStd import TDocStd_Document from OCC.Core.XCAFApp import XCAFApp_Application from OCC.Core.XCAFDoc import ( XCAFDoc_DocumentTool_ShapeTool, XCAFDoc_DocumentTool_ColorTool, XCAFDoc_ColorGen, ) from OCC.Core.TCollection import TCollection_ExtendedString from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB from OCC.Core.STEPCAFControl import STEPCAFControl_Writer class ColoredStepExport: def __init__(self): app = XCAFApp_Application.GetApplication() self.doc = TDocStd_Document(TCollection_ExtendedString("")) app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"), self.doc) self.shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main()) self.color_tool = XCAFDoc_DocumentTool_ColorTool(self.doc.Main()) def add_shape(self, shape, color=None): label = self.shape_tool.AddShape(shape) if color: if isinstance(color, Quantity_Color): self.color_tool.SetColor(label, color, XCAFDoc_ColorGen) elif isinstance(color, (list, tuple)) and len(color) == 3: color = Quantity_Color(*[c / 255 for c in color], Quantity_TOC_RGB) self.color_tool.SetColor(label, color, XCAFDoc_ColorGen) else: raise ValueError("Invalid color format") def export(self, filename): step_writer = STEPCAFControl_Writer() step_writer.SetColorMode(True) step_writer.Perform(self.doc, filename) if __name__ == "__main__": cse = ColoredStepExport() box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() for i, face in enumerate(TopologyExplorer(box).faces()): if i % 3 == 0: cse.add_shape(face) elif i % 3 == 1: color = Quantity_Color(1, 1, 0, Quantity_TOC_RGB) cse.add_shape(face, color=color) elif i % 3 == 2: cse.add_shape(face, color=[0, 128, 255]) cse.export("hogehoge.stp")
現状の問題点
面分ごとに1つの要素として出力されてしまっている?