~lorzeteam/lorze/trunk

« back to all changes in this revision

Viewing changes to Modules/lrzdrawing.py

  • Committer: Andreas Ulrich
  • Date: 2012-12-13 20:54:19 UTC
  • Revision ID: ulrich3110@gmail.com-20121213205419-aucgdskqtqmyrj10
new file structure, new object structure, about dialogue, help dialogue, hep pages in english and german, german translation, ponton5h installer, documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# LORZE erasandcad, a 2D CAD with an intuitive user interface, simple and easy.
 
5
# http://erasand.jimdo.com/python-programme/lorze/
 
6
# (C) 2012, Andreas Ulrich
 
7
 
 
8
# This file is part of “LORZE erasandcad“
 
9
 
 
10
# “LORZE erasandcad“ 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 3 of the License, or (at your option) any later version.
 
11
 
 
12
# “LORZE erasandcad“ 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.
 
13
 
 
14
# You should have received a copy of the GNU General Public License along with LORZE erasandcad.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
from lrzdrawline import DrawLine
 
17
 
 
18
 
 
19
class LorzeDrawing():
 
20
    # Drawing object for LORZE erasandcad
 
21
 
 
22
    def __init__(self, attribute, log, geohelp):
 
23
        # LorzeAttribute
 
24
        self.__attribute= attribute
 
25
 
 
26
        # log object
 
27
        self.__log= log
 
28
 
 
29
        # geometry helper object
 
30
        self.__geohelp= geohelp
 
31
 
 
32
        # string for lorzelog
 
33
        self.__logtext= ''
 
34
 
 
35
        # float: outline rectangle x1, y1, x2, y2
 
36
        self.__recx, self.__recy, self.__recw, self.__rech= 0.0, 0.0, 0.0, 0.0
 
37
 
 
38
        # dictionairy: drawing elements, key= id, value= drawing element
 
39
        self.__elements= {}
 
40
 
 
41
        # lists of free ids, has the list only one value: from this ongoing, B= block, C= circle, E= ellipse, H= hatch, K= thickness, L= line, M= measure, O= clone, P= point, T= text, V= view
 
42
        self.__newidB= [0]
 
43
        self.__newidC= [0]
 
44
        self.__newidE= [0]
 
45
        self.__newidH= [0]
 
46
        self.__newidK= [0]
 
47
        self.__newidL= [0]
 
48
        self.__newidM= [0]
 
49
        self.__newidO= [0]
 
50
        self.__newidP= [0]
 
51
        self.__newidT= [0]
 
52
        self.__newidV= [0]
 
53
 
 
54
        # dictionairy, for indexing lists with free ids: B= block, C= circle,  E= ellipse, H= hatch, K= thickness, L= line, M= measure, O= clone, P= point, T= text, V= view
 
55
        self.__newids= {'B':self.__newidB, 'C':self.__newidC, 'E':self.__newidE, 'H':self.__newidH, 'K':self.__newidK, 'L':self.__newidL, 'M':self.__newidM, 'O':self.__newidO, 'P':self.__newidP, 'T':self.__newidT, 'V':self.__newidV}
 
56
 
 
57
 
 
58
    def SetRec(self, recx, recy, recw, rech):
 
59
        # set outline rectangle (float)
 
60
        self.__recx= float(recx)
 
61
        self.__recy= float(recy)
 
62
        self.__recw= float(recw)
 
63
        self.__rech= float(rech)
 
64
 
 
65
 
 
66
    def GetRec(self):
 
67
        # get outline rectangle (float)
 
68
        return(self.__recx, self.__recy, self.__recw, self.__rech)
 
69
 
 
70
 
 
71
    def Add(self, addlist):
 
72
        # adding drawing elements (list) / list: [('typ', attributes), ..] / typ: B (block), C (circle), E (ellipse), H (hatch), K (thickness), L (line), M (measure), O (clone), P (point), T (text), V (view) / attributes: (color, layer, style, width, coords) - coords: (x1, y1, x2, y2 ..)
 
73
 
 
74
        for i in addlist:
 
75
            # get typ & attributes
 
76
            typ, attributes= i
 
77
            color, layer, style, width, coords= attributes
 
78
 
 
79
            # logtext
 
80
            self.__logtext= 'LorzeDrawing.Add([('+ typ+ ', ('+ color+ ', '+ layer+ ', '+ style+ ', '+ width+ ', ('
 
81
            for i in coords:
 
82
                if i!= 0:
 
83
                    self.__logtext+= ', '+ str(i)
 
84
                else:
 
85
                    self.__logtext+= str(i)
 
86
            self.__logtext+= '))]'
 
87
 
 
88
            if typ== 'B':
 
89
                # add block
 
90
                pass
 
91
 
 
92
            elif typ== 'C':
 
93
                # add circle
 
94
                pass
 
