-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormFastenerStack.vb
More file actions
2189 lines (1703 loc) · 87.1 KB
/
FormFastenerStack.vb
File metadata and controls
2189 lines (1703 loc) · 87.1 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
Option Strict On
Imports System.Xml
Public Class FormFastenerStack
' Interform communication
' https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp
Public Property FMain As Form_Main
'Private _ThreadDepthDouble As Double
'Public Property ThreadDepthDouble As Double
' Get
' Return _ThreadDepthDouble
' End Get
' Set(value As Double)
' If value = -1 Then
' Me.Activate()
' MsgBox("Select a tapped hole", vbOKOnly)
' Else
' _ThreadDepthDouble = value
' End If
' End Set
'End Property
'Private WithEvents Command As SolidEdgeFramework.Command
'Private WithEvents Mouse As SolidEdgeFramework.Mouse
Private _StackConfiguration As StackConfigurationConstants
Public Property StackConfiguration As StackConfigurationConstants
Get
Return _StackConfiguration
End Get
Set(value As StackConfigurationConstants)
_StackConfiguration = value
If Me.TableLayoutPanel1 IsNot Nothing Then
UpdateForm()
SetFastenerMinMaxLength()
End If
End Set
End Property
Public Property TopFilename As String
Public Property BottomFilename As String
Private _FastenerFilename As String
Public Property FastenerFilename As String
Get
Return _FastenerFilename
End Get
Set(value As String)
_FastenerFilename = value
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.LabelTopFastener.Text = IO.Path.GetFileName(_FastenerFilename)
GetRelatedFilenames()
End If
End Set
End Property
Private _FlatWasherFilename As String
Public Property FlatWasherFilename As String
Get
Return _FlatWasherFilename
End Get
Set(value As String)
_FlatWasherFilename = value
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.LabelTopFlatWasher.Text = IO.Path.GetFileName(_FlatWasherFilename)
Me.LabelBottomFlatWasher.Text = IO.Path.GetFileName(_FlatWasherFilename)
End If
End Set
End Property
Private _LockwasherFilename As String
Public Property LockwasherFilename As String
Get
Return _LockwasherFilename
End Get
Set(value As String)
_LockwasherFilename = value
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.LabelTopLockwasher.Text = IO.Path.GetFileName(_LockwasherFilename)
Me.LabelBottomLockwasher.Text = IO.Path.GetFileName(_LockwasherFilename)
End If
End Set
End Property
Private _NutFilename As String
Public Property NutFilename As String
Get
Return _NutFilename
End Get
Set(value As String)
_NutFilename = value
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.LabelBottomNut.Text = IO.Path.GetFileName(_NutFilename)
End If
End Set
End Property
Private _FlatWasherThickness As Double
Public Property FlatWasherThickness As Double
Get
Return _FlatWasherThickness
End Get
Set(value As Double)
_FlatWasherThickness = value
SetFastenerMinMaxLength()
End Set
End Property
Private _LockWasherThickness As Double
Public Property LockWasherThickness As Double
Get
Return _LockWasherThickness
End Get
Set(value As Double)
_LockWasherThickness = value
SetFastenerMinMaxLength()
End Set
End Property
Private _NutThickness As Double
Public Property NutThickness As Double
Get
Return _NutThickness
End Get
Set(value As Double)
_NutThickness = value
SetFastenerMinMaxLength()
End Set
End Property
Private _ClampedThickness As String
Public Property ClampedThickness As String
Get
Return _ClampedThickness
End Get
Set(value As String)
_ClampedThickness = value
SetFastenerMinMaxLength()
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.TextBoxClampedThickness.Text = _ClampedThickness
End If
End Set
End Property
Private _ThreadEngagementMin As String
Public Property ThreadEngagementMin As String
Get
Return _ThreadEngagementMin
End Get
Set(value As String)
_ThreadEngagementMin = value
SetFastenerMinMaxLength()
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.TextBoxThreadEngagementMin.Text = _ThreadEngagementMin
End If
End Set
End Property
Private _ThreadDepth As String
Public Property ThreadDepth As String
Get
Return _ThreadDepth
End Get
Set(value As String)
_ThreadDepth = value
SetFastenerMinMaxLength()
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.TextBoxThreadDepth.Text = _ThreadDepth
End If
End Set
End Property
Private _ExtensionMin As String
Public Property ExtensionMin As String
Get
Return _ExtensionMin
End Get
Set(value As String)
_ExtensionMin = value
SetFastenerMinMaxLength()
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.TextBoxExtensionMin.Text = _ExtensionMin
End If
End Set
End Property
Private _Units As String
Public Property Units As String
Get
Return _Units
End Get
Set(value As String)
_Units = value
If Me.TableLayoutPanel1 IsNot Nothing Then
Me.ComboBoxUnits.Text = _Units
UpdateLabelsWithUnits()
End If
End Set
End Property
Public Property FastenerMinLength As Double
Public Property FastenerMaxLength As Double
Public Property TreeviewFastenerFullPath As String
Public Property TreeviewFlatWasherFullPath As String
Public Property TreeviewLockwasherFullPath As String
Public Property TreeviewNutFullPath As String
' Xml relative search paths starting from a fastener
' SE2024 ..\..\..\Washer_Flat
' SE2019 ..\..\..\..\ISO_WASHERS_-_Steel\ISO_7089_-_Plain_washers_-_Normal_series
Public Property FlatWasherSearchPaths As List(Of String)
' SE2024 ..\..\..\Washer_Lock
' SE2019 NA
Public Property LockWasherSearchPaths As List(Of String)
' SE2024 ..\..\Nut_Hex
' SE2019 ..\..\..\..\ISO_NUTS_-_Steel\ISO_4032_-_Hexagon_regular_nuts, ..\..\..\ISO_NUTS_-_Steel\ISO_8673_-_Hexagon_regular_nuts_-_fine_pitch
Public Property NutSearchPaths As List(Of String)
Private Property ErrorLogger As HCErrorLogger
Private Property FileLogger As Logger
Private Property AssembleCommandComplete As Boolean
'Private SEAppEvents As SolidEdgeFramework.DISEApplicationEvents_Event
Public Enum StackConfigurationConstants
' F Fastener
' CO Clamped Object
' N Nut
' FW Flat Washer
' LW Lock Washer
' TT Thread Thru
' TB Thread Blind
F_CO_N
F_CO_FW_N
F_CO_LW_N
F_CO_FW_LW_N
F_FW_CO_N
F_FW_CO_FW_N
F_FW_CO_LW_N
F_FW_CO_FW_LW_N
F_CO_TT
F_FW_CO_TT
F_LW_CO_TT
F_LW_FW_CO_TT
F_CO_TB
F_FW_CO_TB
F_LW_CO_TB
F_LW_FW_CO_TB
End Enum
Public Sub New(_FMain As Form_Main)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.FMain = _FMain
End Sub
Private Sub Startup()
Dim UP As New UtilsPreferences
UP.GetFormFastenerStackSettings(Me)
If Me.TableLayoutPanel1 IsNot Nothing Then UpdateForm()
If Not (Me.Units = "in" Or Me.Units = "mm") Then
Me.Units = "in"
End If
Dim Proceed As Boolean = UP.CreateSearchPathFiles()
If Not Proceed Then
MsgBox("FFS.Load: Could not create Xml search path files")
Me.Dispose()
Else
Dim DataVersion As String = Nothing
If FMain.DataDirectory.Contains("SE2019") Then
DataVersion = "SE2019"
ElseIf FMain.DataDirectory.Contains("SE2024") Then
DataVersion = "SE2024"
Else
MsgBox($"FFS.Load: Unrecognzied data directory '{FMain.DataDirectory}'")
Me.Dispose()
End If
Me.FlatWasherSearchPaths = UP.GetFlatWasherSearchPath(DataVersion)
Me.LockWasherSearchPaths = UP.GetLockWasherSearchPath(DataVersion)
Me.NutSearchPaths = UP.GetNutSearchPath(DataVersion)
Dim TemplateFilename As String = FMain.GetTemplateNameFormula(ErrorLogger:=New Logger("Form Load", Nothing))
Dim Extension As String = IO.Path.GetExtension(TemplateFilename)
Me.FastenerFilename = FMain.GetFilenameFromPropsFormula(DefaultExtension:=Extension, New Logger("Form Load", Nothing))
Dim i = 0
'Me.FileLogger = Me.ErrorLogger.AddFile(Me.FastenerFilename)
End If
End Sub
Public Sub Process()
Dim Proceed As Boolean = True
'If FMain.SEApp Is Nothing Then
' Proceed = False
' Me.FileLogger.AddMessage("Solid Edge not running")
'End If
'If Proceed And FMain.AsmDoc Is Nothing Then
' Proceed = False
' Me.FileLogger.AddMessage("Assembly file not active")
'End If
If Proceed AndAlso Not CheckStartConditions(Me.FileLogger.AddLogger("Check start conditions")) Then
Proceed = False
'Me.FileLogger.AddMessage("Some start conditions not met")
End If
If Proceed AndAlso Not GenerateNeededFiles(Me.FileLogger.AddLogger("Generate needed files")) Then
Proceed = False
'Me.FileLogger.AddMessage("Could not generate all needed files")
End If
If Proceed Then
FMain.SEApp.DisplayAlerts = False
Dim StackAssyFilenames As List(Of String) = Nothing
Dim NumAddedItems As Dictionary(Of String, Integer) = Nothing
StackAssyFilenames = PrepStackAssemblies(Me.FileLogger.AddLogger("Prep temporary stack subassemblies"))
If StackAssyFilenames IsNot Nothing Then
If StackAssyFilenames.Count = 1 Then
Me.TopFilename = StackAssyFilenames(0)
Me.BottomFilename = ""
ElseIf StackAssyFilenames.Count = 2 Then
Me.TopFilename = StackAssyFilenames(0)
Me.BottomFilename = StackAssyFilenames(1)
Else
Proceed = False
'Me.FileLogger.AddMessage("Problem preparing temporary assembly files")
End If
Else
Proceed = False
'Me.FileLogger.AddMessage("Problem preparing temporary assembly files")
End If
End If
Dim InitialNumOccurrences As Integer = 0
If Proceed Then
InitialNumOccurrences = FMain.AsmDoc.Occurrences.Count
End If
If Proceed Then
Proceed = AddStackElementAndDisperse(Me.TopFilename, Me.FileLogger.AddLogger("Add and disperse top fastener stack"))
'If Not Proceed Then Me.FileLogger.AddMessage("Problem adding or dispersing top fastener stack")
End If
If Proceed And Not Me.BottomFilename = "" Then
Proceed = AddStackElementAndDisperse(Me.BottomFilename, Me.FileLogger.AddLogger("Add and disperse top fastener stack"))
'If Not Proceed Then Me.FileLogger.AddMessage("Problem adding or dispersing bottom fastener stack")
End If
If Proceed Then
Proceed = CreateFastenerStackGroup(InitialNumOccurrences, Me.FileLogger.AddLogger("Create fastener stack group and maybe pattern"))
'If Not Proceed Then Me.FileLogger.AddMessage("Problem creating assembly group")
End If
If FMain.SEApp IsNot Nothing Then FMain.SEApp.DisplayAlerts = True
Me.TopMost = True
End Sub
Private Function CheckStartConditions(_ErrorLogger As Logger) As Boolean
Dim UC As New UtilsCommon
Dim Success As Boolean = True
'If Not IO.File.Exists(Me.FastenerFilename) Then
' Success = False
' Me.FileLogger.AddMessage($"Fastener not found: '{Me.FastenerFilename}'")
'End If
If Not (Me.Units = "in" Or Me.Units = "mm") Then
Success = False
_ErrorLogger.AddMessage("Units not set to 'in' or 'mm'")
End If
Try
Dim V = CDbl(UC.FixLocaleDecimal(Me.ClampedThickness))
Catch ex As Exception
Success = False
_ErrorLogger.AddMessage($"Could not resolve clamped thickness: '{Me.ClampedThickness}'")
End Try
Dim ConfigString As String = Me.StackConfiguration.ToString
If ConfigString.Contains("_N") Then
If Me.NutFilename.ToLower.Contains("not found") Then
Success = False
_ErrorLogger.AddMessage(Me.NutFilename)
End If
End If
If ConfigString.Contains("_FW_") Then
If Me.FlatWasherFilename.ToLower.Contains("not found") Then
Success = False
_ErrorLogger.AddMessage(Me.FlatWasherFilename)
End If
End If
If ConfigString.Contains("_LW_") Then
If Me.LockwasherFilename.ToLower.Contains("not found") Then
Success = False
_ErrorLogger.AddMessage(Me.LockwasherFilename)
End If
End If
If ConfigString.Contains("_N") Or ConfigString.Contains("_TT") Then
Try
Dim V = CDbl(UC.FixLocaleDecimal(Me.ExtensionMin))
Catch ex As Exception
Success = False
_ErrorLogger.AddMessage($"Could not resolve minimum extension: '{Me.ExtensionMin}'")
End Try
End If
If ConfigString.Contains("_TB") Then
Try
Dim V = CDbl(UC.FixLocaleDecimal(Me.ThreadEngagementMin))
Catch ex As Exception
Success = False
_ErrorLogger.AddMessage($"Could not resolve minimum thread engagement: '{Me.ThreadEngagementMin}'")
End Try
Try
Dim V = CDbl(UC.FixLocaleDecimal(Me.ThreadDepth))
Catch ex As Exception
Success = False
_ErrorLogger.AddMessage($"Could not resolve thread depth: '{Me.ThreadDepth}'")
End Try
End If
Return Success
End Function
Private Function GenerateNeededFiles(_ErrorLogger As Logger) As Boolean
Dim Proceed As Boolean = True
Dim tmpTreeviewFastenerFullPath As String
' Get the correct length of fastener for the given parameters
LabelStatus.Text = "Getting fastener with correct length"
tmpTreeviewFastenerFullPath = GetCorrectLengthFastenerFullPath(
Me.TreeviewFastenerFullPath, Me.FastenerMinLength, Me.FastenerMaxLength)
If tmpTreeviewFastenerFullPath IsNot Nothing Then
Me.TreeviewFastenerFullPath = tmpTreeviewFastenerFullPath
Else
_ErrorLogger.AddMessage("No fastener length satisfies given parameters")
LabelStatus.Text = ""
Return False
End If
Dim ConfigString As String = Me.StackConfiguration.ToString
Dim tmpSelectedNodeFullPath = FMain.SelectedNodeFullPath ' Save the original selected node. Used to reset the form after processing.
FMain.AddToLibraryOnly = True ' We don't want to add the individual files to the user's assembly
' Generate the fastener if needed
LabelStatus.Text = "Generating fastener"
FMain.SelectedNodeFullPath = Me.TreeviewFastenerFullPath
Dim DefaultExtension As String = IO.Path.GetExtension(FMain.GetTemplateNameFormula(ErrorLogger:=_ErrorLogger))
Me.FastenerFilename = FMain.GetFilenameFromPropsFormula(DefaultExtension:=DefaultExtension, _ErrorLogger)
Try
Proceed = FMain.Process(ErrorLogger:=_ErrorLogger)
Catch ex As Exception
_ErrorLogger.AddMessage("Error processing file")
_ErrorLogger.AddMessage(ex.ToString)
End Try
' Generate the flat washer if needed
If Proceed And ConfigString.Contains("_FW_") Then
LabelStatus.Text = "Generating flat washer"
FMain.SelectedNodeFullPath = Me.TreeviewFlatWasherFullPath
Try
Proceed = FMain.Process(ErrorLogger:=_ErrorLogger)
Catch ex As Exception
_ErrorLogger.AddMessage("Error processing file")
_ErrorLogger.AddMessage(ex.ToString)
End Try
End If
' Generate the lock washer if needed
If Proceed And ConfigString.Contains("_LW_") Then
LabelStatus.Text = "Generating lock washer"
FMain.SelectedNodeFullPath = Me.TreeviewLockwasherFullPath
Try
Proceed = FMain.Process(ErrorLogger:=_ErrorLogger)
Catch ex As Exception
_ErrorLogger.AddMessage("Error processing file")
_ErrorLogger.AddMessage(ex.ToString)
End Try
End If
' Generate the nut if needed
If Proceed And ConfigString.Contains("_N") Then
LabelStatus.Text = "Generating nut"
FMain.SelectedNodeFullPath = Me.TreeviewNutFullPath
Try
Proceed = FMain.Process(ErrorLogger:=_ErrorLogger)
Catch ex As Exception
_ErrorLogger.AddMessage("Error processing file")
_ErrorLogger.AddMessage(ex.ToString)
End Try
End If
' Reset to original conditions
FMain.AddToLibraryOnly = False
FMain.SelectedNodeFullPath = tmpSelectedNodeFullPath
LabelStatus.Text = ""
Return Proceed
End Function
Private Function PrepStackAssemblies(_ErrorLogger As Logger) As List(Of String)
Dim Outlist As New List(Of String)
If FMain.SEApp Is Nothing Or FMain.AsmDoc Is Nothing Then
_ErrorLogger.AddMessage("Unable to connect to Solid Edge, or an assembly file is not open")
LabelStatus.Text = ""
Return Nothing
End If
LabelStatus.Text = "Generating fastener stack assemblies"
For Each TemplateName In {GetTopAssyTemplateName(), GetBottomAssyTemplateName()}
If TemplateName = "" Then Continue For ' Some configurations do not have a bottom stack assembly
' A blank TemplateName is valid. Check that before this.
If Not IO.File.Exists(TemplateName) Then
_ErrorLogger.AddMessage($"Template file not found `{TemplateName}`")
Return Nothing
End If
Dim tmpAssyFilename As String
tmpAssyFilename = $"{IO.Path.GetDirectoryName(TemplateName)}" ' c:\...\FastenerStackTemplates
tmpAssyFilename = $"{tmpAssyFilename}\Temp" ' c:\...\FastenerStackTemplates\Temp
tmpAssyFilename = $"{tmpAssyFilename}\tmp{IO.Path.GetFileNameWithoutExtension(TemplateName)}" ' c:\...\FastenerStackTemplates\Temp\FastenerStackTop_F-FW
tmpAssyFilename = $"{tmpAssyFilename}_{GetStackAssyFilesLastIdx() + 1:0000}" ' c:\...\FastenerStackTemplates\Temp\FastenerStackTop_F-FW_0024
tmpAssyFilename = $"{tmpAssyFilename}.asm" ' c:\...\FastenerStackTemplates\Temp\FastenerStackTop_F-FW_0024.asm
Dim tmpAsm As SolidEdgeAssembly.AssemblyDocument = Nothing
If Me.FMain.ProcessTemplateInBackground Then
tmpAsm = CType(FMain.SEApp.Documents.Open(TemplateName, 8), SolidEdgeAssembly.AssemblyDocument)
Else
tmpAsm = CType(Me.FMain.SEApp.Documents.Open(TemplateName), SolidEdgeAssembly.AssemblyDocument)
End If
FMain.SEApp.DoIdle()
tmpAsm.SaveAs(tmpAssyFilename)
FMain.SEApp.DoIdle()
Outlist.Add(tmpAssyFilename)
For Each Occurrence As SolidEdgeAssembly.Occurrence In tmpAsm.Occurrences
Dim OccurrenceFilename As String = Occurrence.OccurrenceFileName
Dim ReplacementFilename As String = ""
Select Case IO.Path.GetFileName(OccurrenceFilename)
Case "F.par"
ReplacementFilename = Me.FastenerFilename
Case "FW.par"
ReplacementFilename = Me.FlatWasherFilename
Case "LW.par"
ReplacementFilename = Me.LockwasherFilename
Case "N.par"
ReplacementFilename = Me.NutFilename
Case Else
_ErrorLogger.AddMessage($"FastenerStack.PrepStackAssemblies unrecognized filename: '{IO.Path.GetFileName(OccurrenceFilename)}'")
LabelStatus.Text = ""
Return Nothing
End Select
LabelStatus.Text = $"Processing {IO.Path.GetFileName(ReplacementFilename)}"
If FMain.FailedConstraintSuppress Then
tmpAsm.ReplaceComponents({Occurrence}, ReplacementFilename, SolidEdgeAssembly.ConstraintReplacementConstants.seConstraintReplacementSuppress)
ElseIf FMain.FailedConstraintAllow Then
tmpAsm.ReplaceComponents({Occurrence}, ReplacementFilename, SolidEdgeAssembly.ConstraintReplacementConstants.seConstraintReplacementNone)
Else
_ErrorLogger.AddMessage("Option not set for treatment of for failed constraints. Set it on the Tree Search Options dialog.")
LabelStatus.Text = ""
Return Nothing
End If
Next
tmpAsm.Save()
FMain.SEApp.DoIdle()
tmpAsm.Close()
FMain.SEApp.DoIdle()
Next
LabelStatus.Text = ""
Return Outlist
End Function
Private Function AddStackElementAndDisperse(Filename As String, _ErrorLogger As Logger) As Boolean
Dim Success As Boolean = True
AddHandler FMain.SEAppEvents.AfterCommandRun, AddressOf DISEApplicationEvents_AfterCommandRun
FMain.SEApp.DoIdle()
Me.TopMost = False
System.Windows.Forms.Application.DoEvents()
FMain.AsmDoc.Activate()
FMain.SEApp.DoIdle()
Dim Occurrences As SolidEdgeAssembly.Occurrences = FMain.AsmDoc.Occurrences
Dim Occurrence As SolidEdgeAssembly.Occurrence
Dim PreviousNumOccurrences As Integer = Occurrences.Count
Dim TickCount As Integer = 0
Dim TickCountMax As Integer = 30
Dim HighlightSet As SolidEdgeFramework.HighlightSet
AssembleCommandComplete = False
Clipboard.Clear()
Clipboard.SetText(Filename)
Dim Paste = CType(SolidEdgeConstants.AssemblyCommandConstants.AssemblyEditPaste, SolidEdgeFramework.SolidEdgeCommandConstants)
FMain.SEApp.StartCommand(Paste)
LabelStatus.Text = $"Adding {IO.Path.GetFileName(Filename)}"
' Wait until the new occurrence shows up
While Occurrences.Count = PreviousNumOccurrences
Threading.Thread.Sleep(100)
TickCount += 1
If TickCount >= TickCountMax Then
'LabelStatus.Text = "Paste timeout"
_ErrorLogger.AddMessage("Add occurrence command timed out")
Return False
End If
End While
' Get a reference to the new occurrence
Occurrence = CType(Occurrences(PreviousNumOccurrences), SolidEdgeAssembly.Occurrence)
FMain.AsmDoc.DisperseSubassembly(Occurrence, bAllOccurrences:=False)
' Get a reference to the first occurrence dispersed from the stack assembly
Occurrence = CType(Occurrences(PreviousNumOccurrences), SolidEdgeAssembly.Occurrence)
FMain.SEApp.ActiveSelectSet.RemoveAll()
HighlightSet = FMain.AsmDoc.HighlightSets.Add
'objApp.GetGlobalParameter(SolidEdgeFramework.ApplicationGlobalConstants.seApplicationGlobalColorSelected, objHLSet.Color)
HighlightSet.AddItem(Occurrence)
HighlightSet.Draw()
FMain.SEApp.ActiveSelectSet.Add(HighlightSet)
FMain.SEApp.ActiveSelectSet.RefreshDisplay()
Threading.Thread.Sleep(750)
HighlightSet.Delete()
' TODO Remove the occurrence ground constraint if present
Dim Relations3d As SolidEdgeAssembly.Relations3d = CType(Occurrence.Relations3d, SolidEdgeAssembly.Relations3d)
For i = 0 To Relations3d.Count - 1
Dim Ground As SolidEdgeAssembly.GroundRelation3d = TryCast(Relations3d(i), SolidEdgeAssembly.GroundRelation3d)
If Ground IsNot Nothing Then
Ground.Delete()
Exit For
End If
Next
LabelStatus.Text = "Position stack as needed. Press ESCAPE when done."
' Start the assemble command (CommandID 39002)
FMain.SEApp.StartCommand(CType(39002, SolidEdgeFramework.SolidEdgeCommandConstants))
TickCount = 0
While Not AssembleCommandComplete
Threading.Thread.Sleep(100)
'TickCount += 1
'If TickCount >= TickCountMax Then
' TickCount = 0
' If Not LabelStatus.Text = "Press the escape key once the part is constrained" Then
' LabelStatus.Text = "Press the escape key once the part is constrained"
' Else
' LabelStatus.Text = ""
' End If
'End If
End While
Try
HighlightSet.Delete()
Catch ex As Exception
End Try
LabelStatus.Text = ""
Return Success
End Function
Private Function CreateFastenerStackGroup(InitialNumOccurrences As Integer, _ErrorLogger As Logger) As Boolean
Dim Success As Boolean = True
Dim Occurrences As SolidEdgeAssembly.Occurrences = FMain.AsmDoc.Occurrences
Dim NewOccurrenceList As New List(Of SolidEdgeAssembly.Occurrence)
Dim NumFilesAdded As Integer = Occurrences.Count - InitialNumOccurrences
If NumFilesAdded > 0 Then
For i = InitialNumOccurrences To Occurrences.Count - 1
NewOccurrenceList.Add(CType(Occurrences(i), SolidEdgeAssembly.Occurrence))
Next
' Get a unique name for the new assembly group
Dim AssemblyGroupNames As New List(Of String)
For Each AssemblyGroup As SolidEdgeAssembly.AssemblyGroup In FMain.AsmDoc.AssemblyGroups
AssemblyGroupNames.Add(AssemblyGroup.Name)
Next
Dim j = 1
Dim Stackname As String = Nothing
While True
Stackname = $"FastenerStack {CStr(j)}"
If Not AssemblyGroupNames.Contains(Stackname) Then Exit While
j += 1
End While
' Create the group
Dim NewGroup As SolidEdgeAssembly.AssemblyGroup = FMain.AsmDoc.AssemblyGroups.Add(NumFilesAdded, NewOccurrenceList.ToArray)
NewGroup.Name = Stackname
If FMain.AutoPattern Then
' This returns False if no pattern is found. That is not an error.
Dim tmpSuccess As Boolean = MaybePatternOccurrences(InitialNumOccurrences, NewGroup.Name, _ErrorLogger)
End If
Else
Success = False
_ErrorLogger.AddMessage("No new occurrences detected. Unable to create a fastener stack assembly group.")
End If
Return Success
End Function
Private Function MaybePatternOccurrences(
InitialNumOccurrences As Integer,
FastenerStackName As String,
_ErrorLogger As Logger
) As Boolean
Dim Success As Boolean = True
Dim Occurrences As SolidEdgeAssembly.Occurrences = FMain.AsmDoc.Occurrences
Dim PrimaryOccurrence As SolidEdgeAssembly.Occurrence
Dim PiggyBackOccurrences As New List(Of SolidEdgeAssembly.Occurrence)
Dim NumFilesAdded As Integer = Occurrences.Count - InitialNumOccurrences
If NumFilesAdded > 0 Then
PrimaryOccurrence = CType(Occurrences(InitialNumOccurrences), SolidEdgeAssembly.Occurrence)
For i = InitialNumOccurrences + 1 To Occurrences.Count - 1
PiggyBackOccurrences.Add(CType(Occurrences(i), SolidEdgeAssembly.Occurrence))
Next
Success = FMain.MaybePatternOccurrence(PrimaryOccurrence, PiggyBackOccurrences, _ErrorLogger, FastenerStackName)
Else
Success = False
End If
Return Success
End Function
'Private Function AddStackToAssembly() As Dictionary(Of String, Integer)
' Dim NumAddedItems As New Dictionary(Of String, Integer)
' NumAddedItems("NumOccurrencesAdded") = 0
' NumAddedItems("NumTopSubOccurrencesAdded") = 0
' NumAddedItems("NumBottomSubOccurrencesAdded") = 0
' NumAddedItems("NumRelations3dAdded") = 0
' Dim Success As Boolean = True
' AddHandler FMain.SEAppEvents.AfterCommandRun, AddressOf FMain.DISEApplicationEvents_AfterCommandRun
' FMain.SEApp.DoIdle()
' Me.TopMost = False
' System.Windows.Forms.Application.DoEvents()
' FMain.AsmDoc.Activate()
' FMain.SEApp.DoIdle()
' Dim Occurrences As SolidEdgeAssembly.Occurrences = FMain.AsmDoc.Occurrences
' Dim StartingNumOccurrences As Integer = Occurrences.Count
' Dim Relations3d As SolidEdgeAssembly.Relations3d = FMain.AsmDoc.Relations3d
' Dim StartingNumRelations3d As Integer = Relations3d.Count
' Dim NumFilesAdded As Integer = 0
' For Each Filename As String In ({Me.TopFilename, Me.BottomFilename})
' If Filename = "" Then Continue For
' Dim IsTop As Boolean = False
' Dim IsBottom As Boolean = False
' If IO.Path.GetFileName(Filename).Contains("Top") Then IsTop = True
' If IO.Path.GetFileName(Filename).Contains("Bottom") Then IsBottom = True
' If Not (IsTop Or IsBottom) Then
' MsgBox($"FormFastenerStack.AddStackToAssembly: Filename error '{Filename}'", vbOKOnly, "Filename Error")
' Return Nothing
' End If
' Dim PreviousOccurrencesCount As Integer = Occurrences.Count
' Dim Occurrence As SolidEdgeAssembly.Occurrence = Nothing
' While Not FMain.AssemblyPasteComplete
' Threading.Thread.Sleep(100)
' End While
' Threading.Thread.Sleep(500)
' If Occurrences.Count > PreviousOccurrencesCount Then
' Occurrence = CType(Occurrences(Occurrences.Count - 1), SolidEdgeAssembly.Occurrence)
' If IsTop Then
' NumAddedItems("NumTopSubOccurrencesAdded") += Occurrence.SubOccurrences.Count
' Else
' NumAddedItems("NumBottomSubOccurrencesAdded") += Occurrence.SubOccurrences.Count
' End If
' End If
' Next
' RemoveHandler FMain.SEAppEvents.AfterCommandRun, AddressOf FMain.DISEApplicationEvents_AfterCommandRun
' NumAddedItems("NumOccurrencesAdded") = Occurrences.Count - StartingNumOccurrences
' NumAddedItems("NumRelations3dAdded") = Relations3d.Count - StartingNumRelations3d
' Return NumAddedItems
'End Function
'Private Function Disperse(NumAddedItems As Dictionary(Of String, Integer)) As Boolean
' Dim Success As Boolean = True
' Dim NumOccurrencesAdded = NumAddedItems("NumOccurrencesAdded")
' Dim NumTopSubOccurrencesAdded = NumAddedItems("NumTopSubOccurrencesAdded")
' Dim NumBottomSubOccurrencesAdded = NumAddedItems("NumBottomSubOccurrencesAdded")
' Dim NumRelations3dAdded = NumAddedItems("NumRelations3dAdded")
' ' The stack subassemblies have not been dispersed. When they are,
' ' the assembly relationships will be deleted. If the first part in the
' ' subassembly was grounded, it will be grounded in the assembly.
' ' If not, no new relationships will be added.
' ' We need to capture the relationships from the subassembly and reapply
' ' to the dipersed parts in the assembly.
' Dim Relations3d As SolidEdgeAssembly.Relations3d = FMain.AsmDoc.Relations3d
' 'Dim Relations3dList As New List(Of Object)
' Dim AxialRelation3dNeededInfo As New List(Of Tuple(Of SolidEdgeGeometry.Face, SolidEdgeAssembly.TopologyReference, Boolean))
' Dim PlanarRelation3dNeededInfo As New List(Of Tuple(Of SolidEdgeGeometry.Face, SolidEdgeAssembly.TopologyReference, Boolean))
' For i = Relations3d.Count - NumRelations3dAdded To Relations3d.Count - 1
' Dim AxialRelation3d As SolidEdgeAssembly.AxialRelation3d = TryCast(Relations3d(i), SolidEdgeAssembly.AxialRelation3d)
' If AxialRelation3d IsNot Nothing Then
' AxialRelation3dNeededInfo.Add(ExtractAxialRelation3dInfo(AxialRelation3d))
' End If
' Dim PlanarRelation3d As SolidEdgeAssembly.PlanarRelation3d = TryCast(Relations3d(i), SolidEdgeAssembly.PlanarRelation3d)
' If PlanarRelation3d IsNot Nothing Then
' PlanarRelation3dNeededInfo.Add(ExtractPlanarRelation3dInfo(PlanarRelation3d))
' End If
' Next
' Dim Occurrences As SolidEdgeAssembly.Occurrences = FMain.AsmDoc.Occurrences
' Dim IdxTopAssy As Integer = Occurrences.Count - 2
' FMain.AsmDoc.DisperseSubassembly(Occurrences(IdxTopAssy), bAllOccurrences:=False)
' FMain.AsmDoc.DisperseSubassembly(Occurrences(IdxTopAssy), bAllOccurrences:=False) ' Not a typo. TopAssy occurence was removed.
' Dim IdxTopFirstOccurrence = Occurrences.Count - (NumTopSubOccurrencesAdded + NumBottomSubOccurrencesAdded)
' Dim IdxBottomFirstOccurrence = Occurrences.Count - NumBottomSubOccurrencesAdded
' Dim Face1Ref As SolidEdgeAssembly.TopologyReference
' Dim tmpOccurrence As SolidEdgeAssembly.Occurrence = CType(Occurrences(IdxTopFirstOccurrence), SolidEdgeAssembly.Occurrence)
' Face1Ref = CType(FMain.AsmDoc.CreateReference(tmpOccurrence, AxialRelation3dNeededInfo.Item(0)), SolidEdgeAssembly.TopologyReference)
' Relations3d.AddAxial(Face1Ref, AxialRelation3dNeededInfo.Item(1), NormalsAligned:=True)
' Dim NumFilesAdded = NumTopSubOccurrencesAdded + NumBottomSubOccurrencesAdded
' Dim NewOccurrenceList As New List(Of SolidEdgeAssembly.Occurrence)
' ' Single files do not need to be added to a group
' If NumFilesAdded > 1 Then
' For i = 0 To NumFilesAdded - 1
' ' Need to add in reverse order
' NewOccurrenceList.Add(CType(Occurrences(Occurrences.Count - 1 - i), SolidEdgeAssembly.Occurrence))
' Next
' Dim NewGroup As SolidEdgeAssembly.AssemblyGroup = FMain.AsmDoc.AssemblyGroups.Add(NumFilesAdded, NewOccurrenceList.ToArray)
' NewGroup.Name = $"FastenerStack {FMain.AsmDoc.AssemblyGroups.Count}"
' End If
' Return Success
'End Function
'Private Function ExtractAxialRelation3dInfo(
' AxialRelation3d As SolidEdgeAssembly.AxialRelation3d
' ) As Tuple(Of SolidEdgeGeometry.Face, SolidEdgeAssembly.TopologyReference, Boolean)
' 'https://docs.sw.siemens.com/documentation/external/PL20220830878154140/en-US/api/content/SolidEdgeAssembly~Relations3d~AddAxial.html
' 'Public Function AddAxial( _
' ' ByVal Axis1 As Object, _
' ' ByVal Axis2 As Object, _
' ' ByVal NormalsAligned As Boolean _
' ') As AxialRelation3d
' Dim IsTopoRef1 As Boolean
' Dim IsTopoRef2 As Boolean
' Dim Element1 As SolidEdgeAssembly.TopologyReference = CType(AxialRelation3d.GetElement1(IsTopoRef1), SolidEdgeAssembly.TopologyReference)
' Dim Element2 As SolidEdgeAssembly.TopologyReference = CType(AxialRelation3d.GetElement2(IsTopoRef2), SolidEdgeAssembly.TopologyReference)
' Dim Face1 As SolidEdgeGeometry.Face = TryCast(Element1.Object, SolidEdgeGeometry.Face)
' Dim Face2 As SolidEdgeGeometry.Face = TryCast(Element2.Object, SolidEdgeGeometry.Face)
' Dim Occurrence2 As SolidEdgeAssembly.Occurrence = AxialRelation3d.Occurrence2
' Dim FaceRef2 As SolidEdgeAssembly.TopologyReference = CType(FMain.AsmDoc.CreateReference(Occurrence2, Face2), SolidEdgeAssembly.TopologyReference)
' Dim OutTuple As Tuple(Of SolidEdgeGeometry.Face, SolidEdgeAssembly.TopologyReference, Boolean) = Nothing
' OutTuple = Tuple.Create(Face1, FaceRef2, False)
' Return OutTuple
'End Function
'Private Function ExtractPlanarRelation3dInfo(
' PlanarRelation3d As SolidEdgeAssembly.PlanarRelation3d
' ) As Tuple(Of SolidEdgeGeometry.Face, SolidEdgeAssembly.TopologyReference, Boolean)
' 'https://docs.sw.siemens.com/documentation/external/PL20220830878154140/en-US/api/content/SolidEdgeAssembly~Relations3d~AddPlanar.html
' 'Public Function AddPlanar( _
' ' ByVal Plane1 As Object, _
' ' ByVal Plane2 As Object, _
' ' ByVal NormalsAligned As Boolean, _
' ' ByRef ConstrainingPoint1() As Double, _
' ' ByRef ConstrainingPoint2() As Double _
' ') As PlanarRelation3d
' ' For a Mate, NormalsAligned = True (even if that is backwards)
' Dim IsTopoRef1 As Boolean
' Dim IsTopoRef2 As Boolean
' Dim Element1 As SolidEdgeAssembly.TopologyReference = CType(PlanarRelation3d.GetElement1(IsTopoRef1), SolidEdgeAssembly.TopologyReference)
' Dim Element2 As SolidEdgeAssembly.TopologyReference = CType(PlanarRelation3d.GetElement1(IsTopoRef2), SolidEdgeAssembly.TopologyReference)
' Dim Face1 As SolidEdgeGeometry.Face = TryCast(Element1.Object, SolidEdgeGeometry.Face)
' Dim Face2 As SolidEdgeGeometry.Face = TryCast(Element2.Object, SolidEdgeGeometry.Face)
' Dim Occurrence2 As SolidEdgeAssembly.Occurrence = PlanarRelation3d.Occurrence2
' Dim FaceRef2 As SolidEdgeAssembly.TopologyReference = CType(FMain.AsmDoc.CreateReference(Occurrence2, Face2), SolidEdgeAssembly.TopologyReference)
' Dim OutTuple As Tuple(Of SolidEdgeGeometry.Face, SolidEdgeAssembly.TopologyReference, Boolean) = Nothing
' OutTuple = Tuple.Create(Face1, FaceRef2, False)