Skip to content

Instantly share code, notes, and snippets.

@fragmuffin
Last active September 9, 2017 15:32
Show Gist options
  • Save fragmuffin/814c2e89e7eabdfca2059b9a511695a1 to your computer and use it in GitHub Desktop.
Save fragmuffin/814c2e89e7eabdfca2059b9a511695a1 to your computer and use it in GitHub Desktop.
CadQuery intersection workaround
import cadquery as cq
from Helpers import show
# remake of the wikiedia example of a CSG intersect:
# https://en.wikipedia.org/wiki/Constructive_solid_geometry#Workings
sphere = cq.Workplane("XY").sphere(7.5).translate((5, 5, 5))
box = cq.Workplane("XY").box(10, 10, 10)
def copy(obj):
return obj.translate((0, 0, 0))
#intersection = sphere.intersect(box) # doesn't work
def intersect(wp1, wp2):
"""
Return geometric intersection between 2 cadquery.Workplane instances by
exploiting.
A n B = (A u B) - ((A - B) u (B - A))
"""
neg1 = copy(wp1).cut(wp2)
neg2 = copy(wp2).cut(wp1)
negative = neg1.union(neg2)
return copy(wp1).union(wp2).cut(negative)
intersection = intersect(box, sphere)
# Display the primatives being intersected as shades of red/green
show(sphere, (255, 100, 100, 0.8)) # red
show(box, (100, 255, 100, 0.8)) # green
# Display resulting intersection solid
show(intersection)
@fragmuffin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment