Associative surfaces in AutoCAD offer robust capabilities for maintaining dynamic links between geometric objects. Updating these surfaces programmatically requires careful handling of associated entities and profile curves. In this blog, we’ll explore how to automate the process of updating an associative planar surface with a new profile curve using AutoCAD .NET API.
Scenario Overview
The primary goal of this method is to update the profile curve of a planar surface while preserving its associative behavior. We assume the user selects a planar surface with a rectangular profile curve. If the surface is associative, the existing profile is replaced with a modified version.
Key Highlights
- Surface Selection: Ensures the selected entity is a valid surface.
- Associativity Check: Identifies whether the surface has an associated
AssocActionBody
. - Profile Extraction: Extracts edges from the associative surface and processes them as AutoCAD entities.
- Profile Transformation: Demonstrates scaling the extracted profile using its centroid.
- Input Path Update: Updates the planar surface with a transformed profile.
1. Surface Selection
The method starts by prompting the user to select a surface. It validates the selection to ensure the entity is a
DBSurface
.
PromptEntityOptions surfaceSelectionPrompt = new PromptEntityOptions("\nSelect a surface: ");
surfaceSelectionPrompt.SetRejectMessage("Must be a Surface!");
surfaceSelectionPrompt.AddAllowedClass(typeof(DBSurface), exactMatch: false);
PromptEntityResult selectionResult = documentEditor.GetEntity(surfaceSelectionPrompt);
2. Associativity Check
To determine if the surface is associative, the code checks for a valid AssocActionBody
.
var surfaceCreationActionId = selectedSurface.CreationActionBodyId;
bool isSurfaceAssociative = false;
if (surfaceCreationActionId != ObjectId.Null)
{
AssocActionBody associatedActionBody = transaction.GetObject(surfaceCreationActionId, OpenMode.ForRead) as AssocActionBody;
isSurfaceAssociative = associatedActionBody != null;
}
3. Extracting and Transforming the Profile
If the surface is associative, its input paths are processed. The extracted edges are converted into entities, modified (e.g., scaled), and used to update the surface.
planeActionBody.GetInputPaths(out EdgeRef[][][] edgeReferenceLayers);
foreach (var edgeReferences in edgeReferenceLayers.SelectMany(layer => layer))
{
foreach (var edgeReference in edgeReferences)
{
Entity extractedEntity = edgeReference.CreateEntity();
extractedEntity.ColorIndex = 1;
extractedEntity.SetDatabaseDefaults();
ents.Add(extractedEntity);
}
}
if (ents[0] is Polyline pl)
{
var center = GetCentroid(pl);
profile.TransformBy(Matrix3d.Scaling(10, center));
planeActionBody.UpdateInputPath(0, new PathRef(new EdgeRef[] { new EdgeRef(profile) }));
}
4. Evaluating the Associative Network
Once the profile is updated, AssocManager.EvaluateTopLevelNetwork
ensures all dependencies are
recalculated.
AssocManager.EvaluateTopLevelNetwork(activeDatabase, null, 0);
Conclusion
This approach offers a structured way to programmatically update associative planar surfaces in AutoCAD, making it a valuable tool for design automation. Whether you’re scaling a profile curve, transforming it, or replacing it entirely, understanding the associative network and leveraging the .NET API unlocks a wealth of possibilities for automation.