|
| 1 | +import math |
| 2 | +from compas.geometry import Polygon, Polyline, Rotation |
| 3 | +from compas.geometry import normal_polygon |
| 4 | + |
| 5 | +from compas_occ.geometry import OCCNurbsSurface |
| 6 | +from compas_occ.geometry import OCCNurbsCurve |
| 7 | +from compas_occ.brep import BRep |
| 8 | + |
| 9 | +from compas_view2.app import App |
| 10 | + |
| 11 | +from OCC.Core.BRepFill import BRepFill_Filling |
| 12 | +from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakePolygon |
| 13 | +from OCC.Core.GeomAbs import GeomAbs_C0 |
| 14 | +from OCC.Core.gp import gp_Pnt |
| 15 | +from OCC.Extend.TopologyUtils import TopologyExplorer |
| 16 | + |
| 17 | +from OCC.Core.BRep import BRep_Tool |
| 18 | +from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge |
| 19 | +from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh |
| 20 | +from OCC.Core.TopExp import TopExp_Explorer |
| 21 | +from OCC.Core.TopoDS import topods_Face |
| 22 | +from OCC.Core.TopAbs import TopAbs_FACE |
| 23 | +from OCC.Core.TopLoc import TopLoc_Location |
| 24 | + |
| 25 | +# ============================================================================== |
| 26 | +# Input polygon |
| 27 | +# ============================================================================== |
| 28 | + |
| 29 | +R = Rotation.from_axis_and_angle([1, 1, 0], math.radians(30)) |
| 30 | + |
| 31 | +polygon = Polygon.from_sides_and_radius_xy(5, 2) |
| 32 | +polygon.transform(R) |
| 33 | + |
| 34 | +# ============================================================================== |
| 35 | +# BRep Polygon |
| 36 | +# ============================================================================== |
| 37 | + |
| 38 | +points = [gp_Pnt(* point) for point in polygon] |
| 39 | + |
| 40 | +poly = BRepBuilderAPI_MakePolygon() |
| 41 | +for point in points: |
| 42 | + poly.Add(point) |
| 43 | +poly.Build() |
| 44 | +poly.Close() |
| 45 | + |
| 46 | +# ============================================================================== |
| 47 | +# BRep Filling |
| 48 | +# ============================================================================== |
| 49 | + |
| 50 | +edges = list(TopologyExplorer(poly.Wire()).edges()) |
| 51 | + |
| 52 | +nsided = BRepFill_Filling() |
| 53 | +for edge in edges: |
| 54 | + nsided.Add(edge, GeomAbs_C0) |
| 55 | +nsided.Add(gp_Pnt(* (polygon.centroid + normal_polygon(polygon)))) |
| 56 | +nsided.Build() |
| 57 | + |
| 58 | +# ============================================================================== |
| 59 | +# Surface from BRep Filling Face |
| 60 | +# ============================================================================== |
| 61 | + |
| 62 | +face = nsided.Face() |
| 63 | +surface = OCCNurbsSurface.from_face(face) |
| 64 | + |
| 65 | +# ============================================================================== |
| 66 | +# BRep |
| 67 | +# ============================================================================== |
| 68 | + |
| 69 | +brep = BRep() |
| 70 | +brep.shape = face |
| 71 | + |
| 72 | +# mesh = brep.to_tesselation() |
| 73 | + |
| 74 | +BRepMesh_IncrementalMesh(brep.shape, 0.1, False, 0.1, False) |
| 75 | + |
| 76 | +bt = BRep_Tool() |
| 77 | +ex = TopExp_Explorer(brep.shape, TopAbs_FACE) |
| 78 | +while ex.More(): |
| 79 | + face = topods_Face(ex.Current()) |
| 80 | + location = TopLoc_Location() |
| 81 | + facing = (bt.Triangulation(face, location)) |
| 82 | + tab = facing.Nodes() |
| 83 | + tri = facing.Triangles() |
| 84 | + for i in range(1, facing.NbTriangles() + 1): |
| 85 | + trian = tri.Value(i) |
| 86 | + index1, index2, index3 = trian.Get() |
| 87 | + # for j in range(1, 4): |
| 88 | + # if j == 1: |
| 89 | + # m = index1 |
| 90 | + # n = index2 |
| 91 | + # elif j == 2: |
| 92 | + # n = index3 |
| 93 | + # elif j == 3: |
| 94 | + # m = index2 |
| 95 | + # me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n)) |
| 96 | + # if me.IsDone(): |
| 97 | + # builder.Add(comp, me.Edge()) |
| 98 | + ex.Next() |
| 99 | + |
| 100 | +# ============================================================================== |
| 101 | +# Viz |
| 102 | +# ============================================================================== |
| 103 | + |
| 104 | +viewer = App() |
| 105 | + |
| 106 | +# viewer.add(surface.to_mesh()) |
| 107 | +viewer.add(mesh) |
| 108 | + |
| 109 | +for edge in edges: |
| 110 | + curve = OCCNurbsCurve.from_edge(edge) |
| 111 | + viewer.add(Polyline(curve.locus(resolution=16)), linecolor=(1, 0, 0), linewidth=5) |
| 112 | + |
| 113 | +viewer.run() |
0 commit comments