linkcad.libgraph
High-performance boolean geometry engine for polygon operations. Wraps the native libgraph library.
from linkcad.libgraph import ( ShapeProcessor, ShapeGroup, Operation, FillRule, HolesMode, Result, boolean_and, boolean_or, boolean_xor, boolean_difference,)Convenience Functions
For simple two-polygon operations, use these functions directly. Each function accepts vertex lists as sequences of (x, y) tuples and returns a list of result polygons (each a list of (x, y) tuples).
from linkcad.libgraph import boolean_and, boolean_or, boolean_xor, boolean_difference
square = [(0, 0), (10, 0), (10, 10), (0, 10)]triangle = [(5, 5), (15, 5), (10, 15)]
# Intersectionresult = boolean_and(square, triangle)
# Unionresult = boolean_or(square, triangle)
# Exclusive ORresult = boolean_xor(square, triangle)
# Difference (square minus triangle)result = boolean_difference(square, triangle)| Function | Description |
|---|---|
boolean_and(vtx1, vtx2, fill_rule=FillRule.NonZero) | Intersection of two polygons |
boolean_or(vtx1, vtx2, fill_rule=FillRule.NonZero) | Union of two polygons |
boolean_xor(vtx1, vtx2, fill_rule=FillRule.NonZero) | Symmetric difference of two polygons |
boolean_difference(vtx1, vtx2, fill_rule=FillRule.NonZero) | Difference — regions in vtx1 but not vtx2 |
ShapeProcessor
For multi-polygon operations or full control over the operation, use ShapeProcessor:
from linkcad.libgraph import ShapeProcessor, ShapeGroup, Operation
sp = ShapeProcessor()
sp.add_polygon(ShapeGroup.A, [(0, 0), (10, 0), (10, 10), (0, 10)])sp.add_polygon(ShapeGroup.B, [(5, 5), (15, 5), (10, 15)])
sp.execute(Operation.And)
results = []while True: poly = sp.get_next_polygon() if poly is None: break results.append(poly)| Method | Description |
|---|---|
ShapeProcessor() | Create a new processor instance |
sp.add_polygon(group, vertices) | Add a polygon to group A, B, or AB |
sp.execute(operation) | Run the specified boolean operation |
sp.get_next_polygon() | Retrieve the next result polygon; returns None when exhausted |
sp.result | Result status of the last execute() call |
Enums
ShapeGroup
Determines which operand a polygon belongs to.
| Value | Description |
|---|---|
ShapeGroup.A | First operand |
ShapeGroup.B | Second operand |
ShapeGroup.AB | Belongs to both operands simultaneously |
Operation
The boolean operation to perform.
| Value | Description |
|---|---|
Operation.Or | Union (A ∪ B) |
Operation.And | Intersection (A ∩ B) |
Operation.Xor | Symmetric difference (A △ B) |
Operation.AMinusB | Difference (A − B) |
Operation.BMinusA | Difference (B − A) |
FillRule
Controls how overlapping polygon edges determine “inside” vs “outside”.
| Value | Description |
|---|---|
FillRule.NonZero | Non-zero winding rule (standard EDA convention) |
FillRule.EvenOdd | Even-odd rule |
HolesMode
Controls how holes in output polygons are represented.
| Value | Description |
|---|---|
HolesMode.Extract | Return holes as separate reversed polygons |
HolesMode.Link | Connect holes to the outer boundary with zero-width bridges |
HolesMode.Split | Split result into simple polygons with no holes |
Result
Status of the last ShapeProcessor.execute() call.
| Value | Description |
|---|---|
Result.Ok | Operation succeeded |
Result.Error | Operation failed |