Regions¶
Object API for 2-D paths and regions.
Path2D and Region: object wrappers over the 2-D point maths in paths.py/rounding.py/ transforms.py, so a polygon can be built once and then chained (Path2D(pts).offset(radius=-2).round_corners(radius=1).polygon()) instead of threading raw point lists through free functions.
-
class pybosl2.regions.Region(paths=
())[source]¶ Bases:
objectA 2-D region backed by
shapely(not OpenSCAD/PythonSCAD).Stores
MultiPolygoninternally and derivesPath2Doutlines only when requested. All Boolean operations (union, intersection, difference, symmetric difference) use shapely directly. Operator overloads (|,&,-,^) are provided.Create a region from a single outline (no holes):
Region([[0, 0], [80, 0], [80, 60], [0, 60]])Create a region with holes (outline first, then hole paths):
Region([ [[0, 0], [80, 0], [80, 60], [0, 60]], # outer outline [[20, 20], [60, 20], [60, 40], [20, 40]], # hole 1 ])Or use the shorthand
with_holes()for a more readable call.For native-geometry output (e.g. extrusion), call
geometry()which converts paths toBosl2Shape2D.- Parameters:¶
- paths : Any¶
The outlines; each is coerced to a
Path2D. A single flat point list is treated as one outline. Ashapely.Polygonorshapely.MultiPolygonis also accepted.
Examples
A rectangular plate with a rectangular hole (outline + one hole), extruded into a solid:
from pybosl2 import Region region = Region([ [[0, 0], [80, 0], [80, 60], [0, 60]], [[20, 20], [60, 20], [60, 40], [20, 40]], ]) region.geometry().linear_extrude(height=5).show()Loading 3-D preview…- property paths : list[Path2D]¶
The list of
Path2Dobjects derived from the geometry.Extracted on-demand from the underlying shapely
PolygonorMultiPolygon.- Returns:¶
A list of
Path2Dobjects.
- property geom : MultiPolygon¶
The underlying shapely geometry.
- to_shapely()[source]¶
Return the shapely geometry for this region.
Equivalent to the
geomproperty; provided for explicit usage.
- classmethod with_holes(outline, *holes)[source]¶
Create a region from an outline plus hole outlines.
Convenience constructor. Equivalent to
Region([outline, \\*holes]). See__init__()for the full constructor.
- property outline : Path2D¶
The outer path.
- Returns:¶
The first
Path2Din the region, which is the outer outline.
- property holes : list[Path2D]¶
The hole paths.
- Returns:¶
All
Path2Dobjects after the first, which are the interior holes.
-
round_corners(radius=
None, method=RoundingMethod.CIRCLE, cut=None, joint=None, width=None, curvature=None, closed=None)[source]¶ Round the corners of every path in the region.
- Parameters:¶
- radius : float | list[float] | None¶
The rounding radius. A single float applies to all corners; a list applies per-corner radii.
- method : RoundingMethod¶
The rounding method (
"circle","smooth", etc.).- cut : float | None¶
Cut depth for chamfers.
- joint : float | None¶
Joint distance for rounding.
- width : float | None¶
Width for rounding.
- curvature : float | None¶
Curvature value for rounding.
- closed : bool | None¶
Override whether paths are treated as closed.
- Returns:¶
A new
Regionwith rounded corners on every path.- Return type:¶
- fill()[source]¶
Return this region as 2-D geometry with its holes filled in.
Equivalent to just the outline (OpenSCAD
fill()).
- classmethod hull(*others)[source]¶
Return the 2-D convex hull of all the given regions and paths.
Uses shapely
convex_hull()on the union of all input geometries. Accepts a flat list or multiple arguments:Region.convex_hull(rect, circle) Region.convex_hull([rect, circle])
-
linear_extrude(height, center=
False, twist=0.0, scale=1.0, slices=None, fn=None, fa=None, fs=None)[source]¶ Extrude this region along +Z into a 3-D solid with holes included.
The result depends on the active backend: a
Bosl2Solidunder the default CSG backend, or aPyShapeunderuse_backend("sdf"). Seepybosl2.paths.Path2D.linear_extrude()for per-backend options.The SDF backend’s prism is the union of the outlines’ fields, so it can only express a region of DISJOINT islands; a region with holes raises
UnsupportedByBackendErrorthere.- Parameters:¶
- height : float¶
The extrusion height along +Z.
- center : bool¶
Extrude symmetrically along Z if True (default False).
- twist : float¶
Twist angle in degrees over the full height (default 0).
- scale : float¶
Scale factor for the top cross-section (default 1.0).
- slices : int | None¶
Number of intermediate layers for twist/scale (auto if None).
- fn : int | None¶
Smoothness override for the angular resolution.
- fa : float | None¶
Smoothness override for the minimum angle.
- fs : float | None¶
Smoothness override for the minimum segment length.
- Returns:¶
A
Bosl2Solid(CSG) orPyShape(SDF).- Return type:¶
-
rotate_extrude(angle=
360.0, fn=None, fa=None, fs=None)[source]¶ Revolve this region about the Y axis into a 3-D solid.
-
stroke(width=
1, closed=None, endcap1=CapType.ROUND, endcap2=CapType.ROUND, joints=CapType.ROUND)[source]¶ Stroke every path in the region (closed) and return the union as a Region.
- Parameters:¶
- width : float¶
Stroke width.
- closed : bool | None¶
Accepted for signature parity; a region’s paths are always stroked closed.
- endcap1 : CapType | CapSpec¶
Cap style for the start of each path.
- endcap2 : CapType | CapSpec¶
Cap style for the end of each path.
- joints : CapType | CapSpec¶
Cap style where segments meet.
- Returns:¶
A
Regionof the stroked outlines.- Return type:¶
-
dashed_stroke(dashpat=
None, closed=None, fit=True, mindash=0.5)[source]¶ Break every path in the region into dashed polygon outlines.
Returns a
Regionof all dash polygons.
-
debug_region(size=
1, vertices=True)[source]¶ Visualize this region with vertex labels for debugging.
Produces the filled region as a thin flat solid with every path’s vertices labelled in red – path
agets labelsa0, a1, ..., pathbb0, b1, ...(BOSL2debug_region()). A single-path region defers todebug_polygon().
- intersection(other)[source]¶
Return the 2-D intersection of this region with other (the area they share).
Uses shapely for exact polygon coordinates.
Examples
Two overlapping squares share a rectangular strip:
from pybosl2 import Region a = Region([[0, 0], [40, 0], [40, 30], [0, 30]]) b = Region([[20, 0], [60, 0], [60, 30], [20, 30]]) a.intersection(b).geometry().linear_extrude(height=3).show()Loading 3-D preview…
- union(other)[source]¶
Return the 2-D union of this region and other (all area covered by either).
Uses shapely for exact polygon coordinates.
Examples
Two adjacent squares merge into an L-shape:
from pybosl2 import Region a = Region([[0, 0], [30, 0], [30, 30], [0, 30]]) b = Region([[20, 0], [50, 0], [50, 30], [20, 30]]) a.union(b).geometry().linear_extrude(height=3).show()Loading 3-D preview…
- difference(other)[source]¶
Return the 2-D difference: self with the area of other subtracted.
Uses shapely for exact polygon coordinates.
Examples
Punch a rectangular notch out of a square:
from pybosl2 import Region plate = Region([[0, 0], [60, 0], [60, 40], [0, 40]]) notch = Region([[20, 10], [40, 10], [40, 30], [20, 30]]) plate.difference(notch).geometry().linear_extrude(height=4).show()Loading 3-D preview…
Path and Path3D are re-exported here for convenience but documented on the
Paths page.