Skip to content

Instantly share code, notes, and snippets.

@jdegenstein
Last active July 2, 2022 02:48
Show Gist options
  • Save jdegenstein/1cceb9b280e0a183af1f0e4173904353 to your computer and use it in GitHub Desktop.
Save jdegenstein/1cceb9b280e0a183af1f0e4173904353 to your computer and use it in GitHub Desktop.
CadQuery OCC CUT Hash Testing with Output of New Faces
#CadQuery OCC Hash Testing with Output of New Faces
#cq_occ_hash_woutput.py
import cadquery as cq
from random import randrange as rrr
from random import seed
seed(10) #set seed for consistent colors
from OCP.BRepPrimAPI import (
BRepPrimAPI_MakePrism,
)
from OCP.BRepAlgoAPI import (
BRepAlgoAPI_Common,
BRepAlgoAPI_Fuse,
BRepAlgoAPI_Cut,
BRepAlgoAPI_BooleanOperation,
)
from OCP.TopTools import TopTools_IndexedDataMapOfShapeListOfShape, TopTools_ListOfShape
from OCP.TopExp import TopExp_Explorer # Toplogy explorer
base = cq.Workplane("XY").box(80, 60, 10)
to_cut_out = (base.faces(">Z")
.workplane(offset=1).tag("cutp")
.rect(40,30,forConstruction=True)
.vertices().circle(5)
.extrude(-5,combine=False)
.chamfer(1)
.workplaneFromTagged("cutp")
.rect(10,30)
.extrude(-5)
.faces("<Z").edges()
.fillet(1)
)
print(40*"#"+"\n")#run separator
def rnco(): return rrr(10,100),rrr(10,100),rrr(10,100) #random color, dont want too bright
HASH_UPPER_BOUND = 2147483647
def dump_occ_obj(occ_obj, name="something"):
r = []
r.append("Dumping: " + name)
o = cq.Shape.cast(occ_obj)
#DISABLE vertices and edges for now
#r.append("Vertices::")
#for v in o.Vertices():
# r.append("\t" + str(v.wrapped.HashCode(HASH_UPPER_BOUND)))
#r.append("Edges::")
#for e in o.Edges():
# r.append("\t" + str(e.wrapped.HashCode(HASH_UPPER_BOUND)))
r.append("Faces::")
for f in o.Faces():
r.append("\t" + str(f.wrapped.HashCode(HASH_UPPER_BOUND)))
print("\n".join(r))
def occ_native_cut(to_cut_from, to_cut):
def list_of_one_shape(s):
rv = TopTools_ListOfShape()
rv.Append(s)
return rv
cut_op = BRepAlgoAPI_Cut()
cut_op.SetArguments( list_of_one_shape(to_cut_from))
cut_op.SetTools( list_of_one_shape(to_cut))
cut_op.Build()
return cut_op.Shape()
def show_changed_faces(inob1,inob2,outob):
#inob1, inob2 and outob are OCC objects
#goal is to output faces in outob that
#are NOT in face set(inob1,inob2) [case1]
#are NOT in face set(inob1) [case2]
i1_f = cq.Shape.cast(inob1).Faces()
i2_f = cq.Shape.cast(inob2).Faces()
o_f = cq.Shape.cast(outob).Faces()
def HashList(m_f):
f_hash = []
for m in m_f:
f_hash.append(m.wrapped.HashCode(HASH_UPPER_BOUND))
return f_hash
def set_comp_brandnew(a,b,c):
a1 = a+b #put two parent objects together
bnewf = []
for idx,m in enumerate(c):
if m not in a1: #find new faces
print(True,m,idx)
bnewf.append(o_f[idx]) #face object
return bnewf
result = set_comp_brandnew(HashList(i1_f),HashList(i2_f),HashList(o_f))
show_object(cq.Workplane().add(result).translate((0,-65,0)),name="brandnewfaces",options={"alpha":0.80, "color": rnco()})
def set_comp_notfromOB1(a,c):
notob1f = []
for idx,m in enumerate(c):
if m not in a: #find new faces
print(True,m,idx)
notob1f.append(o_f[idx]) #face object
return notob1f
result2 = set_comp_notfromOB1(HashList(i1_f),HashList(o_f))
show_object(cq.Workplane().add(result2).translate((0,65,0)),name="notfromOB1",options={"alpha":0.80, "color": rnco()})
def set_comp_notfromOB2(b,c):
notob2f = []
for idx,m in enumerate(c):
if m not in a: #find new faces
print(True,m,idx)
notob2f.append(o_f[idx]) #face object
return notob2f
result3 = set_comp_notfromOB1(HashList(i2_f),HashList(o_f))
show_object(cq.Workplane().add(result3).translate((0,65*2,0)),name="notfromOB2",options={"alpha":0.80, "color": rnco()})
occ_base = base.val().wrapped
occ_plug = to_cut_out.val().wrapped
occ_cut_result = occ_native_cut(occ_base,occ_plug)
show_object(occ_cut_result,options={"color":(55,0,55)})
show_changed_faces(occ_base,occ_plug,occ_cut_result)
#show_object(base,options={"color":(0,55,0)})
#show_object(to_cut_out, options={"color":(55,0,0)})
dump_occ_obj(occ_cut_result, name="cut result")
dump_occ_obj(occ_base, name="original")
dump_occ_obj(occ_plug, name="plug")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment