────────────────────────────────────────
- PROBLEM SPACE – WHAT WE ARE REALLY SOLVING
────────────────────────────────────────
A CAD program is a live directed graph whose nodes are
• geometric primitives
• derived geometry (alignments, meshes, rules)
• non-geometric artefacts (annotations, images, GIS layers, etc.)
Edges are dependencies (point → line, surface → volume, rule → cross-fall, etc.).
The graph is mutable at run-time (user edits, parametric changes, undo/redo, scripts).
Every change must propagate immediately to
• dependent objects (recalc)
• render caches
• UI panels (property grid, layer tree, status bar)
• file I/O (incremental save)
Hence the core problem is reactive, incremental, observable change-propagation on a heterogeneous object graph while keeping the system
• deterministic
• undo-able
• thread-safe for background meshing / TIN generation
• extensible (plug-ins can introduce new object types and new dependency kinds)
────────────────────────────────────────
2. HIGH-LEVEL ARCHITECTURE
────────────────────────────────────────
┌────────────────────────────┐
│ Avalonia UI │
└──────────┬─────────────────┘
│ (Reactive UI)
┌──────────┴─────────────────┐
│ View-Models │
│ (INotifyPropertyChanged) │
└──────────┬─────────────────┘
│ (Observables)
┌──────────┴─────────────────┐
│ Presentation │
│ (View Adapters) │
└──────────┬─────────────────┘
│ (Commands / Events)
┌──────────┴─────────────────┐
│ Domain Core │
│ (Document, Objects, │
│ Dependency Engine) │
└──────────┬─────────────────┘
│ (Immutable Kernel)
┌──────────┴─────────────────┐
│ Geometry Kernel │
│ (structs, nuget pkgs) │
└────────────────────────────┘
────────────────────────────────────────
3. DOMAIN CORE – DETAILED DESIGN
────────────────────────────────────────
3.1 Identity & Lifetime
• ObjectId – 128-bit ULID (sortable, unique across sessions, supports merge).
• Name – mutable, user-editable, validated for uniqueness within scope.
• Document – root container, owns every object.
• IObjectRepository – O(1) lookup by id, supports snapshots for undo.
3.2 Object Model
Every object implements one minimal interface:
public interface ICadObject : INotifyPropertyChanged
{
ObjectId Id { get; }
string Name { get; set; }
Layer Layer { get; set; }
IStyle Style { get; set; }
/// <summary>
/// Immutable snapshot of the current state.
/// Used for diffing, undo, render caching.
/// </summary>
object GetSnapshot();
}
Concrete classes are partial (split files) so plug-ins can add new aspects via
partial void OnLayerChanged(Layer newLayer); etc.
3.3 Geometry vs Non-Geometry
public interface IGeometric : ICadObject
{
/// <summary>
/// Kernel structs, immutable.
/// </summary>
IReadOnlyList<Kernel.Geometry> Geometry { get; }
}
public interface INonGeometric : ICadObject { /* marker */ }
3.4 Layer & Style
public sealed class Layer : ICadObject
{
public ObjectId Id { get; }
public string Name { get; set; }
public bool Visible { get; set; }
public IStyle DefaultStyle { get; set; }
public IStyle GetEffectiveStyle(ICadObject obj) => obj.Style ?? DefaultStyle;
}
IStyle is an immutable record (color, line type, thickness, material, etc.).
────────────────────────────────────────
4. DEPENDENCY & REACTIVE ENGINE
────────────────────────────────────────
4.1 Declarative Dependencies
Objects expose what they depend on, not how to update.
Example:
public sealed class CadLine : IGeometric
{
public ObjectId P1Id { get; private set; }
public ObjectId P2Id { get; private set; }
// Computed, cached, observable
public Kernel.Line KernelLine => _kernelLine.Value;
private readonly Observed<Kernel.Line> _kernelLine;
public CadLine(ObjectId id, ObjectId p1Id, ObjectId p2Id, IDependencyEngine de)
{
_kernelLine = de.Observe(() =>
new Kernel.Line(
de.Get<CadPoint>(p1Id).KernelPoint,
de.Get<CadPoint>(p2Id).KernelPoint));
}
}
4.2 Dependency Engine Internals
• IDependencyEngine – façade hiding a topological incremental DAG.
• Internally uses reactive cells inspired by Adaptors in FRP and incremental computation libraries (e.g., Adaptors, Sodium, Knockout).
• Each cell holds
– current value (immutable)
– version number (ULID)
– list of children cells
• Change propagation is push-pull:
– When a primitive mutates, mark dirty and enqueue.
– Background worker pulls dirty cells in topological order, recalculates, pushes new values.
– Recalculation is pure function → thread-safe.
• Dirty flags are bitmaps per layer → fast visibility culling.
4.3 Observable Collections
ObservableHashSet<ObjectId> for layer membership,
ObservableDictionary<ObjectId,ICadObject> for repository.
Both implement INotifyCollectionChanged so Avalonia bindings update instantly.
4.4 Transactions & Undo
public interface ITransaction : IDisposable
{
void Commit();
void Rollback();
}
public sealed class Document : IDocument
{
public ITransaction BeginTransaction(string name);
}
Internally uses Memento + Command pattern.
Each property change is wrapped in a PropertyChangeCommand that stores
• object id
• property name
• old snapshot
• new snapshot
Undo stack is persistent vector (immutable data structure) → O(1) snapshots, structural sharing.
────────────────────────────────────────
5. PERFORMANCE CONSIDERATIONS
────────────────────────────────────────
• Kernel structs are immutable → safe to share across threads.
• Dependency engine uses lock-free queues for dirty cells.
• Render cache is keyed by (ObjectId, Version) → if version unchanged, reuse mesh.
• Spatial index (R*-tree) updated incrementally; only affected nodes reinserted.
• Background TIN generation runs in Task with cancellation token; on completion posts a ReplaceMeshCommand into the transaction queue.
────────────────────────────────────────
6. EXTENSIBILITY
────────────────────────────────────────
• MEF / Source Generators scan assemblies for ICadObject implementations.
• Plug-ins can register new dependency kinds by implementing IDependencyFunction.
• New UI panels use Avalonia’s DataTemplate keyed by ObjectId.Type (string).
• Scripting (IronPython / C#) receives the same IDocument façade → scripts are first-class citizens.
────────────────────────────────────────
7. REACTIVE FLOW – END-TO-END EXAMPLE
────────────────────────────────────────
- User drags a point.
- Avalonia pointer event → ViewModel command.
- Command wraps into
MovePointCommand.
MovePointCommand.Execute inside a transaction:
a. Mutate CadPoint.KernelPoint.
b. Dependency engine marks all dependent cells dirty.
c. Commit → raises PropertyChanged on CadPoint.
- Dependency engine background worker recalculates
CadLine.KernelLine, increments version.
- Render cache invalidates old mesh, generates new mesh on UI thread.
- Avalonia
DrawingContext receives DrawMesh(mesh) → 60 fps.
- Property panel bound to
CadLine.Length updates automatically.
────────────────────────────────────────
8. CODE SKELETON (ESSENTIAL FILES)
────────────────────────────────────────
/Domain
ICadObject.cs
IGeometric.cs
INonGeometric.cs
ObjectId.cs
Layer.cs
IStyle.cs
IDocument.cs
IDependencyEngine.cs
Observed{T}.cs
Transaction.cs
Commands/
PropertyChangeCommand.cs
AddObjectCommand.cs
RemoveObjectCommand.cs
/Kernel
(wrappers – out of scope)
/Infrastructure
ObjectRepository.cs
DependencyEngine.cs
UndoRedoStack.cs
/UI
ViewModels/
CadObjectViewModel.cs
LayerViewModel.cs
Views/
CadCanvas.axaml
PropertyPanel.axaml
────────────────────────────────────────
9. CHECKLIST FOR NEXT STEPS
────────────────────────────────────────
[ ] Implement ObjectId, Layer, IStyle as immutable records.
[ ] Create Document with repository and transaction support.
[ ] Build minimal DependencyEngine for Observed<T> cells.
[ ] Write unit tests:
– change point → line updates
– undo/redo 1000 changes < 16 ms
[ ] Avalonia integration: ItemsControl bound to ObservableCollection<ICadObject>.
[ ] Spatial index for 100k objects < 1 ms query.
[ ] Background TIN task with cancellation.
This architecture is comparable to AutoCAD’s core (ObjectARX), BricsCAD’s BRX, and Bentley’s MDL, but expressed in modern C# with immutable kernel, reactive propagation, and full undo/redo.