95
 
 
96
            elif typ== 'E':
 
97
                # add ellipse
 
98
                pass
 
99
 
 
100
            elif typ== 'H':
 
101
                # add hatch
 
102
                pass
 
103
 
 
104
            elif typ== 'K':
 
105
                # add thickness
 
106
                pass
 
107
 
 
108
            elif typ== 'L':
 
109
                # add line
 
110
                line= DrawLine(self.__geohelp)
 
111
                lrzid= self.GetFreeId('L')
 
112
                self.__logtext+= '|New ID: '+ lrzid
 
113
 
 
114
                line= self.SetBaseAttributes(line, lrzid, color, layer, style, width)
 
115
                line.SetCoords(coords[0], coords[1], coords[2], coords[3])
 
116
                self.__elements[lrzid]= line
 
117
 
 
118
                self.__log.AddEntry(self.__logtext)
 
119
 
 
120
            elif typ== 'M':
 
121
                # add measure
 
122
                pass
 
123
 
 
124
            elif typ== 'O':
 
125
                # add clone
 
126
                pass
 
127
 
 
128
            elif typ== 'P':
 
129
                # add point
 
130
                pass
 
131
 
 
132
            elif typ== 'T':
 
133
                # add text
 
134
                pass
 
135
 
 
136
            elif typ== 'V':
 
137
                # add view
 
138
                pass
 
139
 
 
140
 
 
141
    def Update(self, updatelist):
 
142
        # updateing drawing elements (list) / updatelist: [(lorze-id, new_attributes), ..] / new_attributes: (color, layer, style, width, coords) - coords: (x1, y1, x2, y2 ..)
 
143
        for i in updatelist:
 
144
            # get id & attributes
 
145
            lrzid, attributes= i
 
146
            color, layer, style, width, coords= attributes
 
147
 
 
148
            # logtext
 
149
            self.__logtext= 'LorzeDrawing.Update([('+ lrzid+ ', ('+ color+ ', '+ layer+ ', '+ style+ ', '+ width+ ', ('
 
150
            for i in coords:
 
151
                if i!= 0:
 
152
                    self.__logtext+= ', '+ str(i)
 
153
                else:
 
154
                    self.__logtext+= str(i)
 
155
            self.__logtext+= '))]'
 
156
 
 
157
            typ= lrzid[0]
 
158
 
 
159
            if typ== 'B':
 
160
                # update block
 
161
                pass
 
162
 
 
163
            elif typ== 'C':
 
164
                # update circle
 
165
                pass
 
166
 
 
167
            elif typ== 'E':
 
168
                # update ellipse
 
169
                pass
 
170
 
 
171
            elif typ== 'H':
 
172
                # update hatch
 
173
                pass
 
174
 
 
175
            elif typ== 'K':
 
176
                # update thickness
 
177
                pass
 
178
 
 
179
            elif typ== 'L':
 
180
                # update line
 
181
                line= self.__elements[lrzid]
 
182
                line= self.SetBaseAttributes(line, lrzid, color, layer, style, width)
 
183
                line.SetCoords(coords[0], coords[1], coords[2], coords[3])
 
184
                self.__elements[lrzid]= line
 
185
 
 
186
                self.__log.AddEntry(self.__logtext)
 
187
 
 
188
            elif typ== 'M':
 
189
                # update measure
 
190
                pass
 
191
 
 
192
            elif typ== 'O':
 
193
                # update clone
 
194
                pass
 
195
 
 
196
            elif typ== 'P':
 
197
                # update point
 
198
                pass
 
199
 
 
200
            elif typ== 'T':
 
201
                # update text
 
202
                pass
 
203
 
 
204
            elif typ== 'V':
 
205
                # update view
 
206
                pass
 
207
 
 
208
 
 
209
    def Del(self, idlist):
 
210
        # delete drawing elements (list)
 
211
        for i in idlist:
 
212
            if i in self.__elements:
 
213
                # element with id is in the dictionairy
 
214
                del self.__elements[i]
 
215
                self.SetFreeId(i)
 
216
                self.__log.AddEntry('OK|LorzeDrawing.Del('+ i+ ')')
 
217
            else:
 
218
                # element with not in the dictionairy
 
219
                self.__log.AddEntry('FAIL|Id not in dictionary|LorzeDrawing.Del('+ i+ ')')
 
220
 
 
221
 
 
222
    def Get(self, idlist):
 
223
        # get drawing elements (list)
 
224
        elements= []
 
225
        for i in idlist:
 
226
            elements.append(self.__elements[i])
 
227
        return(elements)
 
228
 
 
229
 
 
230
    def GetElements(self):
 
231
        # get dictionairy with elements (dictionairy)
 
232
        return(self.__elements)
 
233
 
 
234
 
 
235
    def GetFreeId(self, typ):
 
236
        # get free id (list)
 
