Last active
July 2, 2022 02:50
-
-
Save jdegenstein/93868d98120272f493f11a9f43d8015c to your computer and use it in GitHub Desktop.
CadQuery OCC FUSE Hash Testing with Output of New Faces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
layer1 = cq.Workplane("XY").box(80, 60, 20) | |
layer2 = (cq.Workplane("XY") | |
.workplane(offset=10) | |
.box(60,40,20) | |
) | |
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_fuse(to_add1, to_add2): | |
def list_of_one_shape(s): | |
rv = TopTools_ListOfShape() | |
rv.Append(s) | |
return rv | |
fuse_op = BRepAlgoAPI_Fuse() | |
fuse_op.SetArguments( list_of_one_shape(to_add1)) | |
fuse_op.SetTools( list_of_one_shape(to_add2)) | |
fuse_op.Build() | |
return fuse_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.30, "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.30, "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.30, "color": rnco()}) | |
occ_layer1 = layer1.val().wrapped | |
occ_layer2 = layer2.val().wrapped | |
occ_fuse_result = occ_native_fuse(occ_layer1,occ_layer2) | |
show_object(occ_fuse_result,options={"color":(55,0,55)}) | |
show_changed_faces(occ_layer1,occ_layer2,occ_fuse_result) | |
#show_object(layer1.translate((0,65,0)),options={"color":rnco()}) | |
#show_object(layer2.translate((0,65*2,0)), options={"color":rnco()}) | |
dump_occ_obj(occ_fuse_result, name="fuse result") | |
dump_occ_obj(occ_layer1, name="layer1") | |
dump_occ_obj(occ_layer2, name="layer2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment