File: move.py

package info (click to toggle)
pythoncad 0.1.23-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,044 kB
  • ctags: 3,568
  • sloc: python: 54,867; sh: 100; makefile: 39
file content (364 lines) | stat: -rw-r--r-- 12,337 bytes parent folder | download
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
#
# Copyright (c) 2002, 2003, 2004 Art Haas
#
# This file is part of PythonCAD.
# 
# PythonCAD 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.
# 
# PythonCAD 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 PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# code to handle moving objects
#

from PythonCAD.Generic.point import Point
from PythonCAD.Generic.segment import Segment
from PythonCAD.Generic.circle import Circle
from PythonCAD.Generic.arc import Arc
from PythonCAD.Generic.hcline import HCLine
from PythonCAD.Generic.vcline import VCLine
from PythonCAD.Generic.acline import ACLine
from PythonCAD.Generic.segjoint import Chamfer, Fillet
from PythonCAD.Generic.cline import CLine
from PythonCAD.Generic.ccircle import CCircle
from PythonCAD.Generic.leader import Leader
from PythonCAD.Generic.polyline import Polyline
from PythonCAD.Generic.text import TextBlock
from PythonCAD.Generic.dimension import Dimension
from PythonCAD.Generic.dimension import LinearDimension
from PythonCAD.Generic.dimension import AngularDimension
from PythonCAD.Generic.layer import Layer

def _adjust_dimensions(layer, point, newpoint):
    _users = point.getUsers()
    for _uref in _users:
        _user = _uref()
        if _user is not None:
            if isinstance(_user, Dimension):
                if isinstance(_user, LinearDimension):
                    _l1, _l2 = _user.getDimLayers()
                    _p1, _p2 = _user.getDimPoints()
                    if _l1 is layer and _p1 is point:
                        _user.setP1(layer, newpoint)
                    elif _l2 is layer and _p2 is point:
                        _user.setP2(layer, newpoint)
                elif isinstance(_user, AngularDimension):
                    _vl, _l1, _l2 = _user.getDimLayers()
                    _vp, _p1, _p2 = _user.getDimPoints()
                    if _vl is layer and _vp is point:
                        _user.setVertexPoint(layer, newpoint)
                    elif _l1 is layer and _p1 is point:
                        _user.setP1(layer, newpoint)
                    elif _l2 is layer and _p2 is point:
                        _user.setP2(layer, newpoint)
                else:
                    raise TypeError, "Unknown dimension: " + `_user`

def _move_point(layer, point, dx, dy):
    _x, _y = point.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np = layer.find('point', _nx, _ny)
    if _np is None:
        _np = Point(_nx, _ny)
        layer.addObject(_np)
    _adjust_dimensions(layer, point, _np)
    layer.delObject(point)

def _move_segment(layer, segment, dx, dy):
    _p1, _p2 = segment.getEndpoints()
    _x, _y = _p1.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np1 = layer.find('point', _nx, _ny)
    if _np1 is None:
        _np1 = Point(_nx, _ny)
        layer.addObject(_np1)
    _adjust_dimensions(layer, _p1, _np1)
    _x, _y = _p2.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np2 = layer.find('point', _nx, _ny)
    if _np2 is None:
        _np2 = Point(_nx, _ny)
        layer.addObject(_np2)
    _adjust_dimensions(layer, _p2, _np2)
    segment.setP1(_np1)
    segment.setP2(_np2)
    layer.delObject(_p1)
    layer.delObject(_p2)

def _move_arc(layer, arc, dx, dy):
    _cp = arc.getCenter()
    _x, _y = _cp.getCoords()
    _ep1, _ep2 = arc.getEndpoints()
    _lp1 = layer.find('point', _ep1[0], _ep1[1])
    assert _lp1 is not None, "Lost arc first endpoint"
    _lp2 = layer.find('point', _ep2[0], _ep2[1])
    assert _lp2 is not None, "Lost arc second endpoint"
    _nx = _x + dx
    _ny = _y + dy
    _ncp = layer.find('point', _nx, _ny)
    if _ncp is None:
        _ncp = Point(_nx, _ny)
        layer.addObject(_ncp)
    for _ep in arc.getEndpoints():
        _ex, _ey = _ep
        _nx = _ex + dx
        _ny = _ey + dy
        _lp = layer.find('point', _nx, _ny)
        if _lp is None:
            _lp = Point(_nx, _ny)
            layer.addObject(_lp)
    arc.setCenter(_ncp)        
    _adjust_dimensions(layer, _cp, _ncp)
    layer.delObject(_cp)
    _lp1.freeUser(arc)
    layer.delObject(_lp1)
    _lp2.freeUser(arc)
    layer.delObject(_lp2)

