-
Notifications
You must be signed in to change notification settings - Fork 6
/
drawing.py
1322 lines (1122 loc) · 43.4 KB
/
drawing.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
#!/usr/bin/env python
# coding: utf8
"""
Read/Write and handle vector graphics in .dxf, .svg and .pdf formats
:requires:
* `svg.path <http://pypi.python.org/pypisvg.path/>`_ for svg input
* `matplotlib <http://pypi.python.org/pypi/matplotlib/>`_ for bitmap + svg and pdf output
* `dxfwrite <http://pypi.python.org/pypi/dxfwrite/>`_ for dxf output
:optional:
* `dxfgrabber <http://pypi.python.org/pypi/dxfgrabber/>`_ for dxf input
* `pdfminer.six <http://pypi.python.org/pypi/pdfminer.six/>`_ for pdf input
"""
__author__ = "Philippe Guglielmetti"
__copyright__ = "Copyright 2014, Philippe Guglielmetti"
__credits__ = ['http://effbot.org/imagingbook/imagedraw.htm', 'http://images.autodesk.com/adsk/files/acad_dxf0.pdf']
__license__ = "LGPL"
from math import radians, degrees, tan, atan
import logging, base64, io
from .itertools2 import split, filter2, subdict
from .geom import *
from .plot import Plot
from .colors import color_to_aci, aci_to_color
from .interval import Box
from .math2 import rint, isclose
from . import plot #set matplotlib backend
import matplotlib.pyplot as plt # after import .plot
def Trans(scale=1, offset=None, rotation=None):
"""
:param scale: float or (scalex,scaley) tuple of scale factor
:param offset: :class:`~geom.Vector3`
:param rotation: float angle in degrees
:return: :class:`~geom.Matrix3` of generalized scale+offset+rotation
"""
res = Matrix3()
if rotation:
res = res.rotate(radians(rotation))
if scale != 1:
try:
res = res.scale(scale[0],scale[1])
except:
res = res.scale(scale)
if offset:
res = res.translate(offset)
return res
identity=Trans()
class BBox(Box):
"""bounding box"""
def __init__(self, p1=None, p2=None):
"""
:param pt1: :class:`~geom.Point2` first corner (any)
:param pt2: :class:`~geom.Point2` opposite corner (any)
"""
super(BBox,self).__init__(2)
if p1 is not None : self+=p1
if p2 is not None : self+=p2
@property
def xmin(self): return self[0].start
@property
def ymin(self): return self[1].start
@property
def xmax(self): return self[0].end
@property
def ymax(self): return self[1].end
@property
def xmed(self): return (self.xmin+self.xmax)/2
@property
def ymed(self): return (self.ymin+self.ymax)/2
@property
def width(self): return self[0].size
@property
def height(self): return self[1].size
@property
def area(self): return self.width*self.height
def __contains__(self, other):
""":return: True if other lies in bounding box."""
if isinstance(other,(Box,tuple)):
return super(BBox,self).__contains__(other)
#process simple geom entities without building Box objects
if isinstance(other,Point2):
return super(BBox,self).__contains__(other.xy)
if isinstance(other,Segment2):
return (super(BBox,self).__contains__(other.p1.xy)
and super(BBox,self).__contains__(other.p2.xy))
#for more complex entites, get the box
if isinstance(other,Entity):
return super(BBox,self).__contains__(other.bbox())
#suppose other is an iterable
return all(o in self for o in other)
def __iadd__(self, pt):
"""
enlarge box if required to contain specified point
:param pt1: :class:`geom.Point2` point to add
"""
if pt is None:
return self
elif isinstance(pt,Point2):
self+= pt.xy
else:
super(BBox,self).__iadd__(pt)
return self
def __call__(self):
""":return: list of flatten corners"""
return list(self.start)+list(self.end)
def size(self):
""":return: :class:`geom.Vector2` with xy sizes"""
return Vector2(super(BBox,self).size)
def center(self):
""":return: Pt center"""
return Point2(super(BBox,self).center)
def trans(self, trans):
"""
:param trans: Xform
:return: :class:`BBox` = self transformed by trans
"""
res = BBox()
for i in range(4): # add all corners as they matter if we rotate the box
res+=trans(Point2(self.corner(i)))
return res
class Entity(plot.Plot):
"""Base class for all drawing entities"""
color='black' # by default
def setattr(self, **kwargs):
""" set (graphic) attributes to entity
:param kwargs: dict of attributes copied to entity
"""
for key in kwargs:
setattr(self, key, kwargs[key])
@property
def start(self):
return self.p
@property
def end(self):
try:
return self.p2
except AttributeError: #probably a Circle
return self.p
def __repr__(self):
return '%s(%s,%s)' % (self.__class__.__name__,self.start,self.end)
@property
def center(self):
return self.bbox().center
def bbox(self):
"""
:return: :class:`BBox` bounding box of Entity"""
if isinstance(self,Point2):
return BBox(self,self)
elif isinstance(self,Segment2):
return BBox(self.start,self.end)
elif isinstance(self,Arc2):
#http://stackoverflow.com/questions/1336663/2d-bounding-box-of-a-sector
res=BBox(self.p, self.p2)
p=self.c+Vector2(self.r,0)
if p in self : res+=p
p=self.c+Vector2(-self.r,0)
if p in self : res+=p
p=self.c+Vector2(0,self.r)
if p in self : res+=p
p=self.c+Vector2(0,-self.r)
if p in self : res+=p
return res
elif isinstance(self,Circle): #must be after Arc2 case since Arc2 is a Circle too
rr = Vector2(self.r, self.r)
return BBox(self.c - rr, self.c + rr)
else: # suppose Polygon2 or other iterable shape
res = BBox()
for p in self.p:
res+=p
return res
raise NotImplementedError()
def isclosed(self):
return self.end==self.start
def isline(self):
return isinstance(self,Line2) #or derived
def isvertical(self,tol=0.01):
return self.isline() and isclose(self.start.x,self.end.x,tol)
def ishorizontal(self,tol=0.01):
return self.isline() and isclose(self.start.y,self.end.y,tol)
def _dxf_attr(self,attr):
"""
:return: dict of attributes for dxf.entity
"""
res={}
res['color']=color_to_aci(attr.get('color',self.color))
if 'layer' in attr:
res['layer']=attr.get('layer')
else:
try:
res['layer']=self.layer
except:
pass
return res
def to_dxf(self,**attr):
"""
:param attr: dict of attributes passed to the dxf entity, overriding those defined in self
:return: dxf entity
"""
import dxfwrite.entities as dxf
attr=self._dxf_attr(attr)
if isinstance(self,Segment2):
return dxf.Line(start=self.start.xy,end=self.end.xy, **attr)
elif isinstance(self,Arc2):
center=self.c.xy
v=Vector2(Point2(self.start)-self.c)
startangle=degrees(v.angle())
v=Vector2(Point2(self.end)-self.c)
endangle=degrees(v.angle())
if self.dir<0: #DXF handling of ARC direction is somewhat exotic ...
startangle,endangle = 180-startangle,180-endangle #start/end were already swapped
extrusion_direction=(0,0,-1)
center=(-center[0],center[1]) #negative extrusion on Z causes Y axis symmetry... strange...
else:
extrusion_direction=None #default
return dxf.Arc(radius=self.r, center=center, startangle=startangle, endangle=endangle, extrusion_direction=extrusion_direction, **attr)
elif isinstance(self,Circle):
return dxf.Circle(center=self.c.xy, radius=self.r, **attr)
elif isinstance(self,Spline):
from dxfwrite import curves
spline=curves.Bezier(**attr)
spline.start(self.start.xy,(self.p[1]-self.start).xy)
spline.append(self.end.xy,(self.p[2]-self.end).xy)
return spline
return curves.Spline(self.xy, **attr) # builds a POLYLINE3D (???) with 100 segments ...
else:
raise NotImplementedError
@staticmethod
def from_svg(path,color):
"""
:param path: svg path
:return: Entity of correct subtype
"""
return Chain.from_svg(path,color)
@staticmethod
def from_pdf(path,trans,color):
"""
:param path: pdf path
:return: Entity of correct subtype
"""
return Chain.from_pdf(path,trans,color)
@staticmethod
def from_dxf(e,mat3):
"""
:param e: dxf.entity
:param mat3: Matrix3 transform
:return: Entity of correct subtype
"""
if e.dxftype=='LINE':
start=Point2(e.start[:2])
end=Point2(e.end[:2])
res=Segment2(start,end)
elif e.dxftype == 'ARC':
c=Point2(e.center[:2])
startangle=radians(e.start_angle)
start=c+Polar(e.radius,startangle)
endangle=radians(e.end_angle)
end=c+Polar(e.radius,endangle)
res=Arc2(c,start,end)
elif e.dxftype == 'CIRCLE':
res=Circle(Point2(e.center[:2]), e.radius)
elif e.dxftype == 'POLYLINE':
res=Chain.from_dxf(e,mat3)
elif e.dxftype == 'LWPOLYLINE':
res=Chain.from_dxf(e,mat3)
elif e.dxftype == 'SOLID':
return None #TODO: implement
elif e.dxftype == 'POINT':
return None #TODO: implement
elif e.dxftype == 'TEXT':
p=Point2(e.insert[:2])
res=Text(e.text,p,size=e.height,rotation=e.rotation)
else:
logging.warning('unhandled entity type %s'%e.dxftype)
return None
res._apply_transform(mat3)
if res.length==0:
res=Point2(res.p)
res.dxf=e #keep link to source entity
res.color=aci_to_color(e.color)
res.layer=e.layer
return res
def patches(self, **kwargs):
"""
:return: list of (a single) :class:`~matplotlib.patches.Patch` corresponding to entity
:note: this is the only method that needs to be overridden in descendants for draw, render and IPython _repr_xxx_ to work
"""
import matplotlib.patches as patches
from matplotlib.path import Path
kwargs.setdefault('color',self.color)
try:
kwargs.setdefault('linewidth',self.width)
except:
pass
try:
kwargs.setdefault('fill',self.fill)
except:
pass
if isinstance(self,Segment2):
path = Path((self.start.xy,self.end.xy),[Path.MOVETO, Path.LINETO])
return [patches.PathPatch(path, **kwargs)]
if isinstance(self,Arc2):
theta1=degrees(self.a)
theta2=degrees(self.b)
if self.dir<1 : #swap
theta1,theta2=theta2,theta1
d=self.r*2
return [patches.Arc(self.c.xy,d,d,theta1=theta1,theta2=theta2,**kwargs)]
#entities below may be filled, so let's handle the color first
color=kwargs.pop('color')
kwargs.setdefault('edgecolor',color)
kwargs.setdefault('fill',isinstance(self,Point2))
if type(kwargs['fill']) is not bool: #assume it's the fill color
kwargs.setdefault('facecolor',kwargs['fill'])
kwargs['fill']=True
kwargs.setdefault('facecolor',color)
if isinstance(self,Point2):
try:
ms=self.width
except:
ms=0.01
kwargs.setdefault('clip_on',False)
return [patches.Circle(self.xy,ms,**kwargs)]
if isinstance(self,Spline):
path = Path(self.xy, [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4])
return [patches.PathPatch(path, **kwargs)]
if isinstance(self,Ellipse): #must be after Arc2 and Ellipse
return [patches.Ellipse(self.c.xy,2*self.r,2*self.r2,**kwargs)]
if isinstance(self,Circle): #must be after Arc2 and Ellipse
return [patches.Circle(self.c.xy,self.r,**kwargs)]
if isinstance(self,Polygon):
return [patches.Polygon(self.xy,**kwargs)]
raise NotImplementedError
@staticmethod
def figure(box, **kwargs):
"""
:param box: :class:`drawing.BBox` bounds and clipping box
:param kwargs: parameters passed to `~matplotlib.pyplot.figure`
:return: matplotlib axis suitable for drawing
"""
fig=plt.figure(**kwargs)
# ax = fig.add_subplot(111) # unneeded
# TODO: find why this doesn't work:
# plt.gca().set_position([box.xmin,box.ymin,box.width, box.height])
#for some reason we have to plot something in order to size the window
#TODO: find a better way... (found no other way top do it...)
plt.plot((box.xmin,box.xmax),(box.ymin,box.ymax), alpha=0) #draw a transparent diagonal to size everything
plt.axis('equal')
import pylab
pylab.axis('off') # turn off axis
return fig
def draw(self, fig=None, **kwargs):
""" draw entities
:param fig: matplotlib figure where to draw. figure(g) is called if missing
:return: fig,patch
"""
if fig is None:
if not 'box' in kwargs:
kwargs['box']=self.bbox()
fig=self.figure(**kwargs)
args=subdict(kwargs,('color','linewidth'))
p=self.patches(**args) #some of which might be Annotations, which aren't patches but Artists...
from matplotlib.patches import Patch
patches,artists=filter2(p,lambda e:isinstance(e,Patch))
if patches:
from matplotlib.collections import PatchCollection
plt.gca().add_collection(PatchCollection(patches,match_original=True))
if artists:
for e in artists:
plt.gca().add_artist(e)
plt.draw()
return fig #, p
def render(self,fmt,**kwargs):
""" render graph to bitmap stream
:return: matplotlib figure as a byte stream in specified format
"""
transparent=kwargs.pop('transparent',True)
facecolor=kwargs.pop('facecolor', kwargs.pop('background','white'))
fig=self.draw(facecolor=facecolor, **kwargs)
buffer = io.BytesIO()
fig.savefig(
buffer,
format=fmt,
transparent=transparent,
facecolor=fig.get_facecolor(),
)
res=buffer.getvalue()
plt.close(fig)
return res
#Python is FANTASTIC ! here we set Entity as base class of some classes previously defined in geom module !
Point2.__bases__ += (Entity,)
Segment2.__bases__ += (Entity,)
Circle.__bases__ += (Entity,) # adds it also to Arc2
Polygon.__bases__ += (Entity,) # adds it also to Arc2
class Spline(Entity, Geometry):
"""cubic spline segment"""
def __init__(self, points):
""":param points: list of (x,y) tuples"""
super(Spline,self).__init__()
self.p=[Point2(xy) for xy in points]
@property
def start(self):
return self.p[0]
@property
def end(self):
return self.p[-1]
@property
def xy(self):
return [pt.xy for pt in self.p]
@property
def length(self):
""":return: float (very) approximate length"""
return sum((x.dist(self.p[i - 1]) for i, x in enumerate(self.p) if i>0))
def bbox(self):
res=BBox()
for p in self.p:
res+=p
return res
def swap(self):
""" swap start and end"""
self.p.reverse() #reverse in place
def _apply_transform(self, t):
self.p=[t*p for p in self.p]
'''
def Spline(pts):
# uses http://www.charlespetzold.com/blog/2012/12/Bezier-Circles-and-Bezier-Ellipses.html
# TODO understand http://itc.ktu.lt/itc354/Riskus354.pdf and implement Arc cleanly
(p0,p1,p2,p3)=pts
t0=Vector2(Point2(p1)-p0)/0.55
c=Point2(p0)-t0.cross()
return Arc2(c,p0,p3)
'''
class _Group(Entity, Geometry):
""" abstract class for iterable Entities"""
def bbox(self, filter=None):
"""
:param filter: optional function(entity):bool returning True if entity should be considered in box
:return: :class:`BBox` bounding box of Entity
"""
res=BBox()
for entity in self: # do not use sum() as it copies Boxes unnecessarily
if filter is None or filter(entity):
res+=entity.bbox()
return res
@property
def length(self):
return sum((entity.length for entity in self))
def intersect(self, other):
"""
:param other: `geom.Entity`
:result: generate tuples (Point2,Entity_self) of intersections between other and each Entity
"""
# recurse in structures by swapping Entities to intersect
# in order to ensure we call geom intersect methods only with
# "atomic" Entities
try:
iter(other)
except:
recurse=False
else:
recurse=True
for e in self:
inter=other.intersect(e) if recurse else e.intersect(other)
if inter is None:
continue
if isinstance(inter,Point2) :
yield (inter,e)
elif isinstance(inter,Segment2) :
yield (inter.p,e)
yield (inter.p2,e)
elif isinstance(inter,list) : # list of multiple points
for i in inter:
yield (i,e)
else: #recursive call
for i,e2 in inter:
e2.group=e
yield (i,e2)
def connect(self, other):
for (inter, _) in self.intersect(other):
if isinstance(inter,Point2):
return Segment2(inter,inter) #segment of null length
if isinstance(inter,Segment2):
return Segment2(inter.p,inter.p) #segment of null length
raise
try:
iter(other)
except:
recurse=False
else:
recurse=True
return min((other.connect(e).swap() if recurse else e.connect(other) for e in self), key=lambda e:e.length)
def patches(self, **kwargs):
""":return: list of :class:`~matplotlib.patches.Patch` corresponding to group"""
patches=[]
for e in self:
patches.extend(e.patches(**kwargs))
return patches
def to_dxf(self, **kwargs):
""":return: flatten list of dxf entities"""
res=[]
for e in self:
r=e.to_dxf(**kwargs)
if not isinstance(r,list): #flatten
r=[r]
res+=r
return res
class Group(list, _Group):
"""group of Entities
but it is a Geometry since we can intersect, connect and compute distances between Groups
"""
@property
def color(self):
return self[0].color
@color.setter
def color(self, c):
for e in self:
e.color=c
@property
def layer(self):
return self[0].layer
@layer.setter
def layer(self, l):
for e in self:
e.layer=l
def append(self,entity, **kwargs):
""" append entity to group
:param entity: Entity
:param kwargs: dict of attributes copied to entity
:return: Group (or Chain) to which the entity was added, or None if entity was None
"""
if entity is None:
return None #to show nothing was done
entity.setattr(**kwargs)
super(Group,self).append(entity)
return self
def extend(self,entities,**kwargs):
for entity in entities:
self.append(entity,**kwargs)
def __copy__(self):
#in fact it is a deepcopy...
#TODO: make this clearer
res=self.__class__()
for e in self:
res.append(copy(e))
return res
def _apply_transform(self,trans):
for entity in self:
entity._apply_transform(trans)
def swap(self):
""" swap start and end"""
super(Group,self).reverse() #reverse in place
for e in self:
e.swap()
def chainify(self, mergeable):
"""merge all possible entities into chains"""
c=chains(self, mergeable=mergeable)
del self[:] #clear
self.extend(c)
def from_dxf(self, dxf, layers=None, only=[], ignore=['POINT'], trans=identity, flatten=False):
"""
:param dxf: dxf.entity
:param layers: list of layer names to consider. entities not on these layers are ignored. default=None: all layers are read
:param only: list of dxf entity types names that are read. default=[]: all are read
:param ignore: list of dxf entity types names that are ignored. default=['POINT']: points and null length segments are ignored
:param trans: :class:`Trans` optional transform matrix
:parm flatten: bool flatten block structure
:return: :class:`Entity` of correct subtype
"""
self.dxf=dxf
for e in dxf:
if layers and e.layer not in layers:
continue
elif e.dxftype in ignore:
continue
elif only and e.dxftype not in only:
continue
elif e.dxftype == 'INSERT': #TODO: improve insertion on correct layer
t2 = trans*Trans(e.scale[:2], e.insert[:2], e.rotation)
if flatten:
self.from_dxf(self.block[e.name].dxf, layers=None, ignore=ignore, only=None, trans=t2, flatten=flatten)
else:
self.append(Instance.from_dxf(e, self.block, t2))
else:
e=Entity.from_dxf(e, trans)
if type(e) is Point2 and 'POINT' in ignore:
continue
self.append(e)
return self
class Instance(_Group):
def __init__(self, group, trans):
"""
:param group: Group
:param trans: optional mat3 of transformation
"""
self.group=group
self.trans=trans
@staticmethod
def from_dxf(e, blocks, mat3):
"""
:param e: dxf.entity
:param blocks: dict of Groups indexed by name
:param mat3: Matrix3 transform
"""
res=Instance(blocks[e.name],mat3)
res.name=e.name
# code below copied from Entity.from_dxf. TODO: merge
res.dxf=e #keep link to source entity
res.color=aci_to_color(e.color)
res.layer=e.layer
return res
def __repr__(self):
return '%s %s' % (self.__class__.__name__, self.group)
def __iter__(self):
#TODO: optimize when trans is identity
for e in self.group:
res=self.trans*e
yield res
def _apply_transform(self,trans):
self.trans=trans*self.trans
class Chain(Group):
""" group of contiguous Entities (Polyline or similar)"""
def __init__(self,data=[]):
Group.__init__(self)
self.extend(data)
@property
def start(self):
return self[0].start
@property
def end(self):
return self[-1].end
def __repr__(self):
(s,e)=(self.start,self.end) if len(self)>0 else (None,None)
return '%s(%s,%s,%d)' % (self.__class__.__name__,s,e,len(self))
def contiguous(self, edge, abs_tol=1E-6, allow_swap=True):
"""
check if edge can be appended to the chain
:param edge: :class:`Entity` to append
:param tol: float tolerance on contiguity
:param allow_swap: if True (default), tries to swap edge or self to find contiguity
:return: int,bool index where to append in chain, swap of edge required
"""
if len(self)==0: #init the chain with edge
return -1,False
if isclose(self.end.distance(edge.start),0,abs_tol=abs_tol):
return -1,False #append
if isclose(self.start.distance(edge.end),0,abs_tol=abs_tol):
return 0,False #prepend
if not allow_swap:
return None,False
if isclose(self.end.distance(edge.end),0,abs_tol=abs_tol):
return -1,True #append swapped
if isclose(self.start.distance(edge.start),0,abs_tol=abs_tol):
return 0,True #prepend swapped
return None, False
def append(self, entity, tol=1E-6, allow_swap=True, mergeable=None, **attrs):
"""
append entity to chain, ensuring contiguity
:param entity: :class:`Entity` to append
:param tol: float tolerance on contiguity
:param allow_swap: if True (default), tries to swap edge or self to find contiguity
:param mergeable: function of the form f(e1,e2) returning True if entities e1,e2 can be merged
:param attrs: attributes passed to Group.append
:return: self, or None if edge is not contiguous
"""
i,s=self.contiguous(entity, tol, allow_swap)
if i is None or mergeable and not mergeable(self,entity):
return None
if s:
entity.swap()
if not isinstance(entity, Chain):
entity=[entity]
if i==-1:
for e in entity:
super(Chain,self).append(e,**attrs)
return self
if i==0: #prepend
for e in reversed(entity):
e.setattr(**attrs)
self.insert(0,e)
return self
return None
@staticmethod
def from_pdf(path,trans,color):
"""
:param path: pdf path
:return: Entity of correct subtype
:see: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf p. 132
"""
def _pt(*args):
return trans(Point2(*args))
chain=Chain()
chain.color=color #TODO handle multicolor chains
start=None # ensure exception if 'm' is not first
for code in path:
if code[0]=='m':
if start:
logging.error("multiple m's in pdf path %s"%path)
break # return chain till here, ignore the rest
home=_pt(code[1:3])
start=home
entity=None
continue
elif code[0]=='l': #line
end=_pt(code[1:3])
entity=Segment2(start,end)
elif code[0]=='c': #Bezier 2 control points (no arcs in pdf!)
x1,y1,x2,y2,x3,y3=code[1:]
end=_pt(x3,y3)
entity=Spline([start,_pt(x1,y1),_pt(x2,y2),end])
elif code[0]=='v': #Bezier 1 control point
x2,y2,x3,y3=code[1:]
end=_pt(x3,y3)
entity=Spline([start,start,_pt(x2,y2),end])
elif code[0]=='y': #Bezier 0 control point
end=_pt(code[1:3])
entity=Spline([start,start,end,end])
elif code[0]=='h': #close to home
entity=Segment2(start,home)
entity.color=chain.color
else:
logging.warning('unsupported path command %s'%code[0])
raise NotImplementedError
entity.color=color
chain.append(entity)
start=end
if len(chain)==1:
return chain[0] #single entity
return chain
@staticmethod
def from_svg(path,color):
"""
:param path: svg path
:return: Entity of correct subtype
"""
import svg.path
chain=Chain()
for seg in path._segments:
if isinstance(seg,svg.path.Line):
entity=Segment2(Point2(seg.start),Point2(seg.end))
elif isinstance(seg,svg.path.Arc):
raise NotImplementedError
elif isinstance(seg,svg.path.CubicBezier):
entity=Spline([Point2(seg.start),Point2(seg.control1),Point2(seg.control2),Point2(seg.end)])
elif isinstance(seg,svg.path.QuadraticBezier):
raise NotImplementedError
else: #Move
continue
entity.color=color
chain.append(entity)
return chain
@staticmethod
def from_dxf(e,mat3):
"""
:param e: dxf.entity
:param mat3: Matrix3 transform
:return: Entity of correct subtype
"""
def arc_from_bulge(start,end,bulge):
#formula from http://www.afralisp.net/archive/lisp/Bulges1.htm
theta=4*atan(bulge)
chord=Segment2(start,end)
c=chord.length
s=bulge*c/2
r=(c*c/4+s*s)/(2*s) #radius (negative if clockwise)
gamma=(pi-theta)/2
angle=chord.v.angle()+(gamma if bulge>=0 else -gamma)
center=start+Polar(r,angle)
return Arc2(center, start, end,
# dir=1 if bulge>=0 else -1
)
if e.dxftype == 'POLYLINE':
res=Chain()
for i in range(1,len(e.vertices)):
start=e.vertices[i-1].location[:2] #2D only
end=e.vertices[i].location[:2] #2D only
bulge=e.vertices[i-1].bulge
if bulge==0:
res.append(Segment2(start,end))
else:
res.append(arc_from_bulge(start,end,bulge))
if e.is_closed:
res.append(
Segment2(e.vertices[-1].location[:2],
e.vertices[0].location[:2])
)
elif e.dxftype == 'LWPOLYLINE':
res=Chain()
for i in range(1,len(e.points)):
start=e.points[i-1]
end=e.points[i]
if len(end)==2:
res.append(Segment2(start,end))
else:
res.append(arc_from_bulge(start,end,bulge=end[2]))
if e.is_closed:
res.append(Segment2(e.points[-1],e.points[0]))
else:
logging.warning('unhandled entity type %s'%e.dxftype)
return None
res._apply_transform(mat3)
res.dxf=e #keep link to source entity
res.color=aci_to_color(e.color)
res.layer=e.layer
for edge in res:
edge.dxf=e
edge.color=res.color
edge.layer=res.layer
return res
def to_dxf(self, split=False, **attr):
"""
:param split: bool if True, each segment in Chain is saved separately
:param attr: dict of graphic attributes
:return: polyline or list of entities along the chain
"""
if split: #handle polylines as separate entities
return super(Chain,self).to_dxf(**attr)
if len(self)==1:
return self[0].to_dxf(**attr)
#if no color specified assume chain color is the same as the first element's
color=attr.get('color', self.color)
attr['color']=color_to_aci(color)
try:
attr.setdefault('layer',self.layer)
except : pass #no layer defined
from dxfwrite.entities import Polyline
attr['flags']=1 if self.isclosed() else 0
res=Polyline(**attr)
for e in self:
if isinstance(e,Line2):
res.add_vertex(e.start.xy)
elif isinstance(e, Arc2):
bulge=tan(e.angle()/4)
res.add_vertex(e.start.xy,bulge=bulge)
else:
if attr.pop('R12',True): #R12 doesn't handle splines.
attr['color']=color #otherwise it causes trouble
del attr['flags']
return super(Chain,self).to_dxf(**attr)
if not self.isclosed():
res.add_vertex(self.end.xy)
return res
def chains(group, tol=1E-6, mergeable=None):
"""build chains from all possible segments in group
:param mergeable: function(e1,e2) returning True if entities e1,e2 can be merged
"""
res=Group()
changed=False
#step 1 : add all entities in group to chains in res
for e in group:
if e is None or isclose(e.length,0,tol):