From a682a103c1b8b95ca6979fd1416cf1954cc3e689 Mon Sep 17 00:00:00 2001 From: Chen Kasirer Date: Sun, 24 Mar 2024 14:34:37 +0100 Subject: [PATCH 1/4] RhinoBrep.trimmed returns single Brep --- src/compas_rhino/geometry/brep/brep.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/compas_rhino/geometry/brep/brep.py b/src/compas_rhino/geometry/brep/brep.py index 3250b0aab83..1c24491acf1 100644 --- a/src/compas_rhino/geometry/brep/brep.py +++ b/src/compas_rhino/geometry/brep/brep.py @@ -381,11 +381,8 @@ def trim(self, plane, tolerance=TOLERANCE): None """ - results = self.trimmed(plane, tolerance) - if not results: - raise BrepTrimmingError("Trim operation ended with no result") - - self._brep = results[0].native_brep + result = self.trimmed(plane, tolerance) + self._brep = result.native_brep def trimmed(self, plane, tolerance=TOLERANCE): """Returns a trimmed copy of this brep by the given trimming plane. @@ -406,14 +403,13 @@ def trimmed(self, plane, tolerance=TOLERANCE): if isinstance(plane, Frame): plane = Plane.from_frame(plane) results = self._brep.Trim(plane_to_rhino(plane), tolerance) - - breps = [] - for result in results: - capped = result.CapPlanarHoles(TOLERANCE) - if capped: - result = capped - breps.append(RhinoBrep.from_native(result)) - return breps + if not results: + raise BrepTrimmingError("Trim operation ended with no result") + result = results[0] + capped = result.CapPlanarHoles(TOLERANCE) + if capped: + result = capped + return RhinoBrep.from_native(result) @classmethod def from_boolean_difference(cls, breps_a, breps_b): From 3e51ba1aaa5ee21d98807fa0c5feea0eabe2149b Mon Sep 17 00:00:00 2001 From: Chen Kasirer Date: Tue, 26 Mar 2024 10:39:15 +0100 Subject: [PATCH 2/4] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d109a787757..16da0530714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * Changed and update the `compas_view2` examples into `compas_viewer`. +* `RhinoBrep.trimmed` returns single result or raises `BrepTrimmingError` instead of returning a list. ### Removed From 08b4e9999769eeeb2d74a86b74fb9f17c7e56682 Mon Sep 17 00:00:00 2001 From: Chen Kasirer Date: Thu, 28 Mar 2024 08:59:32 +0100 Subject: [PATCH 3/4] completed docstrings --- src/compas_rhino/geometry/brep/brep.py | 30 ++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/compas_rhino/geometry/brep/brep.py b/src/compas_rhino/geometry/brep/brep.py index 1c24491acf1..8ac1813952b 100644 --- a/src/compas_rhino/geometry/brep/brep.py +++ b/src/compas_rhino/geometry/brep/brep.py @@ -372,14 +372,27 @@ def trim(self, plane, tolerance=TOLERANCE): ---------- plane : :class:`compas.geometry.Frame` or :class:`compas.geometry.Plane` The frame or plane to use when trimming. The discarded bit is in the direction of the frame's normal. - tolerance : float The precision to use for the trimming operation. + Notes + ----- + Trimming operation may result in multiple results (breps). When trimming, only one is used. + The used bit is the one on the opposite side of the cutting plane's normal. + Returns ------- None + Raises + ------ + BrepTrimmingError + If the trimming operation ended with no result. + + See Also + -------- + :meth:`compas_rhino.geometry.RhinoBrep.trimmed` + """ result = self.trimmed(plane, tolerance) self._brep = result.native_brep @@ -391,14 +404,27 @@ def trimmed(self, plane, tolerance=TOLERANCE): ---------- plane : :class:`compas.geometry.Frame` or :class:`compas.geometry.Plane` The frame or plane to use when trimming. The discarded bit is in the direction of the plane's normal. - tolerance : float The precision to use for the trimming operation. + Notes + ----- + Trimming operation may result in multiple results (breps). When trimming, only one is used. + The used bit is the one on the opposite side of the cutting plane's normal. + Returns ------- :class:`~compas_rhino.geometry.RhinoBrep` + Raises + ------ + BrepTrimmingError + If the trimming operation ended with no result. + + See Also + -------- + :meth:`compas_rhino.geometry.RhinoBrep.trim` + """ if isinstance(plane, Frame): plane = Plane.from_frame(plane) From 3f24de3ca2da218b6af0aaeeb859c558b3ae1e06 Mon Sep 17 00:00:00 2001 From: Chen Kasirer Date: Tue, 23 Apr 2024 11:49:50 +0200 Subject: [PATCH 4/4] using compas.tolerance in RhinoBrep --- src/compas_rhino/geometry/brep/brep.py | 32 ++++++++++++++------------ 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/compas_rhino/geometry/brep/brep.py b/src/compas_rhino/geometry/brep/brep.py index 8ac1813952b..7c050415015 100644 --- a/src/compas_rhino/geometry/brep/brep.py +++ b/src/compas_rhino/geometry/brep/brep.py @@ -10,6 +10,7 @@ from compas.geometry import BrepError from compas.geometry import Plane from compas.geometry import Point +from compas.tolerance import TOL from compas_rhino.conversions import box_to_rhino from compas_rhino.conversions import transformation_to_rhino @@ -29,8 +30,6 @@ from .vertex import RhinoBrepVertex from .loop import RhinoBrepLoop -TOLERANCE = 1e-6 - class RhinoBrep(Brep): """Rhino Brep backend class. @@ -242,7 +241,7 @@ def from_extrusion(cls, curve, vector, cap_ends=True): raise BrepError("Failed to create extrusion from curve: {} and vector: {}".format(curve, vector)) rhino_brep = extrusion.ToBrep() if cap_ends: - capped = rhino_brep.CapPlanarHoles(TOLERANCE) + capped = rhino_brep.CapPlanarHoles(TOL.absolute) if capped: rhino_brep = capped return cls.from_native(rhino_brep) @@ -327,7 +326,7 @@ def contains(self, object): raise BrepError("Cannot check for containment if brep is not manifold or is not closed") if isinstance(object, Point): - return self._brep.IsPointInside(point_to_rhino(object), TOLERANCE, False) + return self._brep.IsPointInside(point_to_rhino(object), TOL.absolute, False) else: raise NotImplementedError @@ -365,15 +364,15 @@ def transform(self, matrix): """ self._brep.Transform(transformation_to_rhino(matrix)) - def trim(self, plane, tolerance=TOLERANCE): + def trim(self, plane, tolerance=None): """Trim this brep by the given trimming plane. Parameters ---------- plane : :class:`compas.geometry.Frame` or :class:`compas.geometry.Plane` The frame or plane to use when trimming. The discarded bit is in the direction of the frame's normal. - tolerance : float - The precision to use for the trimming operation. + tolerance : float, optional + The precision to use for the trimming operation. Defaults to `TOL.absolute`. Notes ----- @@ -397,15 +396,15 @@ def trim(self, plane, tolerance=TOLERANCE): result = self.trimmed(plane, tolerance) self._brep = result.native_brep - def trimmed(self, plane, tolerance=TOLERANCE): + def trimmed(self, plane, tolerance=None): """Returns a trimmed copy of this brep by the given trimming plane. Parameters ---------- plane : :class:`compas.geometry.Frame` or :class:`compas.geometry.Plane` The frame or plane to use when trimming. The discarded bit is in the direction of the plane's normal. - tolerance : float - The precision to use for the trimming operation. + tolerance : float, optional + The precision to use for the trimming operation. Defaults to `TOL.absolute`. Notes ----- @@ -426,13 +425,14 @@ def trimmed(self, plane, tolerance=TOLERANCE): :meth:`compas_rhino.geometry.RhinoBrep.trim` """ + tolerance = tolerance or TOL.absolute if isinstance(plane, Frame): plane = Plane.from_frame(plane) results = self._brep.Trim(plane_to_rhino(plane), tolerance) if not results: raise BrepTrimmingError("Trim operation ended with no result") result = results[0] - capped = result.CapPlanarHoles(TOLERANCE) + capped = result.CapPlanarHoles(tolerance) if capped: result = capped return RhinoBrep.from_native(result) @@ -461,7 +461,7 @@ def from_boolean_difference(cls, breps_a, breps_b): resulting_breps = Rhino.Geometry.Brep.CreateBooleanDifference( [b.native_brep for b in breps_a], [b.native_brep for b in breps_b], - TOLERANCE, + TOL.absolute, ) return [RhinoBrep.from_native(brep) for brep in resulting_breps] @@ -487,7 +487,9 @@ def from_boolean_union(cls, breps_a, breps_b): if not isinstance(breps_b, list): breps_b = [breps_b] - resulting_breps = Rhino.Geometry.Brep.CreateBooleanUnion([b.native_brep for b in breps_a + breps_b], TOLERANCE) + resulting_breps = Rhino.Geometry.Brep.CreateBooleanUnion( + [b.native_brep for b in breps_a + breps_b], TOL.absolute + ) return [RhinoBrep.from_native(brep) for brep in resulting_breps] @classmethod @@ -514,7 +516,7 @@ def from_boolean_intersection(cls, breps_a, breps_b): resulting_breps = Rhino.Geometry.Brep.CreateBooleanIntersection( [b.native_brep for b in breps_a], [b.native_brep for b in breps_b], - TOLERANCE, + TOL.absolute, ) return [RhinoBrep.from_native(brep) for brep in resulting_breps] @@ -552,5 +554,5 @@ def split(self, cutter): list of zero or more resulting Breps. """ - resulting_breps = self._brep.Split(cutter.native_brep, TOLERANCE) + resulting_breps = self._brep.Split(cutter.native_brep, TOL.absolute) return [RhinoBrep.from_native(brep) for brep in resulting_breps]