From de3f528c744c0f4d9158d70fe50e3075b535163e Mon Sep 17 00:00:00 2001 From: Ashton Ohms Date: Mon, 26 Jun 2023 19:35:54 -0700 Subject: [PATCH] Add enlarge method to BoundBox --- cadquery/occ_impl/geom.py | 16 ++++++++++++++++ tests/test_cadquery.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/cadquery/occ_impl/geom.py b/cadquery/occ_impl/geom.py index 89a396c73..e95e4f447 100644 --- a/cadquery/occ_impl/geom.py +++ b/cadquery/occ_impl/geom.py @@ -846,6 +846,22 @@ def add( return BoundBox(tmp) + def enlarge(self, tol: float) -> "BoundBox": + """Returns a modified (expanded) bounding box, expanded in all + directions by the tolerance value. + + This means that the minimum values of its X, Y and Z intervals + of the bounding box are reduced by the absolute value of tol, while + the maximum values are increased by the same amount. + """ + tmp = Bnd_Box() + tmp.Add(self.wrapped) + tmp.SetGap(self.wrapped.GetGap()) + + tmp.Enlarge(tol) + + return BoundBox(tmp) + @staticmethod def findOutsideBox2D(bb1: "BoundBox", bb2: "BoundBox") -> Optional["BoundBox"]: """Compares bounding boxes diff --git a/tests/test_cadquery.py b/tests/test_cadquery.py index 55424f9ec..0fcce9377 100644 --- a/tests/test_cadquery.py +++ b/tests/test_cadquery.py @@ -1778,6 +1778,25 @@ def testBoundingBox(self): self.assertAlmostEqual(0.0, bb.zmin, 2) self.assertAlmostEqual(100.0, bb.zmax, 2) + def testBoundBoxEnlarge(self): + """ + Tests BoundBox.enlarge(). Confirms that the + bounding box lengths are all enlarged by the + correct amount. + """ + + enlarge_tol = 2.0 + bb = Workplane("XY").rect(10, 10).extrude(10).val().BoundingBox() + bb_enlarged = bb.enlarge(enlarge_tol) + + self.assertTrue(bb_enlarged.isInside(bb)) + self.assertAlmostEqual(bb_enlarged.xmin, bb.xmin - enlarge_tol, 2) + self.assertAlmostEqual(bb_enlarged.xmax, bb.xmax + enlarge_tol, 2) + self.assertAlmostEqual(bb_enlarged.ymin, bb.ymin - enlarge_tol, 2) + self.assertAlmostEqual(bb_enlarged.ymax, bb.ymax + enlarge_tol, 2) + self.assertAlmostEqual(bb_enlarged.zmin, bb.zmin - enlarge_tol, 2) + self.assertAlmostEqual(bb_enlarged.zmax, bb.zmax + enlarge_tol, 2) + def testCutThroughAll(self): """ Tests a model that uses more than one workplane