main
John Lauer
HCF166J3R0J: model, chip artwork, hero image and datasheet facts
b07e6a7
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
"""prim.py <model.json> <out.step>: build a coloured, named STEP from primitives (Adom Z-up, millimetres).
model.json: {"name": "...", "parts": [ {"name": "Body", "rgb": [r,g,b], "shape": <prim>, "cut": [<prim>...], "add": [<prim>...]} ]}
<prim>: {"box": [x0,y0,z0,x1,y1,z1]}
{"cyl": {"p": [x,y,z], "dir": [0,0,1], "r": 3, "h": 10}}
{"tube": {"p": [...], "dir": [...], "r": 3, "ri": 2, "h": 10}}
{"cone": {"p": [...], "dir": [...], "r1": 3, "r2": 1, "h": 4}}
{"sphere": {"c": [x,y,z], "r": 2}}
{"pipe": {"points": [[x,y,z],...], "r": 1}}
{"rbox": [x0,y0,z0,x1,y1,z1], "r": 1} box with its vertical edges rounded
Every dimension in model.json should carry its source in a sibling "sources" list; this script only builds."""
import json, sys
from OCP.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Vec, gp_Circ
from OCP.BRepPrimAPI import BRepPrimAPI_MakeCylinder, BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCone, BRepPrimAPI_MakeSphere
from OCP.BRepAlgoAPI import BRepAlgoAPI_Cut, BRepAlgoAPI_Fuse
from OCP.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace
from OCP.BRepOffsetAPI import BRepOffsetAPI_MakePipe
from OCP.BRepFilletAPI import BRepFilletAPI_MakeFillet
from OCP.TopExp import TopExp_Explorer
from OCP.TopAbs import TopAbs_EDGE
from OCP.TopoDS import TopoDS
from OCP.BRepAdaptor import BRepAdaptor_Curve
from OCP.STEPCAFControl import STEPCAFControl_Writer
from OCP.TDocStd import TDocStd_Document
from OCP.XCAFDoc import XCAFDoc_DocumentTool, XCAFDoc_ColorType
from OCP.TCollection import TCollection_ExtendedString
from OCP.Quantity import Quantity_Color, Quantity_TOC_RGB
from OCP.TDataStd import TDataStd_Name
from OCP.STEPControl import STEPControl_AsIs
from OCP.TopLoc import TopLoc_Location
from OCP.Interface import Interface_Static
def ax(p, d): return gp_Ax2(gp_Pnt(*p), gp_Dir(*d))
def make(pr):
if 'box' in pr:
b = pr['box']; return BRepPrimAPI_MakeBox(gp_Pnt(*b[:3]), gp_Pnt(*b[3:])).Shape()
if 'rbox' in pr:
b = pr['rbox']; s = BRepPrimAPI_MakeBox(gp_Pnt(*b[:3]), gp_Pnt(*b[3:])).Shape()
f = BRepFilletAPI_MakeFillet(s); ex = TopExp_Explorer(s, TopAbs_EDGE)
while ex.More():
e = TopoDS.Edge_s(ex.Current()); c = BRepAdaptor_Curve(e)
p0, p1 = c.Value(c.FirstParameter()), c.Value(c.LastParameter())
if abs(p0.X() - p1.X()) < 1e-6 and abs(p0.Y() - p1.Y()) < 1e-6: f.Add(pr['r'], e)
ex.Next()
return f.Shape()
if 'cyl' in pr:
c = pr['cyl']; return BRepPrimAPI_MakeCylinder(ax(c['p'], c.get('dir', [0, 0, 1])), c['r'], c['h']).Shape()
if 'tube' in pr:
c = pr['tube']; o = BRepPrimAPI_MakeCylinder(ax(c['p'], c.get('dir', [0, 0, 1])), c['r'], c['h']).Shape()
d = c.get('dir', [0, 0, 1]); p = [c['p'][i] - d[i] for i in range(3)]
return BRepAlgoAPI_Cut(o, BRepPrimAPI_MakeCylinder(ax(p, d), c['ri'], c['h'] + 2).Shape()).Shape()
if 'cone' in pr:
c = pr['cone']; return BRepPrimAPI_MakeCone(ax(c['p'], c.get('dir', [0, 0, 1])), c['r1'], c['r2'], c['h']).Shape()
if 'sphere' in pr:
c = pr['sphere']; return BRepPrimAPI_MakeSphere(gp_Pnt(*c['c']), c['r']).Shape()
if 'pipe' in pr:
c = pr['pipe']; pts = c['points']; mw = BRepBuilderAPI_MakeWire()
for a, b in zip(pts, pts[1:]): mw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(*a), gp_Pnt(*b)).Edge())
d = gp_Dir(gp_Vec(gp_Pnt(*pts[0]), gp_Pnt(*pts[1])))
prof = BRepBuilderAPI_MakeFace(BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(gp_Circ(gp_Ax2(gp_Pnt(*pts[0]), d), c['r'])).Edge()).Wire()).Face()
return BRepOffsetAPI_MakePipe(mw.Wire(), prof).Shape()
raise ValueError(pr)
m = json.load(open(sys.argv[1]))
doc = TDocStd_Document(TCollection_ExtendedString("m"))
st = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main()); ct = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
asm = st.NewShape(); TDataStd_Name.Set_s(asm, TCollection_ExtendedString(m['name']))
for p in m['parts']:
s = make(p['shape'])
for a in p.get('add', []): s = BRepAlgoAPI_Fuse(s, make(a)).Shape()
for c in p.get('cut', []): s = BRepAlgoAPI_Cut(s, make(c)).Shape()
lab = st.AddShape(s, False); TDataStd_Name.Set_s(lab, TCollection_ExtendedString(p['name']))
ct.SetColor(lab, Quantity_Color(*p['rgb'], Quantity_TOC_RGB), XCAFDoc_ColorType.XCAFDoc_ColorSurf)
st.AddComponent(asm, lab, TopLoc_Location())
st.UpdateAssemblies()
Interface_Static.SetCVal_s("write.step.unit", "MM")
w = STEPCAFControl_Writer(); w.SetColorMode(True); w.SetNameMode(True); w.Transfer(doc, STEPControl_AsIs); w.Write(sys.argv[2])
print("OK", sys.argv[2], len(m['parts']), "parts")