Updating download.html page with Git information
[pythoncad.git] / PythonCAD / Generic / delete.py
blob6f4144d4832342ef6bd37cfc5865c7b98da07994
2 # Copyright (c) 2002, 2003, 2004, 2006 Art Haas
4 # This file is part of PythonCAD.
5 #
6 # PythonCAD is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # PythonCAD is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with PythonCAD; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # Deleting objects
24 from PythonCAD.Generic.point import Point
25 from PythonCAD.Generic.segment import Segment
26 from PythonCAD.Generic.circle import Circle
27 from PythonCAD.Generic.arc import Arc
28 from PythonCAD.Generic.hcline import HCLine
29 from PythonCAD.Generic.vcline import VCLine
30 from PythonCAD.Generic.acline import ACLine
31 from PythonCAD.Generic.segjoint import Chamfer, Fillet
32 from PythonCAD.Generic.cline import CLine
33 from PythonCAD.Generic.ccircle import CCircle
34 from PythonCAD.Generic.leader import Leader
35 from PythonCAD.Generic.polyline import Polyline
36 from PythonCAD.Generic.text import TextBlock
37 from PythonCAD.Generic.dimension import DimString
38 from PythonCAD.Generic.layer import Layer
40 def delete_objects(objlist):
41 """Remove a list of objects from the parent Layer.
43 delete_objects(objlist)
45 The objlist argument must be either a tuple or list.
46 """
47 if not isinstance(objlist, (list, tuple)):
48 raise TypeError, "Invalid object list: " + `type(objlist)`
49 _delobjs = []
50 for _obj in objlist:
51 if not isinstance(_obj, DimString):
52 _parent = _obj.getParent()
53 if _parent is not None and isinstance(_parent, Layer):
54 _delobjs.append(_obj)
55 _objdict = {}
56 for _obj in _delobjs:
57 _objdict[id(_obj)] = True
58 for _obj in _delobjs:
59 if isinstance(_obj, Point):
60 continue
61 elif isinstance(_obj, Segment):
62 _p1, _p2 = _obj.getEndpoints()
63 _pid = id(_p1)
64 if _pid in _objdict:
65 _objdict[_pid] = False
66 _pid = id(_p2)
67 if _pid in _objdict:
68 _objdict[_pid] = False
69 elif isinstance(_obj, Arc):
70 _cp = _obj.getCenter()
71 _pid = id(_cp)
72 if _pid in _objdict:
73 _objdict[_pid] = False
74 _parent = _obj.getParent()
75 for _ep in _obj.getEndpoints():
76 _lp = None
77 _pts = _parent.find('point', _ep[0], _ep[1])
78 for _pt in _pts:
79 for _user in _pt.getUsers():
80 if _user is _obj:
81 _lp = _pt
82 break
83 if _lp is None:
84 raise RuntimeError, "Arc endpoint missing: " + str(_ep)
85 _pid = id(_lp)
86 if _pid in _objdict:
87 _objdict[_pid] = False
88 elif isinstance(_obj, (Circle, CCircle)):
89 _cp = _obj.getCenter()
90 _pid = id(_cp)
91 if _pid in _objdict:
92 _objdict[_pid] = False
93 elif isinstance(_obj, (HCLine, VCLine, ACLine)):
94 _lp = _obj.getLocation()
95 _pid = id(_lp)
96 if _pid in _objdict:
97 _objdict[_pid] = False
98 elif isinstance(_obj, CLine):
99 _p1, _p2 = _obj.getKeypoints()
100 _pid = id(_p1)
101 if _pid in _objdict:
102 _objdict[_pid] = False
103 _pid = id(_p2)
104 if _pid in _objdict:
105 _objdict[_pid] = False
106 elif isinstance(_obj, (Chamfer, Fillet)):
107 continue # chamfers/fillets do not delete attached segments
108 elif isinstance(_obj, (Leader, Polyline)):
109 for _pt in _obj.getPoints():
110 _pid = id(_pt)
111 if _pid in _objdict:
112 _objdict[_pid] = False
113 elif isinstance(_obj, TextBlock):
114 continue
115 else:
116 pass
117 for _obj in _delobjs:
118 if _objdict[id(_obj)]:
119 _parent = _obj.getParent()
121 # if the parent is None then the object has already been
122 # deleted as a result of earlier deletions such as a
123 # dimension being removed when the referenced points were
124 # removed
126 if _parent is not None:
127 _parent.delObject(_obj)