Skip to content

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)]
# Intersection
result = boolean_and(square, triangle)
# Union
result = boolean_or(square, triangle)
# Exclusive OR
result = boolean_xor(square, triangle)
# Difference (square minus triangle)
result = boolean_difference(square, triangle)
FunctionDescription
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)
MethodDescription
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.resultResult status of the last execute() call

Enums

ShapeGroup

Determines which operand a polygon belongs to.

ValueDescription
ShapeGroup.AFirst operand
ShapeGroup.BSecond operand
ShapeGroup.ABBelongs to both operands simultaneously

Operation

The boolean operation to perform.

ValueDescription
Operation.OrUnion (A ∪ B)
Operation.AndIntersection (A ∩ B)
Operation.XorSymmetric difference (A △ B)
Operation.AMinusBDifference (A − B)
Operation.BMinusADifference (B − A)

FillRule

Controls how overlapping polygon edges determine “inside” vs “outside”.

ValueDescription
FillRule.NonZeroNon-zero winding rule (standard EDA convention)
FillRule.EvenOddEven-odd rule

HolesMode

Controls how holes in output polygons are represented.

ValueDescription
HolesMode.ExtractReturn holes as separate reversed polygons
HolesMode.LinkConnect holes to the outer boundary with zero-width bridges
HolesMode.SplitSplit result into simple polygons with no holes

Result

Status of the last ShapeProcessor.execute() call.

ValueDescription
Result.OkOperation succeeded
Result.ErrorOperation failed