Skip to content

Commit

Permalink
Add enlarge method to BoundBox
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashton Ohms committed Jun 27, 2023
1 parent b96eb8a commit de3f528
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cadquery/occ_impl/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de3f528

Please sign in to comment.