-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudyCode.cs
More file actions
1778 lines (1635 loc) · 83.6 KB
/
Copy pathStudyCode.cs
File metadata and controls
1778 lines (1635 loc) · 83.6 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
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 System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using View = SolidWorks.Interop.sldworks.View;
namespace SolidWorksSecDev
{
public class StudyCode
{
/// <summary>
/// How to get the length of a belt feature?
/// https://www.bilibili.com/video/BV1Lh41127Td?p=5
/// </summary>
/// <param name="swApp"></param>
public void BlueByteP5(SldWorks swApp)
{
string beltPartPath = string.Empty;
string beltFeatureName = "Belt1";
decimal beltLength = 0m;
ModelDoc2 swModel = swApp.ActiveDoc;//get the active doc
if (swModel.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY) return;//check assemdoc
//false to get the top-level features and all child features in the FeatureManager design tree
//Return Value:Array of all of the features
object[] featArr = swModel.FeatureManager.GetFeatures(false);//获取整个特征树中的所有特征
//Traverse all features in the assem
foreach (var item in featArr)
{
Feature swFeature = (Feature)item;
//check if feature is sketch
if (swFeature.GetTypeName2() == "ProfileFeature")//获取特征类型
{
Feature swOwnerFeature = swFeature.GetOwnerFeature();//返回父特征
//check if owner feature is a belt type feature and its name is beltfeaturename
if (swOwnerFeature.GetTypeName2() == "Belt" && swOwnerFeature.Name == beltFeatureName)
{
Sketch swSketch = swFeature.GetSpecificFeature2();
SketchSegment[] swSketchSegmentArr = swSketch.GetSketchSegments();//获取草图中的部件
foreach (var sketchSegment in swSketchSegmentArr)
{
if (sketchSegment.ConstructionGeometry == false)
beltLength += (decimal)sketchSegment.GetLength();
}
Feature swCompFeature = swFeature.GetNextSubFeature();
if (swCompFeature != null)
{
Component2 swComponent = swCompFeature.GetSpecificFeature2();
beltPartPath = swComponent.GetPathName();
}
}
}
}
ModelDoc2 swcompModelDoc = swApp.OpenDoc(beltPartPath, (int)swDocumentTypes_e.swDocPART);
if (swcompModelDoc == null) return;
swcompModelDoc.Visible = true;
//添加自定义属性
swcompModelDoc.Extension.CustomPropertyManager[""].Add2("Belt Length in mm",
(int)swCustomInfoType_e.swCustomInfoText, Math.Round(beltLength, 2).ToString());
swcompModelDoc.Save();
swApp.QuitDoc(swcompModelDoc.GetTitle());
}
/// <summary>
/// Can you split BOM?
/// https://www.bilibili.com/video/BV1Lh41127Td?p=6
/// </summary>
/// <param name="swApp"></param>
public void BlueByteP6(SldWorks swApp)
{
//precondition:Bill of materials seleted
//前置条件:Drawing中必须先选中BOM表
ModelDoc2 swModel = swApp.ActiveDoc;
if (swModel.GetType() != (int)swDocumentTypes_e.swDocDRAWING) return;//check DRAWING
SelectionMgr swSelectionMgr = swModel.SelectionManager;
TableAnnotation parentBomTableAnnotation = swSelectionMgr.GetSelectedObject6(1, 0);
//切割BOM表,从第二行往后切割
TableAnnotation newBomTableAnnotation = parentBomTableAnnotation.Split((int)swTableSplitLocations_e.swTableSplit_AfterRow, 1);
Annotation newBomAnnotation = newBomTableAnnotation.GetAnnotation();
//获取新的表格的坐标位置
double[] position = newBomAnnotation.GetPosition();
//clear the selection
swModel.ClearSelection();
//select new Bom,选中切割后的表,根据坐标
swModel.Extension.SelectByID2("", "ANNOTATIONTABLES", position[0], position[1], position[2], false, 0, null, 0);
swModel.EditCut();//将选中的注释,剪切
DrawingDoc swDrawingDoc = (DrawingDoc)swModel;
swDrawingDoc.ActivateSheet("Sheet1");//激活第一张表
swModel.Paste();//粘贴
}
/// <summary>
/// Feature selection inside an assembly
/// https://www.bilibili.com/video/BV1Lh41127Td?p=8
/// </summary>
/// <param name="swApp"></param>
public void BlueByteP8(SldWorks swApp)
{
//get active model in the solidworks sessin
ModelDoc2 swModel = swApp.ActiveDoc;
AssemblyDoc swAssy = (AssemblyDoc)swModel;
//get an array of the top level components
var swComps = swAssy.GetComponents(true);
Component2 swComp = swComps[0];
//get the first feature of swComp
Feature swFeat = swComp.FirstFeature();
string selectionName = String.Empty;
//traverse first level
while (swFeat != null)
{
if (swFeat.GetTypeName2() == "RefPlane" && swFeat.Name == "Right Plane")
{
string featType = String.Empty;
selectionName = swFeat.GetNameForSelection(out featType);
Debug.Print(selectionName);
}
swFeat = swFeat.GetNextFeature();
}
//select right plane
swModel.Extension.SelectByID2(selectionName, "PLANE", 0, 0, 0, false, 0, null, 0);
}
/// <summary>
/// delete the note of tool box in drawing
/// https://www.bilibili.com/video/BV19K411M7Y7?p=3
/// </summary>
/// <param name="swApp"></param>
public void CADSharpP3(SldWorks swApp)
{
//前提是打开工程图,然后选中视图
//代码的作用是删除标准件的标号
ModelDoc2 swModel = swApp.ActiveDoc;
SelectionMgr swSelectionMgr = swModel.SelectionManager;
View swView = swSelectionMgr.GetSelectedObject6(1, -1);//-1 means ignore mark
if (swView == null) return;
//get the selected drawing view(arry)
List<Annotation> swAnnotations = swView.GetAnnotations();
//traverse the drawing view for all notes
foreach (var item in swAnnotations)
{
//item.Select3(true, null);选中,作为阶段性调试观察
//get the entities the note is attached to
if (item.GetType() != (int)swAnnotationType_e.swNote) return;
object[] swAttachedEntities = item.GetAttachedEntities3();
if (swAttachedEntities.Length == 0) return;
Entity swEntity = (Entity)swAttachedEntities[0];//assume only one entity
//swEntity.Select4(true, null);选中,作为阶段性调试观察
//get the underlying component for the entity
Component2 swComponent = swEntity.GetComponent();
//determine if component is from the tool box,if yes, delete it
Debug.Print(swComponent.GetPathName());
//判断路径中是否包含"solidworks data"//通常tool box存档在该目录中
if (!swComponent.GetPathName().ToLower().Contains("solidworks data")) return;
item.Select3(true, null);//选中annotations
swModel.EditDelete();//删除
}
}
/// <summary>
/// Creat a left hand version of a part,打开目录中的模型,创建配置,镜像零件
/// https://www.bilibili.com/video/BV19K411M7Y7?p=4
/// </summary>
/// <param name="swApp"></param>
public void CADSharpP4(SldWorks swApp)
{
//Creat a left hand version of a part
//Precondition
//SolidWork id open
//DIR exists and contains parts that meet the following specifications:
//part can be mirrored about the front plane
//single solid body
//the x-y plane is called "Front Plane"
//one config called "Default", and has "Suppress new features" enabled
//get the active document
//ModelDoc2 swModel = swApp.ActiveDoc;
string dir = @"D:\model\";
var files = Directory.EnumerateFiles(dir, "*.sldprt", SearchOption.AllDirectories);
if (files == null) return;
foreach (var item in files)
{
int errors = 0;
int warnings = 0;
ModelDoc2 swModel = swApp.OpenDoc6(item, (int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
//add another configuration
swModel.AddConfiguration3("Opposite", "", "",
(int)swConfigurationOptions2_e.swConfigOption_DoDisolveInBOM);
//select the front plane and mark it
//SelectByID2适合于选中特征树中有名字name对象,注意mark参数
swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 2, null, 0);
//select the body and mark it,get all the bodys of the part,assume only one body
PartDoc swPart = (PartDoc)swModel;
object[] swBodies = swPart.GetBodies2((int)swBodyType_e.swAllBodies, false);
Body2 swBody = (Body2)swBodies[0];
SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
SelectData swSelData = (SelectData)swSelMgr.CreateSelectData();
swSelData.Mark = 256;
swBody.Select2(true, swSelData);
//mirror the body,镜像实体
swModel.FeatureManager.InsertMirrorFeature2(true, false, false, false, 0);
//delete the original body,删除实体
swBody.Select2(false, null);
swModel.FeatureManager.InsertDeleteBody();
//save the part
swModel.Save3((int)swSaveAsOptions_e.swSaveAsOptions_Silent, ref errors, ref warnings);
swApp.CloseDoc(swModel.GetPathName());
}
swApp.SendMsgToUser("Done!");
}
#region CADSharp2
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=14
/// Opening, Saving, and Exporting Documents
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P14(SldWorks swApp)
{
//string defaultPartTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
//ModelDoc2 swModel1 = swApp.NewDocument(defaultPartTemplate, 0, 0, 0);
ModelDoc2 swModel2 = swApp.OpenDoc6(@"E:\Videos\SolidWorks Secondary Development\SWModel\Part1.SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0);
DocumentSpecification swDocSpec = swApp.GetOpenDocSpec(@"E:\Videos\SolidWorks Secondary Development\SWModel\Assem4.SLDASM");
swDocSpec.DisplayState = "Transparent";
swDocSpec.UseLightWeightDefault = false;
swDocSpec.LightWeight = true;
ModelDoc2 swModel3 = swApp.OpenDoc7(swDocSpec);
ModelDoc2 swModel = swApp.ActivateDoc2("Part1.SLDPRT", true, 0);
//另存为eDrawing版本
string newPath = swModel.GetPathName().Replace("SLDPRT", "eprt");
swModel.Extension.SaveAs(newPath, 0, 1 + 2, null, 0, 0);
swApp.CloseDoc(swModel.GetPathName());
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=15
/// Working With Configurations
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P15(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
//Test for the number of configuration
if (swModel.GetConfigurationCount() == 1)
{
Debug.Print("Only one configuration");
//Add three configs
swModel.AddConfiguration3("A", "", "", 0);
swModel.AddConfiguration3("B", "", "", 0);
swModel.AddConfiguration3("C", "", "", 0);
}
else
{
Debug.Print("More than one configuration exists.");
}
//get the active configuration's name
ConfigurationManager swConfigMgr = swModel.ConfigurationManager;
Configuration swConfig = swConfigMgr.ActiveConfiguration;
string strConfig = swConfig.Name;
Debug.Print(strConfig);
//delete the first configuration
string[] vConfigNames = swModel.GetConfigurationNames();
swModel.DeleteConfiguration2(vConfigNames[0]);
//Cycle through configs and change names
vConfigNames = swModel.GetConfigurationNames();
for (int i = 0; i < vConfigNames.Length; i++)
{
//swModel.EditConfiguration3(vConfigNames[i], "Config" + (i + 1), "", "", 0);
swConfig = swModel.GetConfigurationByName(vConfigNames[i]);
swConfig.Name = "Config" + (i + 1);
}
//Activate the first configuration
vConfigNames = swModel.GetConfigurationNames();
swModel.ShowConfiguration2(vConfigNames[0]);
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=16
/// Working With Custom Properties
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P16(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
//Access cust prop at doc level
CustomPropertyManager swCustPropMgr = swModel.Extension.CustomPropertyManager[""];
swCustPropMgr.Add2("Description", (int)swCustomInfoType_e.swCustomInfoText, "your desc");
swCustPropMgr.Set("Author", "your name");
string strValue;
string strResolved;
swCustPropMgr.Get3("PartNo", true, out strValue, out strResolved);
Debug.Print(strValue + " " + strResolved);
swCustPropMgr.Delete("PartNo");
//Access cust prop at config level
string[] vConfigNames = swModel.GetConfigurationNames();
for (int i = 0; i < vConfigNames.Length; i++)
{
swCustPropMgr = swModel.Extension.CustomPropertyManager[vConfigNames[i]];
swCustPropMgr.Add2("PartNo", (int)swCustomInfoType_e.swCustomInfoText, strValue + "-" + vConfigNames[i]);
swCustPropMgr.Add2("Material", (int)swCustomInfoType_e.swCustomInfoText, (char)34 + "SW-Material@" + swModel.GetTitle() + (char)34);
}
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=17
/// Selection
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P17(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
SelectionMgr swSelMgr = swModel.SelectionManager;
Debug.Print(swSelMgr.GetSelectedObjectType3(-1, 1).ToString());
for (int i = 0; i < swSelMgr.GetSelectedObjectCount2(-1); i++)
{
if (swSelMgr.GetSelectedObjectType3(i, -1) == (int)swSelectType_e.swSelBODYFEATURES)
{
Feature swFeat = swSelMgr.GetSelectedObject6(i, -1);
MessageBox.Show(swFeat.Name);
}
else
{
MessageBox.Show("Please select a feature from the feature manager tree.");
}
}
if (swSelMgr.GetSelectedObjectType3(1, -1) == (int)swSelectType_e.swSelEDGES)
{
Edge swEdge = swSelMgr.GetSelectedObject6(1, -1);
swModel.ClearSelection2(true);//清除选择
//选择两个相接面
Face2[] vFace = swEdge.GetTwoAdjacentFaces2();
for (int i = 0; i < vFace.Length; i++)
{
Face2 swFace = vFace[i];
Entity swEnt = (Entity)swFace;
SelectData swSelData = default(SelectData);
swEnt.Select4(true, swSelData);
Debug.Print(swSelData.X + "," + swSelData.Y + "," + swSelData.Z); ;
}
}
else
{
MessageBox.Show("Please select an edge.");
}
//SelectByID2
//根据空间坐标点选择面,线
swModel.Extension.SelectByID2("", "FACE", -0.04, -0.140, 0, false, 0, null, 0);
swModel.Extension.SelectByID2("", "EDGE", -0.05, -0.140, 0, true, 0, null, 0);
//根据特征树名称选择草图点,面,特征
swModel.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", -50, -150, 0, true, 0, null, 0);
swModel.Extension.SelectByID2("Front Plane", "PLANE", -50, -150, 0, true, 0, null, 0);
swModel.Extension.SelectByID2("Cut-Extrudel", "BODYFEATURE", -50, -150, 0, true, 0, null, 0);
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=18
/// System and Document Options
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P18(SldWorks swApp)
{
//Input Dimension Value,false取消勾选,true勾选
if (swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate))
{
swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate, false);
}
else
{
swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate, true);
}
Debug.Print(swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate).ToString());
//API搜索:System Options and Document Properties
//数值类型的设置
if (swApp.GetUserPreferenceDoubleValue((int)swUserPreferenceDoubleValue_e.swViewTransitionHideShowComponent) == 0)
{
swApp.SetUserPreferenceDoubleValue((int)swUserPreferenceDoubleValue_e.swViewTransitionHideShowComponent, 0.5);
}
else
{
swApp.SetUserPreferenceDoubleValue((int)swUserPreferenceDoubleValue_e.swViewTransitionHideShowComponent, 0.5);
}
Debug.Print(swApp.GetUserPreferenceDoubleValue((int)swUserPreferenceDoubleValue_e.swViewTransitionHideShowComponent).ToString());
//Document Properties
//标注保持两位小数点
ModelDoc2 swModel = swApp.ActiveDoc;
swModel.Extension.SetUserPreferenceInteger(
(int)swUserPreferenceIntegerValue_e.swDetailingLinearDimPrecision,
(int)swUserPreferenceOption_e.swDetailingDimension, 2);
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=19
/// Working With Sketches
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P19(SldWorks swApp)
{
string defaultPartTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
ModelDoc2 swModel = swApp.NewDocument(defaultPartTemplate, 0, 0, 0);
//ModelDoc2 swModel = swApp.ActiveDoc;
SketchManager swSkethMgr = swModel.SketchManager;
//select front plane and insert sketch
swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
swSkethMgr.InsertSketch(true);
//turn on direct addition to database
swSkethMgr.AddToDB = true;
//create sketch entities
SketchSegment swSketchSeg = swSkethMgr.CreateLine(0, 0, 0, 0, 0.05, 0);
//添加约束
swModel.SketchAddConstraints("sgVERTICAL2D");
//将添加尺寸时出现的弹窗关闭(取消勾选)
if (swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate)) swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate, false);
//添加尺寸
swModel.AddVerticalDimension2(-0.01, 0.025, 0);
//make first sketch pt coincident with origin
//first select both pts then add ralation
//添加直线起点与原点重合配合
swModel.Extension.SelectByID2("", "EXTSKETCHPOINT", 0, 0, 0, false, 0, null, 0);
swModel.Extension.SelectByID2("", "SKETCHPOINT", 0, 0, 0, true, 0, null, 0);
swModel.SketchAddConstraints("sgCOINCIDENT");
swSkethMgr.CreateTangentArc(0, 0.05, 0, 0.05, 0.05, 0, (int)swTangentArcTypes_e.swForward);
//smart dimension
swModel.AddDimension2(0.025, 0.08, 0);
swSkethMgr.CreateLine(0.05, 0.05, 0, 0, 0, 0);
swModel.AddHorizontalDimension2(0.05, 0.025, 0);
swSkethMgr.CreateCircleByRadius(0.025, 0.05, 0, 0.005);
swModel.AddDimension2(0.025, 0.065, 0);
//fully define sketch,草图完全定义
//swModel.Extension.SelectByID2("", "EXSKETCHPOINT", 0, 0, 0, false, 6, null, 0);
//swSkethMgr.FullyDefineSketch(true, true, 1023, true, 1, null, 1, null, 0, 0);
//将添加尺寸时出现的弹窗打开(勾选)
swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate, true);
//turn off direct addition to database
swSkethMgr.AddToDB = false;
swSkethMgr.InsertSketch(true);
//swModel.ClearSelection2(true);
//修改已经存在的草图
//make sure one sketch is selected
//swModel.Extension.SelectByID2("", "SKETCH", 0.05, 0.05, 0, false, 0, null, 0);
SelectionMgr swSelMgr = swModel.SelectionManager;
if (swSelMgr.GetSelectedObjectType3(1, -1) != (int)swSelectType_e.swSelSKETCHES || swSelMgr.GetSelectedObjectCount2(-1) > 1)
{
swModel.Extension.ShowSmartMessage("Please select a single sketch from the FeatureManager tree", 3000, false, true);
}
//open the selected sketch
swSkethMgr.InsertSketch(false);
//get the sketch
Sketch swSketch = swSkethMgr.ActiveSketch;
//get the sketch segments
int intSketchEntCount = 0;
SketchSegment[] vSketchSegments = swSketch.GetSketchSegments();
for (int i = 0; i < vSketchSegments.Length; i++)
{
swSketchSeg = vSketchSegments[i];
//for some reason this won't recognize true...
if (!swSketchSeg.ConstructionGeometry) intSketchEntCount++;
}
//close the sketch
swSkethMgr.InsertSketch(true);
//display the result
swModel.Extension.ShowSmartMessage("Sketch entities:" + intSketchEntCount, 3000, false, true);
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=20
/// Working with Features Part A
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P20(SldWorks swApp)
{
string defaultPartTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
ModelDoc2 swModel = swApp.NewDocument(defaultPartTemplate, 0, 0, 0);
SketchManager swSketchMgr = swModel.SketchManager;
//create plate sketch profile
swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
swSketchMgr.InsertSketch(true);
swSketchMgr.CreateCircleByRadius(0, 0, 0, 0.05);
swModel.Extension.SelectByID2("", "EXTSKETCHPOINT", 0, 0, 0, false, 6, null, 0);
swSketchMgr.FullyDefineSketch(true, true, 1023, true, 1, null, 1, null, 0, 0);
FeatureManager swFeatureMgr = swModel.FeatureManager;
//create mid-plane base extrusion
Feature swFeat = swFeatureMgr.FeatureExtrusion2(true, false, false, 6, 0, 0.01, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, false);
//create extrude cut profile
swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
swSketchMgr.InsertSketch(true);
swSketchMgr.CreateCircleByRadius(-0.04, 0, 0, 0.004);
swModel.Extension.SelectByID2("", "EXTSKETCHPOINT", 0, 0, 0, false, 6, null, 0);
swSketchMgr.FullyDefineSketch(true, true, 1023, true, 1, null, 1, null, 0, 0);
//create tow-side cut extrude
swFeat = swFeatureMgr.FeatureCut3(false, false, false, 1, 1, 0, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false);
//create axis
swModel.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
swModel.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, true, 0, null, 0);
swModel.InsertAxis2(true);
//create circular pattern of extruded cut
swModel.Extension.SelectByID2("Axis1", "AXIS", 0, 0, 0, false, 1, null, 0);
swModel.Extension.SelectByID2("Cut-Extrude1", "BODYFEATURE", 0, 0, 0, true, 4, null, 0);
swFeatureMgr.FeatureCircularPattern2(10, 2 * 4 * Math.Atan(1) / 10, false, "", false);
//create fillet
PartDoc swPart = (PartDoc)swModel;
var vBodies = swPart.GetBodies2((int)swBodyType_e.swSolidBody, false);
//assumes only one solid body in part
Body2 swBody = (Body2)vBodies[0];
var vFace = swBody.GetFaces();
Face2 swFinalFace = default(Face2);
double dblArea = 0;
for (int i = 0; i < vFace.Length; i++)
{
Face2 swFace = (Face2)vFace[i];
Surface swSurf = swFace.GetSurface();
if (swSurf.IsCylinder())
{
//拿到面积最大的圆柱面
if (swFace.GetArea() > dblArea)
{
dblArea = swFace.GetArea();
swFinalFace = swFace;
}
}
}
Entity swEnt = (Entity)swFinalFace;
swEnt.Select4(false, null);
swFeatureMgr.FeatureFillet(2, 0.001, 0, 0, 0, 0, 0);
//change the material
swPart.SetMaterialPropertyName2("", "", "Plain Carbon Steel");
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=21
/// Working with Features Part B
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P21(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
PartDoc swPart = (PartDoc)swModel;
//修改拉伸深度
Feature swFeat = swPart.FeatureByName("Boss-Extrude1");
ExtrudeFeatureData2 swExtrudeFeatData = swFeat.GetDefinition();
swExtrudeFeatData.SetDepth(true, swExtrudeFeatData.GetDepth(true) * 1.5);
swFeat.ModifyDefinition(swExtrudeFeatData, swModel, null);
//modify fillet 修改圆角特征,将圆角特征应用到所有的边线
swFeat = swPart.FeatureByName("Fillet1");
SimpleFilletFeatureData2 swFilletFeatData = swFeat.GetDefinition();
swFilletFeatData.AccessSelections(swModel, null);
var vBodies = swPart.GetBodies2((int)swBodyType_e.swSolidBody, false);
//assumes only one solid body in part
Body2 swBody = (Body2)vBodies[0];
swFilletFeatData.Edges = swBody.GetEdges();
swFeat.ModifyDefinition(swFilletFeatData, swModel, null);
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=22
/// Working with Features Part C
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P22_1(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
Debug.Print(swModel.GetPathName());
Feature swFeat = swModel.FirstFeature();
while (swFeat != null)
{
Debug.Print("FeatName:" + swFeat.Name);
Debug.Print(" Type1:" + swFeat.GetTypeName());
Debug.Print(" Type2:" + swFeat.GetTypeName2());
Feature swSubFeat = (Feature)swFeat.GetFirstSubFeature();
while (swSubFeat != null)
{
Debug.Print(" SubFeatName:" + swSubFeat.Name);
Debug.Print(" Type1:" + swSubFeat.GetTypeName());
Debug.Print(" Type2:" + swSubFeat.GetTypeName2());
swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
}
swFeat = (Feature)swFeat.GetNextFeature();
}
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=22
/// Working with Features Part C
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P22_2(SldWorks swApp)
{
//修改P20中的代码
string defaultPartTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
ModelDoc2 swModel = swApp.NewDocument(defaultPartTemplate, 0, 0, 0);
//create plate sketch profile
//--------replaces SelectByID2--------
Feature swFeat = swModel.FirstFeature();
do
{
if (swFeat.GetTypeName() == "RefPlane") break;
swFeat = swFeat.GetNextFeature();
} while (swFeat != null);
swFeat.Select2(false, 0);
//-------------------------------------
SketchManager swSketchMgr = swModel.SketchManager;
swSketchMgr.InsertSketch(true);
swSketchMgr.CreateCircleByRadius(0, 0, 0, 0.05);
swModel.Extension.SelectByID2("", "EXTSKETCHPOINT", 0, 0, 0, false, 6, null, 0);
swSketchMgr.FullyDefineSketch(true, true, 1023, true, 1, null, 1, null, 0, 0);
FeatureManager swFeatureMgr = swModel.FeatureManager;
//create mid-plane base extrusion
swFeat = swFeatureMgr.FeatureExtrusion2(true, false, false, 6, 0, 0.01, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, false);
//create extrude cut profile
//--------replaces SelectByID2--------
swFeat = swModel.FirstFeature();
do
{
if (swFeat.GetTypeName() == "RefPlane") break;
swFeat = swFeat.GetNextFeature();
} while (swFeat != null);
swFeat.Select2(false, 0);
//-------------------------------------
swSketchMgr.InsertSketch(true);
swSketchMgr.CreateCircleByRadius(-0.04, 0, 0, 0.004);
swModel.Extension.SelectByID2("", "EXTSKETCHPOINT", 0, 0, 0, false, 6, null, 0);
swSketchMgr.FullyDefineSketch(true, true, 1023, true, 1, null, 1, null, 0, 0);
//create tow-side cut extrude
swFeat = swFeatureMgr.FeatureCut3(false, false, false, 1, 1, 0, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false);
//create axis
//--------replaces SelectByID2--------
swFeat = swModel.FirstFeature();
int j = 0;
do
{
if (swFeat.GetTypeName() == "RefPlane")
{
if (j == 1) break;
j++;
}
swFeat = swFeat.GetNextFeature();
} while (swFeat != null);
swFeat.Select2(false, 0);
//-------------------------------------
//--------replaces SelectByID2--------
swFeat = swModel.FirstFeature();
j = 0;
do
{
if (swFeat.GetTypeName() == "RefPlane")
{
if (j == 2) break;
j++;
}
swFeat = swFeat.GetNextFeature();
} while (swFeat != null);
swFeat.Select2(true, 0);
//-------------------------------------
swModel.InsertAxis2(true);
//create circular pattern of extruded cut
//--------replaces SelectByID2--------
swFeat = swModel.FirstFeature();
do
{
if (swFeat.GetTypeName() == "RefAxis") break;
swFeat = swFeat.GetNextFeature();
} while (swFeat != null);
swFeat.Select2(false, 1);
//-------------------------------------
//--------replaces SelectByID2--------
swFeat = swModel.FirstFeature();
do
{
if (swFeat.GetTypeName() == "Cut") break;
swFeat = swFeat.GetNextFeature();
} while (swFeat != null);
swFeat.Select2(true, 4);
//-------------------------------------
swFeatureMgr.FeatureCircularPattern2(10, 2 * 4 * Math.Atan(1) / 10, false, "", false);
//create fillet
PartDoc swPart = (PartDoc)swModel;
var vBodies = swPart.GetBodies2((int)swBodyType_e.swSolidBody, false);
//assumes only one solid body in part
Body2 swBody = (Body2)vBodies[0];
var vFace = swBody.GetFaces();
Face2 swFinalFace = default(Face2);
double dblArea = 0;
for (int i = 0; i < vFace.Length; i++)
{
Face2 swFace = (Face2)vFace[i];
Surface swSurf = swFace.GetSurface();
if (swSurf.IsCylinder())
{
//拿到面积最大的圆柱面
if (swFace.GetArea() > dblArea)
{
dblArea = swFace.GetArea();
swFinalFace = swFace;
}
}
}
Entity swEnt = (Entity)swFinalFace;
swEnt.Select4(false, null);
swFeatureMgr.FeatureFillet(2, 0.001, 0, 0, 0, 0, 0);
//change the material
swPart.SetMaterialPropertyName2("", "", "Plain Carbon Steel");
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=23
/// Geometry and Topology Objects(几何与拓扑)
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P23_1(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
PartDoc swPart = (PartDoc)swModel;
var vBodies = swPart.GetBodies2((int)swBodyType_e.swSolidBody, false);
//assumes only one solid body in part
Body2 swBody = (Body2)vBodies[0];
var vFace = swBody.GetFaces();
Face2 swFinalFace = default(Face2);
double dblArea = 0;
for (int i = 0; i < vFace.Length; i++)
{
Face2 swFace = (Face2)vFace[i];
Surface swSurf = swFace.GetSurface();
if (swSurf.IsCylinder())
{
//拿到面积最大的圆柱面
if (swFace.GetArea() > dblArea)
{
dblArea = swFace.GetArea();
swFinalFace = swFace;
}
}
}
Entity swEnt = (Entity)swFinalFace;
swEnt.Select4(false, null);
FeatureManager swFeatureMgr = swModel.FeatureManager;
swFeatureMgr.FeatureFillet(2, 0.001, 0, 0, 0, 0, 0);
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=23
/// Geometry and Topology Objects(几何与拓扑)
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P23_2(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
SelectionMgr swSelMgr = swModel.SelectionManager;
double dblTotalLength = 0;
for (int i = 0; i < swSelMgr.GetSelectedObjectCount2(-1); i++)
{
if (swSelMgr.GetSelectedObjectType3(i, -1) == (int)swSelectType_e.swSelEDGES)
{
Edge swEdge = swSelMgr.GetSelectedObject6(i, -1);
Curve swCurve = swEdge.GetCurve();
CurveParamData swCurveParams = swEdge.GetCurveParams3();
double dblLength = swCurve.GetLength3(swCurveParams.UMinValue, swCurveParams.UMaxValue);
dblTotalLength += dblLength;
//print to immediate window
Debug.Print("---EDGE #" + i);
switch (swCurve.Identity())
{
case 3001:
Debug.Print(" Type:line");
break;
case 3002:
Debug.Print(" Type:circle");
break;
case 3003:
Debug.Print(" Type:ellipse");
break;
case 3004:
Debug.Print(" Type:intersection");
break;
case 3005:
Debug.Print(" Type:b-curve");
break;
case 3006:
Debug.Print(" Type:sp-curve");
break;
case 3008:
Debug.Print(" Type:constant parameter");
break;
case 3009:
Debug.Print(" Type:trimmed");
break;
}
dblLength = Math.Round(dblLength, 4);
Debug.Print(" Length:" + dblLength * 1000d + "mm");
}
}
dblTotalLength = Math.Round(dblTotalLength, 4);
Debug.Print("---TotalLength:" + dblTotalLength * 1000d + "mm");
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=24
/// Creating A New Assembly
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P24_1(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
if (swModel == null || swModel.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY) return;
AssemblyDoc swAssy = (AssemblyDoc)swModel;
SelectionMgr swSelMgr = swModel.SelectionManager;
if (swSelMgr.GetSelectedObjectType3(1, -1) != (int)swSelectType_e.swSelFACES)
{
MessageBox.Show("Please select a face.");
return;
}
//get the drawer face
Face2 swFace = swSelMgr.GetSelectedObject6(1, -1);
Entity swDrawerFace = (Entity)swFace;
swDrawerFace = swDrawerFace.GetSafeEntity();
Collection<Entity> collDrawerEdge = new Collection<Entity>();
Collection<Entity> collCompFace = new Collection<Entity>();
GetDrawerEdge();
for (int i = 0; i < collDrawerEdge.Count; i++)
{
Component2 swComp= AddComps();
GetCompFace(swComp);
AddMates(swComp, collCompFace[i],collDrawerEdge[i]);
}
swModel.ClearSelection2(true);
swModel.ForceRebuild3(false);
//-----------------------------------------------
//局部方法1 获取圆
void GetDrawerEdge()
{
//get the full circle edge(s) from drawer face
var vEdges = swFace.GetEdges();
Entity swDrawerEdge = default(Entity);
for (int i = 0; i < vEdges.Length; i++)
{
Edge swEdge = (Edge)vEdges[i];
Curve swCurve = swEdge.GetCurve();
if (swCurve.Identity() == (int)swCurveTypes_e.CIRCLE_TYPE) //3002,circle
{
Vertex swVertex = swEdge.GetStartVertex();
if (swVertex == null)
{
swDrawerEdge = (Entity)swEdge;
swDrawerEdge = swDrawerEdge.GetSafeEntity();
collDrawerEdge.Add(swDrawerEdge);
}
}
}
if (swDrawerEdge == null) return;
}
//局部方法2 插入子装配
Component2 AddComps()
{
//add the component
string strCompPath = @"E:\Videos\SolidWorks Secondary Development\SWModel\CADSharp2P20.SLDPRT";
swApp.DocumentVisible(false, (int)swDocumentTypes_e.swDocPART);
swApp.OpenDoc6(strCompPath, (int)swDocumentTypes_e.swDocPART, 0, "", 0, 0);
Component2 swComp = swAssy.AddComponent4(strCompPath, "", 0, 0, 0);//XYZ边界区域的中心bounding box centre
swApp.DocumentVisible(true, (int)swDocumentTypes_e.swDocPART);
return swComp;
}
//局部方法3 获取圆柱面
void GetCompFace(Component2 swComp)
{
//get the cylindrical face(s) from knob(s)
var vBodies = swComp.GetBodies3((int)swBodyType_e.swSolidBody, out _);
Body2 swBody = (Body2)vBodies[0];
var vFaces = swBody.GetFaces();
Entity swCompFace = default(Entity);
for (int i = 0; i < vFaces.Length; i++)
{
swFace = (Face2)vFaces[i];
Surface swSurf = swFace.GetSurface();
if (swSurf.IsCylinder())
{
swCompFace = (Entity)swFace;
collCompFace.Add(swCompFace);
break;
}
}
}
//局部方法4 添加配合
void AddMates(Component2 swComp,Entity swCompFace,Entity swDrawerEdge)
{
//add mates
//1.add coincident mate
swDrawerFace.Select4(false, null);
Debug.Print(swComp.Name2);
//注意对"Front Plane@" + swComp.Name2 + "@Assem1"做修改,Front Plane是swComp中的面,Assem1是swAssy的名称
swModel.Extension.SelectByID2("Front Plane@" + swComp.Name2 + "@Assem1", "PLANE", 0, 0, 0, true, 0, null, 0);
swAssy.AddMate3((int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignALIGNED, false, 0, 0, 0, 0, 0, 0, 0, 0, false, out _);
//2.add concentric mate
swCompFace.Select4(false, null);
swDrawerEdge.Select4(true, null);
swAssy.AddMate3((int)swMateType_e.swMateCONCENTRIC, (int)swMateAlign_e.swMateAlignANTI_ALIGNED, false, 0, 0, 0, 0, 0, 0, 0, 0, false, out _);
}
}
/// <summary>
/// https://www.bilibili.com/video/BV1Mp4y1Y7Bd?p=24
/// Creating A New Assembly
/// </summary>
/// <param name="swApp"></param>
public void CADSharp2P24_2(SldWorks swApp)
{
ModelDoc2 swModel = swApp.ActiveDoc;
AssemblyDoc swAssy = (AssemblyDoc) swModel;
//get the arm face by searching components for face called "ArmFace"
Entity swArmFace=GetArmFace();
swArmFace = swArmFace.GetSafeEntity();
//Get the full circular edge on the arm
GetArmEdge();
//Insety the handle
Component2 swComp =AddComp();
//Get a cylindrical face on the handle
Entity swHandleCylFace = default(Entity);
swHandleCylFace = GetHandleCylFace(swComp);
//Get the required planar face on the handle
GetHandlePlanarFace();
//Add the Mate
AddMate();
swModel.ClearSelection2(true);
swModel.ForceRebuild3(false);
//----------局部方法-------------
Entity GetArmFace()
{
return default(Entity);
}
Entity GetArmEdge()
{
return default(Entity);
}
Component2 AddComp()
{
return default(Component2);
}
Entity GetHandleCylFace(Component2 swHandleComp)
{
var vComps = swAssy.GetComponents(false);
for (int i = 0; i < vComps.Length; i++)
{
var vBodies = swHandleComp.GetBodies3((int) swBodyType_e.swSolidBody, out _);
Body2 swBody = (Body2) vBodies[0];
var vFace = swBody.GetFaces();
for (int j = 0; j < vFace.Length; j++)
{
Face2 swFace = vFace[i];
Entity swHandleCyl = (Entity) swFace;
if (swModel.GetEntityName(swHandleCylFace) == "HandleCylFace") return swHandleCyl;
}
}
return default(Entity);
}
void GetHandlePlanarFace()
{
}
void AddMate()
{