237
        if len(self.__newids[typ])== 1:
 
238
            # only one id: reading id, increase one
 
239
            lrzid= self.__newids[typ][0]
 
240
            self.__newids[typ][0]= lrzid+ 1
 
241
        else:
 
242
            # a few ids: reading id, then delete
 
243
            lrzid= self.__newids[typ][0]
 
244
            del self.__newids[typ][0]
 
245
 
 
246
        # 1st char is the type, the other are the number
 
247
        return(typ+ str(lrzid))
 
248
 
 
249
 
 
250
    def SetFreeId(self, lrzid):
 
251
        # mark id as free (string), 1st char is the type
 
252
        typ= lrzid[0]
 
253
 
 
254
        # the other chars ar a number
 
255
        value= int(lrzid[1:])
 
256
 
 
257
        # set id as free
 
258
        self.__newids[typ].insert(0, value)
 
259
 
 
260
 
 
261
    def SetBaseAttributes(self, element, lrzid, color, layer, style, width):
 
262
        # set basis attributes (LorzeDrawElement, attributes)
 
263
        element.SetId(lrzid)
 
264
 
 
265
        # default drawing attributes and all keys of the attribute dictionairys
 
266
        drawdefcolor= self.__attribute.GetDefaultColor()
 
267
        drawdeflayer= self.__attribute.GetDefaultLayer()
 
268
        drawdefstyle= self.__attribute.GetDefaultStyle()
 
269
        drawdefwidth= self.__attribute.GetDefaultWidth()
 
270
        colorkeys, layerkeys, stylekeys, widthkeys= self.__attribute.GetAllAttrNames()
 
271
 
 
272
        # color
 
273
        if color in colorkeys:
 
274
            element.SetColor(color)
 
275
        else:
 
276
            element.SetColor(drawdefcolor)
 
277
            self.__logtext+= '|color not in dictionairy, set default'
 
278
 
 
279
        #layer
 
280
        if layer in layerkeys:
 
281
            element.SetLayer(layer)
 
282
        else:
 
283
            element.SetLayer(drawdeflayer)
 
284
            self.__logtext+= '|layer not in dictionairy, set default'
 
285
 
 
286
        # style
 
287
        if style in stylekeys:
 
288
            element.SetStyle(style)
 
289
        else:
 
290
            element.SetStyle(drawdefstyle)
 
291
            self.__logtext+= '|style not in dictionairy, set default'
 
292
 
 
293
        # width
 
294
        if width in widthkeys:
 
295
            element.SetWidth(width)
 
296
        else:
 
297
            element.SetWidth(drawdefwidth)
 
298
            self.__logtext+= '|width not in dictionairy, set default'
 
299
 
 
300
        self.__logtext= 'OK|'+ self.__logtext
 
301
 
 
302
        return(element)
 
303
 
 
304
 
 
305
    def GetUsedAttributes(self):
 
306
        # get amounts of used attributes, define amounts
 
307
        color= set()
 
308
        layer= set()
 
309
        style= set()
 
310
        width= set()
 
311
 
 
312
        # get attributes
 
313
        for lrzid, lrzelem in self.__elements.items():
 
314
            color.add(lrzelem.GetColor())
 
315
            layer.add(lrzelem.GetLayer())
 
316
            style.add(lrzelem.GetStyle())
 
317
            width.add(lrzelem.GetWidth())
 
318
 
 
319
        return(color, layer, style, width)
 
320
 
 
321
 
 
322
    def CheckAttrDict(self):
 
323
        # check all attributes in connection with the dictionairys, get default drawing attributes and all keys of the attribute dictionairys
 
324
        drawdefcolor= self.__attribute.GetDefaultColor()
 
325
        drawdeflayer= self.__attribute.GetDefaultLayer()
 
326
        drawdefstyle= self.__attribute.GetDefaultStyle()
 
327
        drawdefwidth= self.__attribute.GetDefaultWidth()
 
328
        colorkeys, layerkeys, stylekeys, widthkeys= self.__attribute.GetAllAttrNames()
 
329
 
 
330
        for lrzid, lrzelem in self.__elements.items():
 
331
            # get attributes
 
332
            color= lrzelem.GetColor()
 
333
            layer= lrzelem.GetLayer()
 
334
            style= lrzelem.GetStyle()
 
335
            width= lrzelem.GetWidth()
 
336
 
 
337
            # set default color, string for lorzelog
 
338
            logtext= 'LorzeDrawing.CheckAttrDict()'
 
339
 
 
340
            # check attributes, if they not exists
 
341
            if color not in colorkeys:
 
342
                lrzelem.SetColor(drawdefcolor)
 
343
                self.__log.AddEntry('FAIL|colour not in dictionairy: '+ color+ '|'+ logtext+ '|Set default: '+ drawdefcolor)
 
