-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAcadCommands.cs
More file actions
272 lines (219 loc) · 8.87 KB
/
Copy pathAcadCommands.cs
File metadata and controls
272 lines (219 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using AcadmAuto;
using AutoCAD;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using GEAuto;
using System;
using System.Runtime.InteropServices;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(GroupingComponents.AcadCommands))]
[assembly: ExtensionApplication(typeof(GroupingComponents.CommandsRegistrar))]
/// <summary>
/// Before running this code, check that the required libraries are referenced in your project.
// 1- Autodesk AutoCAD Mechanical 2025 Type Library (C:\Program Files\Common Files\Autodesk Shared\AcadmAuto25.0.tlb)
// 2 - Autodesk GeAuto 2025 Type Library (C:\Program Files\Common Files\Autodesk Shared\GeAuto25.0.tlb)
// 3 - AXDBLib (C:\Program Files\Common Files\Autodesk Shared\acax25enu.tlb)
// 4- AxDb25enu.tlb (C:\Program Files\Common Files\Autodesk Shared\AxDb25enu.tlb)
/// </summary>
class CLSIDUtils
{
[DllImport("oleaut32.dll", PreserveSig = false)]
static extern void GetActiveObject(
ref Guid rclsid,
IntPtr pvReserved,
[MarshalAs(UnmanagedType.IUnknown)] out Object ppunk
);
[DllImport("ole32.dll")]
static extern int CLSIDFromProgID(
[MarshalAs(UnmanagedType.LPWStr)] string lpszProgID,
out Guid pclsid
);
public static object GetActiveObject(string progId)
{
Guid clsid;
CLSIDFromProgID(progId, out clsid);
object obj;
GetActiveObject(ref clsid, IntPtr.Zero, out obj);
return obj;
}
}
namespace GroupingComponents // This will be replaced by the user's chosen
// namespace
{
/// <summary>
/// This class is used to register the commands in the AutoCAD environment.
/// </summary>
public class CommandsRegistrar : IExtensionApplication
{
/// <summary>
/// Initializes the application.
/// </summary>
public void Initialize()
{
// Register your business logic specific here if needed
// List all commands
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nAcadCommands plugin loaded successfully!");
}
/// <summary>
/// Cleans up resources when the application is terminated.
/// </summary>
public void Terminate()
{
// Cleanup code if necessary
}
}
/// <summary>
/// Contains example AutoCAD commands.
/// </summary>
public class AcadCommands
{
[CommandMethod("CreateComponentGroup")]
public void CreateComponentGroup()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null)
return;
Editor ed = doc.Editor;
// COM AutoCAD application (IAcadApplication) - already available to you
IAcadApplication acadApp = (IAcadApplication)Application.AcadApplication;
if (acadApp == null)
{
ed.WriteMessage("\nUnable to get COM AutoCAD application.");
return;
}
// Mechanical "Acadm" application and 2D structure manager
// IAcadmApplication acadMapp = CLSIDUtils.GetActiveObject("AcadmAuto.AcadmApplication.25") as IAcadmApplication;
IAcadmApplication acadMapp = acadApp.GetInterfaceObject("AcadmAuto.AcadmApplication.25") as IAcadmApplication;
if (acadMapp == null)
{
ed.WriteMessage("\nUnable to get AcadmAuto.AcadmApplication interface.");
return;
}
IMcad2DStructureMgr2 structureMgr2D = acadMapp.ActiveDocument.StructureMgr2D as IMcad2DStructureMgr2;
if (structureMgr2D == null)
{
ed.WriteMessage("\nUnable to get StructureMgr2D.");
return;
}
// Geometry application (GE.Application)
IGeApplication geApp = acadApp.GetInterfaceObject("GE.Application.25") as IGeApplication;
// COM AcadDocument (IAcadDocument) for direct COM operations
IAcadDocument acadDocCom = (IAcadDocument)doc.GetAcadDocument();
if (acadDocCom == null)
{
ed.WriteMessage("\nUnable to get IAcadDocument COM object.");
return;
}
try
{
object pickedEntity = null;
object pickedPoint = null;
try
{
// Typical COM signature: AcadUtility.GetEntity(out AcadEntity, out VariantPoint, promptString)
acadDocCom.Utility.GetEntity(out pickedEntity, out pickedPoint, "Select an entity to add to component group:");
}
catch
{
// If GetEntity signature differs, fall back to .NET selection below.
pickedEntity = null;
}
// If COM GetEntity succeeded, use the returned Acad entity directly
if (pickedEntity != null)
{
// Check free entity via structure manager
bool isFree = false;
try
{
isFree = structureMgr2D.IsFreeEntity((AXDBLib.AcadEntity)pickedEntity);
}
catch (System.Exception exIsFree)
{
ed.WriteMessage($"\nIsFreeEntity call failed: {exIsFree.Message}");
return;
}
if (!isFree)
{
ed.WriteMessage("\nSelected entity already belongs to a component group.");
return;
}
// Ready to create component definition and view
CreateComponentAndView(structureMgr2D, geApp, pickedEntity, acadDocCom, ed);
return;
}
}
catch (System.Exception)
{
throw;
}
}
private void CreateComponentAndView(IMcad2DStructureMgr2 structureMgr2D, IGeApplication geApp, object comEntity, IAcadDocument acadDocCom, Editor ed)
{
GePoint startPoint = geApp.Point();
GePoint endPoint = geApp.Point();
GeVector compTranslation = geApp.Vector();
GeMatrix compGeMatrix = geApp.Matrix();
AcadEntity acadEntity = comEntity as AcadEntity;
long entId = acadEntity.ObjectID;
endPoint.Set(10, 10, 0);
compTranslation.Set(startPoint, endPoint);
compGeMatrix.SetToTranslation(compTranslation);
string compName = "MyComponent";
McadComponentDefinition compDef = null;
bool hasComp = false;
try
{
hasComp = structureMgr2D.HasComponentDefinition(compName, out compDef);
}
catch
{
ed.WriteMessage("\nError checking for existing component definition.");
}
if (hasComp)
{
ed.WriteMessage($"\nComponent '{compName}' already exists.");
return;
}
try
{
compDef = structureMgr2D.AddNewComponentDefinition(compName);
}
catch (System.Exception ex)
{
ed.WriteMessage($"\nFailed to add component definition: {ex.Message}");
return;
}
McadComponentDefinition parentCompDef = structureMgr2D.RootComponentDefinition;
McadComponent pCompInstance = parentCompDef.AddComponent(compDef, compGeMatrix);
McadComponentViewDefinition pNewViewDef = null;
McadComponentView pNewViewInstance = null;
object addViews = Type.Missing;
long[] entityIds = [entId];
object targetView = Type.Missing;
object name = "FirstView";
try
{
structureMgr2D.AddNewComponentViewDefinition(
out pNewViewDef, out pNewViewInstance, pCompInstance, compGeMatrix,
addViews, entityIds, targetView, name);
}
catch (System.Exception ex)
{
ed.WriteMessage($"\nFailed to create component view definition: {ex.Message}");
return;
}
try
{
acadDocCom.Application.ZoomExtents();
}
catch { }
ed.WriteMessage("\nComponent and view created.");
}
}
}
// This file is part of the AutoCAD .NET Plugin template.