-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtilsCommon.vb
More file actions
1108 lines (848 loc) · 38.3 KB
/
UtilsCommon.vb
File metadata and controls
1108 lines (848 loc) · 38.3 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.IO
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports ExcelDataReader
'Imports PanoramicData.NCalcExtensions
Public Class UtilsCommon
Public tmpList As Collection
Public Function FilenameIsOK(ByVal fileName As String) As Boolean
Try
Dim fi As New IO.FileInfo(fileName)
Catch ex As Exception
Return False
End Try
Return True
End Function
'Public Sub FindLinked(DMDoc As RevisionManager.Document)
' Dim Filename As String
' Dim LinkedDocs As RevisionManager.LinkedDocuments
' Dim LinkedDoc As RevisionManager.Document
' ' Loop through the items in the assembly
' LinkedDocs = CType(DMDoc.LinkedDocuments, RevisionManager.LinkedDocuments)
' If Not IsNothing(LinkedDocs) Then
' For Each LinkedDoc In LinkedDocs
' Filename = LinkedDoc.FullName
' If Not tmpList.Contains(Filename) Then
' tmpList.Add(Filename, Filename)
' If IO.Path.GetExtension(Filename).ToLower = ".asm" Then
' ' there is a problem in traversing back up through the tree that throws an exception, so using a try-catch.
' Try
' FindLinked(LinkedDoc)
' Catch ex As Exception
' 'MsgBox("Problem with traversing back up the tree " & objLinkedDocs(i).fullname)
' End Try
' End If
' End If
' Next
' End If
'End Sub
'Public Function GetDocRange(SEDoc As SolidEdgeFramework.SolidEdgeDocument) As List(Of Double)
' ' Returns range in a list. The order is X, Y, Z
' Dim Range As New List(Of Double)
' Dim ModelX As Double
' Dim ModelY As Double
' Dim ModelZ As Double
' Dim XMin As Double
' Dim YMin As Double
' Dim ZMin As Double
' Dim XMax As Double
' Dim YMax As Double
' Dim ZMax As Double
' Dim Models As SolidEdgePart.Models = Nothing
' Dim DocType As String = GetDocType(SEDoc)
' Select Case DocType
' Case "asm"
' Dim tmpAsm As SolidEdgeAssembly.AssemblyDocument = CType(SEDoc, SolidEdgeAssembly.AssemblyDocument)
' tmpAsm.Range(XMin, YMin, ZMin, XMax, YMax, ZMax)
' ModelX = XMax - XMin
' ModelY = YMax - YMin
' ModelZ = ZMax - ZMin
' Case "par"
' Dim tmpPar As SolidEdgePart.PartDocument = CType(SEDoc, SolidEdgePart.PartDocument)
' Models = tmpPar.Models
' Case "psm"
' Dim tmpPsm As SolidEdgePart.SheetMetalDocument = CType(SEDoc, SolidEdgePart.SheetMetalDocument)
' Models = tmpPsm.Models
' Case Else
' MsgBox(String.Format("{0} DocType '{1}' not recognized", "Task_Common", DocType))
' End Select
' If (DocType = "par") Or (DocType = "psm") Then
' Dim Model As SolidEdgePart.Model
' Dim Body As SolidEdgeGeometry.Body
' Dim UF As New UtilsFeatures
' Dim PointsList As New List(Of Double)
' Dim tmpPointsList As New List(Of Double)
' Dim Point As Double
' If (Models.Count = 0) Then
' ModelX = 0
' ModelY = 0
' ModelZ = 0
' Else
' For Each Model In Models
' ' Some Models do not have a Body
' Try
' Body = CType(Model.Body, SolidEdgeGeometry.Body)
' tmpPointsList = UF.GetBodyRange(Body)
' If PointsList.Count = 0 Then
' For Each Point In tmpPointsList
' PointsList.Add(Point)
' Next
' Else
' For i As Integer = 0 To 2
' If tmpPointsList(i) < PointsList(i) Then
' PointsList(i) = tmpPointsList(i)
' End If
' Next
' For i As Integer = 3 To 5
' If tmpPointsList(i) > PointsList(i) Then
' PointsList(i) = tmpPointsList(i)
' End If
' Next
' End If
' Catch ex As Exception
' ModelX = 0
' ModelY = 0
' ModelZ = 0
' End Try
' Next
' ModelX = PointsList(3) - PointsList(0) ' XMax - XMin
' ModelY = PointsList(4) - PointsList(1) ' YMax - YMin
' ModelZ = PointsList(5) - PointsList(2) ' ZMax - ZMin
' End If
' End If
' Range.Add(ModelX)
' Range.Add(ModelY)
' Range.Add(ModelZ)
' Return Range
'End Function
Public Function GetDocType(SEDoc As SolidEdgeFramework.SolidEdgeDocument) As String
' See SolidEdgeFramework.DocumentTypeConstants
' If the type is not recognized, the empty string is returned.
Dim DocType As String = ""
If Not IsNothing(SEDoc) Then
Select Case SEDoc.Type
Case Is = SolidEdgeFramework.DocumentTypeConstants.igAssemblyDocument
DocType = "asm"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igWeldmentAssemblyDocument
DocType = "asm"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igSyncAssemblyDocument
DocType = "asm"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igPartDocument
DocType = "par"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igSyncPartDocument
DocType = "par"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igSheetMetalDocument
DocType = "psm"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igSyncSheetMetalDocument
DocType = "psm"
Case Is = SolidEdgeFramework.DocumentTypeConstants.igDraftDocument
DocType = "dft"
Case Else
MsgBox(String.Format("{0} DocType '{1}' not recognized", "Task_Common", SEDoc.Type.ToString))
End Select
End If
Return DocType
End Function
Public Function FormatMsgBoxText(
ErrorList As List(Of String),
Title As String,
Optional MaxErrorsToShow As Integer = 20
) As String
Dim ErrorMessage As String = ""
If Not ErrorList.Count = 0 Then
Dim s As String = ""
Dim Indent As String = " "
If MaxErrorsToShow <= 0 Then MaxErrorsToShow = 1
For i As Integer = 0 To ErrorList.Count - 1
s = String.Format("{0}{1}{2}{3}", s, Indent, ErrorList(i), vbCrLf)
If i = MaxErrorsToShow - 1 Then Exit For
Next
If ErrorList.Count > MaxErrorsToShow Then
ErrorMessage = String.Format("{0}{1}", Title, vbCrLf)
ErrorMessage = String.Format("{0}{1}(Showing {2} of {3}){4}", ErrorMessage, Indent, MaxErrorsToShow, ErrorList.Count, vbCrLf)
Else
ErrorMessage = String.Format("{0}{1}", Title, vbCrLf)
End If
ErrorMessage = String.Format("{0}{1}", ErrorMessage, s)
End If
Return ErrorMessage
End Function
'###### DIMENSIONS AND VARIABLES ######
Public Function GetDocDimensions(SEDoc As SolidEdgeFramework.SolidEdgeDocument
) As Dictionary(Of String, SolidEdgeFrameworkSupport.Dimension)
Dim DocDimensionDict As New Dictionary(Of String, SolidEdgeFrameworkSupport.Dimension)
Dim Variables As SolidEdgeFramework.Variables = Nothing
Dim VariableListObject As SolidEdgeFramework.VariableList = Nothing
Dim Variable As SolidEdgeFramework.variable = Nothing
Dim Dimension As SolidEdgeFrameworkSupport.Dimension = Nothing
Dim VariableTypeName As String
Try
Variables = DirectCast(SEDoc.Variables, SolidEdgeFramework.Variables)
VariableListObject = DirectCast(Variables.Query(pFindCriterium:="*",
NamedBy:=SolidEdgeConstants.VariableNameBy.seVariableNameByBoth,
VarType:=SolidEdgeConstants.VariableVarType.SeVariableVarTypeBoth),
SolidEdgeFramework.VariableList)
' Populate dictionary
For Each VariableListItem In VariableListObject.OfType(Of Object)()
VariableTypeName = Microsoft.VisualBasic.Information.TypeName(VariableListItem)
If VariableTypeName.ToLower() = "dimension" Then
Dimension = CType(VariableListItem, SolidEdgeFrameworkSupport.Dimension)
DocDimensionDict(Dimension.DisplayName) = Dimension
End If
Next
Catch ex As Exception
End Try
Return DocDimensionDict
End Function
Public Function GetDocVariables(SEDoc As SolidEdgeFramework.SolidEdgeDocument
) As Dictionary(Of String, SolidEdgeFramework.variable)
Dim DocVariableDict As New Dictionary(Of String, SolidEdgeFramework.variable)
Dim Variables As SolidEdgeFramework.Variables = Nothing
Dim VariableListObject As SolidEdgeFramework.VariableList = Nothing
Dim Variable As SolidEdgeFramework.variable = Nothing
Dim Dimension As SolidEdgeFrameworkSupport.Dimension = Nothing
Dim VariableTypeName As String
Try
Variables = DirectCast(SEDoc.Variables, SolidEdgeFramework.Variables)
VariableListObject = DirectCast(Variables.Query(pFindCriterium:="*",
NamedBy:=SolidEdgeConstants.VariableNameBy.seVariableNameByBoth,
VarType:=SolidEdgeConstants.VariableVarType.SeVariableVarTypeBoth),
SolidEdgeFramework.VariableList)
' Populate dictionary
For Each VariableListItem In VariableListObject.OfType(Of Object)()
VariableTypeName = Microsoft.VisualBasic.Information.TypeName(VariableListItem)
If VariableTypeName.ToLower() = "variable" Then
Variable = CType(VariableListItem, SolidEdgeFramework.variable)
DocVariableDict(Variable.DisplayName) = Variable
End If
Next
Catch ex As Exception
End Try
Return DocVariableDict
End Function
Public Function IsVariablePresent(SEDoc As SolidEdgeFramework.SolidEdgeDocument, VariableName As String) As Boolean
Dim VariableFound As Boolean = False
Dim DocVariableDict As New Dictionary(Of String, SolidEdgeFramework.variable)
DocVariableDict = GetDocVariables(SEDoc)
VariableFound = False
For Each Key As String In DocVariableDict.Keys
If Key.ToLower = VariableName.ToLower Then
VariableFound = True
Exit For
End If
Next
Return VariableFound
End Function
Public Function IsDimensionPresent(SEDoc As SolidEdgeFramework.SolidEdgeDocument, DimensionName As String) As Boolean
Dim DimensionFound As Boolean = False
Dim DocDimensionDict As New Dictionary(Of String, SolidEdgeFrameworkSupport.Dimension)
DocDimensionDict = GetDocDimensions(SEDoc)
DimensionFound = False
For Each Key As String In DocDimensionDict.Keys
If Key.ToLower = DimensionName.ToLower Then
DimensionFound = True
Exit For
End If
Next
Return DimensionFound
End Function
Public Function GetUnitType(Name As String) As SolidEdgeConstants.UnitTypeConstants
Dim UnitType As SolidEdgeConstants.UnitTypeConstants
UnitType = SolidEdgeConstants.UnitTypeConstants.igUnitDistance
For Each UnitTypeConstant As SolidEdgeConstants.UnitTypeConstants In System.Enum.GetValues(GetType(SolidEdgeConstants.UnitTypeConstants))
If Name = UnitTypeConstant.ToString.Replace("igUnit", "") Then
UnitType = UnitTypeConstant
Exit For
End If
Next
Return UnitType
End Function
'###### MISC ######
Public Function FixLocaleDecimal(Instring As String) As String
Dim OutString As String = ""
'https://stackoverflow.com/questions/14513468/detect-decimal-separator
Dim a As Char = CChar(Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)
If a = CChar(".") Then
OutString = Instring.Replace(",", ".")
Else
OutString = Instring.Replace(".", ",")
End If
Return OutString
End Function
'Public Function GetSheets(
' Doc As SolidEdgeDraft.DraftDocument,
' SectionType As String
' ) As List(Of SolidEdgeDraft.Sheet)
' Dim SheetList As New List(Of SolidEdgeDraft.Sheet)
' Dim Sheet As SolidEdgeDraft.Sheet
' Dim Section As SolidEdgeDraft.Section = Nothing
' Dim SectionSheets As SolidEdgeDraft.SectionSheets
' Dim SheetGroups As SolidEdgeDraft.SheetGroups
' Dim SheetGroup As SolidEdgeDraft.SheetGroup
' Dim count As Integer
' If SectionType = "Working" Then
' Section = Doc.Sections.WorkingSection
' ElseIf SectionType = "Background" Then
' Section = Doc.Sections.BackgroundSection
' ElseIf SectionType = "2DModel" Then
' Section = Doc.Sections.WorkingSection ' Ignored below
' ElseIf SectionType = "UserGenerated" Then
' Section = Doc.Sections.WorkingSection
' SheetGroups = CType(Doc.SheetGroups, SolidEdgeDraft.SheetGroups)
' ElseIf SectionType = "AutoGenerated" Then
' Section = Doc.Sections.WorkingSection
' SheetGroups = CType(Doc.SheetGroups, SolidEdgeDraft.SheetGroups)
' Else
' MsgBox(String.Format("SectionType '{0}' not recognized. Quitting...", SectionType))
' End If
' SectionSheets = Section.Sheets
' If (SectionType = "Working") Or (SectionType = "Background") Then
' For Each Sheet In SectionSheets.OfType(Of SolidEdgeDraft.Sheet)()
' SheetList.Add(Sheet)
' Next
' ElseIf (SectionType = "2DModel") Then
' SheetList.Add(Doc.Sections.Get2DModelSheet)
' Else ' So, 'UserGenerated' or 'AutoGenerated'
' SheetGroups = CType(Doc.SheetGroups, SolidEdgeDraft.SheetGroups)
' ' 'count' is an index on SheetGroups. The first SheetGroup is user generated.
' ' The others appear to be automatically generated for PartsLists
' count = 0
' For Each SheetGroup In SheetGroups
' For Each Sheet In SheetGroup.Sheets.OfType(Of SolidEdgeDraft.Sheet)()
' If (SectionType = "UserGenerated") And (count = 0) Then
' SheetList.Add(Sheet)
' End If
' If (SectionType = "AutoGenerated") And (count > 0) Then
' SheetList.Add(Sheet)
' End If
' Next
' count += 1
' Next
' End If
' Return SheetList
'End Function
Public Function GlobToRegex(GlobString As String) As String
Dim wildcard As String = GlobString
Dim outstring As String = ""
Dim in_character_class As Boolean = False
Dim r As String
For i As Integer = 0 To Len(wildcard) - 1
If in_character_class Then
If wildcard(i) = "]" Then
in_character_class = False
outstring = outstring + "_META_CLOSE_BRACKET_"
ElseIf wildcard(i) = "*" Then
outstring = outstring + "_LITERAL_ASTERIX_"
ElseIf wildcard(i) = "?" Then
outstring = outstring + "_LITERAL_QUESTION_MARK_"
ElseIf wildcard(i) = "[" Then
outstring = outstring + "_LITERAL_OPEN_BRACKET_"
ElseIf wildcard(i) = "#" Then
outstring = outstring + "_LITERAL_HASH_MARK_"
Else
outstring = outstring + wildcard(i)
End If
Else
If wildcard(i) = "[" Then
in_character_class = True
outstring = outstring + "_META_OPEN_BRACKET_"
Else
outstring = outstring + wildcard(i)
End If
End If
Next
r = Regex.Escape(outstring)
r = r.Replace("\*", ".*")
r = r.Replace("\?", ".")
r = r.Replace("\[", "[")
' r = r.Replace("[!", "[^")
r = r.Replace("_META_OPEN_BRACKET_!", "[^")
r = r.Replace("_META_OPEN_BRACKET_", "[")
r = r.Replace("\#", "[0-9]")
r = r.Replace("_LITERAL_OPEN_BRACKET_", "\[")
r = r.Replace("_META_CLOSE_BRACKET_", "]")
r = r.Replace("_LITERAL_ASTERIX_", "\*")
r = r.Replace("_LITERAL_QUESTION_MARK_", "\?")
r = r.Replace("_LITERAL_HASH_MARK_", "\#")
' The following prevents a second match on the ending empty string.
' https://stackoverflow.com/questions/52351217/why-do-some-regex-engines-match-twice-in-a-single-input-string/52352744#52352744
If r = ".*" Then
r = "^.*"
End If
Return r
End Function
Public Function ReadExcel(FileName As String) As String()
Dim tmpList As String() = Nothing
Dim i As Integer = 0
Using stream = System.IO.File.Open(FileName, FileMode.Open, FileAccess.Read)
' Auto-detect format, supports:
' - Binary Excel files (2.0-2003 format; *.xls)
' - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
Using reader = ExcelReaderFactory.CreateReader(stream)
' Choose one of either 1 or 2:
' 1. Use the reader methods
Do
While reader.Read()
i += 1
ReDim Preserve tmpList(i)
tmpList(i) = CStr(reader.GetValue(0))
End While
Loop While reader.NextResult()
'' 2. Use the AsDataSet extension method
'Dim result = reader.AsDataSet()
'' The result of each spreadsheet is in result.Tables
End Using
End Using
Return tmpList
End Function
'Public Sub SaveAsPNG(
' View As SolidEdgeFramework.View,
' NewFilename As String,
' ErrorLogger As Logger
' )
' Dim TempFilename As String = NewFilename.Replace(".png", "-Housekeeper.tif")
' Try
' View.SaveAsImage(TempFilename)
' Try
' Using fs As New IO.FileStream(TempFilename, IO.FileMode.Open, IO.FileAccess.Read)
' System.Drawing.Image.FromStream(fs).Save(NewFilename, Imaging.ImageFormat.Png)
' End Using
' IO.File.Delete(TempFilename)
' Catch ex As Exception
' ErrorLogger.AddMessage(String.Format("Unable to save '{0}'. ", NewFilename))
' End Try
' Catch ex As Exception
' ErrorLogger.AddMessage(String.Format("Unable to save '{0}'. ", TempFilename))
' End Try
'End Sub
Public Function GetFOAFilename(SEDocFullName As String) As String
Dim Filename As String
If SEDocFullName.Contains("!") Then
Filename = SEDocFullName.Split("!"c)(0)
Else
Filename = SEDocFullName
End If
Return Filename
End Function
Public Function GetFOAMembername(SEDocFullName As String) As String
Dim MemberName As String
If SEDocFullName.Contains("!") Then
MemberName = SEDocFullName.Split("!"c)(1)
Else
MemberName = ""
End If
Return MemberName
End Function
'Public Function CompareListOfColumns(A As List(Of PropertyColumn), B As List(Of PropertyColumn)) As Boolean
' Dim IsEqual As Boolean = True
' If A Is Nothing Or B Is Nothing Then
' IsEqual = False
' End If
' If IsEqual Then
' If Not A.Count = B.Count Then
' IsEqual = False
' End If
' End If
' If IsEqual Then
' Dim AJSON As String
' Dim BJSON As String
' For i As Integer = 0 To A.Count - 1
' AJSON = A(i).ToJSON
' BJSON = B(i).ToJSON
' IsEqual = AJSON = BJSON
' If Not IsEqual Then Exit For
' Next
' End If
' Return IsEqual
'End Function
Public Function CompareListOfJSON(A As List(Of String), B As List(Of String)) As Boolean
Dim IsEqual As Boolean = True
If A Is Nothing Or B Is Nothing Then
IsEqual = False
End If
If IsEqual Then
If Not A.Count = B.Count Then
IsEqual = False
End If
End If
If IsEqual Then
For i As Integer = 0 To A.Count - 1
IsEqual = A(i) = B(i)
If Not IsEqual Then Exit For
Next
End If
Return IsEqual
End Function
'###### PROPERTY FUNCTIONS ######
Public Function GetPropValue(
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
PropertySetName As String,
PropertyName As String,
ModelLinkIdx As Integer,
AddProp As Boolean
) As Object
Dim PropValue As Object = Nothing
PropValue = ProcessSpecialProperty(SEDoc, PropertySetName, PropertyName, ModelLinkIdx)
If PropValue Is Nothing Then
Dim Prop As SolidEdgeFramework.Property
Prop = GetProp(SEDoc, PropertySetName, PropertyName, ModelLinkIdx, AddProp)
If Prop IsNot Nothing Then
PropValue = Prop.Value
End If
End If
Return PropValue
End Function
Public Function SetPropValue(
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
PropertySetName As String,
PropertyName As String,
ModelLinkIdx As Integer,
AddProp As Boolean,
PropValue As Object
) As Boolean
Dim Success As Boolean = True
Dim PropertySets As SolidEdgeFramework.PropertySets
Dim Prop As SolidEdgeFramework.Property
PropertySets = CType(SEDoc.Properties, SolidEdgeFramework.PropertySets)
Dim UC As New UtilsCommon
Prop = GetProp(SEDoc, PropertySetName, PropertyName, ModelLinkIdx, AddProp)
If Prop IsNot Nothing AndAlso Not Prop.Value.ToString = PropValue.ToString Then
Try
Prop.Value = PropValue
Catch ex As Exception
Success = False
End Try
Else
Success = False
End If
If Success Then PropertySets.Save()
Return Success
End Function
Public Function GetProp(
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
PropertySetName As String,
PropertyName As String,
ModelLinkIdx As Integer,
AddProp As Boolean
) As SolidEdgeFramework.Property
Dim PropertySets As SolidEdgeFramework.PropertySets = Nothing
Dim PropertySet As SolidEdgeFramework.Properties = Nothing
Dim Prop As SolidEdgeFramework.Property = Nothing
Dim FoundProp As SolidEdgeFramework.Property = Nothing
Dim Proceed As Boolean = True
Dim tf As Boolean
Dim tf1 As Boolean
Dim tf2 As Boolean
Dim PropertyFound As Boolean
If ModelLinkIdx = 0 Then ' Implies the document is a model file
Try
PropertySets = CType(SEDoc.Properties, SolidEdgeFramework.PropertySets)
Catch ex As Exception
Proceed = False
End Try
Else ' Must be a draft file
Dim ModelLink As SolidEdgeDraft.ModelLink
Dim Typename As String = ""
Dim ModelDocName As String = ""
Try
Dim tmpSEDoc As SolidEdgeDraft.DraftDocument
tmpSEDoc = CType(SEDoc, SolidEdgeDraft.DraftDocument)
ModelLink = tmpSEDoc.ModelLinks.Item(ModelLinkIdx)
Typename = GetDocType(CType(ModelLink.ModelDocument, SolidEdgeFramework.SolidEdgeDocument))
tf = True
If Typename.ToLower = "par" Then
Dim ModelDoc As SolidEdgePart.PartDocument = CType(ModelLink.ModelDocument, SolidEdgePart.PartDocument)
PropertySets = CType(ModelDoc.Properties, SolidEdgeFramework.PropertySets)
ModelDocName = ModelDoc.FullName
End If
If Typename.ToLower = "psm" Then
Dim ModelDoc As SolidEdgePart.SheetMetalDocument = CType(ModelLink.ModelDocument, SolidEdgePart.SheetMetalDocument)
PropertySets = CType(ModelDoc.Properties, SolidEdgeFramework.PropertySets)
ModelDocName = ModelDoc.FullName
End If
If Typename.ToLower = "asm" Then
Dim ModelDoc As SolidEdgeAssembly.AssemblyDocument = CType(ModelLink.ModelDocument, SolidEdgeAssembly.AssemblyDocument)
PropertySets = CType(ModelDoc.Properties, SolidEdgeFramework.PropertySets)
ModelDocName = ModelDoc.FullName
End If
Catch ex As Exception
Proceed = False
End Try
End If
If Proceed Then
For Each PropertySet In PropertySets
For Each Prop In PropertySet
'Check if both names are 'custom' or neither are
tf1 = (PropertySetName.ToLower = "custom") And (PropertySet.Name.ToLower = "custom")
tf2 = (PropertySetName.ToLower <> "custom") And (PropertySet.Name.ToLower <> "custom")
tf = tf1 Or tf2
If Not tf Then
Continue For
End If
' Some properties do not have names.
Try
If Not IsNothing(Prop.Name) Then
If Prop.Name.ToLower = PropertyName.ToLower Then
FoundProp = Prop
PropertyFound = True
Exit For
End If
End If
Catch ex As Exception
End Try
Next
If PropertyFound Then
Exit For
End If
Next
End If
'If the property was not found, add it.
tf = PropertySetName.ToLower = "custom"
tf = tf And AddProp
tf = tf And (Not PropertyFound)
If tf Then
' Can't add a duplicate property
Try
PropertySet = PropertySets.Item("Custom")
' PropertySet.Add arguments are (Name As Object, Value As Object)
' Documentation says the value's Type determines the new property's Type.
' So we are currently creating a String property.
' If PropertiesData holds the Type info for the new property,
' we could make the appropriate decision here.
FoundProp = PropertySet.Add(PropertyName, "")
PropertySet.Save()
PropertySets.Save()
Catch ex As Exception
' Might want to report an error.
End Try
End If
Return FoundProp
End Function
Private Function ProcessSpecialProperty(
SEDoc As SolidEdgeFramework.SolidEdgeDocument,
PropSetName As String,
PropName As String,
ModelLinkIdx As Integer
) As String
' Returns Nothing if not a special property
' Special properties:
' "System.File Name (full path)" ' C:\project\part.par -> C:\project\part.par
' "System.File Name" ' C:\project\part.par -> part.par
' "System.File Name (no extension)" ' C:\project\part.par -> part
PropName = PropName.ToLower
Dim Proceed As Boolean = True
Dim Fullname As String = Nothing
Dim SpecialProperty As String = Nothing
If ModelLinkIdx = 0 Then ' Model file
Fullname = GetFOAFilename(SEDoc.FullName)
Else ' Must be a draft file
Dim ModelLink As SolidEdgeDraft.ModelLink
Try
Dim tmpSEDoc As SolidEdgeDraft.DraftDocument
tmpSEDoc = CType(SEDoc, SolidEdgeDraft.DraftDocument)
ModelLink = tmpSEDoc.ModelLinks.Item(ModelLinkIdx)
Fullname = GetFOAFilename(ModelLink.FileName)
Catch ex As Exception
Proceed = False
End Try
End If
If Proceed Then
If PropSetName.ToLower = "system" Then
If PropName = "File Name".ToLower Then
SpecialProperty = System.IO.Path.GetFileName(Fullname)
ElseIf PropName = "File Name (full path)".ToLower Then
SpecialProperty = Fullname
ElseIf PropName = "File Name (no extension)".ToLower Then
SpecialProperty = System.IO.Path.GetFileNameWithoutExtension(Fullname)
End If
End If
End If
Return SpecialProperty
End Function
Public Function SubstitutePropertyFormula(
ByVal SEDoc As SolidEdgeFramework.SolidEdgeDocument,
ByVal FullName As String,
ByVal Instring As String,
ValidFilenameRequired As Boolean,
Optional Expression As Boolean = False
) As String
' Replaces property formulas in a string
' Examples
' "%{Custom.Engineer|R1}" --> "Fred" (From index reference 1)
' "%{System.Titulo}" --> "Ciao Pizza!"
' "%{Server.Query|R3}" --> "Some response from server" (From server query 3)
' "Material: %{System.Material}, Engineer: %{Custom.Engineer}" --> "Material: STEEL, Engineer: FRED"
Dim Outstring As String = ""
Dim Proceed As Boolean = True
Dim PropertySet As String = ""
Dim PropertyName As String = ""
Dim ModelIdx As Integer
Dim FoundProp As SolidEdgeFramework.Property
Dim DocValues As New List(Of String)
Dim i As Integer
Dim Formulas As New List(Of String)
Dim Formula As String
Dim Matches As MatchCollection
Dim MatchString As Match
Dim Pattern As String
Dim UFC As New UtilsFilenameCharmap
FullName = GetFOAFilename(FullName)
Pattern = "%{[^}]*}" ' Any number of substrings that start with "%{" and end with the first encountered "}".
Matches = Regex.Matches(Instring, Pattern)
If Matches.Count = 0 Then
Outstring = Instring
Proceed = False
Else
For Each MatchString In Matches
Formulas.Add(MatchString.Value)
Next
End If
If Proceed Then
For Each Formula In Formulas
PropertySet = PropSetFromFormula(Formula)
PropertyName = PropNameFromFormula(Formula)
ModelIdx = ModelIdxFromFormula(Formula)
If Not ((PropertySet = "System") Or (PropertySet = "Custom") Or (PropertySet = "Server")) Then
Proceed = False
End If
'Check for special properties %{System.File Name}, %{System.File Name (full path)}, %{System.File Name (no extension)}
Dim tmpValue As String = Nothing
If PropertyName.ToLower = "File Name".ToLower Then
tmpValue = System.IO.Path.GetFileName(FullName) ' C:\project\part.par -> part.par
ElseIf PropertyName.ToLower = "File Name (full path)".ToLower Then
tmpValue = FullName ' C:\project\part.par -> C:\project\part.par
ElseIf PropertyName.ToLower = "File Name (no extension)".ToLower Then
tmpValue = System.IO.Path.GetFileNameWithoutExtension(FullName) ' C:\project\part.par -> part
'ElseIf PropertyName = "Query" And PropertySet = "Server" Then
' tmpValue = Form_Main.ExecuteQuery(FullName, Form_Main.ServerQuery, ModelIdx).Replace(vbCrLf, " ")
Else
FoundProp = GetProp(SEDoc, PropertySet, PropertyName, ModelIdx, False)
If FoundProp IsNot Nothing Then tmpValue = FoundProp.Value.ToString
End If
If tmpValue IsNot Nothing Then
If ValidFilenameRequired Then
tmpValue = UFC.SubstituteIllegalCharacters(tmpValue)
End If
DocValues.Add(tmpValue)
Else
Proceed = False
Outstring = Nothing
Exit For
End If
Next
End If
If Proceed Then
Outstring = Instring
For i = 0 To DocValues.Count - 1
Outstring = Outstring.Replace(Formulas(i), DocValues(i))
Next
End If
'If Proceed Then
' If Expression Then
' Dim nCalcExpression As New ExtendedExpression(Outstring)
' Try
' Dim A = nCalcExpression.Evaluate()
' Outstring = A.ToString
' Catch ex As Exception
' Outstring = Nothing
' End Try
' End If
'End If
Return Outstring
End Function
Public Function CheckValidPropertyFormulas(Instring As String) As Boolean
Dim Valid As Boolean = True
Dim Matches As MatchCollection
Dim MatchString As Match
Dim Pattern As String
Dim tf As Boolean
Pattern = "%{[^}]*}"
Matches = Regex.Matches(Instring, Pattern)
If Matches.Count > 0 Then
For Each MatchString In Matches
tf = MatchString.Value.Contains("%{System.")