This project, GroupingComponents, is an AutoCAD Mechanical sample. It provides a custom command for working with 2D Mechanical Structure in AutoCAD Mechanical using C# and .NET 8.0.
This example demonstrates how to use the AutoCAD Mechanical API to create an object and add it as a component to the mechanical structure, complete with a component view.
The first step is to create a standard AutoCAD entity. For brevity, we'll create a single circle centered at (0,0,0) with a radius of 10:
// Create a circle in ModelSpace
using (var tr = db.TransactionManager.StartTransaction())
{
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
var btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
var center = new Point3d(0,0,0);
var circle = new Circle(center, Vector3d.ZAxis,10);
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
tr.Commit();
// Save the ObjectId for later use
var gObjId = circle.ObjectId;
}Get the AutoCAD Mechanical application and the 2D structure manager:
// Get the Acadm application and structure manager
IAcadApplication acadApp = (IAcadApplication)Application.AcadApplication;
IAcadmApplication acadMapp = acadApp.GetInterfaceObject("AcadmAuto.AcadmApplication.25") as IAcadmApplication;
IMcad2DStructureMgr2 gMgr = acadMapp.ActiveDocument.StructureMgr2D as IMcad2DStructureMgr2;Get the Geometry API root object and related objects:
IGeApplication gGeApp = acadApp.GetInterfaceObject("GE.Application.25") as IGeApplication;
GePoint startPoint = gGeApp.Point();
GePoint endPoint = gGeApp.Point();
GeVector compTranslation = gGeApp.Vector();
GeMatrix compGeMatrix = gGeApp.Matrix();
endPoint.Set(10,10,0);
compTranslation.Set(startPoint, endPoint);
compGeMatrix.SetToTranslation(compTranslation);// Get the entity by ObjectId and check if it's free
AcadEntity selectedObj = ... // obtain from ObjectId
gMgr.IsFreeEntity(selectedObj);string compName = "MyComponent";
string viewName = "FirstView";McadComponentDefinition compDef = null;
bool hasComp = gMgr.HasComponentDefinition(compName, out compDef);
if (hasComp)
{
// Component already exists
return;
}compDef = gMgr.AddNewComponentDefinition(compName);
McadComponentDefinition parentCompDef = gMgr.RootComponentDefinition;
McadComponent comp = parentCompDef.AddComponent(compDef, compGeMatrix);object[] entityIds = new object[] { gObjId }; // Use the ObjectId of the circle
McadComponentViewDefinition compViewDef = null;
McadComponentView compView = null;
McadComponentView targetView = null;
gMgr.AddNewComponentViewDefinition(out compViewDef, out compView, comp, compGeMatrix, Type.Missing, entityIds, targetView, viewName);acadDocCom.Application.ZoomExtents();Before running this code, ensure the following type libraries are referenced Please refer How to add COM references in .NET projects for more details.
- Autodesk AutoCAD Mechanical 2025 Type Library (
AcadmAuto25.0.tlb) - Autodesk GeAuto 2025 Type Library (
GeAuto25.0.tlb) - AutoCAD Type libraries AXDBLib (
acax25enu.tlb) - AutoCAD Type libraries AxDb25enu (
AxDb25enu.tlb)
Follow these steps to build the AutoCAD Add-in:
-
Restore and Build: Open your terminal or command prompt and navigate to the project's root directory. Then, execute the following commands:
dotnet restore dotnet build -c Release
-
Locate Output: Upon successful build, the compiled output will be available in the following directory:
bin\Release\net8.0-windows\
Madhukar Moogala (Autodesk)