344
 
 
345
            if layer not in layerkeys:
 
346
                lrzelem.SetLayer(drawdeflayer)
 
347
                self.__log.AddEntry('FAIL|layer not in dictionairy: '+ layer+ '|'+ logtext+ '|Set default: '+ drawdeflayer)
 
348
 
 
349
            if style not in stylekeys:
 
350
                lrzelem.SetStyle(drawdefstyle)
 
351
                self.__log.AddEntry('FAIL|style not in dictionairy: '+ style+ '|'+ logtext+ '|Set default: '+ drawdefstyle)
 
352
 
 
353
            if width not in widthkeys:
 
354
                lrzelem.SetWidth(drawdefwidth)
 
355
                self.__log.AddEntry('FAIL|width not in dictionairy: '+ width+ '|'+ logtext+ '|Set default: '+ drawdefwidth)
 
356
 
 
357
 
 
358
if __name__== '__main__':
 
359
    from lrzoptions import LorzeOptions
 
360
    from lrzlog import LorzeLog
 
361
    from lrzvalchck import LorzeValueCheck
 
362
    from lrzattribute import LorzeAttribute
 
363
    from lrzgeohelp import LorzeGeoHelp
 
364
 
 
365
    options= LorzeOptions()
 
366
    log= LorzeLog()
 
367
    valchck= LorzeValueCheck()
 
368
    attribute= LorzeAttribute(options, log, valchck)
 
369
    geohelp= LorzeGeoHelp(options)
 
370
 
 
371
    drawing= LorzeDrawing(attribute, log, geohelp)
 
372
    drawing.SetRec(1, 2, 3, 4)
 
373
    print('Rec', drawing.GetRec())
 
374
    print('')
 
375
    print('Add 2 lines; Red, Test 1, Solid, 0.18 mm, 0, 0, 10, 10; White, Test 2, Dashed 1, 0.25 mm, 10, 10, 0, 0')
 
376
    line1= ('L', ('Red', 'Test 1', 'Solid', '0.18 mm', (0, 0, 10, 10)))
 
377
    line2= ('L', ('White', 'Test 2', 'Dashed 1', '0.25 mm', (10, 10, 0, 0)))
 
378
    drawing.Add([line1, line2])
 
379
    a= drawing.GetElements()
 
380
    for i in a.keys():
 
381
        print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
382
    print('')
 
383
    print('Get with ids: L1')
 
384
    for i in drawing.Get(['L1']):
 
385
        print (i.GetId(), i.GetColor(),i.GetLayer(), i.GetStyle(), i.GetWidth(), i.GetCoords())
 
386
    print('')
 
387
    print('Update lines with ids: L0, L1; White, error, Dashed 1, error, 0.1, 0.1, 10.1, 10.1; error, Test 1, error, 0.18 mm, 11, 11, 1, 1')
 
388
    update1= ('L0', ('White', 'error', 'Dashed 1', 'error', (0.1, 0.1, 10.1, 10.1)))
 
389
    update2= ('L1', ('error', 'Test 1', 'error', '0.18 mm', (11, 11, 1, 1)))
 
390
    drawing.Update([update1, update2])
 
391
    a= drawing.GetElements()
 
392
    for i in a.keys():
 
393
        print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
394
    print('')
 
395
    print('Add 2 lines; Red, Test 2, Dashed 1, 0.18 mm; White, Test 1, Solid, 0.25 mm / remove 2 lines; L0, L2')
 
396
    line3= ('L', ('Red', 'Test 2', 'Dashed 1', '0.18 mm', (12, 12, 2, 2)))
 
397
    line4= ('L', ('White', 'Test 1', 'Solid', '0.25 mm', (13, 13, 3, 3)))
 
398
    drawing.Add([line3, line4])
 
399
    drawing.Del(['L0', 'L2'])
 
400
    print('After deleting element L0 and L2')
 
401
    a= drawing.GetElements()
 
402
    for i in a.keys():
 
403
        print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
404
    print('')
 
405
    print('Add 2 line; error, error, error, error, 14, 14, 4, 4; error, error, error, error, 14, 14, 4, 4')
 
406
    line5= ('L', ('error', 'error', 'error', 'error', (14, 14, 4, 4)))
 
407
    drawing.Add([line5, line5])
 
408
    a= drawing.GetElements()
 
409
    for i in a.keys():
 
410
        print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
411
    print('')
 
412
    drawing.Del(['L5'])
 
413
    print('Trying to delete element L5')
 
414
    print('')
 
415
    print('Get used attributes')
 
416
    print(drawing.GetUsedAttributes())
 
417
    print('')
 
418
    print('check attribute dictionairy')
 
419
    drawing.CheckAttrDict()
 
420
    print('')
 
421
    print('LorzeLog')
 
422
    for i in log.GetLog():
 
423
        print(i)