-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForm_Main.vb
More file actions
3471 lines (2760 loc) · 139 KB
/
Form_Main.vb
File metadata and controls
3471 lines (2760 loc) · 139 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.Text.RegularExpressions
Imports System.Xml
Imports Microsoft.WindowsAPICodePack.Dialogs
Public Class Form_Main
Private Property Version As String = "2026.1"
Private Property PreviewVersion As String = "01" ' Empty string if not a preview
Private Property SearchingTVFilename As Boolean = False
Private _SelectedNodeFullPath As String
Public Property SelectedNodeFullPath As String
Get
Return _SelectedNodeFullPath
End Get
Set(value As String)
_SelectedNodeFullPath = value
If Me.XmlDoc IsNot Nothing Then
UpdatePropertyTab()
' Deal with possible multiple entries in MaterialFormula
'%{MaterialFormula}:STEEL
'%{MaterialFormula}:STAINLESS, NYLON, TITANIUM, Wood\, Walnut
Dim MaterialFormulas As List(Of Prop) = Props.GetPropsOfType("SEPropertyFormulaMaterial")
If MaterialFormulas.Count > 0 Then
' Use the lowest material formula in the tree
Dim LastIdx As Integer = MaterialFormulas.Count - 1
Dim MaterialName = MaterialFormulas(LastIdx).Value ' Can be a single value or comma delimited list
' Temporarily replace escaped commas for splitting
MaterialName = MaterialName.Replace("\,", "LITERALCOMMA")
Dim tmpMaterialsList As List(Of String) = MaterialName.Split(CChar(",")).ToList
' Restore escaped commas and trim whitespace
For i = 0 To tmpMaterialsList.Count - 1
tmpMaterialsList(i) = tmpMaterialsList(i).Replace("LITERALCOMMA", ",").Trim
Next
Me.MaterialsList = tmpMaterialsList
If Me.ActiveMaterial = "" Then
If Me.MaterialsList.Count = 0 Then
' No materials defined. No action needed.
ElseIf Me.MaterialsList.Count = 1 Then
' Only one material found. Use it.
Me.ActiveMaterial = Me.MaterialsList(0)
Else
' Prompt
'MsgBox("Select a material", vbOKOnly, "Select a Material")
End If
End If
End If
'MaterialFormulas(LastIdx).Value = MaterialName
End If
End Set
End Property
Public Property LibraryDirectory As String
Public Property TemplateDirectory As String
Public Property DataDirectory As String
Public Property MaterialTable As String
Public Property AlwaysReadExcel As Boolean
Private _AutoPattern As Boolean
Public Property AutoPattern As Boolean
Get
Return _AutoPattern
End Get
Set(value As Boolean)
_AutoPattern = value
If Me.TabControl1 IsNot Nothing Then
If AutoPattern Then
ButtonAutoPattern.Image = My.Resources.auto_pattern_enabled
Else
ButtonAutoPattern.Image = My.Resources.auto_pattern_disabled
End If
End If
End Set
End Property
Public Property AddProp As Boolean
Public Property DisableFineThreadWarning As Boolean
Public Property CheckNewVersion As Boolean
Public Property PropertiesToSearchList As List(Of String)
Public Property PropertiesData As HCPropertiesData
Public Property PropertiesCache As HCPropertiesCache
Public Property AssemblyTemplate As String
Public Property PartTemplate As String
Public Property SheetmetalTemplate As String
Private Property AlwaysOnTopTimer As Timer
Public Property AlwaysOnTopRefreshTime As String
Private _AlwaysOnTop As Boolean
Public Property AlwaysOnTop As Boolean
Get
Return _AlwaysOnTop
End Get
Set(value As Boolean)
_AlwaysOnTop = value
If Me.TabControl1 IsNot Nothing Then
If AlwaysOnTop Then
If AlwaysOnTopTimer IsNot Nothing Then AlwaysOnTopTimer.Start()
ButtonAlwaysOnTop.Image = My.Resources.always_on_top_enabled
Else
If AlwaysOnTopTimer IsNot Nothing Then AlwaysOnTopTimer.Stop()
ButtonAlwaysOnTop.Image = My.Resources.always_on_top_disabled
End If
End If
End Set
End Property
Private _SaveInLibrary As Boolean
Public Property SaveInLibrary As Boolean
Get
Return _SaveInLibrary
End Get
Set(value As Boolean)
_SaveInLibrary = value
If Me.TabControl1 IsNot Nothing Then
If SaveInLibrary Then ComboBoxSaveIn.Text = "Library"
End If
End Set
End Property
Private _SaveInAssemblyDirectory As Boolean
Public Property SaveInAssemblyDirectory As Boolean
Get
Return _SaveInAssemblyDirectory
End Get
Set(value As Boolean)
_SaveInAssemblyDirectory = value
If Me.TabControl1 IsNot Nothing Then
If SaveInAssemblyDirectory Then ComboBoxSaveIn.Text = "Assy Dir"
End If
End Set
End Property
Private _SaveInOther As Boolean
Public Property SaveInOther As Boolean
Get
Return _SaveInOther
End Get
Set(value As Boolean)
_SaveInOther = value
If Me.TabControl1 IsNot Nothing Then
If SaveInOther Then ComboBoxSaveIn.Text = "Other"
End If
End Set
End Property
'Public Property AssemblyDirectory As String
'Private _PrePopulate As Boolean
'Public Property PrePopulate As Boolean
' Get
' Return _PrePopulate
' End Get
' Set(value As Boolean)
' _PrePopulate = value
' If Me.TabControl1 IsNot Nothing Then
' ButtonPrepopulate.Checked = PrePopulate
' ButtonAddToLibrary.Visible = PrePopulate
' LabelAddToLibrary.Visible = PrePopulate
' If PrePopulate Then
' Me.Cursor = Cursors.WaitCursor
' TreeView1.CheckBoxes = True
' ButtonPrepopulate.Image = My.Resources.icons8_Checkbox_Checked
' LabelPrePopulate.BackColor = System.Drawing.Color.Orange
' Me.Cursor = Cursors.Default
' Else
' ButtonPrepopulate.Image = My.Resources.icons8_Checkbox_Unchecked
' LabelPrePopulate.BackColor = System.Drawing.Color.Transparent
' TreeView1.CheckBoxes = False
' TreeView1.CollapseAll()
' TreeView1.Nodes(0).Expand()
' End If
' End If
' End Set
'End Property
Public Property FileLogger As Logger
Public Property ProcessTemplateInBackground As Boolean = True
Public Property FailedConstraintSuppress As Boolean
Public Property FailedConstraintAllow As Boolean = True
Public Property SuspendMRU As Boolean = False
'Public Property AllowCommaDelimiters As Boolean = False
'Public Property XmlCommaIndicator As String = "...."
Public Property CacheProperties As Boolean
Public Property XmlDoc As System.Xml.XmlDocument
Public Property AddToLibraryOnly As Boolean
Public Property SEApp As SolidEdgeFramework.Application
Public Property AsmDoc As SolidEdgeAssembly.AssemblyDocument
Public Property IncludeDrawing As Boolean
Private _MaterialsList As List(Of String)
Public Property MaterialsList As List(Of String)
Get
Return _MaterialsList
End Get
Set(value As List(Of String))
_MaterialsList = value
If Me.ComboBoxMaterials IsNot Nothing Then
If Not Me.SearchingTVFilename Then
ComboBoxMaterials.Items.Clear()
ComboBoxMaterials.Items.Add("")
For Each s As String In _MaterialsList
ComboBoxMaterials.Items.Add(s)
Next
If _MaterialsList.Contains(Me.ActiveMaterial) Then
ComboBoxMaterials.Text = Me.ActiveMaterial
Else
Me.ActiveMaterial = "" ' Should take care of the combobox automatically
End If
' Set combobox width
' https://stackoverflow.com/questions/4842160/auto-width-of-comboboxs-content
'int maxWidth = 0, temp = 0;
' foreach (var obj in myCombo.Items)
' {
' temp = TextRenderer.MeasureText(obj.ToString(), myCombo.Font).Width;
' if (temp > maxWidth)
' {
' maxWidth = temp;
' }
' }
Dim ComboboxAbsoluteMaxWidth = 150
Dim PreviousWidth = ComboBoxMaterials.Width
Dim MaxWidth = 0
For Each s As String In _MaterialsList
Dim tmpWidth = TextRenderer.MeasureText(s, ComboBoxMaterials.Font).Width
If tmpWidth > MaxWidth Then MaxWidth = tmpWidth
Next
MaxWidth += 20
ComboBoxMaterials.DropDownWidth = MaxWidth
If MaxWidth > PreviousWidth Then
If MaxWidth <= ComboboxAbsoluteMaxWidth Then
ComboBoxMaterials.Size = New Drawing.Size(MaxWidth, 25)
End If
End If
End If
End If
End Set
End Property
Private _ActiveMaterial As String
Public Property ActiveMaterial As String
Get
Return _ActiveMaterial
End Get
Set(value As String)
_ActiveMaterial = value
If Me.ComboBoxMaterials IsNot Nothing Then
If Not SearchingTVFilename Then
If Not ComboBoxMaterials.Text = _ActiveMaterial Then ComboBoxMaterials.Text = _ActiveMaterial
End If
End If
End Set
End Property
Public Property PartPlacementTimeout As String
Private _FavoritesOnly As Boolean
Public Property FavoritesOnly As Boolean
Get
Return _FavoritesOnly
End Get
Set(value As Boolean)
_FavoritesOnly = value
If Me.ButtonFavoritesOnly IsNot Nothing Then
If _FavoritesOnly Then
ButtonFavoritesOnly.Image = My.Resources.favorites_enabled
Else
ButtonFavoritesOnly.Image = My.Resources.favorites_disabled
End If
End If
End Set
End Property
Private Property Props As Props
Private Property TemplateDoc As SolidEdgeFramework.SolidEdgeDocument
Public Property AssemblyPasteComplete As Boolean
Private Property NodeCount As Integer
Private Property ErrorLogger As HCErrorLogger
Private Property StringToXmlDict As Dictionary(Of String, String)
Private Property StringFromXmlDict As Dictionary(Of String, String)
' https://community.sw.siemens.com/s/question/0D5Vb00000Krsy5KAB/handling-events-how-to-use-help-example
' https://github.com/SolidEdgeCommunity/Samples/blob/master/General/EventHandling/vb/EventHandling/MainForm.vb
Public SEAppEvents As SolidEdgeFramework.DISEApplicationEvents_Event
Private Sub Startup()
Dim Splash As New FormSplash()
Splash.Show()
Splash.UpdateStatus("Initializing")
PopulateStringToXmlDicts()
OleMessageFilter.Register()
TextBoxStatus.Text = ""
'AddHandler TreeView1.AfterSelect, AddressOf TreeView1_AfterSelect
AddHandler TreeView1.NodeMouseClick, AddressOf TreeView1_NodeMouseClick
Me.Props = New Props
Dim UP As New UtilsPreferences
Dim tmpStartupDirectory As String = UP.GetStartupDirectory
' ###### CANNOT START WITH THESE ERRORS ######
Try
SEApp = CType(MarshalHelper.GetActiveObject("SolidEdge.Application", throwOnError:=True), SolidEdgeFramework.Application)
Catch ex As Exception
MsgBox("Solid Edge must be running for the program to start.", vbOKOnly, "Solid Edge Not Found")
End
End Try
Dim ErrorMessage As String = ""
Dim Suffix As String = "SE2024"
Dim tmpDirList As New List(Of String) From {UP.GetDefaultDataDirectory(Suffix), UP.GetDefaultTemplatesDirectory(Suffix)}
For Each d As String In tmpDirList
If Not IO.Directory.Exists(d) Then ErrorMessage = $"{ErrorMessage} {d}{vbCrLf}"
Next
If Not ErrorMessage = "" Then
ErrorMessage = $"Cannot continue without the following directories{vbCrLf}{ErrorMessage}{vbCrLf}{vbCrLf}"
ErrorMessage = $"{ErrorMessage}If you cloned the program from the repo, "
ErrorMessage = $"{ErrorMessage}please see the Installation section of the Readme "
ErrorMessage = $"{ErrorMessage}to learn how to get the missing files. "
MsgBox(ErrorMessage, vbOKOnly, "Startup Not Successful")
End
End If
' ###### SETUP ######
Dim tmpPreferencesDirectory As String = UP.GetPreferencesDirectory
If Not IO.Directory.Exists(tmpPreferencesDirectory) Then
' First run. Set defaults.
Me.AlwaysReadExcel = True
Me.AutoPattern = True
Me.AddProp = False
Me.DisableFineThreadWarning = False
Me.CheckNewVersion = True
Me.AlwaysOnTop = False
Me.SaveInLibrary = True
Me.SaveInAssemblyDirectory = False
Me.SaveInOther = False
Me.ProcessTemplateInBackground = False
Me.FailedConstraintSuppress = False
Me.FailedConstraintAllow = True
Me.SuspendMRU = False
Me.CacheProperties = False
Me.IncludeDrawing = False
Me.ActiveMaterial = ""
Me.AlwaysOnTopRefreshTime = "1000"
Me.PartPlacementTimeout = "60000"
Me.FavoritesOnly = False
End If
UP.CreatePreferencesDirectory(Me)
UP.GetFormMainSettings(Me)
UP.CreateFilenameCharmap()
LoadXml(Splash)
If Me.MaterialsList Is Nothing Then
Me.MaterialsList = New List(Of String)
End If
If Me.PropertiesToSearchList Is Nothing Then
Me.PropertiesToSearchList = New List(Of String)
End If
Dim ColIdx As Integer
DataGridViewVendorParts.Columns.Clear()
ColIdx = DataGridViewVendorParts.Columns.Add("Filename", "Filename")
DataGridViewVendorParts.Columns(ColIdx).Width = 150
ColIdx = DataGridViewVendorParts.Columns.Add("Path", "Path")
DataGridViewVendorParts.Columns(ColIdx).Width = 50
For i = 0 To PropertiesToSearchList.Count - 1
Dim PropString As String = PropertiesToSearchList(i)
ColIdx = DataGridViewVendorParts.Columns.Add($"Prop_{i + 1}", PropString)
DataGridViewVendorParts.Columns(ColIdx).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
Next
Me.PropertiesData = New HCPropertiesData ' Automatically loads saved settings if any.
Splash.UpdateStatus("Checking version")
UP.CheckVersionFormat(Me.Version)
' Form title
Me.Text = String.Format("Solid Edge Storekeeper {0}", Me.Version)
If Not Me.PreviewVersion = "" Then Me.Text = $"{Me.Text} Preview {Me.PreviewVersion}"
If Me.CheckNewVersion Then
UP.CheckForNewerVersion(Me.Version)
End If
TextBoxStatus.Text = $"{Me.NodeCount} items available"
Splash.UpdateStatus("Initializing timer")
If Me.AlwaysOnTopRefreshTime Is Nothing OrElse Me.AlwaysOnTopRefreshTime = "" Then
Me.AlwaysOnTopRefreshTime = "1000"
End If
Dim tmpInterval As Integer = 1000
Try
tmpInterval = CInt(Me.AlwaysOnTopRefreshTime)
If tmpInterval < 100 Then tmpInterval = 100
Me.AlwaysOnTopRefreshTime = CStr(tmpInterval)
Catch ex As Exception
Me.AlwaysOnTopRefreshTime = CStr(tmpInterval)
End Try
AlwaysOnTopTimer = New Timer
AlwaysOnTopTimer.Interval = tmpInterval
AddHandler AlwaysOnTopTimer.Tick, AddressOf HandleAlwaysOnTopTimerTick
If Me.AlwaysOnTop Then AlwaysOnTopTimer.Start()
' Trigger an update
If Me.SelectedNodeFullPath Is Nothing Then Me.SelectedNodeFullPath = ""
Me.SelectedNodeFullPath = Me.SelectedNodeFullPath
Splash.Close()
If Not IO.File.Exists(Me.MaterialTable) Then
MsgBox($"Specify a material table before continuing.{vbCrLf}It is set on the Tree Search Options page.", vbOKOnly, "Specify a Material Table")
End If
End Sub
' ###### PROCESS SELECTED ITEM ######
Private Function CheckStartConditions(
PropertySearchFilename As String,
ErrorLogger As Logger
) As Boolean
Dim Success As Boolean = True
Dim IsTreeSearch = PropertySearchFilename Is Nothing
If Not IO.Directory.Exists(Me.LibraryDirectory) Then
Success = False
ErrorLogger.AddMessage($"Library directory not found '{Me.LibraryDirectory}'")
End If
If IsTreeSearch Then
If Not IO.Directory.Exists(Me.TemplateDirectory) Then
Success = False
ErrorLogger.AddMessage($"Template directory not found '{Me.TemplateDirectory}'")
End If
If Not IO.Directory.Exists(Me.DataDirectory) Then
Success = False
ErrorLogger.AddMessage($"Data directory not found '{Me.DataDirectory}'")
End If
If Not IO.File.Exists(Me.MaterialTable) Then
Success = False
ErrorLogger.AddMessage($"Material table not found '{Me.MaterialTable}'")
End If
If Me.ActiveMaterial = "" And Me.MaterialsList.Count > 0 Then
Success = False
ErrorLogger.AddMessage($"Select a material")
End If
Else
If PropertiesToSearchList.Count = 0 Then
Success = False
ErrorLogger.AddMessage("Enter at least one property to search on the options page")
End If
If Not IO.File.Exists(Me.AssemblyTemplate) Then
Success = False
ErrorLogger.AddMessage($"Assembly template not found '{Me.AssemblyTemplate}'")
End If
If Not IO.File.Exists(Me.PartTemplate) Then
Success = False
ErrorLogger.AddMessage($"Part template not found '{Me.PartTemplate}'")
End If
If Not IO.File.Exists(Me.SheetmetalTemplate) Then
Success = False
ErrorLogger.AddMessage($"Sheetmetal template not found '{Me.SheetmetalTemplate}'")
End If
End If
If SEApp Is Nothing Then
Success = False
ErrorLogger.AddMessage("Unable to connect to Solid Edge")
End If
'Try
' SEApp = CType(MarshalHelper.GetActiveObject("SolidEdge.Application", throwOnError:=True), SolidEdgeFramework.Application)
'Catch ex As Exception
' Success = False
' ErrorLogger.AddMessage("Solid Edge not detected. This command requires a running instance of Solid Edge with an assembly file active")
'End Try
'If SEApp IsNot Nothing And Not Me.PrePopulate Then
' Try
' AsmDoc = CType(SEApp.ActiveDocument, SolidEdgeAssembly.AssemblyDocument)
' Catch ex As Exception
' Success = False
' ErrorLogger.AddMessage("No assembly file active. This command requires a running instance of Solid Edge with an assembly file active")
' End Try
'End If
If SEApp IsNot Nothing Then
Try
AsmDoc = CType(SEApp.ActiveDocument, SolidEdgeAssembly.AssemblyDocument)
Catch ex As Exception
Success = False
ErrorLogger.AddMessage("No assembly file active. This command requires a running instance of Solid Edge with an assembly file active")
End Try
End If
'If Not Me.PrePopulate Then
' If SEApp IsNot Nothing And AsmDoc IsNot Nothing AndAlso AsmDoc.Path = "" Then
' Success = False
' ErrorLogger.AddMessage("Assembly must be saved before adding parts")
' End If
'End If
If SEApp IsNot Nothing And AsmDoc IsNot Nothing AndAlso AsmDoc.Path = "" Then
Success = False
ErrorLogger.AddMessage("Assembly must be saved before adding parts")
End If
If SEApp IsNot Nothing Then
Dim MaterialTableFolder As String
Dim MaterialTableFolderObject As Object = Nothing
Dim MTFConstant As SolidEdgeFramework.ApplicationGlobalConstants
MTFConstant = SolidEdgeFramework.ApplicationGlobalConstants.seApplicationGlobalMatTableFolder
Try
SEApp.GetGlobalParameter(MTFConstant, MaterialTableFolderObject)
MaterialTableFolder = CStr(MaterialTableFolderObject)
If Not Me.MaterialTable.Contains(MaterialTableFolder) Then
Success = False
ErrorLogger.AddMessage($"Material table not in required folder: '{MaterialTableFolder}'")
End If
Catch ex As Exception
Success = False
ErrorLogger.AddMessage("Unable to connect to Solid Edge")
End Try
End If
Return Success
End Function
Public Function Process(
Optional PropertySearchFilename As String = Nothing,
Optional Replace As Boolean = False,
Optional ReplaceAll As Boolean = False,
Optional ErrorLogger As Logger = Nothing
) As Boolean
Dim Proceed As Boolean = True
If ErrorLogger Is Nothing Then ErrorLogger = Me.FileLogger
Dim UC As New UtilsCommon
If Not CheckStartConditions(PropertySearchFilename, ErrorLogger) Then
TextBoxStatus.Text = ""
Return False
End If
'OleMessageFilter.Register()
SEAppEvents = CType(SEApp.ApplicationEvents, SolidEdgeFramework.DISEApplicationEvents_Event)
Dim Filename As String = Nothing
If PropertySearchFilename Is Nothing Then
TextBoxStatus.Text = "Getting filename"
Dim SubLogger As Logger = ErrorLogger.AddLogger("Get filename")
Filename = GetFilenameFromPropsFormula(DefaultExtension:=IO.Path.GetExtension(GetTemplateNameFormula()), SubLogger)
If Filename Is Nothing Then
TextBoxStatus.Text = ""
Proceed = False
End If
Else
Filename = PropertySearchFilename
End If
If Proceed And Not IO.File.Exists(Filename) Then
If Me.SuspendMRU Then
' API in SE2020 and earlier does not have SuspendMRU
Try
SEApp.SuspendMRU()
Catch ex As Exception
End Try
End If
TextBoxStatus.Text = "Getting template name"
Dim TemplateName As String = GetTemplateNameFormula()
If TemplateName Is Nothing Then
TextBoxStatus.Text = ""
Proceed = False
ErrorLogger.AddMessage("Unable to get template name")
End If
TextBoxStatus.Text = $"Opening '{IO.Path.GetFileName(TemplateName)}'"
Dim SEDoc As SolidEdgeFramework.SolidEdgeDocument = Nothing
If Proceed Then
If Me.ProcessTemplateInBackground Then
SEDoc = CType(SEApp.Documents.Open(TemplateName, 8), SolidEdgeFramework.SolidEdgeDocument)
Else
SEDoc = CType(SEApp.Documents.Open(TemplateName), SolidEdgeFramework.SolidEdgeDocument)
End If
SEApp.DoIdle()
TextBoxStatus.Text = $"Saving '{IO.Path.GetFileName(Filename)}'"
Dim Dir As String = IO.Path.GetDirectoryName(Filename)
If Not IO.Directory.Exists(Dir) Then
IO.Directory.CreateDirectory(Dir)
End If
SEDoc.SaveAs(Filename)
SEApp.DoIdle()
' Copy template drawing if present
If Me.IncludeDrawing Then
Dim SubLogger As Logger = ErrorLogger.AddLogger("Copy template drawing")
CopyTemplateDrawing(SEApp, SEDoc, TemplateName, SubLogger)
End If
End If
TextBoxStatus.Text = "Processing variables"
If Proceed Then
Dim SubLogger As Logger = ErrorLogger.AddLogger("Process variables")
Proceed = ProcessVariables(SEApp, SEDoc, SubLogger)
End If
TextBoxStatus.Text = "Processing parameters"
If Proceed Then
Dim SubLogger As Logger = ErrorLogger.AddLogger("Process parameters")
Proceed = ProcessParameters(SEApp, SEDoc, SubLogger)
End If
TextBoxStatus.Text = "Processing SE properties"
If Proceed Then
Dim SubLogger As Logger = ErrorLogger.AddLogger("Process SE properties")
Proceed = ProcessSEProperties(SEApp, SEDoc, SubLogger)
End If
TextBoxStatus.Text = "Saving file"
If Proceed Then
SEDoc.Save()
SEApp.DoIdle()
SEDoc.Close()
SEApp.DoIdle()
Else
SEDoc.Close()
SEApp.DoIdle()
End If
If Me.SuspendMRU Then
Try
SEApp.ResumeMRU()
Catch ex As Exception
End Try
End If
End If
' ###### ADD PART TO ASSEMBLY ######
If Proceed And Not AddToLibraryOnly Then
AddHandler SEAppEvents.AfterCommandRun, AddressOf DISEApplicationEvents_AfterCommandRun
SEApp.DoIdle()
AssemblyPasteComplete = False
Me.TopMost = False
System.Windows.Forms.Application.DoEvents()
SEApp.Activate()
SEApp.DoIdle()
If Not Replace Then
TextBoxStatus.Text = $"Adding '{IO.Path.GetFileName(Filename)}'"
Dim Occurrences As SolidEdgeAssembly.Occurrences = AsmDoc.Occurrences
Dim PreviousOccurrencesCount As Integer = Occurrences.Count
Dim Occurrence As SolidEdgeAssembly.Occurrence
'Dim Occurrence = AsmDoc.Occurrences.AddByFilename(Filename)
'Dim SelectSet = AsmDoc.SelectSet
'SelectSet.RemoveAll()
'SelectSet.Add(Occurrence)
'Dim Cut = SolidEdgeConstants.AssemblyCommandConstants.AssemblyEditCut
'SEApp.StartCommand(CType(Cut, SolidEdgeFramework.SolidEdgeCommandConstants))
'Dim Paste = SolidEdgeConstants.AssemblyCommandConstants.AssemblyEditPaste
Try
'SEApp.StartCommand(CType(Paste, SolidEdgeFramework.SolidEdgeCommandConstants))
Clipboard.Clear()
Clipboard.SetText(Filename)
SEApp.StartCommand(CType(SolidEdgeConstants.AssemblyCommandConstants.AssemblyEditPaste, SolidEdgeFramework.SolidEdgeCommandConstants))
' Wait for Paste command to complete
Dim ElapsedMilliseconds As Integer = 0
Dim SleepMilliseconds As Integer = 500
While Not AssemblyPasteComplete
Threading.Thread.Sleep(SleepMilliseconds)
ElapsedMilliseconds += SleepMilliseconds
If ElapsedMilliseconds >= CInt(Me.PartPlacementTimeout) Then
AssemblyPasteComplete = True
Proceed = False
ErrorLogger.AddMessage("Place part command timed out.")
SEApp.StartCommand(CType(SolidEdgeConstants.AssemblyCommandConstants.AssemblyAssemblyToolsSelect, SolidEdgeFramework.SolidEdgeCommandConstants))
End If
'SEApp.DoIdle()
End While
Catch ex As Exception
Proceed = False
ErrorLogger.AddMessage("Could not add part. Please try again.")
End Try
RemoveHandler SEAppEvents.AfterCommandRun, AddressOf DISEApplicationEvents_AfterCommandRun
If Proceed And Me.AutoPattern And Occurrences.Count > PreviousOccurrencesCount Then
Occurrence = CType(Occurrences(Occurrences.Count - 1), SolidEdgeAssembly.Occurrence)
MaybePatternOccurrence(Occurrence, PiggybackOccurrences:=Nothing, ErrorLogger)
End If
Else
TextBoxStatus.Text = $"Replacing selected with'{IO.Path.GetFileName(Filename)}'"
If SEApp.ActiveSelectSet.Count >= 1 Then
Dim objOcc As SolidEdgeAssembly.Occurrence = CType(SEApp.ActiveSelectSet.Item(1), SolidEdgeAssembly.Occurrence)
Dim objAsm As SolidEdgeAssembly.AssemblyDocument = CType(SEApp.ActiveDocument, SolidEdgeAssembly.AssemblyDocument)
If objOcc.Type = SolidEdgeFramework.ObjectType.igPart Or objOcc.Type = SolidEdgeFramework.ObjectType.igSubAssembly Then
SEApp.DelayCompute = True
If ReplaceAll Then
Dim tmpColl As New List(Of SolidEdgeAssembly.Occurrence)
For i = 1 To objAsm.Occurrences.Count
If objAsm.Occurrences.Item(i).OccurrenceFileName = objOcc.OccurrenceFileName Then tmpColl.Add(objAsm.Occurrences.Item(i))
Next
Dim tmpOcc = tmpColl.ToArray
If Me.FailedConstraintSuppress Then
objAsm.ReplaceComponents(CType(tmpOcc, Array), Filename, SolidEdgeAssembly.ConstraintReplacementConstants.seConstraintReplacementSuppress)
ElseIf Me.FailedConstraintAllow Then
objAsm.ReplaceComponents(CType(tmpOcc, Array), Filename, SolidEdgeAssembly.ConstraintReplacementConstants.seConstraintReplacementNone)
Else
Proceed = False
ErrorLogger.AddMessage("Option not set for treatment of failed constraints. Set it on the Tree Search Options dialog.")
End If
Else
Dim tmpOcc As System.Array = {objOcc}
If Me.FailedConstraintSuppress Then
objAsm.ReplaceComponents(tmpOcc, Filename, SolidEdgeAssembly.ConstraintReplacementConstants.seConstraintReplacementSuppress)
ElseIf Me.FailedConstraintAllow Then
objAsm.ReplaceComponents(tmpOcc, Filename, SolidEdgeAssembly.ConstraintReplacementConstants.seConstraintReplacementNone)
Else
Proceed = False
ErrorLogger.AddMessage("Option not set for treatment of failed constraints. Set it on the Tree Search Options dialog.")
End If
End If
SEApp.ActiveSelectSet.RefreshDisplay()
SEApp.DelayCompute = False
End If
Else
'Clipboard.Clear()
'Clipboard.SetText(Filename)
'SEApp.StartCommand(CType(SolidEdgeConstants.AssemblyCommandConstants.AssemblyEditPaste, SolidEdgeFramework.SolidEdgeCommandConstants))
Proceed = False
ErrorLogger.AddMessage("No parts selected")
End If
RemoveHandler SEAppEvents.AfterCommandRun, AddressOf DISEApplicationEvents_AfterCommandRun 'just because it is early initialized
End If
If Me.AlwaysOnTop Then
Me.TopMost = True
System.Windows.Forms.Application.DoEvents()
Me.TopMost = False
End If
End If
TextBoxStatus.Text = ""
Return Proceed
End Function
Private Function CopyTemplateDrawing(
SEApp As SolidEdgeFramework.Application,
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
ModelTemplateName As String,
ErrorLogger As Logger
) As Boolean
Dim Success As Boolean = True
Dim DraftTemplateName As String = IO.Path.ChangeExtension(ModelTemplateName, "dft")
Dim DraftName As String = IO.Path.ChangeExtension(SEDoc.FullName, "dft")
Dim DraftDoc As SolidEdgeDraft.DraftDocument
If Not IO.File.Exists(DraftTemplateName) Then
Return True
Else
If Me.ProcessTemplateInBackground Then
DraftDoc = CType(SEApp.Documents.Open(DraftTemplateName, 8), SolidEdgeDraft.DraftDocument)
Else
DraftDoc = CType(SEApp.Documents.Open(DraftTemplateName), SolidEdgeDraft.DraftDocument)
End If
SEApp.DoIdle()
DraftDoc.SaveAs(DraftName)
SEApp.DoIdle()
Dim ModelLinks As SolidEdgeDraft.ModelLinks = DraftDoc.ModelLinks
If ModelLinks IsNot Nothing Then
For Each ModelLink As SolidEdgeDraft.ModelLink In ModelLinks
Dim tmpFilename As String = Nothing
If ModelLink.IsAssemblyFamilyMember Then
tmpFilename = ModelLink.FileName.Split("!"c)(0)
Else
tmpFilename = ModelLink.FileName
End If
If tmpFilename = ModelTemplateName Then
Try
ModelLink.ChangeSource(SEDoc.FullName)
SEApp.DoIdle()
Catch ex As Exception
Success = False
ErrorLogger.AddMessage("Unable to change drawing link")
End Try
End If
Next
End If
For Each Sheet As SolidEdgeDraft.Sheet In DraftDoc.Sheets
If Sheet.SectionType = SolidEdgeDraft.SheetSectionTypeConstants.igWorkingSection Then
For Each DrawingView As SolidEdgeDraft.DrawingView In Sheet.DrawingViews
Try
DrawingView.Update()
SEApp.DoIdle()
Catch ex As Exception
Success = False
ErrorLogger.AddMessage("Unable to update drawing view")
End Try
Next
End If
Next
DraftDoc.Save()
SEApp.DoIdle()
DraftDoc.Close()
SEApp.DoIdle()
End If
Return Success
End Function
Private Function ProcessVariables(
SEApp As SolidEdgeFramework.Application,
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
ErrorLogger As Logger
) As Boolean
Dim Success As Boolean = True
Dim UC As New UtilsCommon
Dim VariableProps As List(Of Prop) = Props.GetPropsOfType("Variable")
Dim VariableDict As Dictionary(Of String, SolidEdgeFramework.variable) = UC.GetDocVariables(SEDoc)
SEApp.DelayCompute = True
For Each Prop As Prop In VariableProps
If VariableDict.Keys.Contains(Prop.Name) Then
Try
VariableDict(Prop.Name).Formula = Prop.Value
'VariableDict(Prop.Name).Formula = Prop.Value.Replace(".", ",")
Catch ex As Exception
ErrorLogger.AddMessage($"Cannot process value for '{Prop.Name}': '{Prop.Value}'")
End Try
Else
ErrorLogger.AddMessage($"Variable not found: '{Prop.Name}'")
End If
Next
SEApp.DelayCompute = False
SEApp.DoIdle()
If Not Me.ProcessTemplateInBackground And Success Then
Select Case UC.GetDocType(SEDoc)
Case "asm"
SEApp.StartCommand(CType(SolidEdgeConstants.AssemblyCommandConstants.AssemblyViewFit, SolidEdgeFramework.SolidEdgeCommandConstants))
'##### Why viewfit ??? F.Arfilli
Case "par", "psm"
SEApp.StartCommand(CType(SolidEdgeConstants.PartCommandConstants.PartViewFit, SolidEdgeFramework.SolidEdgeCommandConstants))
End Select
End If
Return Success
End Function
Private Function ProcessParameters(
SEApp As SolidEdgeFramework.Application,
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
ErrorLogger As Logger
) As Boolean
Dim Success As Boolean = True
Dim UC As New UtilsCommon
Dim ParameterStrings As List(Of Prop) = Props.GetPropsOfType("ParameterString")
If ParameterStrings.Count = 0 Then Return True
Dim Prop = ParameterStrings(0)
Dim ThreadDescription = Prop.Value
'Dim UnitsFormulas As List(Of Prop) = Props.GetPropsOfType("UnitsOfMeasure")
'If UnitsFormulas.Count = 0 Then Return False
'Dim UnitOfMeasure As String = UnitsFormulas(0).Value
Dim Models As SolidEdgePart.Models = Nothing
Dim Model As SolidEdgePart.Model
Dim HoleData As SolidEdgePart.HoleData = Nothing
Select Case UC.GetDocType(SEDoc)
Case "par"
Dim tmpSEDoc As SolidEdgePart.PartDocument = CType(SEDoc, SolidEdgePart.PartDocument)
Models = tmpSEDoc.Models
Case "psm"
Dim tmpSEDoc As SolidEdgePart.SheetMetalDocument = CType(SEDoc, SolidEdgePart.SheetMetalDocument)
Models = tmpSEDoc.Models
End Select
Model = CType(Models(0), SolidEdgePart.Model)
If Model.Threads Is Nothing And Model.Holes Is Nothing Then
Return True
End If
Dim ExternalThreads As SolidEdgePart.Threads = Model.Threads
Dim ThreadedHoles As List(Of SolidEdgePart.Hole) = Nothing
If Model.Holes.Count > 0 Then
For Each Hole As SolidEdgePart.Hole In Model.Holes
Dim tmpHoleData As SolidEdgePart.HoleData = CType(Hole.HoleData, SolidEdgePart.HoleData)
Dim SubType As String = tmpHoleData.SubType
If SubType.ToLower.Contains("Thread") Then
ThreadedHoles.Add(Hole)
End If
Next
End If
Dim HasExternalThreads As Boolean = ExternalThreads.Count > 0
Dim HasThreadedHoles As Boolean = ThreadedHoles IsNot Nothing AndAlso ThreadedHoles.Count > 0
If HasExternalThreads And HasThreadedHoles Then
ErrorLogger.AddMessage("Cannot currently process models with both threaded holes AND external threads")
Return False