-
Notifications
You must be signed in to change notification settings - Fork 123
/
Model.cs
1228 lines (1029 loc) · 47.6 KB
/
Model.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace AngelSix.SolidDna
{
/// <summary>
/// Represents a SolidWorks model of any type (Drawing, Part or Assembly)
/// </summary>
public class Model : SharedSolidDnaObject<ModelDoc2>
{
#region Public Properties
/// <summary>
/// The absolute file path of this model if it has been saved
/// </summary>
public string FilePath { get; protected set; }
/// <summary>
/// Indicates if this file has been saved (so exists on disk).
/// If not, it's a new model currently only in-memory and will not have a file path
/// </summary>
public bool HasBeenSaved => !string.IsNullOrEmpty(FilePath);
/// <summary>
/// Indicates if this file needs saving (has file changes).
/// </summary>
public bool NeedsSaving => BaseObject.GetSaveFlag();
/// <summary>
/// The type of document such as a part, assembly or drawing
/// </summary>
public ModelType ModelType { get; protected set; }
/// <summary>
/// True if this model is a part
/// </summary>
public bool IsPart => ModelType == ModelType.Part;
/// <summary>
/// True if this model is an assembly
/// </summary>
public bool IsAssembly => ModelType == ModelType.Assembly;
/// <summary>
/// True if this model is a drawing
/// </summary>
public bool IsDrawing => ModelType == ModelType.Drawing;
/// <summary>
/// Contains extended information about the model
/// </summary>
public ModelExtension Extension { get; protected set; }
/// <summary>
/// Contains the current active configuration information
/// </summary>
public ModelConfiguration ActiveConfiguration { get; protected set; }
/// <summary>
/// The selection manager for this model
/// </summary>
public SelectionManager SelectionManager { get; protected set; }
/// <summary>
/// Get the number of configurations
/// </summary>
public int ConfigurationCount => BaseObject.GetConfigurationCount();
/// <summary>
/// Gets the configuration names
/// </summary>
public List<string> ConfigurationNames => new List<string>((string[])BaseObject.GetConfigurationNames());
/// <summary>
/// The mass properties of the part
/// </summary>
public MassProperties MassProperties => Extension.GetMassProperties();
#endregion
#region Public Events
/// <summary>
/// Called after the a drawing sheet was added
/// </summary>
public event Action<string> DrawingSheetAdded = (sheetName) => { };
/// <summary>
/// Called after the active drawing sheet has changed
/// </summary>
public event Action<string> DrawingActiveSheetChanged = (sheetName) => { };
/// <summary>
/// Called before the active drawing sheet changes
/// </summary>
public event Action<string> DrawingActiveSheetChanging = (sheetName) => { };
/// <summary>
/// Called after the a drawing sheet was deleted
/// </summary>
public event Action<string> DrawingSheetDeleted = (sheetName) => { };
/// <summary>
/// Called as the model is about to be closed
/// </summary>
public event Action ModelClosing = () => { };
/// <summary>
/// Called when the model is first modified since it was last saved.
/// SOLIDWORKS marks the file as Dirty and sets <see cref="IModelDoc2.GetSaveFlag"/>
/// </summary>
public event Action ModelModified = () => { };
/// <summary>
/// Called after a model was rebuilt (any model type) or if the rollback bar position changed (for parts and assemblies).
/// NOTE: Does not always fire on normal rebuild (Ctrl+B) on assemblies.
/// </summary>
public event Action ModelRebuilt = () => { };
/// <summary>
/// Called as the model has been saved
/// </summary>
public event Action ModelSaved = () => { };
/// <summary>
/// Called when the user cancels the save action and <see cref="ModelSaved"/> will not be fired.
/// </summary>
public event Action ModelSaveCanceled = () => { };
/// <summary>
/// Called before a model is saved with a new file name.
/// Called before the Save As dialog is shown.
/// Allows you to make changes that need to be included in the save.
/// </summary>
public event Action<string> ModelSavingAs = (fileName) => { };
/// <summary>
/// Called when any of the model properties changes
/// </summary>
public event Action ModelInformationChanged = () => { };
/// <summary>
/// Called when the active configuration has changed
/// </summary>
public event Action ActiveConfigurationChanged = () => { };
/// <summary>
/// Called when the selected objects in the model have changed
/// </summary>
public event Action SelectionChanged = () => { };
#endregion
#region Constructor
/// <summary>
/// Default constructor
/// </summary>
public Model(ModelDoc2 model) : base(model)
{
// Update information about this model
ReloadModelData();
// Attach event handlers
SetupModelEventHandlers();
}
#endregion
#region Model Data
/// <summary>
/// Reloads all variables and data about this model
/// </summary>
protected void ReloadModelData()
{
// Clean up any previous data
DisposeAllReferences();
// Can't do much if there is no document
if (BaseObject == null)
return;
// Get the file path
FilePath = BaseObject.GetPathName();
// Get the models type
ModelType = (ModelType)BaseObject.GetType();
// Get the extension
Extension = new ModelExtension(BaseObject.Extension, this);
// Get the active configuration
ActiveConfiguration = new ModelConfiguration(BaseObject.IGetActiveConfiguration());
// Get the selection manager
SelectionManager = new SelectionManager(BaseObject.ISelectionManager);
// Set drawing access
Drawing = IsDrawing ? new DrawingDocument((DrawingDoc)BaseObject) : null;
// Set part access
Part = IsPart ? new PartDocument((PartDoc)BaseObject) : null;
// Set assembly access
Assembly = IsAssembly ? new AssemblyDocument((AssemblyDoc)BaseObject) : null;
// Inform listeners
ModelInformationChanged();
}
/// <summary>
/// Hooks into model-specific events for keeping track of up-to-date information
/// </summary>
protected void SetupModelEventHandlers()
{
// Based on the type of model this is...
switch (ModelType)
{
// Hook into the save and destroy events to keep data fresh
case ModelType.Assembly:
AsAssembly().ActiveConfigChangePostNotify += ActiveConfigChangePostNotify;
AsAssembly().DestroyNotify += FileDestroyedNotify;
AsAssembly().FileSaveAsNotify2 += FileSaveAsPreNotify;
AsAssembly().FileSavePostCancelNotify += FileSaveCanceled;
AsAssembly().FileSavePostNotify += FileSavePostNotify;
AsAssembly().ModifyNotify += FileModified;
AsAssembly().RegenPostNotify2 += AssemblyOrPartRebuilt;
AsAssembly().UserSelectionPostNotify += UserSelectionPostNotify;
AsAssembly().ClearSelectionsNotify += UserSelectionPostNotify;
break;
case ModelType.Part:
AsPart().ActiveConfigChangePostNotify += ActiveConfigChangePostNotify;
AsPart().DestroyNotify += FileDestroyedNotify;
AsPart().FileSaveAsNotify2 += FileSaveAsPreNotify;
AsPart().FileSavePostCancelNotify += FileSaveCanceled;
AsPart().FileSavePostNotify += FileSavePostNotify;
AsPart().ModifyNotify += FileModified;
AsPart().RegenPostNotify2 += AssemblyOrPartRebuilt;
AsPart().UserSelectionPostNotify += UserSelectionPostNotify;
AsPart().ClearSelectionsNotify += UserSelectionPostNotify;
break;
case ModelType.Drawing:
AsDrawing().ActivateSheetPostNotify += SheetActivatePostNotify;
AsDrawing().ActivateSheetPreNotify += SheetActivatePreNotify;
AsDrawing().AddItemNotify += DrawingItemAddNotify;
AsDrawing().DeleteItemNotify += DrawingDeleteItemNotify;
AsDrawing().DestroyNotify += FileDestroyedNotify;
AsDrawing().FileSaveAsNotify2 += FileSaveAsPreNotify;
AsDrawing().FileSavePostCancelNotify += FileSaveCanceled;
AsDrawing().FileSavePostNotify += FileSavePostNotify;
AsDrawing().ModifyNotify += FileModified;
AsDrawing().RegenPostNotify += DrawingRebuilt;
AsDrawing().UserSelectionPostNotify += UserSelectionPostNotify;
AsDrawing().ClearSelectionsNotify += UserSelectionPostNotify;
break;
}
}
/// <summary>
/// Packs up the current model into a flattened structure to a new location
/// </summary>
/// <param name="outputFolder">The output folder. If left blank will go to Local App Data folder under a unique name</param>
/// <param name="filenamePrefix">A prefix to add to all files once packed</param>
/// <returns></returns>
public string PackAndGo(string outputFolder = null, string filenamePrefix = "")
{
// Wrap any error
return SolidDnaErrors.Wrap(() =>
{
// If no output path specified...
if (string.IsNullOrEmpty(outputFolder))
// Set it to app data folder
outputFolder = Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),
"SolidDna",
"PackAndGo",
// Unique folder name of current time
DateTime.UtcNow.ToString("MM-dd-yyyy-HH-mm-ss"));
// Create output folder
Directory.CreateDirectory(outputFolder);
// If folder is not empty
if (Directory.GetFiles(outputFolder)?.Length > 0)
throw new ArgumentException("Output folder is not empty");
// Get pack and go object
var packAndGo = BaseObject.Extension.GetPackAndGo();
// Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components
packAndGo.IncludeDrawings = true;
// NOTE: We could include more files...
// packAndGo.IncludeSimulationResults = true;
// packAndGo.IncludeToolboxComponents = true;
// Add prefix to all files
packAndGo.AddPrefix = filenamePrefix;
// Get current paths and filenames of the assembly's documents
if (!packAndGo.GetDocumentNames(out var filesArray))
// Throw error
throw new ArgumentException("Failed to get document names");
// Cast filenames
var filenames = (string[])filesArray;
// If fails to set folder where to save the files
if (!packAndGo.SetSaveToName(true, outputFolder))
// Throw error
throw new ArgumentException("Failed to set save to folder");
// Flatten the Pack and Go folder so all files are in single folder
packAndGo.FlattenToSingleFolder = true;
// Save all files
var results = (PackAndGoSaveStatus[])BaseObject.Extension.SavePackAndGo(packAndGo);
// There is a result per file, so all must be successful
if (!results.All(f => f == PackAndGoSaveStatus.Success))
// Throw error
throw new ArgumentException("Failed to save pack and go");
// Return the output folder
return outputFolder;
},
SolidDnaErrorTypeCode.SolidWorksModel,
SolidDnaErrorCode.SolidWorksModelPackAndGoError,
Localization.GetString("SolidWorksModelPackAndGoError"));
}
/// <summary>
/// Unhooks model-specific events when model becomes inactive.
/// Model becomes inactive when it is closed or when another model becomes the active model.
/// </summary>
protected void ClearModelEventHandlers()
{
switch (ModelType)
{
case ModelType.Part when AsPart() == null:
case ModelType.Assembly when AsAssembly() == null:
case ModelType.Drawing when AsDrawing() == null:
{
// Happens in multiple cases:
// 1: When SolidWorks is being closed
// 1: When the non-last model is being closed
// 2: When the first model is opened after all models were closed.
return;
}
}
// Based on the type of model this is...
switch (ModelType)
{
// Hook into the save and destroy events to keep data fresh
case ModelType.Assembly:
AsAssembly().ActiveConfigChangePostNotify -= ActiveConfigChangePostNotify;
AsAssembly().DestroyNotify -= FileDestroyedNotify;
AsAssembly().FileSaveAsNotify2 -= FileSaveAsPreNotify;
AsAssembly().FileSavePostCancelNotify -= FileSaveCanceled;
AsAssembly().FileSavePostNotify -= FileSavePostNotify;
AsAssembly().ModifyNotify -= FileModified;
AsAssembly().RegenPostNotify2 -= AssemblyOrPartRebuilt;
AsAssembly().UserSelectionPostNotify -= UserSelectionPostNotify;
AsAssembly().ClearSelectionsNotify -= UserSelectionPostNotify;
break;
case ModelType.Part:
AsPart().ActiveConfigChangePostNotify -= ActiveConfigChangePostNotify;
AsPart().DestroyNotify -= FileDestroyedNotify;
AsPart().FileSaveAsNotify2 -= FileSaveAsPreNotify;
AsPart().FileSavePostCancelNotify -= FileSaveCanceled;
AsPart().FileSavePostNotify -= FileSavePostNotify;
AsPart().ModifyNotify -= FileModified;
AsPart().RegenPostNotify2 -= AssemblyOrPartRebuilt;
AsPart().UserSelectionPostNotify -= UserSelectionPostNotify;
AsPart().ClearSelectionsNotify -= UserSelectionPostNotify;
break;
case ModelType.Drawing:
AsDrawing().ActivateSheetPostNotify -= SheetActivatePostNotify;
AsDrawing().ActivateSheetPreNotify -= SheetActivatePreNotify;
AsDrawing().AddItemNotify -= DrawingItemAddNotify;
AsDrawing().DeleteItemNotify -= DrawingDeleteItemNotify;
AsDrawing().DestroyNotify -= FileDestroyedNotify;
AsDrawing().FileSaveAsNotify2 -= FileSaveAsPreNotify;
AsDrawing().FileSavePostCancelNotify -= FileSaveCanceled;
AsDrawing().FileSavePostNotify -= FileSavePostNotify;
AsDrawing().ModifyNotify -= FileModified;
AsDrawing().RegenPostNotify -= DrawingRebuilt;
AsDrawing().UserSelectionPostNotify -= UserSelectionPostNotify;
AsDrawing().ClearSelectionsNotify -= UserSelectionPostNotify;
break;
}
}
#endregion
#region Model Event Methods
/// <summary>
/// Called when a part or assembly has its active configuration changed
/// </summary>
/// <returns></returns>
protected int ActiveConfigChangePostNotify()
{
// Refresh data
ReloadModelData();
// Inform listeners
ActiveConfigurationChanged();
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called after an assembly or part was rebuilt or if the rollback bar position changed.
/// </summary>
/// <param name="firstFeatureBelowRollbackBar"></param>
/// <returns></returns>
protected int AssemblyOrPartRebuilt(object firstFeatureBelowRollbackBar)
{
// Inform listeners
ModelRebuilt();
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when a drawing item is added to the feature tree
/// </summary>
/// <param name="entityType">Type of entity that is changed</param>
/// <param name="itemName">Name of entity that is changed</param>
/// <returns></returns>
protected int DrawingItemAddNotify(int entityType, string itemName)
{
// Check if a sheet is added.
// SolidWorks always activates the new sheet, but the sheet activate events aren't fired.
if (EntityIsDrawingSheet(entityType))
{
SheetAddedNotify(itemName);
SheetActivatePostNotify(itemName);
}
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when a drawing item is removed from the feature tree
/// </summary>
/// <param name="entityType">Type of entity that is changed</param>
/// <param name="itemName">Name of entity that is changed</param>
/// <returns></returns>
protected int DrawingDeleteItemNotify(int entityType, string itemName)
{
// Check if the removed items is a sheet
if (EntityIsDrawingSheet(entityType))
{
// Inform listeners
DrawingSheetDeleted(itemName);
}
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called after a drawing was rebuilt.
/// </summary>
/// <returns></returns>
protected int DrawingRebuilt()
{
// Inform listeners
ModelRebuilt();
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when the user changes the selected objects
/// </summary>
/// <returns></returns>
protected int UserSelectionPostNotify()
{
// Inform Listeners
SelectionChanged();
return 0;
}
/// <summary>
/// Called when the user cancels the save action and <see cref="FileSavePostNotify"/> will not be fired.
/// </summary>
/// <returns></returns>
private int FileSaveCanceled()
{
// Inform listeners
ModelSaveCanceled();
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when a model has been saved
/// </summary>
/// <param name="fileName">The name of the file that has been saved</param>
/// <param name="saveType">The type of file that has been saved</param>
/// <returns></returns>
protected int FileSavePostNotify(int saveType, string fileName)
{
// Were we saved or is this a new file?
var wasNewFile = !HasBeenSaved;
// Update filepath
FilePath = BaseObject.GetPathName();
// Inform listeners
ModelSaved();
// NOTE: Due to bug in SolidWorks, saving new files refreshes the COM reference
// without it ever being so kind as to inform us via ANY callback in
// the SolidWorksApplication or this model
//
// So to fix, wait for an idle moment and refresh our info.
// Best fix I can think of for now.
//
// We could keep checking the COM instance BaseObject doesn't throw
// an error to detect when it got disposed but I think the idle
// is less intensive and works fine so far
if (wasNewFile)
{
void refreshEvent()
{
SolidWorksEnvironment.Application.RequestActiveModelChanged();
SolidWorksEnvironment.Application.Idle -= refreshEvent;
}
SolidWorksEnvironment.Application.Idle += refreshEvent;
}
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when a model is about to be saved with a new file name.
/// Called before the Save As dialog is shown.
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private int FileSaveAsPreNotify(string fileName)
{
// Inform listeners
ModelSavingAs(fileName);
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when a model is about to be destroyed.
/// This is a pre-notify so just clean up data, don't reload for new data yet
/// </summary>
/// <returns></returns>
protected int FileDestroyedNotify()
{
// Inform listeners
ModelClosing();
// This is a pre-notify but we are going to be dead
// so dispose ourselves (our underlying COM objects)
Dispose();
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called when the model is first modified since it was last saved.
/// </summary>
/// <returns></returns>
protected int FileModified()
{
// Inform listeners
ModelModified();
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called after the active drawing sheet is changed.
/// </summary>
/// <param name="sheetName">Name of the sheet that is activated</param>
/// <returns></returns>
protected int SheetActivatePostNotify(string sheetName)
{
// Inform listeners
DrawingActiveSheetChanged(sheetName);
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called before the active drawing sheet changes
/// </summary>
protected int SheetActivatePreNotify(string sheetName)
{
// Inform listeners
DrawingActiveSheetChanging(sheetName);
// NOTE: 0 is success, anything else is an error
return 0;
}
/// <summary>
/// Called after a sheet is added.
/// </summary>
/// <param name="sheetName">Name of the new sheet</param>
/// <returns></returns>
protected int SheetAddedNotify(string sheetName)
{
// Inform listeners
DrawingSheetAdded(sheetName);
// NOTE: 0 is success, anything else is an error
return 0;
}
#endregion
#region Specific Model
/// <summary>
/// Casts the current model to an assembly
/// NOTE: Check the <see cref="ModelType"/> to confirm this model is of the correct type before casting
/// </summary>
/// <returns></returns>
public AssemblyDoc AsAssembly() => (AssemblyDoc)BaseObject;
/// <summary>
/// Casts the current model to a part
/// NOTE: Check the <see cref="ModelType"/> to confirm this model is of the correct type before casting
/// </summary>
/// <returns></returns>
public PartDoc AsPart() => (PartDoc)BaseObject;
/// <summary>
/// Casts the current model to a drawing
/// NOTE: Check the <see cref="ModelType"/> to confirm this model is of the correct type before casting
/// </summary>
/// <returns></returns>
public DrawingDoc AsDrawing() => (DrawingDoc)BaseObject;
/// <summary>
/// Accesses the current model as a drawing to expose all Drawing API calls.
/// Check <see cref="IsDrawing"/> before calling into this.
/// </summary>
public DrawingDocument Drawing { get; private set; }
/// <summary>
/// Accesses the current model as a part to expose all Part API calls.
/// Check <see cref="IsPart"/> before calling into this.
/// </summary>
public PartDocument Part { get; private set; }
/// <summary>
/// Accesses the current model as an assembly to expose all Assembly API calls.
/// Check <see cref="IsAssembly"/> before calling into this.
/// </summary>
public AssemblyDocument Assembly { get; private set; }
#endregion
#region Custom Properties
/// <summary>
/// Sets a custom property to the given value.
/// If a configuration is specified then the configuration-specific property is set
/// </summary>
/// <param name="name">The name of the property</param>
/// <param name="value">The value of the property</param>
/// <param name="configuration">The configuration to set the properties from, otherwise set custom property</param>
public void SetCustomProperty(string name, string value, string configuration = null)
{
// Get the custom property editor
using (var editor = Extension.CustomPropertyEditor(configuration))
{
// Set the property
editor.SetCustomProperty(name, value);
}
}
/// <summary>
/// Gets a custom property by the given name
/// </summary>
/// <param name="name">The name of the custom property</param>
/// <param name="configuration">The configuration to get the properties from, otherwise get custom property</param>
///<param name="resolved">True to get the resolved value of the property, false to get the actual text</param>
/// <returns></returns>
public string GetCustomProperty(string name, string configuration = null, bool resolved = false)
{
// Get the custom property editor
using (var editor = Extension.CustomPropertyEditor(configuration))
{
// Get the property
return editor.GetCustomProperty(name, resolve: resolved);
}
}
/// <summary>
/// Gets all of the custom properties in this model.
/// Simply set the Value of the custom property to edit it
/// </summary>
/// <param name="action">The custom properties list to be worked on inside the action. NOTE: Do not store references to them outside of this action</param>
/// <param name="configuration">Specify a configuration to get configuration-specific properties</param>
/// <returns></returns>
public void CustomProperties(Action<List<CustomProperty>> action, string configuration = null)
{
// Get the custom property editor
using (var editor = Extension.CustomPropertyEditor(configuration))
{
// Get the properties
var properties = editor.GetCustomProperties();
// Let the action use them
action(properties);
}
}
/// <summary>
/// Gets all of the custom properties in this model including any configuration specific properties
/// </summary>
/// <param name="action">The custom properties list to be worked on inside the action. NOTE: Do not store references to them outside of this action</param>
/// <returns>Custom property and the configuration name it belongs to (or null if none)</returns>
public IEnumerable<(string configuration, CustomProperty property)> AllCustomProperties()
{
// Custom properties
using (var editor = Extension.CustomPropertyEditor(null))
{
// Get the properties
var properties = editor.GetCustomProperties();
// Loop each property
foreach (var property in properties)
// Return result
yield return (null, property);
}
// Configuration specific properties
foreach (var configuration in ConfigurationNames)
{
// Get the custom property editor
using (var editor = Extension.CustomPropertyEditor(configuration))
{
// Get the properties
var properties = editor.GetCustomProperties();
// Loop each property
foreach (var property in properties)
// Return result
yield return (configuration, property);
}
}
}
#endregion
#region Drawings
/// <summary>
/// Check if an entity that was added, changed or removed is a drawing sheet.
/// </summary>
/// <param name="entityType">Type of the entity</param>
/// <returns></returns>
private static bool EntityIsDrawingSheet(int entityType)
{
return entityType == (int)swNotifyEntityType_e.swNotifyDrawingSheet;
}
#endregion
#region Material
/// <summary>
/// Read the material from the model
/// </summary>
/// <returns></returns>
public Material GetMaterial()
{
// Wrap any error
return SolidDnaErrors.Wrap(() =>
{
// Get the Id's
var idString = BaseObject.MaterialIdName;
// Make sure we have some data
if (idString == null || !idString.Contains("|"))
return null;
// The Id string is split by pipes |
var ids = idString.Split('|');
// We need at least the first and second
// (first is database file name, second is material name)
if (ids.Length < 2)
throw new ArgumentOutOfRangeException(Localization.GetString("SolidWorksModelGetMaterialIdMissingError"));
// Extract data
var databaseName = ids[0];
var materialName = ids[1];
// See if we have a database file with the same name
var fullPath = SolidWorksEnvironment.Application.GetMaterials()?.FirstOrDefault(f => string.Equals(databaseName, Path.GetFileNameWithoutExtension(f.Database), StringComparison.InvariantCultureIgnoreCase));
var found = fullPath != null;
// Now we have the file, try and find the material from it
if (found)
{
var foundMaterial = SolidWorksEnvironment.Application.FindMaterial(fullPath.Database, materialName);
if (foundMaterial != null)
return foundMaterial;
}
// If we got here, the material was not found
// So fill in as much information as we have
return new Material
{
Database = databaseName,
Name = materialName
};
},
SolidDnaErrorTypeCode.SolidWorksModel,
SolidDnaErrorCode.SolidWorksModelGetMaterialError,
Localization.GetString("SolidWorksModelGetMaterialError"));
}
/// <summary>
/// Sets the material for the model
/// </summary>
/// <param name="material">The material</param>
/// <param name="configuration">The configuration to set the material on, null for the default</param>
///
public void SetMaterial(Material material, string configuration = null)
{
// Wrap any error
SolidDnaErrors.Wrap(() =>
{
// Make sure we are a part
if (!IsPart)
throw new InvalidOperationException(Localization.GetString("SolidWorksModelSetMaterialModelNotPartError"));
// If the material is null, remove the material
if (material == null || !material.DatabaseFileFound)
AsPart().SetMaterialPropertyName2(string.Empty, string.Empty, string.Empty);
// Otherwise set the material
else
AsPart().SetMaterialPropertyName2(configuration, material.Database, material.Name);
},
SolidDnaErrorTypeCode.SolidWorksModel,
SolidDnaErrorCode.SolidWorksModelSetMaterialError,
Localization.GetString("SolidWorksModelSetMaterialError"));
}
#endregion
#region Selected Entities
/// <summary>
/// Gets all of the selected objects in the model
/// </summary>
/// <param name="action">The selected objects list to be worked on inside the action. NOTE: Do not store references to them outside of this action</param>
/// <returns></returns>
public void SelectedObjects(Action<List<SelectedObject>> action)
{
SelectionManager?.SelectedObjects(action);
}
#endregion
#region Features
/// <summary>
/// Recurses the model for all of it's features and sub-features
/// </summary>
/// <param name="featureAction">The callback action that is called for each feature in the model</param>
public void Features(Action<ModelFeature, int> featureAction)
{
RecurseFeatures(featureAction, UnsafeObject.FirstFeature() as Feature);
}
#region Private Feature Helpers
/// <summary>
/// Recurses features and sub-features and provides a callback action to process and work with each feature
/// </summary>
/// <param name="featureAction">The callback action that is called for each feature in the model</param>
/// <param name="startFeature">The feature to start at</param>
/// <param name="featureDepth">The current depth of the sub-features based on the original calling feature</param>
private static void RecurseFeatures(Action<ModelFeature, int> featureAction, Feature startFeature = null, int featureDepth = 0)
{
// Get the current feature
var currentFeature = startFeature;
// While that feature is not null...
while (currentFeature != null)
{
// Now get the first sub-feature
var subFeature = currentFeature.GetFirstSubFeature() as Feature;
// Get the next feature if we should
var nextFeature = default(Feature);
if (featureDepth == 0)
nextFeature = currentFeature.GetNextFeature() as Feature;
// Create model feature
using (var modelFeature = new ModelFeature((Feature)currentFeature))
{
// Inform callback of the feature
featureAction(modelFeature, featureDepth);
}
// While we have a sub-feature...
while (subFeature != null)
{
// Get its next sub-feature
var nextSubFeature = subFeature.GetNextSubFeature() as Feature;
// Recurse all of the sub-features
RecurseFeatures(featureAction, subFeature, featureDepth + 1);
// And once back up out of the recursive dive
// Move to the next sub-feature and process that
subFeature = nextSubFeature;
}
// If we are at the top-level...
if (featureDepth == 0)
{
// And update the current feature reference to the next feature
// to carry on the loop
currentFeature = nextFeature;
}
// Otherwise...
else
{
// Remove the current feature as it is a sub-feature
// and is processed in the `while (subFeature != null)` loop
currentFeature = null;
}
}
}
#endregion
#endregion
#region Components
/// <summary>
/// Recurses the model for all of it's components and sub-components
/// </summary>
public IEnumerable<(Component component, int depth)> Components()
{
// Component to be set
Component component;
try
{
// Try and create component object from active configuration
component = new Component(ActiveConfiguration.UnsafeObject?.GetRootComponent3(true));
}
// If COM failure...
catch (InvalidComObjectException)
{
// Re-get configuration
ActiveConfiguration = new ModelConfiguration(BaseObject.IGetActiveConfiguration());
// Try once more
component = new Component(ActiveConfiguration.UnsafeObject?.GetRootComponent3(true));
}
// Return components
return RecurseComponents(component);
}
#region Private Component Helpers