Skip to content

Commit 994fafe

Browse files
committed
move serialisation mechanism to faces
1 parent be2f920 commit 994fafe

File tree

2 files changed

+102
-39
lines changed

2 files changed

+102
-39
lines changed

src/compas_occ/brep/brep.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,9 @@ class OCCBrep(Brep):
9797
@property
9898
def __data__(self) -> dict:
9999
return {
100-
"points": [],
101-
"curves": [],
102-
"surfaces": [],
103-
"vertices": [v.__data__ for v in self.vertices],
104-
"edges": [e.__data__ for e in self.edges],
105-
"faces": [f.__data__ for f in self.faces],
100+
# "vertices": [vertex.__data__ for vertex in self.vertices],
101+
# "edges": [edge.__data__ for edge in self.edges],
102+
"faces": [face.__data__ for face in self.faces],
106103
}
107104

108105
@classmethod
@@ -119,7 +116,11 @@ def __from_data__(cls, data: dict) -> "OCCBrep":
119116
:class:`compas_occ.geometry.OCCBrep`
120117
121118
"""
122-
raise NotImplementedError
119+
from .builder import OCCBrepBuilder
120+
121+
builder = OCCBrepBuilder()
122+
brep = builder.build(data["faces"])
123+
return brep
123124

124125
def __init__(self) -> None:
125126
super().__init__()

src/compas_occ/brep/brepface.py

Lines changed: 94 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from OCC.Core import BRepBuilderAPI
66
from OCC.Core import BRepGProp
77
from OCC.Core import BRepTools
8+
from OCC.Core import Geom
9+
from OCC.Core import Geom2d
810
from OCC.Core import GeomAbs
911
from OCC.Core import GeomConvert
1012
from OCC.Core import GProp
@@ -15,10 +17,20 @@
1517
from OCC.Core import gp
1618

1719
import compas.geometry
20+
import compas_occ.conversions
21+
from compas.geometry import Bezier
22+
from compas.geometry import BrepEdge
1823
from compas.geometry import BrepFace
24+
from compas.geometry import Circle
1925
from compas.geometry import Cone
2026
from compas.geometry import Cylinder
27+
from compas.geometry import Ellipse
2128
from compas.geometry import Frame
29+
from compas.geometry import Hyperbola
30+
from compas.geometry import Line
31+
from compas.geometry import NurbsCurve
32+
from compas.geometry import NurbsSurface
33+
from compas.geometry import Parabola
2234
from compas.geometry import Plane
2335
from compas.geometry import Polygon
2436
from compas.geometry import Sphere
@@ -27,6 +39,7 @@
2739
from compas_occ.brep import OCCBrepEdge
2840
from compas_occ.brep import OCCBrepLoop
2941
from compas_occ.brep import OCCBrepVertex
42+
from compas_occ.brep.brepedge import CurveType
3043
from compas_occ.conversions import cone_to_occ
3144
from compas_occ.conversions import cylinder_to_compas
3245
from compas_occ.conversions import cylinder_to_occ
@@ -36,6 +49,7 @@
3649
from compas_occ.conversions import sphere_to_compas
3750
from compas_occ.conversions import sphere_to_occ
3851
from compas_occ.conversions import torus_to_occ
52+
from compas_occ.geometry import OCCCurve2d
3953
from compas_occ.geometry import OCCNurbsSurface
4054
from compas_occ.geometry import OCCSurface
4155

@@ -66,14 +80,64 @@ class OCCBrepFace(BrepFace):
6680

6781
@property
6882
def __data__(self) -> dict:
69-
return {
83+
loops = []
84+
for loop in self.loops:
85+
edges = []
86+
for edge in loop.edges:
87+
edgedata = {
88+
"type": edge.type,
89+
"curve": edge.curve,
90+
"domain": edge.domain,
91+
"start": edge.first_vertex.point,
92+
"end": edge.last_vertex.point,
93+
"orientation": edge.orientation,
94+
"dimension": 3,
95+
}
96+
if edge.type == CurveType.CURVE2D:
97+
adaptor2 = BRepAdaptor.BRepAdaptor_Curve2d(edge.occ_edge, self.occ_face)
98+
type2 = adaptor2.GetType()
99+
100+
if type2 == GeomAbs.GeomAbs_CurveType.GeomAbs_Line:
101+
ctype = CurveType.LINE
102+
curve = compas_occ.conversions.line2d_to_compas(adaptor2.Line())
103+
104+
elif type2 == GeomAbs.GeomAbs_CurveType.GeomAbs_Circle:
105+
ctype = CurveType.CIRCLE
106+
curve = compas_occ.conversions.circle2d_to_compas(adaptor2.Circle())
107+
108+
elif type2 == GeomAbs.GeomAbs_CurveType.GeomAbs_Ellipse:
109+
ctype = CurveType.ELLIPSE
110+
curve = compas_occ.conversions.ellipse2d_to_compas(adaptor2.Ellipse())
111+
112+
elif type2 == GeomAbs.GeomAbs_CurveType.GeomAbs_Hyperbola:
113+
ctype = CurveType.HYPERBOLA
114+
curve = compas_occ.conversions.hyperbola2d_to_compas(adaptor2.Hyperbola())
115+
116+
elif type2 == GeomAbs.GeomAbs_CurveType.GeomAbs_Parabola:
117+
ctype = CurveType.PARABOLA
118+
curve = compas_occ.conversions.parabola2d_to_compas(adaptor2.Parabola())
119+
120+
else:
121+
raise NotImplementedError
122+
123+
edgedata["type"] = ctype
124+
edgedata["curve"] = curve
125+
edgedata["domain"] = [adaptor2.FirstParameter(), adaptor2.LastParameter()]
126+
edgedata["dimension"] = 2
127+
128+
edges.append(edgedata)
129+
loops.append(edges)
130+
131+
data = {
70132
"type": self.type,
71-
"surface": self.surface.__data__,
133+
"surface": self.surface,
72134
"domain_u": self.domain_u,
73135
"domain_v": self.domain_v,
74-
"frame": Frame.worldXY().__data__,
75-
"loops": [self.outerloop.__data__] + [loop.__data__ for loop in self.innerloops],
136+
"frame": Frame.worldXY(),
137+
"loops": loops,
138+
"orientation": self.orientation,
76139
}
140+
return data
77141

78142
@classmethod
79143
def __from_data__(cls, data: dict) -> "OCCBrepFace":
@@ -172,13 +236,6 @@ def occ_adaptor(self) -> BRepAdaptor.BRepAdaptor_Surface:
172236
def orientation(self) -> TopAbs.TopAbs_Orientation:
173237
return self.occ_face.Orientation()
174238

175-
# @property
176-
# def surface(self) -> OCCSurface:
177-
# if not self._surface:
178-
# occ_surface = self.occ_adaptor.Surface().Surface() # this is weird
179-
# self._surface = OCCSurface(occ_surface)
180-
# return self._surface
181-
182239
# remove this if possible
183240
@property
184241
def nurbssurface(self) -> OCCNurbsSurface:
@@ -199,8 +256,8 @@ def surface(self):
199256
return self.to_sphere()
200257
if self.is_torus:
201258
return self.to_torus()
202-
# if self.is_bspline:
203-
# return self.to_nurbs()
259+
if self.is_bspline:
260+
return self.to_nurbs()
204261
raise NotImplementedError
205262

206263
# ==============================================================================
@@ -231,11 +288,16 @@ def is_torus(self) -> bool:
231288
def is_cone(self) -> bool:
232289
return self.type == SurfaceType.CONE
233290

291+
@property
292+
def is_bezier(self) -> bool:
293+
return self.type == SurfaceType.BEZIER_SURFACE
294+
234295
@property
235296
def is_bspline(self) -> bool:
236297
return self.type == SurfaceType.BSPLINE_SURFACE
237298

238-
# bezier
299+
# other types of surfaces:
300+
# -----------------------
239301
# revolved
240302
# extruded
241303
# offset
@@ -575,8 +637,7 @@ def to_plane(self) -> Plane:
575637
if not self.is_plane:
576638
raise Exception("Face is not a plane.")
577639

578-
surface = self.occ_adaptor.Surface()
579-
plane = surface.Plane()
640+
plane = self.occ_adaptor.Plane()
580641
return plane_to_compas(plane)
581642

582643
def to_cylinder(self) -> Cylinder:
@@ -591,8 +652,7 @@ def to_cylinder(self) -> Cylinder:
591652
if not self.is_cylinder:
592653
raise Exception("Face is not a cylinder.")
593654

594-
surface = self.occ_adaptor.Surface()
595-
cylinder = surface.Cylinder()
655+
cylinder = self.occ_adaptor.Cylinder()
596656
return cylinder_to_compas(cylinder)
597657

598658
def to_cone(self) -> Cone:
@@ -604,12 +664,6 @@ def to_cone(self) -> Cone:
604664
:class:`compas.geometry.Cone`
605665
606666
"""
607-
# if not self.is_cone:
608-
# raise Exception("Face is not a cone.")
609-
610-
# surface = self.occ_adaptor.Surface()
611-
# cone = surface.Cone()
612-
# return cone_to_compas(cone) # noqa: F821
613667
raise NotImplementedError
614668

