-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCustomCommand.cs
427 lines (355 loc) · 17.3 KB
/
MyCustomCommand.cs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
namespace MyCustomCommand
{
public class MyCustomCommand : SwAddin
{
#region Private Members
private SldWorks swApp = null;
private int swCookie;
CommandManager swCommandManager;
private int swCommandGroupID = 0;
#endregion Private Members
struct Point
{
public double X;
public double Y;
public double Z;
}
#region Solidworks Add-In Callbacks
public bool ConnectToSW(object ThisSW, int Cookie)
{
#region Local Variables
string swCommandGroupTitle = "My Command Group";
string swCommandGroupToolTip = "My Command Group Title";
string swCommandGroupHint = "My Command Group Hint";
int Error = 0;
string TabName = "My Tab";
int[] swCommandID = { 0, 1, 2 };
int[] docTypes = { (int)swDocumentTypes_e.swDocASSEMBLY,
(int)swDocumentTypes_e.swDocPART,
(int)swDocumentTypes_e.swDocDRAWING};
#endregion
swApp = (SldWorks)ThisSW;
swCookie = Cookie;
_ = swApp.SetAddinCallbackInfo2(0, this, swCookie);
swCommandManager = swApp.GetCommandManager(swCookie);
#region Add Commands
CommandGroup swCommandGroup = swCommandManager.CreateCommandGroup2(swCommandGroupID, // UserID
swCommandGroupTitle, // Title
swCommandGroupToolTip, // Tool tip
swCommandGroupHint, // Hint
-1, // Position
true, // Ignore previous version
Error); // out variable
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string directory = Directory.GetParent(assemblyFolder).Parent.FullName + "\\Resources\\";
string[] IconLists = { directory + "ComboIcon20x20.png",
directory + "ComboIcon32x32.png",
directory + "ComboIcon40x40.png",
directory + "ComboIcon64x64.png",
directory + "ComboIcon96x96.png",
directory + "ComboIcon128x128.png"};
string[] MainIconLists = {directory + "icon20x20.png",
directory + "icon32x32.png",
directory + "icon40x40.png"};
swCommandGroup.IconList = IconLists;
swCommandGroup.MainIconList = MainIconLists;
string Name;
int Position;
string HintString;
string ToolTip;
int ImageListIndex;
string CallbackFunction;
string EnableMethod;
int MenuTBOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem);
Name = "CreateRectangleBox";
Position = 0;
HintString = "Create a Rectangle Box";
ToolTip = "Rectangle Box";
ImageListIndex = 0;
CallbackFunction = "CreateRectangleBox()";
EnableMethod = "EnableCreateRectangleBox()";
int swCommandIndex0 = swCommandGroup.AddCommandItem2(Name, Position, HintString, ToolTip, ImageListIndex, CallbackFunction, EnableMethod, swCommandID[0], MenuTBOption);
Name = "CreateCylinder";
Position = 1;
HintString = "Create a cylinder";
ToolTip = "Cylinder";
ImageListIndex = 1;
CallbackFunction = "CreateCylinder()";
EnableMethod = "EnableCreateCylinder()";
int swCommandIndex1 = swCommandGroup.AddCommandItem2(Name, Position, HintString, ToolTip, ImageListIndex, CallbackFunction, EnableMethod, swCommandID[1], MenuTBOption);
Name = "CreateSphere";
Position = 2;
HintString = "Create a sphere";
ToolTip = "Sphere";
ImageListIndex = 2;
CallbackFunction = "CreateSphere()";
EnableMethod = "EnableCreateSphere()";
int swCommandIndex2 = swCommandGroup.AddCommandItem2(Name, Position, HintString, ToolTip, ImageListIndex, CallbackFunction, EnableMethod, swCommandID[2], MenuTBOption);
swCommandGroup.HasToolbar = true;
swCommandGroup.HasMenu = true;
swCommandGroup.Activate();
foreach(int docType in docTypes)
{
CommandTab swCommandTab;
swCommandTab = swCommandManager.GetCommandTab(docType, TabName);
while (swCommandTab != null)
{
swCommandManager.RemoveCommandTab(swCommandTab);
swCommandTab = swCommandManager.GetCommandTab(docType, TabName);
}
if (swCommandTab == null)
{
swCommandTab = swCommandManager.AddCommandTab(docType, TabName);
CommandTabBox swCommandBox = swCommandTab.AddCommandTabBox();
int[] swCommandIDs = new int[3];
int[] swTextTypes = new int[3];
swCommandIDs[0] = swCommandGroup.get_CommandID(swCommandIndex0);
swTextTypes[0] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow;
swCommandIDs[1] = swCommandGroup.get_CommandID(swCommandIndex1);
swTextTypes[1] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow;
swCommandIDs[2] = swCommandGroup.get_CommandID(swCommandIndex2);
swTextTypes[2] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow;
_ = swCommandBox.AddCommands(swCommandIDs, swTextTypes);
}
}
#endregion
return true;
}
public bool DisconnectFromSW()
{
swCommandManager.RemoveCommandGroup2(swCommandGroupID, false);
swCommandManager = null;
swApp = null;
return true;
}
#endregion Solidworks Add-In callbacks
#region Create Rectangle Box
public void CreateRectangleBox()
{
ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
SketchManager swSketchManager = (SketchManager)swModel.SketchManager;
FeatureManager swFeatureManager = (FeatureManager)swModel.FeatureManager;
swSketchManager.InsertSketch(true);
swSketchManager.AddToDB = true;
double width_mm = 40.0;
double height_mm = 40.0;
Point Point1, Point2;
Point1.X = 0;
Point1.Y = 0;
Point1.Z = 0;
Point2.X = width_mm / 2 / 1000; // milimiter to meter
Point2.Y = height_mm / 2/ 1000; // milimiter to meter
Point2.Z = 0;
_ = swSketchManager.CreateCenterRectangle(Point1.X, Point1.Y, Point1.Z, Point2.X, Point2.Y, Point2.Z);
swModel.ClearSelection2(true);
swSketchManager.AddToDB = true;
bool Sd = true;
bool Flip = false;
bool Dir = true;
int T1 = (int)swEndConditions_e.swEndCondBlind;
int T2 = (int)swEndConditions_e.swEndCondBlind;
double D1 = 30.0 / 1000;
double D2 = 0;
bool Dchk1 = false;
bool Dchk2 = false;
bool Ddir1 = false;
bool Ddir2 = false;
double Dang1 = 0;
double Dang2 = 0;
bool OffsetReverse1 = false;
bool OffsetReverse2 = false;
bool TranslateSurface1 = false;
bool TranslateSurface2 = false;
bool Merge = true;
bool UseFeatScope = true;
bool UseAutoSelect = true;
int T0 = 0;
double StartOffset = 0;
bool FlipStartOffset = false;
_ = swFeatureManager.FeatureExtrusion3(Sd, Flip, Dir, T1, T2, D1, D2, Dchk1, Dchk2, Ddir1, Ddir2, Dang1, Dang2, OffsetReverse1, OffsetReverse2, TranslateSurface1, TranslateSurface2, Merge, UseFeatScope, UseAutoSelect, T0, StartOffset, FlipStartOffset);
swModel.ClearSelection2(true);
swModel.ForceRebuild3(true);
swModel.ViewZoomtofit2();
}
#endregion
#region Create Cylinder
public void CreateCylinder()
{
ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
SketchManager swSketchManager = (SketchManager)swModel.SketchManager;
FeatureManager swFeatureManager = (FeatureManager)swModel.FeatureManager;
ModelDocExtension swModelDocExtension = (ModelDocExtension)swModel.Extension;
swSketchManager.InsertSketch(true);
swSketchManager.AddToDB = true;
Point Point1, Point2;
Point1.X = 0;
Point1.Y = 0;
Point1.Z = 0; // milimeter to meter
Point2.X = 10.0/2/1000;
Point2.Y = 20.0/2/1000;
Point2.Z = 0; // milimeter to meter
swSketchManager.CreateCornerRectangle(Point1.X, Point1.Y, Point1.Z, Point2.X, Point2.Y, Point2.Z);
swModel.ClearSelection2(true);
_ = swModelDocExtension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, true, 16, null, (int)swSelectOption_e.swSelectOptionDefault);
swFeatureManager = swModel.FeatureManager;
bool SingleDir = true;
bool IsSolid = true;
bool IsThin = false;
bool IsCut = false;
bool ReverseDir = false;
bool BothDirectionUpToSameEntity = false;
int Dir1Type = (int)swEndConditions_e.swEndCondBlind;
int Dir2Type = (int)swEndConditions_e.swEndCondBlind;
double Dir1Angle = 360.0*Math.PI/180; // Degree to Radian
double Dir2Angle = 0;
bool OffsetReverse1 = false;
bool OffsetReverse2 = false;
double OffsetDistance1 = 0;
double OffsetDistance2 = 0;
int ThinType = 0;
double ThinThickness1 = 0;
double ThinThickness2 = 0;
bool Merge = true;
bool UseFeatScope = true;
bool UseAutoSelect = true;
swFeatureManager.FeatureRevolve2(SingleDir,
IsSolid,
IsThin,
IsCut,
ReverseDir,
BothDirectionUpToSameEntity,
Dir1Type,
Dir2Type,
Dir1Angle,
Dir2Angle,
OffsetReverse1,
OffsetReverse2,
OffsetDistance1,
OffsetDistance2,
ThinType,
ThinThickness1,
ThinThickness2,
Merge,
UseFeatScope,
UseAutoSelect);
swModel.ClearSelection2(true);
swModel.ForceRebuild3(true);
swModel.ViewZoomtofit2();
}
#endregion #region Create Cylinder
#region #region Create Sphere
public void CreateSphere()
{
ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
SketchManager swSketchManager = (SketchManager)swModel.SketchManager;
FeatureManager swFeatureManager = (FeatureManager)swModel.FeatureManager;
ModelDocExtension swModelDocExtension = (ModelDocExtension)swModel.Extension;
swSketchManager.InsertSketch(true);
swSketchManager.AddToDB = true;
Point Point1, Point2;
Point1.X = 0; Point1.Y = 0; Point1.Z = 0; // milimeter to meter
Point2.X = 0; Point2.Y = 30.0 / 1000; Point2.Z = 0;
_ = swSketchManager.CreateLine(Point1.X, Point1.Y, Point1.Z, Point2.X, Point2.Y, Point2.Z);
swModel.ClearSelection2(true);
Point1.X = 0; Point1.Y = Point2.Y; Point1.Z = 0; // milimeter to meter
Point2.X = 0; Point2.Y = 0; Point2.Z = 0;
_ = swSketchManager.CreateTangentArc(Point1.X, Point1.Y, Point1.Z, Point2.X, Point2.Y, Point2.Z, 4);
swModel.ClearSelection2(true);
_ = swModelDocExtension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, true, 16, null, (int)swSelectOption_e.swSelectOptionDefault);
swFeatureManager = swModel.FeatureManager;
bool SingleDir = true;
bool IsSolid = true;
bool IsThin = false;
bool IsCut = false;
bool ReverseDir = false;
bool BothDirectionUpToSameEntity = false;
int Dir1Type = (int)swEndConditions_e.swEndCondBlind;
int Dir2Type = (int)swEndConditions_e.swEndCondBlind;
double Dir1Angle = 360.0 * Math.PI / 180; // Degree to Radian
double Dir2Angle = 0;
bool OffsetReverse1 = false;
bool OffsetReverse2 = false;
double OffsetDistance1 = 0;
double OffsetDistance2 = 0;
int ThinType = 0;
double ThinThickness1 = 0;
double ThinThickness2 = 0;
bool Merge = true;
bool UseFeatScope = true;
bool UseAutoSelect = true;
swFeatureManager.FeatureRevolve2(SingleDir,
IsSolid,
IsThin,
IsCut,
ReverseDir,
BothDirectionUpToSameEntity,
Dir1Type,
Dir2Type,
Dir1Angle,
Dir2Angle,
OffsetReverse1,
OffsetReverse2,
OffsetDistance1,
OffsetDistance2,
ThinType,
ThinThickness1,
ThinThickness2,
Merge,
UseFeatScope,
UseAutoSelect);
swModel.ClearSelection2(true);
swModel.ForceRebuild3(true);
swModel.ViewZoomtofit2();
}
#endregion #region Create Sphere
#region Enable Method
/// <summary>
/// Optional function that controls the state of the item; if specified, then SOLIDWORKS calls this function before displaying the item
/// if return 1, the Solidworks deselects and enables the item; this is the default state if no update function is specified.
/// See detail about AddCommandItem2() method here: https://help.solidworks.com/2023/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.icommandgroup~addcommanditem2.html
/// </summary>
/// <returns></returns>
public int EnableCreateRectangleBox()
{
return 1;
}
public int EnableCreateCylinder()
{
return 1;
}
public int EnableCreateSphere()
{
return 1;
}
#endregion Enable Method
#region COM Registration
[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
RegistryKey mLocalMachine = Registry.LocalMachine;
string subKey = "SOFTWARE\\SOLIDWORKS\\ADDINS\\{" + t.GUID.ToString() + "}";
RegistryKey mRegistryKey = mLocalMachine.CreateSubKey(subKey);
mRegistryKey.SetValue(null, 0);
mRegistryKey.SetValue("Description", "My Solidworks Add-In");
mRegistryKey.SetValue("Title", "My Custom Command");
}
[ComUnregisterFunction]
public static void UnregisterFunction(Type t)
{
RegistryKey mLocalMachine = Registry.LocalMachine;
string subkey = "SOFTWARE\\SOLIDWORKS\\ADDINS\\{" + t.GUID.ToString() + "}";
mLocalMachine.DeleteSubKey(subkey);
mLocalMachine = null;
}
#endregion COM Registration
}
}