-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathimporterClasses.py
1903 lines (1667 loc) · 63.2 KB
/
importerClasses.py
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
# -*- coding: utf-8 -*-
'''
importerClasses.py:
Collection of classes necessary to read and analyse Autodesk (R) Invetor (R) files.
'''
import sys, os, Part
from importerUtils import IntArr2Str, FloatArr2Str, logWarning, logError, getInventorFile, getUInt16, getUInt16A, isEqual, isEqual1D, UID, Color
from math import degrees, radians, pi
from FreeCAD import Vector as VEC
from PySide.QtCore import *
from PySide.QtGui import *
from importerConstants import VAL_GUESS, VAL_UINT8, VAL_UINT16, VAL_UINT32, VAL_STR8, VAL_STR16, VAL_REF, VAL_ENUM
__author__ = "Jens M. Plonka"
__copyright__ = 'Copyright 2018, Germany'
__url__ = "https://www.github.com/jmplonka/InventorLoader"
model = None
PART_LINE = Part.Line
if (hasattr(Part, "LineSegment")):
PART_LINE = Part.LineSegment
SEG_APP = 'AppSegmentType'
SEG_APP_AM = 'AmAppSegmentType'
SEG_APP_PM = 'PmAppSegmentType'
SEG_BREP_AM = 'AmBREPSegmentType'
SEG_BREP_MB = 'MbBrepSegmentType'
SEG_BREP_PM = 'PmBrepSegmentType'
SEG_BROWSER_AM = 'AmBRxSegmentType'
SEG_BROWSER_DL = 'DlBRxSegmentType'
SEG_BROWSER_DX = 'DxBRxSegmentType'
SEG_BROWSER_PM = 'PmBRxSegmentType'
SEG_BROWSER_PM_OLD = 'PmBrowserSegment'
SEG_DC_AM = 'AmDcSegmentType'
SEG_DC_DL = 'DlDocDcSegmentType'
SEG_DC_DX = 'DxDcSegmentType'
SEG_DC_PM = 'PmDcSegmentType'
SEG_DESIGN_VIEW = 'FWxDesignViewType'
SEG_DESIGN_VIEW_MGR = 'FWxDesignViewManagerType'
SEG_DIRECTORY_DL = 'DlDirectorySegmentType'
SEG_EE_DATA = 'EeDataSegmentType'
SEG_EE_SCENE = 'EeSceneSegmentType'
SEG_FB_ATTRIBUTE = 'FBAttributeSegment'
SEG_GRAPHICS_AM = 'AmGRxSegmentType'
SEG_GRAPHICS_MB = 'MbGRxSegmentType'
SEG_GRAPHICS_PM = 'PmGRxSegmentType'
SEG_NOTEBOOK = 'NotebookSegmentType'
SEG_RESULT_AM = 'AmRxSegmentType'
SEG_RESULT_PM = 'PmResultSegmentType'
SEG_SHEET_DC_DL = 'DlSheetDcSegmentType'
SEG_SHEET_DL_DL = 'DlSheetDlSegmentType'
SEG_SHEET_SM_DL = 'DlSheetSmSegmentType'
SEGMENTS_APP = [SEG_APP, SEG_APP_AM, SEG_APP_PM]
SEGMENTS_BRP = [SEG_BREP_AM, SEG_BREP_MB, SEG_BREP_PM]
SEGMENTS_BRX = [SEG_BROWSER_AM, SEG_BROWSER_DL, SEG_BROWSER_DX, SEG_BROWSER_PM]
SEGMENTS_DOC = [SEG_DC_AM, SEG_DC_DL, SEG_DC_DX, SEG_DC_PM]
SEGMENTS_DVW = [SEG_DESIGN_VIEW, SEG_DESIGN_VIEW_MGR]
SEGMENTS_DIR = [SEG_DIRECTORY_DL]
SEGMENTS_EED = [SEG_EE_DATA]
SEGMENTS_EES = [SEG_EE_SCENE]
SEGMENTS_FBA = [SEG_FB_ATTRIBUTE]
SEGMENTS_GRX = [SEG_GRAPHICS_AM, SEG_GRAPHICS_MB, SEG_GRAPHICS_PM]
SEGMENTS_NTB = [SEG_NOTEBOOK]
SEGMENTS_RSX = [SEG_RESULT_AM, SEG_RESULT_PM]
SEGMENTS_SHT = [SEG_SHEET_DC_DL, SEG_SHEET_DL_DL, SEG_SHEET_SM_DL]
class VersionInfo(object):
def __init__(self):
self.revision = 0
self.minor = 0
self.major = 0
self.data = (0, 0, 0, 0, 0)
def getDisplayName(self):
if (self.major > 11):
return "Version %d.%d%d" %(self.major + 1996, self.minor, self.revision)
return "Version %d.%d%d" %(self.major, self.minor, self.revision)
def getBits(self): return 64 if ((self.data[0] & 0x40)) > 0 else 32
def __str__(self): return "Version %d.%d.%d [%s]" %(self.major, self.minor, self.revision, IntArr2Str(self.data, 2))
def __repr__(self): return self.__str__()
class RSeDatabase(object):
def __init__(self):
self.segInfo = RSeSegInformation()
self.uid = None # Internal-Name of the object
self.schema = -1
self.vers1 = None
self.dat1 = None
self.arr2 = []
self.vers2 = None
self.dat2 = None
self.txt = u""
class RSeSegInformation(object):
def __init__(self):
self.text = u""
self.vers = []
self.date = None
self.uid = None
self.arr2 = []
self.arr3 = []
self.u16 = 0
self.text2 = u""
self.arr4 = []
self.segments = {}
self.val = [] # UInt16[2]
self.uidList1 = []
self.uidList2 = []
class RSeSegmentObject(object):
def __init__(self):
self.revisionRef = None # reference to RSeDbRevisionInfo
self.values = []
self.segRef = None
self.value1 = 0
self.value2 = 0
def __str__(self):
return '[%s],%02X,%02X' % (IntArr2Str(self.values, 4), self.value1, self.value2)
class RSeSegmentValue2(object):
def __init__(self):
self.index = -1
self.indexSegList1 = -1
self.indexSegList2 = -1
self.values = []
self.number = -1
def __str__(self):
return '%02X,%02X,%X,[%s],%04X' % (self.indexSegList1, self.indexSegList2, self.index, IntArr2Str(self.values, 4), self.number)
class RSeSegment(object):
def __init__(self):
self.name = ''
self.ID = None
self.revisionRef = None # reference to RSeDbRevisionInfo
self.value1 = 0
self.count1 = 0
self.count2 = 0
self.type = ''
self.metaData = None
self.arr1 = [] # ???, ???, ???, numSec1, ???
self.arr2 = []
self.version = None
self.value2 = 0
self.objects = []
self.nodes = []
def __str__(self):
return u"%s:%s, count=(%d/%d), ID={%s}, value1=%04X, arr1=[%s], arr2=[%s], value2=%04X, %s" %(self.type, self.name, self.count1, self.count2, self.ID, self.value1, IntArr2Str(self.arr1, 4), IntArr2Str(self.arr2, 4), self.value2, self.version)
def __repr__(self):
return self.__str__()
def __lt__(self, other):
return self.name < other.name
class RSeStorageBlockSize(object):
'''
# The first section in the RSeMetaStream (Mxyz-files) contains the information
# about the block lengths in the RSeBinaryData (Bxyz-files).
# length = The length in bytes of one of the MetaData blocks
# flags = The flags of one of the MetaData blocks.
# parent = The segments the
'''
def __init__(self, parent, value):
self.parent = parent
self.length = (value & 0x7FFFFFFF)
self.flags = ((value & 0x80000000) > 0)
def __str__(self):
return 'f=%X, l=%X' %(self.flags, self.length)
class RSeStorageSection2(object):
'''
# arr[1] = RSeDbRevisionInfo.data[0]
# arr[3] = RSeDbRevisionInfo.data[2]
# arr[4] = RSeDbRevisionInfo.data[3]
'''
def __init__(self, parent):
self.parent = parent
self.revision = None # reference to RSeDbRevisionInfo
self.flag = None
self.val = 0
self.arr = []
def __str__(self):
a = ''
u = ''
if (len(self.arr) > 0):
a = ' [%s]' %(IntArr2Str(self.arr, 4))
if (self.revision is not None):
u = ' - %s' %(self.revision)
return '%X, %X%s%s' %(self.flag, self.val, u, a)
class RSeStorageSection3(object):
def __init__(self, parent):
self.uid = None
self.parent = parent
self.arr = [] # UInt16[6]
def __str__(self):
return '%s: [%s]' %(self.uid, IntArr2Str(self.arr, 4))
class RSeStorageSection4Data(object):
def __init__(self):
self.num = 0 # UInt16
self.val = 0 # UInt32
def __str__(self):
return '(%04X,%08X)' %(self.num, self.val)
class RSeStorageBlockType(object):
def __init__(self, parent):
self.parent = parent
self.uid = None
self.arr = [] # RSeStorageSection4Data[2]
def __str__(self):
return '%s: [%s,%s]' %(self.uid, self.arr[0], self.arr[1])
class RSeStorageSection4Data1(object):
def __init__(self, uid, val):
self.uid = uid
self.val = val
def __str__(self):
return '[%s,%d]' %(self.uid, self.val)
class RSeStorageSection5(object):
def __init__(self, parent):
self.parent = parent
self.indexSec4 = []
class RSeStorageSection6(object):
def __init__(self, parent):
self.parent = parent
self.arr1 = []
self.arr2 = []
class RSeStorageSection7(object):
def __init__(self, parent):
self.parent = parent
self.segRef = None
self.segName = None
self.revisionRef = None
self.dbRef = None
self.arr1 = []
self.txt1 = ''
self.arr2 = []
self.txt2 = ''
self.arr3 = []
self.txt3 = ''
def __str__(self):
if (self.dbRef is None):
if (self.segName is None):
return '%r' %(self.segRef)
return u"'%s'" %(self.segName)
if (self.segName is None):
return '[%s] [%s] [%s] [%s] %r %r %r' %(self.segRef, self.arr1, self.arr2, self.arr3, self.txt1, self.txt2, self.txt3)
return '[%s] [%s] [%s] [%s] %r %r %r' %(self.segName, self.arr1, self.arr2, self.arr3, self.txt1, self.txt2, self.txt3)
class RSeStorageSection8(object):
def __init__(self, parent):
self.parent = parent
self.dbRevisionInfoRef = None
self.arr = [] # UInt16[2]
def __str__(self):
return '[%s]' %(IntArr2Str(self.arr, 4))
class RSeStorageSection9(object):
def __init__(self, parent):
self.parent = parent
self.uid = None
self.arr = [] # UInt16[3]
def __str__(self):
return '%s: [%s]' %(self.uid, IntArr2Str(self.arr, 4))
class RSeStorageSectionA(object):
def __init__(self, parent):
self.parent = parent
self.uid = None
self.arr = [] # UInt16[4]
def __str__(self):
return '[%s]' %(IntArr2Str(self.arr, 4))
class RSeStorageSectionB(object):
def __init__(self, parent):
self.parent = parent
self.uid = None
self.arr = [] # UInt16[2]
def __str__(self):
return '[%s]' %(IntArr2Str(self.arr, 4))
class RSeRevisions(object):
def __init__(self):
self.mapping = {}
self.infos = []
def __del__(self):
self.mapping.clear()
self.infos[:] = []
class Inventor(object):
def __init__(self):
self.UFRxDoc = None
self.RSeDb = RSeDatabase()
self.RSeRevisions = RSeRevisions()
self.iProperties = {}
self.RSeMetaData = {}
def __del__(self):
self.iProperties.clear()
self.RSeMetaData.clear()
def __repr__(self):
if (getInventorFile() is None): return u"#NV#"
return u"[%d]: %s" %(self.RSeDb.vers1.DisplayName(), os.path.split(os.path.abspath(getInventorFile()))[-1])
def getApp(self):
'''
Returns the segment that contains the application settings.
'''
for seg in self.RSeMetaData.values():
if (seg.isApp()): return seg
return EMPTY_SEGMENT
def getBRep(self):
'''
Returns the segment that contains the boundary representation.
'''
for seg in self.RSeMetaData.values():
if (seg.isBRep()): return seg
return EMPTY_SEGMENT
def getBrowser(self):
for seg in self.RSeMetaData.values():
if (seg.isBrowser()): return seg
return EMPTY_SEGMENT
def getDC(self):
'''
Returns the segment that contains the 3D-objects.
'''
for seg in self.RSeMetaData.values():
if (seg.isDC()): return seg
return EMPTY_SEGMENT
def getDesignViews(self):
views = []
for seg in self.RSeMetaData.values():
if (seg.isDesignView()):
views.append(seg)
return views
def getDirectory(self):
for seg in self.RSeMetaData.values():
if (seg.isDirectory()): return seg
return EMPTY_SEGMENT
def getEeData(self):
for seg in self.RSeMetaData.values():
if (seg.isEeData()): return seg
return EMPTY_SEGMENT
def getEeScene(self):
for seg in self.RSeMetaData.values():
if (seg.isEeScene()): return seg
return EMPTY_SEGMENT
def getFBAttribute(self):
for seg in self.RSeMetaData.values():
if (seg.isFBAttribute()): return seg
return EMPTY_SEGMENT
def getGraphics(self):
'''
Returns the segment that contains the graphic objects.
'''
for seg in self.RSeMetaData.values():
if (seg.isGraphics()): return seg
return EMPTY_SEGMENT
def getNBNotebook(self):
for seg in self.RSeMetaData.values():
if (seg.isNBNotebook()): return seg
return EMPTY_SEGMENT
def getResult(self):
for seg in self.RSeMetaData.values():
if (seg.isResult()): return seg
return EMPTY_SEGMENT
def getSheets(self):
sheets = []
for seg in self.RSeMetaData.values():
if (seg.isSheet()):
sheets.append(seg)
return sheets
class DbInterface(object):
TYPE_MAPPING = {
0x01: 'BOOL',
0x04: 'SINT',
0x10: 'UUID',
0x30: 'FLOAT[]',
0x54: 'MAP'
}
def __init__(self, name):
self.name = name
self.type = 0
self.data = []
self.uid = None
self.value = None
def __str__(self):
typeName = DbInterface.TYPE_MAPPING.get(self.type, '%4X' % self.type)
return '%s=%s:\t%s\t%s' % (self.name, self.value, typeName, self.uid)
class RSeDbRevisionInfo(object):
def __init__(self):
self.ID = ''
self.flags = 0
self.type = 0
self.b = 0
self.a = []
def __repr__(self):
return "%s" %(self.ID)
def __str__(self):
if len(self.a) == 2: return u"{%s},%06X,%04X,%02X,[%g,%08X]" %(str(self.ID).upper(), self.flags, self.type, self.b, self.a[0], self.a[1])
if len(self.a) == 4: return u"{%s},%06X,%04X,%02X,[%g,%08X]" %(str(self.ID).upper(), self.flags, self.type, self.b, self.a[0], self.a[1])
return u"{%s},%06X,%04X,%02X,%s" %(str(self.ID).upper(), self.flags, self.type, self.b, self.a)
def __repr__(self):
return self.__str__()
class ResultItem4(object):
a0 = None
def __init__(self):
self.a0 = []
self.a1 = []
self.a2 = []
def __str__(self):
return '[%s] (%s)-(%s)' %(IntArr2Str(self.a0, 4), FloatArr2Str(self.a1), FloatArr2Str(self.a2))
class GraphicsFont(object):
def __init__(self):
self.number = -1 # UInt32
self.ukn1 = 0 # UInt16[4]
self.ukn2 = [] # UInt8[2]
self.ukn3 = [] # UInt16[2]
self.name = [] # getLen32Text16
self.ukn4 = [] # Float32[2]
self.ukn5 = [] # UInt8[3]
def __str__(self):
return u"(%d) %s %r %r %r %r %r" %(self.number, self.name, self.ukn1, self.ukn2, self.ukn3, self.ukn4, self.ukn5)
class Lightning(object):
def __init__(self):
self.n1 = 0
self.c1 = None
self.c2 = None
self.c3 = None
self.a1 = []
self.a2 = []
def __str__(self):
return '%d: %s, %s, %s, [%s], [%s]' %(self.n1, self.c1, self.c2, self.c3, FloatArr2Str(self.a1), FloatArr2Str(self.a2))
class AbstractValue(object):
def __init__(self, x, factor, offset, unit):
self.x = x
self.factor = factor
self.offset = offset
self.unit = unit
def __str__(self): return u"%g%s" %(self.x / self.factor - self.offset, self.unit)
def __repr__(self): return self.toStandard()
def toStandard(self): return self.__str__()
def getNominalValue(self):
return self.x / self.factor + self.offset
def __sub__(self):
return self.__class__(-self.x, self.factor, self.unit)
def __neg__(self):
return self.__class__(-self.x, self.factor, self.unit)
def __sub__(self, other):
if (isinstance(other, AbstractValue)):
return self.__class__(self.x - other.x, self.factor, self.unit)
return self.__class__(self.x - other, self.factor, self.unit)
def __add__(self, other):
if (isinstance(other, AbstractValue)):
return self.__class__(self.x + other.x, self.factor, self.unit)
return self.__class__(self.x + other, self.factor, self.unit)
def __mul__(self, other):
if (isinstance(other, AbstractValue)):
return self.__class__(self.x * other.x, self.factor, self.unit)
return self.__class__(self.x * other, self.factor, self.unit)
class Length(AbstractValue):
def __init__(self, x, factor = 0.1, unit = 'mm'):
super(Length, self).__init__(x, factor, 0.0, unit)
def getMM(self): return self.x / 0.1
def toStandard(self): return '%g mm' %(self.x / 0.1)
class Angle(AbstractValue):
def __init__(self, a, factor, unit):
super(Angle, self).__init__(a, factor, 0.0, unit)
def getRAD(self): return self.x
def getGRAD(self): return degrees(self.x)
def toStandard(self): return '%g\xC2\xB0' %(self.getGRAD())
class Mass(AbstractValue):
def __init__(self, m, factor, unit):
super(Mass, self).__init__(m, factor, 0.0, unit)
def getGram(self): return self.x
def toStandard(self): return '%ggr' %(self.getGram())
class Time(AbstractValue):
def __init__(self, t, factor, unit):
super(Time, self).__init__(t, factor, 0.0, unit)
class Temperature(AbstractValue):
def __init__(self, t, factor, offset, unit):
super(Temperature, self).__init__(t, factor, offset, unit)
def toStandard(self): return '%g K' %(self.x)
class Velocity(AbstractValue):
def __init__(self, v, factor, unit):
super(Velocity, self).__init__(v, factor, 0.0, unit)
class Area(AbstractValue):
def __init__(self, a, factor, unit):
super(Area, self).__init__(a, factor, 0.0, unit)
class Volume(AbstractValue):
def __init__(self, v, factor, unit):
super(Volume, self).__init__(v, factor, 0.0, unit)
class Force(AbstractValue):
def __init__(self, F, factor, unit):
super(Force, self).__init__(F, factor, 0.0, unit)
class Pressure(AbstractValue):
def __init__(self, p, factor, unit):
super(Pressure, self).__init__(p, factor, 0.0, unit)
class Power(AbstractValue):
def __init__(self, p, factor, unit):
super(Power, self).__init__(p, factor, 0.0, unit)
class Work(AbstractValue):
def __init__(self, w, factor, unit):
super(Work, self).__init__(w, factor, 0.0, unit)
class Electrical(AbstractValue):
def __init__(self, l, factor, unit):
super(Electrical, self).__init__(l, factor, 0.0, unit)
class Luminosity(AbstractValue):
def __init__(self, l, unit):
super(Luminosity, self).__init__(l, 1.0, 0.0, unit)
class Substance(AbstractValue):
def __init__(self, s, unit):
super(Substance, self).__init__(s, 1.0, 0.0, unit)
class Scalar(AbstractValue):
def __init__(self, s):
super(Scalar, self).__init__(s, 1.0, 0.0, u'')
class Derived(AbstractValue):
def __init__(self, s, unit):
super(Derived, self).__init__(s, 1.0, 0.0, unit)
class DataNode(object):
def __init__(self, data):
## data must be an instance of AbstractData!
if (data):
assert isinstance(data, AbstractData), 'Data is not a AbstractData (%s)!' %(data.__class__.__name__)
self.data = data
self.isRef = False
self.children = []
@property
def typeName(self):
if (self.data): return self.data.typeName
return ''
@property
def index(self):
if (self.data): return self.data.index
return -1
@property
def handled(self):
if (self.data): return self.data.handled
return False
@handled.setter
def handled(self, handled):
if (self.data): self.data.handled = handled
@property
def valid(self):
if (self.data): return self.data.valid
return False
@valid.setter
def valid(self, valid):
if (self.data): self.data.valid = valid
@property
def geometry(self):
if (self.data): return self.data.geometry
return None
@property
def segment(self):
if (self.data): return self.data.segment
return None
def size(self):
return len(self.children)
def isLeaf(self):
return self.size() == 0
@property
def name(self):
if (self.data): return self.data.getName()
return None
@property
def sketchIndex(self):
if (self.data): return self.data.sketchIndex
return None
def setGeometry(self, geometry, index=1):
if (self.data):
self.data.geometry = geometry
self.data.sketchIndex = index
def append(self, node):
self.children.append(node)
node.parent = self
return node
@property
def next(self):
p = self.parent
if (p is None):
return None
for i, e in enumerate(p.children):
if (e.index == self.index):
if (i < p.size()-1):
return p.children[i+1]
return None
def getFirstChild(self, key):
for child in self.children:
if (child.typeName == key): return child
return None
def get(self, name):
if (self.data): return self.data.get(name)
return None
def set(self, name, value, cls = VAL_GUESS):
if (self.data): self.data.set(name, value, cls)
def getSegment(self):
if (self.data): return self.data.segment
return None
def getRefText(self): # return unicode
name = self.name
if (name):
return u"(%04X): %s '%s'" %(self.index, self.typeName, name)
return u"(%04X): %s" %(self.index, self.typeName)
def getUnitName(self): # return unicode
if (self.data): return self.data.getUnitName()
return u''
def getDerivedUnitName(self):
if (self.data): return self.data.getDerivedUnitName()
return u''
def __str__(self):
node = self.data
if (node is not None):
content = node.content
if (sys.version_info.major < 3) and (not isinstance(content, unicode)):
content = unicode(content)
name = node.name
if (name):
if (sys.version_info.major < 3) and (not isinstance(name, unicode)):
name = unicode(name)
return u"(%04X): %s '%s'%s" %(node.index, node.typeName, name, content)
return u'(%04X): %s%s' %(node.index, node.typeName, content)
return "<NONE>"
def __repr__(self):
return self.__str__()
def getSubTypeName(self):
node = self.data
if (node is not None):
return node.typeName
return None
def getFxAttributes(self):
attributes = {}
nxt = self
while (nxt):
nxt_old = nxt
nxt = nxt_old.get('next')
if (nxt):
attributes[nxt.typeName] = nxt
return attributes
def getParticipants(self):
attributes = self.getFxAttributes()
for atrName in attributes:
atribute = attributes[atrName]
participants = atribute.get('participants')
if (participants):
return participants
return []
class ParameterNode(DataNode):
def __init__(self, data):
super(ParameterNode, self).__init__(data)
def getValueRaw(self):
return self.get('valueNominal')
def getRefText(self): # return unicode
x = self.getValue()
try:
if (isinstance(x, Angle)): return u"(%04X): %s '%s'=%s" %(self.index, self.typeName, self.name, x)
if (isinstance(x, Length)): return u"(%04X): %s '%s'=%s" %(self.index, self.typeName, self.name, x)
return u"(%04X): %s '%s'=%s" %(self.index, self.typeName, self.name, x)
except Exception as e:
return u"(%04X): %s '%s'=%s - %s" %(self.index, self.typeName, self.name, x, e)
def getParameterFormula(self, parameterData, asText):
subFormula = ''
typeName = parameterData.typeName
if (typeName == 'ParameterValue'):
type = parameterData.get('type')
unitName = ''
if (asText):
unitName = parameterData.getUnitName()
if (len(unitName) > 0): unitName = ' ' + unitName
if (type == 0xFFFF):
subFormula = '%g%s' %(parameterData.get('value'), unitName)
else:
value = parameterData.get('value')
offset = parameterData.getUnitOffset()
factor = parameterData.getUnitFactor()
if (type == 0x0000): # Integer value!
subFormula = '%d%s' %(round((value / factor) - offset, 0), unitName)
else: # floating point value!
subFormula = '%g%s' %((value / factor) - offset, unitName)
elif (typeName == 'ParameterConstant'):
unitName = ''
if (asText):
unitName = parameterData.getUnitName()
if (len(unitName) > 0): unitName = ' ' + unitName
subFormula = '%s%s' %(parameterData.name, unitName)
elif (typeName == 'ParameterRef'):
target = parameterData.get('operand1')
if (asText):
subFormula = target.name
else:
subFormula = '%s_' %(target.name)
elif (typeName == 'ParameterFunction'):
function = parameterData.name
operandRefs = parameterData.get('operands')
subFormula = "%s(%s)" %(function, ';'.join(["%s" %(self.getParameterFormula(ref, asText)) for ref in operandRefs]))
if ((function in FunctionsNotSupported) and (not asText)):
nominalValue = self.getValue().getNominalValue()
logWarning(u"Function '%s' not supported in formula of '%s' (%s) - using nominal value %g!", function, self.name, subFormula, nominalValue)
subFormula = '%g' %(nominalValue)
elif (typeName == 'ParameterOperatorUnaryMinus'):
subFormula = '-' + self.getParameterFormula(parameterData.get('operand1'), asText)
elif (typeName == 'ParameterOperatorPowerIdent'):
subFormula = self.getParameterFormula(parameterData.get('operand1'), asText)
elif (typeName.startswith('ParameterOperator')):
operation = parameterData.name
operand1 = self.getParameterFormula(parameterData.get('operand1'), asText)
operand2 = self.getParameterFormula(parameterData.get('operand2'), asText)
subFormula = '(%s %s %s)' %(operand1, operation, operand2)
if ((not asText) and (operation == '%')):
subFormula = 'mod(%s, %s)' %(operand1, operand2)
else:
logError(u" Don't now how to build formula for %s: %s!", typeName, parameterData)
return subFormula
def getFormula(self, asText):
data = self.data
if (data):
refValue = data.get('value')
if (refValue):
if (asText):
return u"'" + self.getParameterFormula(refValue, asText)
try:
return u"=" + self.getParameterFormula(refValue, asText)
except BaseException as be:
# replace by nominal value and unit!
value = self.getValue()
logWarning(u" %s - replacing by nominal value %s!" %(be, value))
else:
value = self.getValue()
if (asText):
return u"'%s" %(value)
return u"=%s" %(value.getNominalValue())
return u''
def getValue(self):
x = self.getValueRaw()
#unitRef = self.get('unit')
#type = unitRef.get('type')
type = self.getUnitName()
# Length
if (type == 'km') : return Length(x, 100000.00000, type)
if (type == 'm') : return Length(x, 100.00000, type)
if (type == 'dm') : return Length(x, 10.00000, type)
if (type == 'cm') : return Length(x, 1.00000, type)
if (type == 'mm') : return Length(x, 0.10000, type)
if (type == u'\xB5m'): return Length(x, 0.00100, type)
if (type == 'in') : return Length(x, 2.54000, type)
if (type == 'ft') : return Length(x, 30.48000, type)
if (type == 'sm') : return Length(x, 185324.52180, type)
if (type == 'mil') : return Length(x, 0.00254, type)
# Mass
if (type == 'kg') : return Mass(x, 1.0000000, type)
if (type == 'g') : return Mass(x, 0.0010000, type)
if (type == 'slug') : return Mass(x, 14.5939000, type)
if (type == 'lb') : return Mass(x, 0.4535920, type)
if (type == 'oz') : return Mass(x, 0.0283495, type)
# Time
if (type == 's') : return Time(x, 1.0000000, type)
if (type == 'min') : return Time(x, 60.0000000, type)
if (type == 'h') : return Time(x, 3600.0000000, type)
# Temperature
if (type == 'K') : return Temperature(x, 1.0, 0.00, type)
if (type == u'\xB0C'): return Temperature(x, 1.0, 273.15, type)
if (type == u'\xB0F'): return Temperature(x, 5.0/9.0, 523.67, type)
# Angularity
if (type == 'rad') : return Angle(x, 1.0 , type)
if (type == u'\xb0') : return Angle(x, pi/180.0, type)
if (type == 'gon') : return Angle(x, pi/200.0, type)
# Velocity
if (type == 'm/s') : return Velocity(x, 100.0 , type)
# Area
if (type == 'mm^2') : return Area(x, 1.0 , type)
# Volume
if (type == 'l') : return Volume(x, 1.0 , type)
# Force
if (type == 'N') : return Force(x, 1.0 , type)
if (type == 'dyn') : return Force(x, 1.0 , type)
if (type == 'ozf') : return Force(x, 0.278013851 , type)
# Pressure
if (type == 'psi') : return Pressure(x, 6890.0 , type)
if (type == 'ksi') : return Pressure(x, 6890000.0 , type)
# Work
if (type == 'J') : return Work(x, 1.0 , type)
if (type == 'erg') : return Work(x, 1.0 , type)
if (type == 'Cal') : return Work(x, 4.184 , type)
# Electrical
if (type == 'A') : return Electrical(x, 1.0 , type)
# Luminosity
if (type == 'cd') : return Luminosity(x, type)
# Substance
if (type == 'mol') : return Substance(x, type)
# without Unit
if (type == '') : return Scalar(x) # parameter has no unit
derivedUnit = self.getDerivedUnitName()
if (derivedUnit is not None):
# Length
# Mass
# Temperature
# Angularity
if (derivedUnit == 'sr') : return Angle(x , 1.0, type)
# Velocity
if (derivedUnit == 'f/s') : return Velocity(x, 1.0, type)
if (derivedUnit == 'mil/h') : return Velocity(x, 1.0, type)
if (derivedUnit == '1/min') : return Velocity(x, 1.0, type)
# Area
if (derivedUnit == 'circ.mil') : return Area(x, 1.0, type)
# Volume
if (derivedUnit == 'gal') : return Volume(x, 1.0, type)
# Force
if (derivedUnit == 'lbf') : return Force(x, 1.0, type)
# Pressure
if (derivedUnit == 'Pa') : return Pressure(x, 1.0, type)
# Power
if (derivedUnit == 'W') : return Power(x, 1.0, type)
if (derivedUnit == 'hp') : return Power(x, 1.0, type)
# Work
if (derivedUnit == 'BTU') : return Work(x, 1.0, type)
# Electrical
if (derivedUnit == 'V') : return Electrical(x, 1.0, type)
if (derivedUnit == 'ohm') : return Electrical(x, 1.0, type)
if (derivedUnit == 'C') : return Electrical(x, 1.0, type)
if (derivedUnit == 'F') : return Electrical(x, 1.0, type)
if (derivedUnit == 'y') : return Electrical(x, 1.0, type)
if (derivedUnit == 'Gs') : return Electrical(x, 1.0, type)
if (derivedUnit == 'H') : return Electrical(x, 1.0, type)
if (derivedUnit == 'Hz') : return Electrical(x, 1.0, type)
if (derivedUnit == 'maxwell') : return Electrical(x, 1.0, type)
if (derivedUnit == 'mho') : return Electrical(x, 1.0, type)
if (derivedUnit == 'Oe') : return Electrical(x, 1.0, type)
if (derivedUnit == 'S') : return Electrical(x, 1.0, type)
if (derivedUnit == 'T') : return Electrical(x, 1.0, type)
if (derivedUnit == 'Wb') : return Electrical(x, 1.0, type)
# Luminosity
if (derivedUnit == 'lx') : return Luminosity(x, type)
if (derivedUnit == 'lm') : return Luminosity(x, type)
logWarning(u" found unsuppored derived unit - [%s] using [%s] instead!", derivedUnit, type)
else:
logWarning(u"WARNING: unknown unit (%04X): '%s' - [%s]", self.index, self.typeName, type)
return Derived(x, type)
class ParameterTextNode(DataNode):
def __init__(self, data):
super(ParameterTextNode, self).__init__(data)
def getValueRaw(self):
return self.get('value')
def getUnitName(self): # return unicode
return u''
def getRefText(self): # return unicode
return u"(%04X): %s '%s'='%s'" %(self.index, self.typeName, self.name, self.getValue())
def getValue(self):
x = self.getValueRaw()
return x
class ParameterValue(object):
def __init__(self, value):
self.value = value
def getValue(self):
return self.value
def getName(self):
return ''
def getTypeName(self):
return 'Parameter'
class EnumNode(DataNode):
def __init__(self, data):
super(EnumNode, self).__init__(data)
def getValueText(self):
enum = self.get('Values')
value = self.get('value')
if (type(enum) is list):
if (value < len(enum)):
return "%s" % enum[value]
return value
assert (type(enum) is dict), "Expected %s to contain dict or list as enum values!"
if (value in enum.keys()):
return "%s" % enum[value]
return value
def getRefText(self): # return unicode
return "(%04X): %s='%s'" %(self.index, self.get('Enum'), self.getValueText())
def __str__(self):
node = self.data
name = self.get('Enum')
return '(%04X): %s %s%s' %(node.index, node.typeName, name, node.content)
class DirectionNode(DataNode):
def __init__(self, data):
super(DirectionNode, self).__init__(data)
def getDirection(self):
dir = self.get('dir')
if (dir):
return dir
face = self.get('face')
# if (face):
return None