Skip to content

Commit 04fff5f

Browse files
committed
Reworked edge curve type conversions
1 parent fd65232 commit 04fff5f

File tree

1 file changed

+57
-22
lines changed
  • src/compas_rhino/geometry/brep

1 file changed

+57
-22
lines changed

src/compas_rhino/geometry/brep/edge.py

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@
22
from compas.geometry import Line
33
from compas.geometry import Circle
44
from compas.geometry import Ellipse
5+
from compas.geometry import Frame
6+
from compas.geometry import Arc
57
from compas_rhino.geometry import RhinoNurbsCurve
68
from compas_rhino.conversions import curve_to_compas_line
9+
from compas_rhino.conversions import plane_to_compas_frame
10+
from compas_rhino.conversions import circle_to_compas
11+
from compas_rhino.conversions import ellipse_to_compas
12+
from compas_rhino.conversions import ellipse_to_rhino
13+
from compas_rhino.conversions import circle_to_rhino
14+
from compas_rhino.conversions import frame_to_rhino_plane
15+
from compas_rhino.conversions import line_to_rhino
16+
from compas_rhino.conversions import arc_to_compas
17+
from compas_rhino.conversions import arc_to_rhino
18+
19+
from Rhino.Geometry import ArcCurve
20+
from Rhino.Geometry import NurbsCurve
21+
from Rhino.Geometry import LineCurve
22+
from Rhino.Geometry import NurbsCurve
23+
from Rhino.Geometry import Interval
724

825
# from compas_rhino.conversions import curve_to_compas_circle
926
# from compas_rhino.conversions import curve_to_compas_ellipse
1027
from compas_rhino.conversions import line_to_rhino_curve
1128
from compas_rhino.conversions import circle_to_rhino_curve
1229
from compas_rhino.conversions import ellipse_to_rhino_curve
1330

14-
1531
from .vertex import RhinoBrepVertex
1632

1733

@@ -62,17 +78,19 @@ def _set_edge(self, rhino_edge):
6278

6379
@property
6480
def data(self):
65-
curve_type, curve = self._get_curve_geometry()
81+
curve_type, curve, plane, domain = self._get_curve_geometry()
6682
return {
6783
"curve_type": curve_type,
6884
"curve": curve.data,
85+
"frame": plane_to_compas_frame(plane).data,
6986
"start_vertex": self._edge.StartVertex.VertexIndex,
7087
"end_vertex": self._edge.EndVertex.VertexIndex,
88+
"domain": domain,
7189
}
7290

7391
@data.setter
7492
def data(self, value):
75-
edge_curve = self._create_curve_from_data(value["curve_type"], value["curve"])
93+
edge_curve = self._create_curve_from_data(value["curve_type"], value["curve"], value["frame"], value["domain"])
7694
edge = self._builder.add_edge(edge_curve, value["start_vertex"], value["end_vertex"])
7795
self._set_edge(edge)
7896

@@ -131,28 +149,45 @@ def is_ellipse(self):
131149

132150
def _get_curve_geometry(self):
133151
curve = self._edge.EdgeCurve
134-
if self.is_line:
135-
type_ = "line"
136-
curve = curve_to_compas_line(curve)
137-
# TODO: there is an edge/trim direction issue when creating and edge from circle
138-
# elif self.is_circle:
139-
# type_ = "circle"
140-
# curve = curve_to_compas_circle(curve)
141-
# elif self.is_ellipse:
142-
# type_ = "ellipse"
143-
# curve = curve_to_compas_ellipse(curve)
144-
else:
145-
type_ = "nurbs"
146-
curve = self._curve
147-
return type_, curve
152+
domain = [self._edge.Domain[0], self._edge.Domain[1]]
153+
_, frame = curve.FrameAt(0)
154+
if isinstance(curve, LineCurve):
155+
return "line", curve_to_compas_line(curve), frame, domain
156+
if isinstance(curve, NurbsCurve):
157+
return "nurbs", RhinoNurbsCurve.from_rhino(curve), frame, domain
158+
if isinstance(curve, ArcCurve):
159+
if not curve.IsClosed:
160+
return "arc", arc_to_compas(curve.Arc), curve.Arc.Plane, domain
161+
is_circle, circle = curve.TryGetCircle()
162+
if is_circle:
163+
return "circle", circle_to_compas(circle), circle.Plane, domain
164+
is_ellipse, ellipse = curve.TryGetEllipse()
165+
if is_ellipse:
166+
return "ellipse", ellipse_to_compas(ellipse), ellipse.Plane, domain
167+
return "nurbs", curve.ToNurbsCurve(), frame, domain
168+
raise ValueError("Unknown curve type: {}".format(curve.__class__.__name__))
148169

149170
@staticmethod
150-
def _create_curve_from_data(curve_type, curve_data):
171+
def _create_curve_from_data(curve_type, curve_data, frame_data, domain):
172+
frame = Frame.from_data(frame_data)
151173
if curve_type == "line":
152-
return line_to_rhino_curve(Line.from_data(curve_data))
174+
line = Line.from_data(curve_data)
175+
curve = LineCurve(line_to_rhino(line))
153176
elif curve_type == "circle":
154-
return circle_to_rhino_curve(Circle.from_data(curve_data))
177+
circle = circle_to_rhino(Circle.from_data(curve_data))
178+
circle.Plane = frame_to_rhino_plane(frame)
179+
curve = ArcCurve(circle)
155180
elif curve_type == "ellipse":
156-
return ellipse_to_rhino_curve(Ellipse.from_data(curve_data))
181+
ellipse = ellipse_to_rhino(Ellipse.from_data(curve_data))
182+
ellipse.Plane = frame_to_rhino_plane(frame)
183+
curve = NurbsCurve.CreateFromEllipse(ellipse)
184+
elif curve_type == "arc":
185+
arc = arc_to_rhino(Arc.from_data(curve_data))
186+
curve = ArcCurve(arc)
187+
elif curve_type == "nurbs":
188+
curve = RhinoNurbsCurve.from_data(curve_data).rhino_curve
157189
else:
158-
return RhinoNurbsCurve.from_data(curve_data).rhino_curve
190+
raise ValueError("Unknown curve type: {}".format(curve_type))
191+
curve.Domain = (Interval(*domain))
192+
return curve
193+

0 commit comments

Comments
 (0)