Foundational Modeling of Data Types for 2D CAD Program
Overview
The goal is to design a robust, maintainable, and extensible architecture for a 2D CAD program focused on road designs. The program will utilize a custom geometry kernel and Avalonia for the frontend. This document will explore the problem space and propose a high-quality architecture for the foundational modeling of data types.
Problem Breakdown
- CAD Objects: Represent entities in the CAD world, such as lines, arcs, polylines, polygons, and other non-geometric data. Each CAD object has a unique ID, name, and belongs to a layer.
- Dependencies: CAD objects have dependencies on other objects or data, such as points defining a line. Changes to these dependencies should trigger updates to the dependent CAD objects.
- Reactivity: The program needs to be reactive to changes in CAD objects, their properties, and dependencies.
- Layer and Styling: Layers have their own styling, which applies to the CAD objects they contain.
Proposed Architecture
1. CAD Object Model
Introduce an abstract base class CadObject that encapsulates common properties and behaviors of CAD objects.
public abstract class CadObject
{
public Guid Id { get; }
public string Name { get; set; }
public Layer Layer { get; set; }
protected CadObject(Guid id, string name, Layer layer)
{
Id = id;
Name = name;
Layer = layer;
}
public abstract void UpdateDependencies();
}
Concrete CAD objects (e.g., CadLine, CadArc, CadPolyline) will inherit from CadObject and implement UpdateDependencies() to handle their specific dependencies.
2. Dependency Management
Implement a dependency management system using a graph data structure. Each node in the graph represents a CAD object or a dependency (e.g., a point). Edges represent the dependencies between nodes.
public class DependencyGraph
{
private readonly Dictionary<Guid, Node> _nodes = new();
public void AddNode(Node node)
{
_nodes[node.Id] = node;
}
public void AddDependency(Guid dependentId, Guid dependencyId)
{
var dependentNode = _nodes[dependentId];
var dependencyNode = _nodes[dependencyId];
dependentNode.Dependencies.Add(dependencyNode);
}
public void NotifyDependencyChanged(Guid dependencyId)
{
var dependencyNode = _nodes[dependencyId];
foreach (var dependentNode in dependencyNode.Dependents)
{
dependentNode.Update();
}
}
}
public abstract class Node
{
public Guid Id { get; }
public List<Node> Dependencies { get; } = new();
public List<Node> Dependents { get; } = new();
protected Node(Guid id)
{
Id = id;
}
public abstract void Update();
}
CadObject will be wrapped in a CadObjectNode that inherits from Node. This allows CadObject instances to be part of the dependency graph.
public class CadObjectNode : Node
{
public CadObject CadObject { get; }
public CadObjectNode(Guid id, CadObject cadObject) : base(id)
{
CadObject = cadObject;
}
public override void Update()
{
CadObject.UpdateDependencies();
}
}
3. Reactivity
Utilize the Reactive Extensions (Rx) library to create a reactive system. Each CadObject will expose an IObservable<T> for its properties, allowing other parts of the program to react to changes.
public abstract class CadObject
{
// ...
public IObservable<string> NameChanged => _nameChangedSubject.AsObservable();
private readonly Subject<string> _nameChangedSubject = new();
public string Name
{
get => _name;
set
{
_name = value;
_nameChangedSubject.OnNext(value);
}
}
private string _name;
}
4. Layer and Styling
Introduce a Layer class that encapsulates styling information.
public class Layer
{
public Guid Id { get; }
public string Name { get; set; }
public Style Style { get; set; }
public Layer(Guid id, string name, Style style)
{
Id = id;
Name = name;
Style = style;
}
}
public class Style
{
// Styling properties (e.g., color, line width, etc.)
}
CadObject will reference its containing Layer, allowing it to inherit the layer's styling.
Benefits
- Decoupling: The proposed architecture decouples CAD objects from each other and from the dependency management system, making it easier to modify and extend the system.
- Reactivity: The use of Rx enables a reactive system, allowing the program to respond to changes in CAD objects and their properties.
- Flexibility: The dependency graph and node structure provide a flexible way to manage dependencies between CAD objects and other data.
- Maintainability: The separation of concerns and clear structure make the codebase more maintainable.
Example Use Case
- Create a
CadLine object with a dependency on three points.
- Add the
CadLine object to the dependency graph.
- When one of the points changes, the dependency graph notifies the
CadLine object, triggering an update.
var point1 = new Point(0, 0);
var point2 = new Point(1, 0);
var point3 = new Point(1, 1);
var cadLine = new CadLine(Guid.NewGuid(), "Line1", layer, point1, point2, point3);
var cadLineNode = new CadObjectNode(cadLine.Id, cadLine);
dependencyGraph.AddNode(cadLineNode);
// When point1 changes
point1.X = 2;
dependencyGraph.NotifyDependencyChanged(point1.Id);
In this example, the CadLine object will be updated when point1 changes, demonstrating the reactivity and dependency management capabilities of the proposed architecture.