615669
def to_sphere(self) -> Sphere:
@@ -624,8 +678,7 @@ def to_sphere(self) -> Sphere:
624678
if not self.is_sphere:
625679
raise Exception("Face is not a sphere.")
626680

627-
surface = self.occ_adaptor.Surface()
628-
sphere = surface.Sphere()
681+
sphere = self.occ_adaptor.Sphere()
629682
return sphere_to_compas(sphere)
630683

631684
def to_torus(self) -> Torus:
@@ -637,14 +690,23 @@ def to_torus(self) -> Torus:
637690
:class:`compas.geometry.Torus`
638691
639692
"""
640-
# if not self.is_torus:
641-
# raise Exception("Face is not a torus.")
642-
643-
# surface = self.occ_adaptor.Surface()
644-
# torus = surface.Torus()
645-
# return torus_to_compas(torus) # noqa: F821
646693
raise NotImplementedError
647694

695+
def to_nurbs(self) -> NurbsSurface:
696+
"""
697+
Convert the face surface geometry to a torus.
698+
699+
Returns
700+
-------
701+
:class:`compas.geometry.NurbsSurface`
702+
703+
"""
704+
if not self.is_bspline:
705+
raise Exception("Face is not a nurbs surface.")
706+
707+
bspline = self.occ_adaptor.BSpline()
708+
return NurbsSurface.from_native(bspline)
709+
648710
# ==============================================================================
649711
# Methods
650712
# ==============================================================================

0 commit comments

Comments
 (0)