Appearance
Spec: Auto-Layout (Graph Algorithms)
Overview
Implement automatic circuit layout using graph algorithms to arrange components and route wires for optimal readability without manual positioning. Supports both full auto-layout and selective re-layout of sub-sections.
Motivation
Manually arranging components in complex circuits is time-consuming. Imported netlists, auto-generated circuits (from boolean expressions or HDL), and messy hand-built circuits all benefit from automatic layout. Good auto-layout dramatically improves circuit readability.
Requirements
Functional Requirements
- Full Auto-Layout: Automatically position all components and route all wires in the circuit.
- Selective Layout: Auto-layout only selected components while keeping others fixed.
- Layout Algorithms:
- Layered/Sugiyama: For dataflow circuits (combinational logic, pipelines). Left-to-right or top-to-bottom flow.
- Force-Directed: For general circuits without clear flow direction.
- Wire Routing: After placement, auto-route wires using Manhattan routing with minimal crossings.
- Constraint Respect: Honor user-locked component positions (pinned components stay in place).
- Spacing Configuration: Configurable minimum spacing between components.
- Flow Direction: User selects preferred signal flow direction (L→R, T→B, R→L, B→T).
- Crossing Minimization: Algorithm actively minimizes wire crossings.
- Grouping Awareness: Components in a group are laid out together as a cluster.
- Preview Mode: Show a preview of the auto-layout result before applying (with undo support).
- Incremental Layout: When adding a new component, suggest optimal placement relative to existing layout.
Non-Functional Requirements
- Layout computation completes in under 5 seconds for circuits with up to 500 components.
- Result must produce no overlapping components.
- Layout must respect grid snapping settings.
- Integrated with undo/redo (auto-layout is one undoable action).
Design
Algorithm Selection
| Circuit Type | Best Algorithm | Rationale |
|---|---|---|
| Combinational (gates→output) | Layered (Sugiyama) | Clear input→output flow |
| Sequential (feedback loops) | Modified Layered + cycle breaking | Handles back-edges |
| General/messy | Force-directed (spring model) | No assumed flow |
Layered Layout Pipeline (Sugiyama)
1. Cycle Removal → Break feedback loops with temporary edge reversal
2. Layer Assignment → Assign components to layers (longest-path method)
3. Crossing Reduction → Barycenter heuristic within layers
4. Coordinate Assignment → Brandes-Köpf algorithm for vertical alignment
5. Edge Routing → Manhattan routing with bend minimizationForce-Directed Layout
1. Initialize positions (random or current)
2. Iterate until convergence:
- Repulsive force between all component pairs (Coulomb)
- Attractive force along wires (Hooke's spring)
- Apply forces with damping
3. Snap to grid
4. Route wiresIntegration Point
csharp
public class AutoLayoutEngine
{
public LayoutResult ComputeLayout(Circuit circuit, LayoutOptions options);
public LayoutResult ComputePartialLayout(Circuit circuit, IEnumerable<Component> selected, LayoutOptions options);
}
public class LayoutOptions
{
public LayoutAlgorithm Algorithm { get; set; }
public FlowDirection Direction { get; set; }
public double MinSpacing { get; set; }
public bool RespectGroups { get; set; }
public bool SnapToGrid { get; set; }
public HashSet<Guid> LockedComponents { get; set; }
}UI/UX
- Menu: Arrange → Auto-Layout (full circuit)
- Menu: Arrange → Layout Selection
- Toolbar button for quick access.
- Options dialog before layout (algorithm, direction, spacing).
- Preview overlay showing proposed positions before confirming.
- Keyboard shortcut: Ctrl+Shift+L.
Implementation Tasks
- Create
DigitalWorks.Routing/Layout/namespace. - Implement graph abstraction (components as nodes, wires as edges).
- Implement Sugiyama layered layout algorithm.
- Implement force-directed layout algorithm.
- Implement crossing minimization (barycenter heuristic).
- Implement coordinate assignment with grid snapping.
- Integrate with existing
AutoRouterfor post-layout wire routing. - Add layout preview overlay on canvas.
- Create layout options dialog.
- Integrate with undo/redo system (whole layout as one action).
- Add incremental placement suggestion for new components.
Risks & Open Questions
- How to handle very large circuits (1000+ components) — progressive layout with LOD?
- Should the algorithm optimize for different objectives (compactness vs. readability)?
- How to handle macros — layout internal circuits separately?
- Should locked components act as fixed constraints or be moved slightly for better results?
Priority
Medium-High — Dramatically improves usability for complex circuits.