File: CocoaDimensions.py

package info (click to toggle)
pythoncad 0.1.35-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,536 kB
  • ctags: 4,286
  • sloc: python: 62,752; sh: 743; makefile: 39
file content (295 lines) | stat: -rw-r--r-- 10,964 bytes parent folder | download | duplicates (5)
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
#
# Copyright (c) 2002-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
#
# Handles creation of dimensions
#

import PythonCAD.Generic.segment
from PythonCAD.Interface.Cocoa import CocoaEntities

#
# Linear, Horizontal, Vertical dimensions all work the same way.
#
def ldim_mode_init(doc, tool):
    doc.setPrompt("Click on the first point for the dimension.")
    tool.setHandler("initialize", ldim_mode_init)
    tool.setHandler("left_button_press", ldim_first_left_button_press_cb)

def ldim_first_mouse_move_cb(doc, np, tool):
    _l1, _p1 = tool.getFirstPoint()
    _p2 = (np.x, np.y)
    _seg = PythonCAD.Generic.segment.Segment(_p1, _p2)
    _da = doc.getDA()
    _da.setTempObject(_seg)

def ldim_second_mouse_move_cb(doc, np, tool):
    _ldim = tool.getDimension()
    _ldim.setLocation(np.x, np.y)
    _ldim.calcDimValues()
    _da = doc.getDA()
    _da.setTempObject(_ldim)

def ldim_first_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    _viewLoc = _da.convertPoint_fromView_(_loc, None)
    _tol = _da.pointSize().width
    _image = doc.getImage()
    _layers = [_image.getTopLayer()]
    while len(_layers):
        _layer = _layers.pop()
        if _layer.isVisible():
            _pt = _layer.find('point', _viewLoc.x, _viewLoc.y, _tol)
            if _pt is not None:
                _x, _y = _pt.getCoords()
                _da.setTempPoint(_viewLoc)
                tool.setLocation(_x, _y)
                tool.setFirstPoint(_layer, _pt)
                tool.setHandler("left_button_press", ldim_second_left_button_press_cb)
                tool.setHandler("mouse_move", ldim_first_mouse_move_cb)
                doc.setPrompt("Click on the second point for the dimension.")
            break
        _layers.extend(_layer.getSublayers())

              
def ldim_second_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    _viewLoc = _da.convertPoint_fromView_(_loc, None)
    _tol = _da.pointSize().width
    _image = doc.getImage()
    _layers = [_image.getTopLayer()]
    while len(_layers):
        _layer = _layers.pop()
        if _layer.isVisible():
            _pt = _layer.find('point', _viewLoc.x, _viewLoc.y, _tol)
            if _pt is not None:
                _x, _y = _pt.getCoords()
                tool.setSecondPoint(_layer, _pt)
                tool.setDimPosition(_x, _y)
                tool.clearCurrentPoint()
                tool.makeDimension(_image)
                tool.setHandler("left_button_press", ldim_third_left_button_press_cb)
                tool.setHandler("mouse_move", ldim_second_mouse_move_cb)
                doc.setPrompt("Click where the dimension text should be placed.")
                break
        _layers.extend(_layer.getSublayers())
            
def ldim_third_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    _viewLoc = _da.convertPoint_fromView_(_loc, None)
    _ldim = tool.getDimension()
    _ldim.setLocation(_viewLoc.x, _viewLoc.y)
    _ldim.calcDimValues()
    _ldim.reset()
    CocoaEntities.create_entity(doc, tool)

#
# Radial
#
def radial_mode_init(doc, tool):
    doc.setPrompt("Click on an arc or a circle to dimension.")
    tool.setHandler("initialize", radial_mode_init)
    tool.setHandler("left_button_press", radial_first_left_button_press_cb)

def radial_first_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    _viewLoc = _da.convertPoint_fromView_(_loc, None)
    _tol = _da.pointSize().width
    _image = doc.getImage()
    _layers = [_image.getTopLayer()]
    _dc = _dl = None
    while len(_layers):
        _layer = _layers.pop()
        if _layer.isVisible():
            _cobjs = (_layer.getLayerEntities("circle") +
                      _layer.getLayerEntities("arc"))
            for _cobj in _cobjs:
                _mp = _cobj.mapCoords(_viewLoc.x, _viewLoc.y, _tol)
                if _mp is not None:
                    _dc = _cobj
                    _dl = _layer
                    break
        _layers.extend(_layer.getSublayers())
    if _dc is not None:
        _x, _y = _mp
        tool.setDimObject(_dl, _dc)
        tool.setDimPosition(_x, _y)
        tool.makeDimension(_image)
        tool.setHandler("mouse_move", radial_mouse_move_cb)
        tool.setHandler("left_button_press", radial_second_left_button_press_cb)
        doc.setPrompt("Click where the dimension text should be placed.")

def radial_mouse_move_cb(doc, np, tool):
    _rdim = tool.getDimension()
    _rdim.setLocation(np.x, np.y)
    _rdim.calcDimValues()
    _da = doc.getDA()
    _da.setTempObject(_rdim)

def radial_second_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    _viewLoc = _da.convertPoint_fromView_(_loc, None)
    _ldim = tool.getDimension()
    _ldim.setLocation(_viewLoc.x, _viewLoc.y)
    _ldim.calcDimValues()
    _ldim.reset()
    CocoaEntities.create_entity(doc, tool)
      

