Skip to content

Instantly share code, notes, and snippets.

@jdegenstein
Created June 30, 2022 20:17
Show Gist options
  • Save jdegenstein/0b46074571ef33dee82e238e827033c7 to your computer and use it in GitHub Desktop.
Save jdegenstein/0b46074571ef33dee82e238e827033c7 to your computer and use it in GitHub Desktop.
CadQuery OCC Hash Testing
import cadquery as cq
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().circle(10).extrude(-5,combine=False)
print(40*"#"+"\n")#run separator
HASH_UPPER_BOUND = 2147483647
def dump_occ_obj(occ_obj, name="something"):
r = []
r.append("Dumping: " + name)
o = cq.Shape.cast(occ_obj)
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()
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":(255,0,255)})
show_object(base,options={"color":(0,255,0)})
show_object(to_cut_out, options={"color":(255,0,0)})
dump_occ_obj(occ_base, name="original")
dump_occ_obj(occ_plug, name="plug")
dump_occ_obj(occ_cut_result, name="cut result")
'''
Example Output
########################################
Dumping: original
Vertices::
424920253
424920861
424929037
424921613
424925485
424920525
424920813
424927837
Edges::
406179373
406177581
406074029
406076973
406177837
406075949
406072493
406176813
406073773
406075565
406076205
406179757
Faces::
407557965
407556493
407453677
407555661
407554541
407556701
Dumping: plug
Vertices::
1944709900
1944711180
Edges::
21368556
21365484
21365228
Faces::
21102124
21101996
21103244
Dumping: cut result
Vertices::
424920253
424920861
424929037
424921613
424920525
424925485
424920813
1944709900
424927837
1944711180
Edges::
406179373
406177581
406074029
406076973
406073773
406177837
406075565
406179757
406075949
21365484
406076205
406072493
406176813
21368556
21365228
Faces::
407557965
407453677
21097420
407555661
407554541
407556493
21102124
21103244
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment