Skip to content

Instantly share code, notes, and snippets.

@jmwright
Created January 25, 2023 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmwright/f541f2bf78697a1f157349ccc4a63351 to your computer and use it in GitHub Desktop.
Save jmwright/f541f2bf78697a1f157349ccc4a63351 to your computer and use it in GitHub Desktop.
from OCP.Quantity import Quantity_Color, Quantity_TOC_RGB
from OCP.TopoDS import TopoDS_Compound
from OCP.STEPControl import STEPControl_Writer, STEPControl_AsIs
from OCP.BRep import BRep_Tool, BRep_Builder
from OCP.TCollection import TCollection_ExtendedString
from OCP.TDocStd import TDocStd_Application, TDocStd_Document
from OCP.XCAFDoc import XCAFDoc_DocumentTool, XCAFDoc_ColorType, XCAFDoc_ColorGen
from OCP.STEPCAFControl import STEPCAFControl_Writer
from OCP.XSControl import XSControl_WorkSession
from OCP.TDataStd import TDataStd_Name
import cadquery as cq
# Build a simple assembly
assy = cq.Assembly()
box1 = cq.Workplane().box(10, 10, 10)
assy.add(box1, color=cq.Color(1, 0, 0))
box2 = cq.Workplane().center(5, 5).box(10, 10, 10)
assy.add(box2, color=cq.Color(0, 1, 0))
##############################
## EXPORT ####################
##############################
# The document
app = TDocStd_Application()
doc = TDocStd_Document(TCollection_ExtendedString("XmlOcaf"))
app.InitDocument(doc)
# The shape tool
shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main())
# The color tool
color_tool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
# Set up the compound
comp = TopoDS_Compound()
comp_builder = BRep_Builder()
comp_builder.MakeCompound(comp)
comp_builder.Add(comp, box1.val().wrapped)
comp_builder.Add(comp, box2.val().wrapped)
# Compound assembly type
assy_prototype = {}
assy_prototype["shape"] = comp
assy_prototype["label"] = shape_tool.AddShape(assy_prototype["shape"], False)
# First box prototype
box1_prototype = {}
box1_prototype["shape"] = box1.val().wrapped
box1_prototype["label"] = shape_tool.AddSubShape(assy_prototype["label"], box1_prototype["shape"])
# Second box prototype
box2_prototype = {}
box2_prototype["shape"] = box2.val().wrapped
box2_prototype["label"] = shape_tool.AddSubShape(assy_prototype["label"], box2_prototype["shape"])
# Set a sane name for the label
TDataStd_Name.Set_s(assy_prototype["label"], TCollection_ExtendedString("boxes"))
# Set the colors
color_tool.SetColor(box1_prototype["label"], Quantity_Color(1, 0, 0, Quantity_TOC_RGB), XCAFDoc_ColorGen)
color_tool.SetColor(box2_prototype["label"], Quantity_Color(0, 1, 0, Quantity_TOC_RGB), XCAFDoc_ColorGen)
# Export to STEP
session = XSControl_WorkSession()
writer = STEPCAFControl_Writer(session, False)
# writer.SetLayerMode(False)
# writer.SetSHUOMode(False)
writer.Transfer(doc, STEPControl_AsIs)
status = writer.Write("/home/jwright/Downloads/xde_step_export_test.step")
# status = app.SaveAs(doc, TCollection_ExtendedString("/home/jwright/Downloads/xde_dump.xbf"))
# log(status)
show_object(assy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment