Skip to content

linkcad.edit

Geometry manipulation tasks. Each task is constructed against a Drawing (or, for FlattenTask, a Cell) and is run by calling task.execute(). Tasks must be run inside a WriteLock (standalone scripts) or a Transaction (tool plugins).

from linkcad.v1.edit import (
ITask,
SnapToGridTask,
FlattenTask,
RemoveDuplicatesTask,
MergeTask,
ClosePolylinesTask,
JoinPolylinesTask,
PolylineToPolygonTask,
ExplodeToPolygonTask,
RemoveZeroWidthPolylinesTask,
RemoveOverlappingPolylinesTask,
FixSelfIntersectingPolygonsTask,
FillRule,
HolesMode,
EdgePolicy,
PolygonSnapMode,
ShapesToConvert,
CellFilter,
LayerFilter,
ShapeFilter,
)

Base Class

ITask

All tasks extend ITask.

Property / MethodDescription
task.execute()Run the task. May raise on failure
task.cancel()Cancel a running task
task.progressProgress percentage (0-100)
task.cell_filterWhich cells the task processes; assignable
task.layer_filterWhich layers the task includes; assignable
task.shape_filterWhich shapes the task includes; assignable
from linkcad.v1.edit import CellFilter, RemoveDuplicatesTask, ShapeFilter
task = RemoveDuplicatesTask(drawing)
task.cell_filter = CellFilter.EnabledCells
task.shape_filter = ShapeFilter.SelectedShapes
task.execute()

Tasks

SnapToGridTask

Snaps shape vertices to the nearest grid point.

from linkcad.v1.db import Transaction
from linkcad.v1.edit import PolygonSnapMode, SnapToGridTask
with Transaction(drawing, "Snap to grid"):
SnapToGridTask(drawing, 100, PolygonSnapMode.AllAngle).execute()
ParameterTypeDefaultDescription
drawingDrawingrequiredTarget drawing
gridintrequiredGrid spacing in database units
snap_modePolygonSnapModeAllAngleWhether to force Manhattan geometry

FlattenTask

Flattens the cell hierarchy below a cell — expands all cell references into that cell. Note that it takes the starting cell, not the drawing.

from linkcad.v1.edit import FlattenTask
FlattenTask(drawing.main_cell).execute()
ParameterTypeDescription
start_cellCellCell whose references are expanded

RemoveDuplicatesTask

Removes polygons and polylines with identical geometry.

from linkcad.v1.edit import RemoveDuplicatesTask
RemoveDuplicatesTask(drawing).execute()

MergeTask

Merges overlapping polygons on each layer using a boolean union. Curved shapes are tessellated first using the provided Resolution.

from linkcad.v1.edit import EdgePolicy, FillRule, HolesMode, MergeTask
from linkcad.v1.geom import Resolution
res = Resolution()
res.minimum_facets = 32
res.maximum_error = 1
task = MergeTask(
drawing,
res,
FillRule.NonZero,
HolesMode.Link,
EdgePolicy.Merge,
dont_explode=False,
process_vertically=False,
)
task.execute()
ParameterTypeDefaultDescription
drawingDrawingrequiredTarget drawing
resolutionResolutionrequiredCurve tessellation settings
fill_ruleFillRulerequiredWinding rule for determining polygon interior
holes_modeHolesModerequiredHow to represent holes in output polygons
edge_policyEdgePolicyrequiredHow to handle inner boundaries
dont_explodeboolFalseIf True, do not decompose complex result polygons
process_verticallyboolFalseIf True, process cells bottom-up (for hierarchical drawings)

ClosePolylinesTask

Closes open polylines by connecting first and last vertices.

from linkcad.v1.edit import ClosePolylinesTask
ClosePolylinesTask(drawing).execute()

JoinPolylinesTask

Joins polylines whose endpoints touch, within tolerance.

from linkcad.v1.edit import JoinPolylinesTask
task = JoinPolylinesTask(drawing, wire_end_tolerance=10, width_tolerance=1)
task.execute()
print(task.unique_object_count, task.total_object_count)
ParameterTypeDescription
drawingDrawingTarget drawing
wire_end_toleranceintMaximum endpoint gap, in database units
width_toleranceintMaximum width difference between joined polylines

