-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdxfImportObjects.py
1340 lines (1034 loc) · 40.1 KB
/
dxfImportObjects.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
"""This module provides wrapper objects for dxf entities.
The wrappers expect a "dxf object" as input. The dxf object is
an object with a type and a data attribute. Type is a lowercase
string matching the 0 code of a dxf entity. Data is a list containing
dxf objects or lists of [code, data] pairs.
This module is not general, and is only for dxf import.
"""
# --------------------------------------------------------------------------
# DXF Import Objects v0.8 by Ed Blake (AKA Kitsu)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
from math import *
# from Stani's dxf writer v1.1 (c)www.stani.be (GPL)
#---color values
BYBLOCK=0
BYLAYER=256
#---block-type flags (bit coded values, may be combined):
ANONYMOUS =1 # This is an anonymous block generated by hatching, associative dimensioning, other internal operations, or an application
NON_CONSTANT_ATTRIBUTES =2 # This block has non-constant attribute definitions (this bit is not set if the block has any attribute definitions that are constant, or has no attribute definitions at all)
XREF =4 # This block is an external reference (xref)
XREF_OVERLAY =8 # This block is an xref overlay
EXTERNAL =16 # This block is externally dependent
RESOLVED =32 # This is a resolved external reference, or dependent of an external reference (ignored on input)
REFERENCED =64 # This definition is a referenced external reference (ignored on input)
#---mtext flags
#attachment point
TOP_LEFT = 1
TOP_CENTER = 2
TOP_RIGHT = 3
MIDDLE_LEFT = 4
MIDDLE_CENTER = 5
MIDDLE_RIGHT = 6
BOTTOM_LEFT = 7
BOTTOM_CENTER = 8
BOTTOM_RIGHT = 9
#drawing direction
LEFT_RIGHT = 1
TOP_BOTTOM = 3
BY_STYLE = 5 #the flow direction is inherited from the associated text style
#line spacing style (optional):
AT_LEAST = 1 #taller characters will override
EXACT = 2 #taller characters will not override
#---polyline flags
CLOSED =1 # This is a closed polyline (or a polygon mesh closed in the M direction)
CURVE_FIT =2 # Curve-fit vertices have been added
SPLINE_FIT =4 # Spline-fit vertices have been added
POLYLINE_3D =8 # This is a 3D polyline
POLYGON_MESH =16 # This is a 3D polygon mesh
CLOSED_N =32 # The polygon mesh is closed in the N direction
POLYFACE_MESH =64 # The polyline is a polyface mesh
CONTINOUS_LINETYPE_PATTERN =128 # The linetype pattern is generated continuously around the vertices of this polyline
#---text flags
#horizontal
LEFT = 0
CENTER = 1
RIGHT = 2
ALIGNED = 3 #if vertical alignment = 0
MIDDLE = 4 #if vertical alignment = 0
FIT = 5 #if vertical alignment = 0
#vertical
BASELINE = 0
BOTTOM = 1
MIDDLE = 2
TOP = 3
class Object:
"""Empty container class for dxf objects"""
def __init__(self, _type=''):
"""_type expects a string value."""
self.type = _type
self.name = ''
self.data = []
def __str__(self):
if self.name:
return self.name
else:
return self.type
def __repr__(self):
return str(self.data)
def get_type(self, kind=''):
"""Despite the name, this method actually returns all objects of type 'kind' from self.data."""
if type:
objects = []
for item in self.data:
if type(item) != list and item.type == kind:
# we want this type of object
objects.append(item)
elif type(item) == list and item[0] == kind:
# we want this type of data
objects.append(item[1])
return objects
class Layer:
"""Class for objects representing dxf layers."""
def __init__(self, obj):
"""Expects an entity object of type line as input."""
self.type = obj.type
self.data = obj.data[:]
self.name = obj.get_type(2)[0]
try:
self.color = obj.get_type(62)[0]
except:
# fix for badly formatted layers that have no color!! bad, bad layer!
self.color = 7
self.flags = obj.get_type(70)[0]
self.frozen = self.flags&1
def __repr__(self):
return "%s: name - %s, color - %s" %(self.__class__.__name__, self.name, self.color)
class Line:
"""Class for objects representing dxf lines."""
def __init__(self, obj):
"""Expects an entity object of type line as input."""
if not obj.type == 'line':
raise TypeError("Wrong type %s for line object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.points = self.get_points(obj.data)
def get_points(self, data):
"""Gets start and end points for a line type object.
Lines have a fixed number of points (two) and fixed codes for each value.
"""
# start x, y, z and end x, y, z = 0
sx, sy, sz, ex, ey, ez = 0, 0, 0, 0, 0, 0
for item in data:
if item[0] == 10: # 10 = x
sx = item[1]
elif item[0] == 20: # 20 = y
sy = item[1]
elif item[0] == 30: # 30 = z
sz = item[1]
elif item[0] == 11: # 11 = x
ex = item[1]
elif item[0] == 21: # 21 = y
ey = item[1]
elif item[0] == 31: # 31 = z
ez = item[1]
return [[sx, sy, sz], [ex, ey, ez]]
def __repr__(self):
return "%s: layer - %s, points - %s" %(self.__class__.__name__, self.layer, self.points)
class LWpolyline:
"""Class for objects representing dxf LWpolylines."""
def __init__(self, obj):
"""Expects an entity object of type lwpolyline as input."""
if not obj.type == 'lwpolyline':
raise TypeError("Wrong type %s for polyline object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.num_points = obj.get_type(90)[0]
# optional data (with defaults)
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
self.elevation = obj.get_type(38)
if self.elevation:
self.elevation = self.elevation[0]
else:
self.elevation = 0
self.flags = obj.get_type(70)
if self.flags:
self.flags = self.flags[0]
else:
self.flags = 0
self.closed = self.flags&1 # byte coded, 1 = closed, 128 = plinegen
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.points = self.get_points(obj.data)
self.extrusion = self.get_extrusion(obj.data)
def get_points(self, data):
"""Gets points for a polyline type object.
Polylines have no fixed number of verts, and
each vert can have a number of properties.
Verts should be coded as
10:xvalue
20:yvalue
40:startwidth or 0
41:endwidth or 0
42:bulge or 0
for each vert
"""
num = self.num_points
point = None
points = []
for item in data:
if item[0] == 10: # 10 = x
if point:
points.append(point)
point = Vertex()
point.x = item[1]
elif item[0] == 20: # 20 = y
point.y = item[1]
elif item[0] == 40: # 40 = start width
point.swidth = item[1]
elif item[0] == 41: # 41 = end width
point.ewidth = item[1]
elif item[0] == 42: # 42 = bulge
point.bulge = item[1]
points.append(point)
return points
def get_extrusion(self, data):
"""Find the axis of extrusion.
Used to get the objects Object Coordinate System (ocs).
"""
vec = [0,0,1]
for item in data:
if item[0] == 210: # 210 = x
vec[0] = item[1]
elif item[0] == 220: # 220 = y
vec[1] = item[1]
elif item[0] == 230: # 230 = z
vec[2] = item[1]
return vec
def __repr__(self):
return "%s: layer - %s, points - %s" %(self.__class__.__name__, self.layer, self.points)
class Polyline:
"""Class for objects representing dxf LWpolylines."""
def __init__(self, obj):
"""Expects an entity object of type polyline as input."""
if not obj.type == 'polyline':
raise TypeError("Wrong type %s for polyline object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
self.points = []
# optional data (with defaults)
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
self.elevation = obj.get_type(30)
if self.elevation:
self.elevation = self.elevation[0]
else:
self.elevation = 0
self.flags = obj.get_type(70)
if self.flags:
self.flags = self.flags[0]
else:
self.flags = 0
self.closed = self.flags&1 # byte coded, 1 = closed, 128 = plinegen
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.extrusion = self.get_extrusion(obj.data)
def get_extrusion(self, data):
"""Find the axis of extrusion.
Used to get the objects Object Coordinate System (ocs).
"""
vec = [0,0,1]
for item in data:
if item[0] == 210: # 210 = x
vec[0] = item[1]
elif item[0] == 220: # 220 = y
vec[1] = item[1]
elif item[0] == 230: # 230 = z
vec[2] = item[1]
return vec
def __repr__(self):
return "%s: layer - %s, points - %s" %(self.__class__.__name__, self.layer, self.points)
class Vertex(object):
"""Generic vertex object used by polylines (and maybe others)."""
def __init__(self, obj=None):
"""Initializes vertex data.
The optional obj arg is an entity object of type vertex.
"""
self.loc = [0,0,0]
self.bulge = 0
self.swidth = 0
self.ewidth = 0
self.flags = 0
if obj is not None:
if not obj.type == 'vertex':
raise TypeError("Wrong type %s for vertex object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
self.get_props(obj.data)
def get_props(self, data):
"""Gets coords for a vertex type object.
Each vert can have a number of properties.
Verts should be coded as
10:xvalue
20:yvalue
40:startwidth or 0
41:endwidth or 0
42:bulge or 0
"""
for item in data:
if item[0] == 10: # 10 = x
self.x = item[1]
elif item[0] == 20: # 20 = y
self.y = item[1]
elif item[0] == 30: # 30 = z
self.z = item[1]
elif item[0] == 40: # 40 = start width
self.swidth = item[1]
elif item[0] == 41: # 41 = end width
self.ewidth = item[1]
elif item[0] == 42: # 42 = bulge
self.bulge = item[1]
elif item[0] == 70: # 70 = vert flags
self.flags = item[1]
def __len__(self):
return 3
def __getitem__(self, key):
return self.loc[key]
def __setitem__(self, key, value):
if key in [0,1,2]:
self.loc[key]
def __iter__(self):
return self.loc.__iter__()
def __str__(self):
return str(self.loc)
def __repr__(self):
return "Vertex %s, swidth=%s, ewidth=%s, bulge=%s" %(self.loc, self.swidth, self.ewidth, self.bulge)
def getx(self):
return self.loc[0]
def setx(self, value):
self.loc[0] = value
x = property(getx, setx)
def gety(self):
return self.loc[1]
def sety(self, value):
self.loc[1] = value
y = property(gety, sety)
def getz(self):
return self.loc[2]
def setz(self, value):
self.loc[2] = value
z = property(getz, setz)
class Text:
"""Class for objects representing dxf Text."""
def __init__(self, obj):
"""Expects an entity object of type text as input."""
if not obj.type == 'text':
raise TypeError("Wrong type %s for text object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.height = obj.get_type(40)[0]
self.value = obj.get_type(1)[0] # The text string value
# optional data (with defaults)
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
self.rotation = obj.get_type(50) # radians?
if not self.rotation:
self.rotation = 0
else:
self.rotation = self.rotation[0]
self.width_factor = obj.get_type(41) # Scaling factor along local x axis
if not self.width_factor:
self.width_factor = 1
else:
self.width_factor = self.width_factor[0]
self.oblique = obj.get_type(51) # skew in degrees -90 <= oblique <= 90
if not self.oblique:
self.oblique = 0
else:
self.oblique = self.oblique[0]
self.halignment = obj.get_type(72) # horiz. alignment
if not self.halignment: # 0=left, 1=center, 2=right, 3=aligned, 4=middle, 5=fit
self.halignment = 0
else:
self.halignment = self.halignment[0]
self.valignment = obj.get_type(73) # vert. alignment
if not self.valignment: # 0=baseline, 1=bottom, 2=middle, 3=top
self.valignment = 0
else:
self.valignment = self.valignment[0]
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data, self.halignment, self.valignment)
self.extrusion = self.get_extrusion(obj.data)
def get_loc(self, data, halign, valign):
"""Gets adjusted location for text type objects.
If group 72 and/or 73 values are nonzero then the first alignment point values
are ignored and AutoCAD calculates new values based on the second alignment
point and the length and height of the text string itself (after applying the
text style). If the 72 and 73 values are zero or missing, then the second
alignment point is meaningless.
I don't know how to calc text size...
"""
# bottom left x, y, z and justification x, y, z = 0
x, y, z, jx, jy, jz = 0, 0, 0, 0, 0, 0
for item in data:
if item[0] == 10: # 10 = x
x = item[1]
elif item[0] == 20: # 20 = y
y = item[1]
elif item[0] == 30: # 30 = z
z = item[1]
elif item[0] == 11: # 11 = x
jx = item[1]
elif item[0] == 21: # 21 = y
jy = item[1]
elif item[0] == 31: # 31 = z
jz = item[1]
if halign or valign:
x, y, z = jx, jy, jz
return [x, y, z]
def get_extrusion(self, data):
"""Find the axis of extrusion.
Used to get the objects Object Coordinate System (ocs).
"""
vec = [0,0,1]
for item in data:
if item[0] == 210: # 210 = x
vec[0] = item[1]
elif item[0] == 220: # 220 = y
vec[1] = item[1]
elif item[0] == 230: # 230 = z
vec[2] = item[1]
return vec
def __repr__(self):
return "%s: layer - %s, value - %s" %(self.__class__.__name__, self.layer, self.value)
class Mtext:
"""Class for objects representing dxf Mtext."""
def __init__(self, obj):
"""Expects an entity object of type mtext as input."""
if not obj.type == 'mtext':
raise TypeError("Wrong type %s for mtext object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.height = obj.get_type(40)[0]
self.width = obj.get_type(41)[0]
self.alignment = obj.get_type(71)[0] # alignment 1=TL, 2=TC, 3=TR, 4=ML, 5=MC, 6=MR, 7=BL, 8=BC, 9=BR
self.value = self.get_text(obj.data) # The text string value
# optional data (with defaults)
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
self.rotation = obj.get_type(50) # radians
if not self.rotation:
self.rotation = 0
else:
self.rotation = self.rotation[0]
self.width_factor = obj.get_type(42) # Scaling factor along local x axis
if not self.width_factor:
self.width_factor = 1
else:
self.width_factor = self.width_factor[0]
self.line_space = obj.get_type(44) # percentage of default
if not self.line_space:
self.line_space = 1
else:
self.line_space = self.line_space[0]
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.extrusion = self.get_extrusion(obj.data)
def get_text(self, data):
"""Reconstructs mtext data from dxf codes."""
primary = ''
secondary = []
for item in data:
if item[0] == 1: # There should be only one primary...
primary = item[1]
elif item[0] == 3: # There may be any number of extra strings (in order)
secondary.append(item[1])
if not primary:
#raise ValueError("Empty Mtext Object!")
string = "Empty Mtext Object!"
if not secondary:
string = primary.replace(r'\P', '\n')
else:
string = ''.join(secondary)+primary
string = string.replace(r'\P', '\n')
return string
def get_loc(self, data):
"""Gets location for a mtext type objects.
Mtext objects have only one point indicating location.
"""
loc = [0,0,0]
for item in data:
if item[0] == 10: # 10 = x
loc[0] = item[1]
elif item[0] == 20: # 20 = y
loc[1] = item[1]
elif item[0] == 30: # 30 = z
loc[2] = item[1]
return loc
def get_extrusion(self, data):
"""Find the axis of extrusion.
Used to get the objects Object Coordinate System (ocs).
"""
vec = [0,0,1]
for item in data:
if item[0] == 210: # 210 = x
vec[0] = item[1]
elif item[0] == 220: # 220 = y
vec[1] = item[1]
elif item[0] == 230: # 230 = z
vec[2] = item[1]
return vec
def __repr__(self):
return "%s: layer - %s, value - %s" %(self.__class__.__name__, self.layer, self.value)
class Circle:
"""Class for objects representing dxf Circles."""
def __init__(self, obj):
"""Expects an entity object of type circle as input."""
if not obj.type == 'circle':
raise TypeError("Wrong type %s for circle object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.radius = obj.get_type(40)[0]
# optional data (with defaults)
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.extrusion = self.get_extrusion(obj.data)
def get_loc(self, data):
"""Gets the center location for circle type objects.
Circles have a single coord location.
"""
loc = [0, 0, 0]
for item in data:
if item[0] == 10: # 10 = x
loc[0] = item[1]
elif item[0] == 20: # 20 = y
loc[1] = item[1]
elif item[0] == 30: # 30 = z
loc[2] = item[1]
return loc
def get_extrusion(self, data):
"""Find the axis of extrusion.
Used to get the objects Object Coordinate System (ocs).
"""
vec = [0,0,1]
for item in data:
if item[0] == 210: # 210 = x
vec[0] = item[1]
elif item[0] == 220: # 220 = y
vec[1] = item[1]
elif item[0] == 230: # 230 = z
vec[2] = item[1]
return vec
def __repr__(self):
return "%s: layer - %s, radius - %s" %(self.__class__.__name__, self.layer, self.radius)
class Arc:
"""Class for objects representing dxf arcs."""
def __init__(self, obj):
"""Expects an entity object of type arc as input."""
if not obj.type == 'arc':
raise TypeError("Wrong type %s for arc object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.radius = obj.get_type(40)[0]
self.start_angle = obj.get_type(50)[0]
self.end_angle = obj.get_type(51)[0]
# optional data (with defaults)
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
self.extrusion = self.get_extrusion(obj.data)
def get_loc(self, data):
"""Gets the center location for arc type objects.
Arcs have a single coord location.
"""
loc = [0, 0, 0]
for item in data:
if item[0] == 10: # 10 = x
loc[0] = item[1]
elif item[0] == 20: # 20 = y
loc[1] = item[1]
elif item[0] == 30: # 30 = z
loc[2] = item[1]
return loc
def get_extrusion(self, data):
"""Find the axis of extrusion.
Used to get the objects Object Coordinate System (ocs).
"""
vec = [0,0,1]
for item in data:
if item[0] == 210: # 210 = x
vec[0] = item[1]
elif item[0] == 220: # 220 = y
vec[1] = item[1]
elif item[0] == 230: # 230 = z
vec[2] = item[1]
return vec
def __repr__(self):
return "%s: layer - %s, radius - %s" %(self.__class__.__name__, self.layer, self.radius)
class BlockRecord:
"""Class for objects representing dxf block_records."""
def __init__(self, obj):
"""Expects an entity object of type block_record as input."""
if not obj.type == 'block_record':
raise TypeError("Wrong type %s for block_record object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.name = obj.get_type(2)[0]
# optional data (with defaults)
self.insertion_units = obj.get_type(70)
if not self.insertion_units:
self.insertion_units = None
else:
self.insertion_units = self.insertion_units[0]
self.insert_units = obj.get_type(1070)
if not self.insert_units:
self.insert_units = None
else:
self.insert_units = self.insert_units[0]
def __repr__(self):
return "%s: name - %s, insert units - %s" %(self.__class__.__name__, self.name, self.insertion_units)
class Block:
"""Class for objects representing dxf blocks."""
def __init__(self, obj):
"""Expects an entity object of type block as input."""
if not obj.type == 'block':
raise TypeError("Wrong type %s for block object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
if obj.get_type(70):
self.flags = obj.get_type(70)[0]
else:
self.flags = 0
self.entities = Object('block_contents')
self.entities.data = objectify([ent for ent in obj.data if type(ent) != list])
# optional data (with defaults)
self.name = obj.get_type(3)
if self.name:
self.name = self.name[0]
else:
self.name = obj.get_type(2)
if self.name:
self.name = self.name[0]
else:
self.name = 'blank'
self.path = obj.get_type(1)
if self.path:
self.path = self.path[0]
else:
self.path = ''
self.discription = obj.get_type(4)
if self.discription:
self.discription = self.discription[0]
else:
self.discription = ''
discard, self.layer, discard_index = get_layer(obj.data)
del obj.data[discard_index]
self.loc = self.get_loc(obj.data)
def get_loc(self, data):
"""Gets the insert point of the block."""
loc = [0, 0, 0]
for item in data:
if type(item) != list:
continue
if item[0] == 10: # 10 = x
loc[0] = item[1]
elif item[0] == 20: # 20 = y
loc[1] = item[1]
elif item[0] == 30: # 30 = z
loc[2] = item[1]
return loc
def __repr__(self):
return "%s: name - %s, description - %s, xref-path - %s" %(self.__class__.__name__, self.name, self.discription, self.path)
class Insert:
"""Class for objects representing dxf inserts."""
def __init__(self, obj):
"""Expects an entity object of type insert as input."""
if not obj.type == 'insert':
raise TypeError("Wrong type %s for insert object!" %obj.type)
self.type = obj.type
self.data = obj.data[:]
# required data
self.block = obj.get_type(2)[0]
# optional data (with defaults)
self.rotation = obj.get_type(50)
if self.rotation:
self.rotation = self.rotation[0]
else:
self.rotation = 0
self.space = obj.get_type(67)
if self.space:
self.space = self.space[0]
else:
self.space = 0
self.color_index = obj.get_type(62)
if self.color_index:
self.color_index = self.color_index[0]
else:
self.color_index = BYLAYER
discard, self.layer, discard_index = get_layer(obj.data)