|
| 1 | +from compas_viewer import Viewer |
| 2 | + |
| 3 | +import compas |
| 4 | +from compas.geometry import Box |
| 5 | +from compas.geometry import Circle |
| 6 | +from compas.geometry import Cylinder |
| 7 | +from compas.geometry import Frame |
| 8 | +from compas.geometry import NurbsCurve |
| 9 | +from compas.geometry import NurbsSurface |
| 10 | +from compas.geometry import Plane |
| 11 | +from compas.geometry import Point |
| 12 | +from compas.geometry import Sphere |
| 13 | +from compas.geometry import Vector |
| 14 | +from compas_occ.brep import OCCBrep |
| 15 | +from compas_occ.brep import OCCBrepEdge |
| 16 | +from compas_occ.brep import OCCBrepFace |
| 17 | +from compas_occ.brep import OCCBrepLoop |
| 18 | + |
| 19 | + |
| 20 | +def brep_with_hole(): |
| 21 | + points = [ |
| 22 | + [Point(0, 0, 0), Point(1, 0, 0), Point(2, 0, 0), Point(3, 0, 0)], |
| 23 | + [Point(0, 1, 0), Point(1, 1, 2), Point(2, 1, 2), Point(3, 1, 0)], |
| 24 | + [Point(0, 2, 0), Point(1, 2, 2), Point(2, 2, 2), Point(3, 2, 0)], |
| 25 | + [Point(0, 3, 0), Point(1, 3, 0), Point(2, 3, 0), Point(3, 3, 0)], |
| 26 | + ] |
| 27 | + surface = NurbsSurface.from_points(points=points) |
| 28 | + circle = Circle( |
| 29 | + 0.5, |
| 30 | + frame=Frame( |
| 31 | + Point(1.5, 1.5, 1.5), |
| 32 | + Vector(1, 0, 0), |
| 33 | + Vector(0, 1, 0), |
| 34 | + ), |
| 35 | + ) |
| 36 | + curve = NurbsCurve.from_circle(circle) |
| 37 | + edge = OCCBrepEdge.from_curve_and_surface(curve=curve, surface=surface) |
| 38 | + loop = OCCBrepLoop.from_edges([edge]) |
| 39 | + face = OCCBrepFace.from_surface(surface) |
| 40 | + face.add_loop(loop) |
| 41 | + brep = OCCBrep.from_brepfaces([face]) |
| 42 | + return brep |
| 43 | + |
| 44 | + |
| 45 | +def brep_with_holes(): |
| 46 | + circle1 = Circle(1.0, frame=Frame([2, 2, 0])) |
| 47 | + circle2 = Circle(2.0, frame=Frame([-2, -2, 0])) |
| 48 | + circle3 = Circle(0.5, frame=Frame([2, -2, 0])) |
| 49 | + |
| 50 | + loop1 = OCCBrepLoop.from_edges([OCCBrepEdge.from_circle(circle1)]) |
| 51 | + loop2 = OCCBrepLoop.from_edges([OCCBrepEdge.from_circle(circle2)]) |
| 52 | + loop3 = OCCBrepLoop.from_edges([OCCBrepEdge.from_circle(circle3)]) |
| 53 | + |
| 54 | + face = OCCBrepFace.from_plane(Plane.worldXY(), domain_u=(-5, 5), domain_v=(-5, 5)) |
| 55 | + face.add_loops([loop1, loop2, loop3], reverse=True) |
| 56 | + |
| 57 | + brep = OCCBrep.from_brepfaces([face]) |
| 58 | + return brep |
| 59 | + |
| 60 | + |
| 61 | +def brep_from_booleans(): |
| 62 | + R = 1.4 |
| 63 | + |
| 64 | + box = Box(2 * R).to_brep() |
| 65 | + sphere = Sphere(radius=1.25 * R).to_brep() |
| 66 | + |
| 67 | + cylx = Cylinder(radius=0.7 * R, height=3 * R, frame=Frame.worldYZ()).to_brep() |
| 68 | + cyly = Cylinder(radius=0.7 * R, height=3 * R, frame=Frame.worldZX()).to_brep() |
| 69 | + cylz = Cylinder(radius=0.7 * R, height=3 * R, frame=Frame.worldXY()).to_brep() |
| 70 | + |
| 71 | + brep = OCCBrep.from_boolean_intersection(box, sphere) |
| 72 | + brep = OCCBrep.from_boolean_difference(brep, cylz) |
| 73 | + brep = OCCBrep.from_boolean_difference(brep, cylx) |
| 74 | + brep = OCCBrep.from_boolean_difference(brep, cyly) |
| 75 | + return brep |
| 76 | + |
| 77 | + |
| 78 | +# ============================================================================= |
| 79 | +# Dump/Load |
| 80 | +# ============================================================================= |
| 81 | + |
| 82 | +# brep = OCCBrep.from_box(Box(1)) |
| 83 | +# brep = OCCBrep.from_sphere(Sphere(1.0)) |
| 84 | +# brep = OCCBrep.from_cylinder(Cylinder(1.0, 2.0)) |
| 85 | +brep = brep_from_booleans() |
| 86 | +# brep = brep_with_hole() |
| 87 | +# brep = brep_with_holes() |
| 88 | + |
| 89 | +brep: OCCBrep = compas.json_loads(compas.json_dumps(brep)) # type: ignore |
| 90 | + |
| 91 | +# ============================================================================= |
| 92 | +# Viz |
| 93 | +# ============================================================================= |
| 94 | + |
| 95 | +viewer = Viewer() |
| 96 | +viewer.scene.add(brep) |
| 97 | +viewer.show() |
0 commit comments