def _move_circle(layer, circle, dx, dy): # moves circle and ccircles
    _cp = circle.getCenter()
    _x, _y = _cp.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _ncp = layer.find('point', _nx, _ny)
    if _ncp is None:
        _ncp = Point(_nx, _ny)
        layer.addObject(_ncp)
    _adjust_dimensions(layer, _cp, _ncp)
    circle.setCenter(_ncp)
    layer.delObject(_cp)

def _move_leader(layer, leader, dx, dy):
    _p1, _p2, _p3 = leader.getPoints()
    _x, _y = _p1.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np1 = layer.find('point', _nx, _ny)
    if _np1 is None:
        _np1 = Point(_nx, _ny)
        layer.addObject(_np1)
    _adjust_dimensions(layer, _p1, _np1)
    _x, _y = _p2.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np2 = layer.find('point', _nx, _ny)
    if _np2 is None:
        _np2 = Point(_nx, _ny)
        layer.addObject(_np2)
    _adjust_dimensions(layer, _p2, _np2)
    _x, _y = _p3.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np3 = layer.find('point', _nx, _ny)
    if _np3 is None:
        _np3 = Point(_nx, _ny)
        layer.addObject(_np3)
    _adjust_dimensions(layer, _p3, _np3)
    leader.setP1(_np1)
    leader.setP2(_np2)
    leader.setP3(_np3)
    layer.delObject(_p1)
    layer.delObject(_p2)
    layer.delObject(_p3)

def _move_polyline(layer, polyline, dx, dy):
    _pts = polyline.getPoints()
    _npts = []
    for _pt in _pts:
        _x, _y = _pt.getCoords()
        _nx = _x + dx
        _ny = _y + dy
        _np = layer.find('point', _nx, _ny)
        if _np is None:
            _np = Point(_nx, _ny)
            layer.addObject(_np)
        _adjust_dimensions(layer, _pt, _np)
        _npts.append(_np)
    for _i in range(len(_npts)):
        polyline.setPoint(_i, _npts[_i])
    for _pt in _pts:
        layer.delObject(_pt)

def _move_segjoint(layer, segjoint, dx, dy):
    _s1, _s2 = segjoint.getSegments()
    _move_segment(layer, _s1, dx, dy)
    _move_segment(layer, _s2, dx, dy)

def _move_spcline(layer, spcline, dx, dy):
    _p = spcline.getLocation()
    _x, _y = _p.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np = layer.find('point', _nx, _ny)
    if _np is None:
        _np = Point(_nx, _ny)
        layer.addObject(_np)
    _adjust_dimensions(layer, _p, _np)
    spcline.setLocation(_np)
    layer.delObject(_p)

def _move_cline(layer, cline, dx, dy):
    _p1, _p2 = cline.getKeypoints()
    _x, _y = _p1.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np1 = layer.find('point', _nx, _ny)
    if _np1 is None:
        _np1 = Point(_nx, _ny)
        layer.addObject(_np1)
    _adjust_dimensions(layer, _p1, _np1)
    _x, _y = _p2.getCoords()
    _nx = _x + dx
    _ny = _y + dy
    _np2 = layer.find('point', _nx, _ny)
    if _np2 is None:
        _np2 = Point(_nx, _ny)
        layer.addObject(_np2)
    _adjust_dimensions(layer, _p2, _np2)
    cline.setP1(_np1)
    cline.setP2(_np2)
    layer.delObject(_p1)
    layer.delObject(_p2)

def _move_dimension(dim, dx, dy):
    _x, _y = dim.getLocation()
    _nx = _x + dx
    _ny = _y + dy
    dim.setLocation(_nx, _ny)

