Skip to content

Commit a7a2265

Browse files
jacobrkerstetterpyansys-ci-botpre-commit-ci[bot]MaxJPRey
authored
feat: NURBSCurve conversions (#2053)
Co-authored-by: Jacob Kerstetter <jacob.kerstetter@ansys.com> Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxime Rey <87315832+MaxJPRey@users.noreply.github.com>
1 parent 443a380 commit a7a2265

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

doc/changelog.d/2053.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Nurbscurve conversions

src/ansys/geometry/core/_grpc/_services/v0/conversions.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
Ellipse as GRPCEllipse,
4141
Frame as GRPCFrame,
4242
Geometries as GRPCGeometries,
43+
Knot as GRPCKnot,
4344
Line as GRPCLine,
4445
Material as GRPCMaterial,
4546
MaterialProperty as GRPCMaterialProperty,
47+
NurbsCurve as GRPCNurbsCurve,
4648
Plane as GRPCPlane,
4749
Point as GRPCPoint,
4850
Polygon as GRPCPolygon,
@@ -73,6 +75,7 @@
7375
ParameterUpdateStatus,
7476
)
7577
from ansys.geometry.core.shapes.curves.curve import Curve
78+
from ansys.geometry.core.shapes.curves.nurbs import NURBSCurve
7679
from ansys.geometry.core.shapes.curves.trimmed_curve import TrimmedCurve
7780
from ansys.geometry.core.shapes.surfaces.surface import Surface
7881
from ansys.geometry.core.shapes.surfaces.trimmed_surface import TrimmedSurface
@@ -621,6 +624,7 @@ def from_curve_to_grpc_curve(curve: "Curve") -> GRPCCurveGeometry:
621624
from ansys.geometry.core.shapes.curves.circle import Circle
622625
from ansys.geometry.core.shapes.curves.ellipse import Ellipse
623626
from ansys.geometry.core.shapes.curves.line import Line
627+
from ansys.geometry.core.shapes.curves.nurbs import NURBSCurve
624628

625629
grpc_curve = None
626630

@@ -645,12 +649,88 @@ def from_curve_to_grpc_curve(curve: "Curve") -> GRPCCurveGeometry:
645649
major_radius=curve.major_radius.m,
646650
minor_radius=curve.minor_radius.m,
647651
)
652+
elif isinstance(curve, NURBSCurve):
653+
grpc_curve = GRPCCurveGeometry(nurbs_curve=from_nurbs_curve_to_grpc_nurbs_curve(curve))
648654
else:
649655
raise ValueError(f"Unsupported curve type: {type(curve)}")
650656

651657
return grpc_curve
652658

653659

660+
def from_nurbs_curve_to_grpc_nurbs_curve(curve: "NURBSCurve") -> GRPCNurbsCurve:
661+
"""Convert a ``NURBSCurve`` to a NURBS curve gRPC message.
662+
663+
Parameters
664+
----------
665+
curve : NURBSCurve
666+
Curve to convert.
667+
668+
Returns
669+
-------
670+
GRPCNurbsCurve
671+
Geometry service gRPC ``NURBSCurve`` message.
672+
"""
673+
from ansys.api.geometry.v0.models_pb2 import (
674+
ControlPoint as GRPCControlPoint,
675+
NurbsData as GRPCNurbsData,
676+
)
677+
678+
# Convert control points
679+
control_points = [
680+
GRPCControlPoint(
681+
position=from_point3d_to_grpc_point(pt),
682+
weight=curve.weights[i],
683+
)
684+
for i, pt in enumerate(curve.control_points)
685+
]
686+
687+
# Convert nurbs data
688+
nurbs_data = GRPCNurbsData(
689+
degree=curve.degree,
690+
knots=from_knots_to_grpc_knots(curve.knots),
691+
order=curve.degree + 1,
692+
)
693+
694+
return GRPCNurbsCurve(
695+
control_points=control_points,
696+
nurbs_data=nurbs_data,
697+
)
698+
699+
700+
def from_knots_to_grpc_knots(knots: list[float]) -> list[GRPCKnot]:
701+
"""Convert a list of knots to a list of gRPC knot messages.
702+
703+
Parameters
704+
----------
705+
knots : list[float]
706+
Source knots data.
707+
708+
Returns
709+
-------
710+
list[GRPCKnot]
711+
Geometry service gRPC knot messages.
712+
"""
713+
from collections import Counter
714+
715+
# Count multiplicities
716+
multiplicities = Counter(knots)
717+
718+
# Get unique knots (parameters) in order
719+
unique_knots = sorted(set(knots))
720+
knot_multiplicities = [(knot, multiplicities[knot]) for knot in unique_knots]
721+
722+
# Convert to gRPC knot messages
723+
grpc_knots = [
724+
GRPCKnot(
725+
parameter=knot,
726+
multiplicity=multiplicity,
727+
)
728+
for knot, multiplicity in knot_multiplicities
729+
]
730+
731+
return grpc_knots
732+
733+
654734
def from_grpc_curve_to_curve(curve: GRPCCurveGeometry) -> "Curve":
655735
"""Convert a curve gRPC message to a ``Curve``.
656736

src/ansys/geometry/core/shapes/parameterization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class Interval:
150150
@check_input_types
151151
def __init__(self, start: Real, end: Real) -> None:
152152
"""Initialize ``Interval`` class."""
153-
if end <= start:
153+
if end < start:
154154
raise ValueError("Start value must be less than end value")
155155

156156
self._start = start

0 commit comments

Comments
 (0)