PolylineToPolygonTask

Converts closed polylines to filled polygons.

from linkcad.v1.edit import PolylineToPolygonTask
from linkcad.v1.geom import Resolution
PolylineToPolygonTask(drawing, tolerance=0, resolution=Resolution()).execute()
ParameterTypeDescription
drawingDrawingTarget drawing
toleranceintEndpoint gap still treated as closed
resolutionResolutionCurve tessellation settings

ExplodeToPolygonTask

Converts complex shapes to plain polygons and polylines by tessellation.

from linkcad.v1.edit import ExplodeToPolygonTask, FillRule, HolesMode, ShapesToConvert
from linkcad.v1.geom import Resolution
res = Resolution()
res.minimum_facets = 32
task = ExplodeToPolygonTask(
drawing, res, FillRule.NonZero, HolesMode.Link, ShapesToConvert.All
)
task.execute()
print(task.unique_object_count, task.total_object_count)
ParameterTypeDescription
drawingDrawingTarget drawing
resolutionResolutionCurve tessellation settings
fill_ruleFillRuleWinding rule
holes_modeHolesModeHow to represent holes
shapes_to_convertShapesToConvertWhich shape types to explode

unique_object_count and total_object_count are properties, and are only defined on the two tasks that count anything: ExplodeToPolygonTask and JoinPolylinesTask.

RemoveZeroWidthPolylinesTask

Deletes all polylines with a width of zero.

from linkcad.v1.edit import RemoveZeroWidthPolylinesTask
RemoveZeroWidthPolylinesTask(drawing).execute()

RemoveOverlappingPolylinesTask

Removes polyline segments that overlap other segments on the same layer.

from linkcad.v1.edit import RemoveOverlappingPolylinesTask
RemoveOverlappingPolylinesTask(drawing).execute()

FixSelfIntersectingPolygonsTask

Splits self-intersecting polygons into simple polygons.

from linkcad.v1.edit import FillRule, FixSelfIntersectingPolygonsTask, HolesMode
FixSelfIntersectingPolygonsTask(drawing, FillRule.NonZero, HolesMode.Link).execute()
ParameterTypeDescription
drawingDrawingTarget drawing
fill_ruleFillRuleWinding rule
holes_modeHolesModeHow to represent holes

Enums

FillRule

Winding rule used when determining which regions are “inside” a polygon. Re-exported from linkcad.v1.db.

ValueDescription
FillRule.NonZeroNon-zero winding rule (standard CAD/EDA convention)
FillRule.EvenOddEven-odd (alternating fill) rule

HolesMode

Controls how holes in result polygons are represented. Re-exported from linkcad.v1.plugin.

ValueDescription
HolesMode.LinkLink holes to the outer boundary with zero-width bridges
HolesMode.SplitSplit polygons so no holes appear in output
HolesMode.ExtractReturn holes as separate polygons
HolesMode.KeepKeep holes as they are

EdgePolicy

Controls how inner boundaries are handled during merge.

ValueDescription
EdgePolicy.KeepKeep original inner boundaries
EdgePolicy.MergeMerge inner boundaries

PolygonSnapMode

Controls the angles vertices may take after snapping.

ValueDescription
PolygonSnapMode.AllAngleAllow all angles
PolygonSnapMode.ForceManhattanForce Manhattan (orthogonal) geometry

ShapesToConvert

Selects which shape types ExplodeToPolygonTask converts. Values combine.

ValueDescription
ShapesToConvert.NoneNothing
ShapesToConvert.AllEvery convertible shape type
ShapesToConvert.PolylinesPolylines
ShapesToConvert.TextText
ShapesToConvert.CirclesCircles and ellipses
ShapesToConvert.ArcsArcs
ShapesToConvert.DonutsDonuts
ShapesToConvert.NurbsNURBS curves
ShapesToConvert.ZeroWidthClosedPolylinesClosed polylines of zero width

None is a Python keyword, so that member is reachable only as getattr(ShapesToConvert, "None").