def move_objects(layer, objlist, dx, dy):
    """Move objects in a layer.

move_objects(layer, objlist, dx, dy)

layer; The layer in which all the objects are located
objlist: The list of objects to move. This argument must be a list
dx: The displacement along the x-axis
dy: The displacement along the y-axix
    """
    if not isinstance(layer, Layer):
        raise TypeError, "Invalid layer: " + str(layer)
    if not isinstance(objlist, list):
        raise TypeError, "Invalid object list: " + str(objlist)
    _dx = dx
    if not isinstance(_dx, float):
        _dx = float(dx)
    _dy = dy
    if not isinstance(_dy, float):
        _dy = float(dy)
    if abs(_dx) < 1e-10 and abs(_dy) < 1e-10:
        return
    _objlist = []
    _objdict = {}
    for _obj in objlist:
        _lobj = layer.findObject(_obj)
        if _lobj is not _obj:
            raise ValueError, "Object not found in layer: " + str(_obj)
        _objdict[id(_obj)] = True
        _objlist.append(_obj)
    for _obj in _objlist:
        if isinstance(_obj, Segment):
            _p1, _p2 = _obj.getEndpoints()
            _pid = id(_p1)
            if _pid in _objdict:
                _objdict[_pid] = False
            _pid = id(_p2)
            if _pid in _objdict:
                _objdict[_pid] = False
        elif isinstance(_obj, (Circle, Arc, CCircle)):
            _cp = _obj.getCenter()
            _cpid = id(_cp)
            if _cpid in _objdict:
                _objdict[_cpid] = False
            if isinstance(_obj, Arc):
                for _ep in _obj.getEndpoints():
                    _lp = layer.find('point', _ep[0], _ep[1])
                    if _lp is None:
                        raise ValueError, "Lost arc endpoint: " + str(_ep)
                    _pid = id(_lp)
                    if _pid in _objdict:
                        _objdict[_pid] = False
        elif isinstance(_obj, (HCLine, VCLine, ACLine)):
            _p = _obj.getLocation()
            _pid = id(_p)
            if _pid in _objdict:
                _objdict[_pid] = False
        elif isinstance(_obj, CLine):
            _p1, _p2 = _obj.getKeypoints()
            _pid = id(_p1)
            if _pid in _objdict:
                _objdict[_pid] = False
            _pid = id(_p2)
            if _pid in _objdict:
                _objdict[_pid] = False
        elif isinstance(_obj, (Leader, Polyline)):
            for _pt in _obj.getPoints():
                _pid = id(_pt)
                if _pid in _objdict:
                    _objdict[_pid] = False
        elif isinstance(_obj, (Chamfer, Fillet)):
            _s1, _s2 = _obj.getSegments()
            _sid = id(_s1)
            if _sid in _objdict:
                _objdict[_sid] = False
            _p1, _p2 = _s1.getEndpoints()
            _pid = id(_p1)
            if _pid in _objdict:
                _objdict[_pid] = False
            _pid = id(_p2)
            if _pid in _objdict:
                _objdict[_pid] = False
            _sid = id(_s2)
            if _sid in _objdict:
                _objdict[_sid] = False
            _p1, _p2 = _s2.getEndpoints()
            _pid = id(_p1)
            if _pid in _objdict:
                _objdict[_pid] = False
            _pid = id(_p2)
            if _pid in _objdict:
                _objdict[_pid] = False
        else:
            pass
    for _obj in _objlist:
        if _objdict[id(_obj)]:
            if isinstance(_obj, Point):
                _move_point(layer, _obj, _dx, _dy)
            elif isinstance(_obj, Segment):
                _move_segment(layer, _obj, _dx, _dy)
            elif isinstance(_obj, Arc):
                _move_arc(layer, _obj, _dx, _dy)
            elif isinstance(_obj, (Circle, CCircle)):
                _move_circle(layer, _obj, _dx, _dy)
            elif isinstance(_obj, Leader):
                _move_leader(layer, _obj, _dx, _dy)
            elif isinstance(_obj, Polyline):
                _move_polyline(layer, _obj, _dx, _dy)
            elif isinstance(_obj, (Chamfer, Fillet)):
                _move_segjoint(layer, _obj, _dx, _dy)
            elif isinstance(_obj, (HCLine, VCLine, ACLine)):
                _move_spcline(layer, _obj, _dx, _dy)
            elif isinstance(_obj, CLine):
                _move_cline(layer, _obj, _dx, _dy)
            elif isinstance(_obj, Dimension):
                _move_dimension(_obj, _dx, _dy)
            elif isinstance(_obj, TextBlock):
                _x, _y = _obj.getLocation()
                _nx = _x + _dx
                _ny = _y + _dy
                _obj.setLocation(_nx, _ny)
            else:
                pass