#
# Angular
#
def angular_mode_init(doc, tool):
    doc.setPrompt("Click on the angle vertex point or an arc.")
    tool.setHandler("initialize", angular_mode_init)
    tool.setHandler("left_button_press", angular_first_left_button_press_cb)

def angular_first_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    _viewLoc = _da.convertPoint_fromView_(_loc, None)
    _tol = _da.pointSize().width
    _image = doc.getImage()
    _layers = [_image.getTopLayer()]
    _pt = _arc = None
    while len(_layers):
        _layer = _layers.pop()
        if _layer.isVisible():
            _pt, _arc = _test_layer(_layer, _viewLoc.x, _viewLoc.y, _tol)
            if _pt is not None or _arc is not None:
                break
        _layers.extend(_layer.getSublayers())
    if _pt is not None:
        tool.setVertexPoint(_layer, _pt)
        _da.setTempPoint(_viewLoc)
        tool.setHandler("left_button_press", angular_second_left_button_press_cb)
        tool.setHandler("mouse_move", angular_segment_mouse_move_cb)
        doc.setPrompt("Click on the first endpoint for the dimension.")
    elif _arc is not None:
        _cp = _arc.getCenter()
        tool.setVertexPoint(_layer, _cp)
        _ep1, _ep2 = _arc.getEndpoints()
        _ex, _ey = _ep1
        _p1 = _layer.find('point', _ex, _ey)
        assert _p1 is not None, "Missing arc endpoint"
        tool.setFirstPoint(_layer, _p1)
        _ex, _ey = _ep2
        _p2 = _layer.find('point', _ex, _ey)
        assert _p2 is not None, "Missing arc endpoint"
        tool.setSecondPoint(_layer, _p2)
        tool.setDimPosition(_viewLoc.x, _viewLoc.y)
        tool.makeDimension(_image)
        tool.setHandler("left_button_press", angular_fourth_left_button_press_cb)
        tool.setHandler("mouse_move", angular_text_mouse_move_cb)
        doc.setPrompt("Click where the dimension text should be located.")
       
def angular_second_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    (_x, _y) = _da.convertPoint_fromView_(_loc, None)
    _tol = _da.pointSize().width
    _image = doc.getImage()
    _layers = [_image.getTopLayer()]
    while len(_layers):
        _layer = _layers.pop()
        if _layer.isVisible():
            _pt = _layer.find('point', _x, _y, _tol)
            if _pt is not None:
                _x, _y = _pt.getCoords()
                tool.setLocation(_x, _y)
                tool.setFirstPoint(_layer, _pt)
                tool.setHandler("left_button_press", angular_third_left_button_press_cb)
                tool.setHandler("mouse_move", angular_segment_mouse_move_cb)
                doc.setPrompt("Click on the second endpoint for the dimension.")
                break
        _layers.extend(_layer.getSublayers())

def angular_third_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    (_x, _y) = _da.convertPoint_fromView_(_loc, None)
    _tol = _da.pointSize().width
    _image = doc.getImage()
    _layers = [_image.getTopLayer()]
    while len(_layers):
        _layer = _layers.pop()
        if _layer.isVisible():
            _pt = _layer.find('point', _x, _y, _tol)
            if _pt is not None:
                _x, _y = _pt.getCoords()
                tool.setLocation(_x, _y)
                tool.setSecondPoint(_layer, _pt)
                tool.setDimPosition(_x, _y)
                tool.makeDimension(_image)
                tool.setHandler("left_button_press", angular_fourth_left_button_press_cb)
                tool.setHandler("mouse_move", angular_text_mouse_move_cb)
                doc.setPrompt("Click where the dimension text should be located.")
                break
        _layers.extend(_layer.getSublayers())
    
def angular_fourth_left_button_press_cb(doc, event, tool):
    _loc = event.locationInWindow()
    _da = doc.getDA()
    (_x, _y) = _da.convertPoint_fromView_(_loc, None)
    _adim = tool.getDimension()
    _adim.setLocation(_x, _y)
    _adim.calcDimValues()
    _adim.reset()
    CocoaEntities.create_entity(doc, tool)

def angular_segment_mouse_move_cb(doc, np, tool):
    _l1, _p1 = tool.getVertexPoint()
    _l2, _p2 = tool.getFirstPoint()
    _da = doc.getDA()
    _flag = True
    if _p2 is not None:
        _seg = PythonCAD.Generic.segment.Segment(_p1, _p2)
        _da.setTempObject(_seg, _flag)
        _flag = False
    _p2 = (np.x, np.y)
    _seg = PythonCAD.Generic.segment.Segment(_p1, _p2)
    _da.setTempObject(_seg, _flag)
    

def angular_text_mouse_move_cb(doc, np, tool):
    _adim = tool.getDimension()
    _adim.setLocation(np.x, np.y)
    _adim.calcDimValues()
    _da = doc.getDA()
    _da.setTempObject(_adim)
    
        
def _test_layer(layer, x, y, tol):
    _arc = None
    _pt = layer.find('point', x, y)
    if _pt is None:
        _pt = layer.find('point', x, y, tol)
        if _pt is None:
            _arc_pt = None
            for _arc in layer.getLayerEntities("arc"):
                _arc_pt = _arc.mapCoords(x, y, tol)
                if _arc_pt is not None:
                    break
            if _arc_pt is None:
                _arc = None # no hits on any arcs ...
    return _pt, _arc