~lorzeteam/lorze/trunk

« back to all changes in this revision

Viewing changes to Scripts/lrzdrawing.py

  • Committer: Andreas Ulrich
  • Date: 2012-10-20 15:43:56 UTC
  • Revision ID: ulrich3110@gmail.com-20121020154356-8szy0rhp8128owyo
LORZE erasandcad 0.2 alpha 2012/10/20
attributes, attribute dialogue, protocoll

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
class LorzeDrawing():
18
18
 
19
 
    def __init__(self, defaultattr):
20
 
        # defaultattr: default attributes (colour, layer, penstyle, pensize)
21
 
 
22
 
        # default drawing attributes: colour, layer, penstyle, pensize
23
 
        self.__drawdefcolour, self.__drawdeflayer, self.__drawdefstyle, self.__drawdefsize= defaultattr
 
19
    def __init__(self, parent):
 
20
        # parent, LorzeGraphicPanel
 
21
        self.__parent= parent
24
22
 
25
23
        # float: outline rectangle x1, y1, x2, y2
26
24
        self.__recx, self.__recy, self.__recw, self.__rech= 0.0, 0.0, 0.0, 0.0
59
57
 
60
58
 
61
59
    def Add(self, addlist):
62
 
        # adding drawing elements (list) - addlist: [('typ', attributes), ..] - attributes: (colour, layer, penstyle, pensize, coords) - coords: (x1, y1, x2, y2 ..)
 
60
        # 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 ..)
63
61
        for i in addlist:
64
62
            typ, attributes= i
65
 
            colour, layer, penstyle, pensize, coords= attributes
 
63
            color, layer, style, width, coords= attributes
66
64
 
67
65
            if typ== 'B':
68
66
                # add block
88
86
                # add line
89
87
                line= DrawLine()
90
88
                lrzid= self.GetFreeId('L')
91
 
                line= self.SetBaseAttributes(line, lrzid, colour, layer, penstyle, pensize)
 
89
                line= self.SetBaseAttributes(line, lrzid, color, layer, style, width)
92
90
                line.SetCoords(coords[0], coords[1], coords[2], coords[3])
93
91
                self.__elements[lrzid]= line
94
92
 
114
112
 
115
113
 
116
114
    def Update(self, updatelist):
117
 
        # updateing drawing elements (list) - updatelist: [(lorze-id, new_attributes), ..] - new_attributes: (colour, layer, penstyle, pensize, coords) - coords: (x1, y1, x2, y2 ..)
 
115
        # updateing drawing elements (list) / updatelist: [(lorze-id, new_attributes), ..] / new_attributes: (color, layer, style, width, coords) - coords: (x1, y1, x2, y2 ..)
118
116
        for i in updatelist:
119
117
            lrzid, attributes= i
120
 
            colour, layer, penstyle, pensize, coords= attributes
 
118
            color, layer, style, width, coords= attributes
121
119
            typ= lrzid[0]
122
120
 
123
121
            if typ== 'B':
143
141
            elif typ== 'L':
144
142
                # update line
145
143
                line= self.__elements[lrzid]
146
 
                line= self.SetBaseAttributes(line, lrzid, colour, layer, penstyle, pensize)
 
144
                line= self.SetBaseAttributes(line, lrzid, color, layer, style, width)
147
145
                line.SetCoords(coords[0], coords[1], coords[2], coords[3])
148
146
                self.__elements[lrzid]= line
149
147
 
214
212
        self.__newids[typ].insert(0, value)
215
213
 
216
214
 
217
 
    def SetBaseAttributes(self, element, lrzid, colour, layer, penstyle, pensize):
 
215
    def SetBaseAttributes(self, element, lrzid, color, layer, style, width):
218
216
        # set basis attributes (LorzeDrawElement, attributes)
219
217
        element.SetId(lrzid)
220
 
        element.SetColour(colour)
221
 
        element.SetLayer(layer)
222
 
        element.SetPenStyle(penstyle)
223
 
        element.SetPenSize(pensize)
 
218
 
 
219
        # default drawing attributes and all keys of the attribute dictionairys
 
220
        drawdefcolor, drawdeflayer, drawdefstyle, drawdefwidth= self.__parent.GetDefAttributes()
 
221
        colorkeys, layerkeys, stylekeys, widthkeys= self.__parent.GetAllAttrNames()
 
222
 
 
223
        # color
 
224
        if color in colorkeys:
 
225
            element.SetColor(color)
 
226
        else:
 
227
            element.SetColor(drawdefcolor)
 
228
 
 
229
        #layer
 
230
        if layer in layerkeys:
 
231
            element.SetLayer(layer)
 
232
        else:
 
233
            element.SetLayer(drawdeflayer)
 
234
 
 
235
        # style
 
236
        if style in stylekeys:
 
237
            element.SetStyle(style)
 
238
        else:
 
239
            element.SetStyle(drawdefstyle)
 
240
 
 
241
        # width
 
242
        if width in widthkeys:
 
243
            element.SetWidth(width)
 
244
        else:
 
245
            element.SetWidth(drawdefwidth)
 
246
 
224
247
        return(element)
225
248
 
226
249
 
227
 
    def GetDefAttr(self):
228
 
        # Get default attributes for new drawing elements
229
 
        return(self.__drawdefcolour, self.__drawdeflayer, self.__drawdefstyle, self.__drawdefsize)
230
 
 
231
 
 
232
 
    def SetDefAttr(self, drawdefcolour, drawdeflayer, drawdefstyle, drawdefsize):
233
 
        # Set default attributes for new drawing elements
234
 
        self.__drawdefcolour= drawdefcolour
235
 
        self.__drawdeflayer= drawdeflayer
236
 
        self.__drawdefstyle= drawdefstyle
237
 
        self.__drawdefsize= drawdefsize
238
 
 
239
 
 
240
250
    def GetUsedAttributes(self):
241
251
        # get amounts of used attributes, define amounts
242
 
        colour= set()
 
252
        color= set()
243
253
        layer= set()
244
254
        style= set()
245
 
        size= set()
 
255
        width= set()
246
256
 
247
 
        # set attributes
 
257
        # get attributes
248
258
        for lrzid, lrzelem in self.__elements.items():
249
 
            colour.add(lrzelem.GetColour())
 
259
            color.add(lrzelem.GetColor())
250
260
            layer.add(lrzelem.GetLayer())
251
 
            style.add(lrzelem.GetPenStyle()[0]+str(lrzelem.GetPenStyle()[1]))
252
 
            size.add(lrzelem.GetPenSize())
253
 
 
254
 
        return(colour, layer, style, size)
 
261
            style.add(lrzelem.GetStyle())
 
262
            width.add(lrzelem.GetWidth())
 
263
 
 
264
        return(color, layer, style, width)
 
265
 
 
266
 
 
267
    def CheckAttrDict(self):
 
268
        # check all attributes in connection with the dictionairys, get default drawing attributes and all keys of the attribute dictionairys
 
269
        drawdefcolor, drawdeflayer, drawdefstyle, drawdefwidth= self.__parent.GetDefAttributes()
 
270
        colorkeys, layerkeys, stylekeys, widthkeys= self.__parent.GetAllAttrNames()
 
271
 
 
272
        for lrzid, lrzelem in self.__elements.items():
 
273
            # get attributes
 
274
            color= lrzelem.GetColor()
 
275
            layer= lrzelem.GetLayer()
 
276
            style= lrzelem.GetStyle()
 
277
            width= lrzelem.GetWidth()
 
278
 
 
279
            # check attributes, if they not exists
 
280
            if color not in colorkeys:
 
281
                lrzelem.SetColor(drawdefcolor)
 
282
 
 
283
            if layer not in layerkeys:
 
284
                lrzelem.SetLayer(drawdeflayer)
 
285
 
 
286
            if style not in stylekeys:
 
287
                lrzelem.SetStyle(drawdefstyle)
 
288
 
 
289
            if widht not in widthkeys:
 
290
                lrzelem.SetWidth(drawdefwidth)
 
291
 
 
292
 
 
293
class TestParent():
 
294
    # object for testing
 
295
 
 
296
    def __init__(self):
 
297
        self.__drawing= LorzeDrawing(self)
 
298
        self.__drawing.SetRec(1, 2, 3, 4)
 
299
        print('Rec', self.__drawing.GetRec())
 
300
 
 
301
        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')
 
302
        line1= ('L', ('Red', 'Test 1', 'Solid', '0.18 mm', (0, 0, 10, 10)))
 
303
        line2= ('L', ('White', 'Test 2', 'Dashed 1', '0.25 mm', (10, 10, 0, 0)))
 
304
        self.__drawing.Add([line1, line2])
 
305
        a= self.__drawing.GetElements()
 
306
        for i in a.keys():
 
307
            print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
308
 
 
309
        print('Get with ids: L1')
 
310
        for i in self.__drawing.Get(['L1']):
 
311
            print (i.GetId(), i.GetColor(),i.GetLayer(), i.GetStyle(), i.GetWidth(), i.GetCoords())
 
312
 
 
313
        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')
 
314
        update1= ('L0', ('White', 'error', 'Dashed 1', 'error', (0.1, 0.1, 10.1, 10.1)))
 
315
        update2= ('L1', ('error', 'Test 1', 'error', '0.18 mm', (11, 11, 1, 1)))
 
316
        self.__drawing.Update([update1, update2])
 
317
        a= self.__drawing.GetElements()
 
318
        for i in a.keys():
 
319
            print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
320
 
 
321
        print('Add 2 lines; Red, Test 2, Dashed 1, 0.18 mm; White, Test 1, Solid, 0.25 mm / remove 2 lines; L0, L2')
 
322
        line3= ('L', ('Red', 'Test 2', 'Dashed 1', '0.18 mm', (12, 12, 2, 2)))
 
323
        line4= ('L', ('White', 'Test 1', 'Solid', '0.25 mm', (13, 13, 3, 3)))
 
324
        self.__drawing.Add([line3, line4])
 
325
        self.__drawing.Del(['L0', 'L2'])
 
326
        print('After deleting element L0 and L2')
 
327
        a= self.__drawing.GetElements()
 
328
        for i in a.keys():
 
329
            print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
330
 
 
331
        print('Add 2 line; error, error, error, error, 14, 14, 4, 4; error, error, error, error, 14, 14, 4, 4')
 
332
        line5= ('L', ('error', 'error', 'error', 'error', (14, 14, 4, 4)))
 
333
        self.__drawing.Add([line5, line5])
 
334
        a= self.__drawing.GetElements()
 
335
        for i in a.keys():
 
336
            print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
 
337
 
 
338
        print('')
 
339
        print('Get used attributes')
 
340
        print(self.__drawing.GetUsedAttributes())
 
341
 
 
342
 
 
343
    def GetDefAttributes(self):
 
344
        # get defaults for drawing, all values are strings
 
345
        return('Black', 'System', 'Solid', '1')
 
346
 
 
347
 
 
348
    def GetAllAttrNames(self):
 
349
        # return all attribute names, colors, layers, styles, widths
 
350
        return(['Red', 'White'], ['Test 1', 'Test 2'], ['Solid', 'Dashed 1'], ['0.18 mm', '0.25 mm'])
255
351
 
256
352
 
257
353
if __name__== '__main__':
258
 
    data= LorzeDrawing(('#GHIJKL', 'testlayer', ('DOT_DASH', []), 7.07))
259
 
    data.SetRec(1, 2, 3, 4)
260
 
    print('Rec '+ str(data.GetRec()))
261
 
 
262
 
    elements= [('L', ('BLACK', 'abc', ('SOLID', []), 1, (0, 0, 10, 10))), ('L', ('RED', 'def', ('DOT', []), 2, (10, 10, 0, 0))), ('L', ('GREEN', 'ghi', ('LONG_DASH', []), 3, (5, 5, -5, -5))), ('L', ('BLUE', 'jkl', ('SHORT_DASH', []), 4, (-5, 5, 5, -5))), ('L', ('CYAN', 'mno', ('DOT_DASH', []), 5, (20, 20, -10, -10)))]
263
 
    print('Adding elements')
264
 
    data.Add(elements)
265
 
    a= data.GetElements()
266
 
    for i in a.keys():
267
 
        print (i, a[i].GetId(), a[i].GetCoords(), a[i].GetColour(), a[i].GetLayer(), a[i].GetPenStyle(), a[i].GetPenSize())
268
 
 
269
 
    b= a.keys()[1:4]
270
 
    print('Get with ids ', b)
271
 
    for i in data.Get(b):
272
 
        print (i.GetId(), i.GetCoords(), i.GetColour(),i.GetLayer(), i.GetPenStyle(), i.GetPenSize())
273
 
 
274
 
    elements= [('L2', ('BLUE VIOLET', 'aaa', ('TRANSPARENT', []), 6, (1.1, 2.1, 3.1, 4.1))), ('L4', ('DARK GREY', 'bbb', ('USER_DASH', [1,1]), 7, (11.1, 12.1, 13.1, 14.1)))]
275
 
    print('Updating elements & GetElements():')
276
 
    data.Update(elements)
277
 
    a= data.GetElements()
278
 
    for i in a.keys():
279
 
        print (i, a[i].GetId(), a[i].GetCoords(), a[i].GetColour(), a[i].GetLayer(), a[i].GetPenStyle(), a[i].GetPenSize())
280
 
 
281
 
    b= ['L1', 'L3']
282
 
    print('Deleting elements wiht ids:', b, '& GetElements():')
283
 
    data.Del(b)
284
 
    a= data.GetElements()
285
 
    for i in a.keys():
286
 
        print (i, a[i].GetId(), a[i].GetCoords(), a[i].GetColour(), a[i].GetLayer(), a[i].GetPenStyle(), a[i].GetPenSize())
287
 
 
288
 
    elements= [('L', ('DARK ORCHID', 'ccc', ('SOLID', []), 8, (11, 22, 33, 44))), ('L', ('FOREST GREEN', 'ddd', ('DOT', []), 9, (55, 66, 77, 88))), ('L', ('GOLDENROD', 'eee', ('LONG_DASH', []), 0.1, (99, -11, -22, -33)))]
289
 
    print('Adding new elements & GetElements():')
290
 
    data.Add(elements)
291
 
    a= data.GetElements()
292
 
    for i in a.keys():
293
 
        print (i, a[i].GetId(), a[i].GetCoords(), a[i].GetColour(), a[i].GetLayer(), a[i].GetPenStyle(), a[i].GetPenSize())
294
 
 
295
 
    b= ['L0', 'L5']
296
 
    print('Deleting elements wiht ids:', b, '& GetElements():')
297
 
    data.Del(b)
298
 
    a= data.GetElements()
299
 
    for i in a.keys():
300
 
        print (i, a[i].GetId(), a[i].GetCoords(), a[i].GetColour(), a[i].GetLayer(), a[i].GetPenStyle(), a[i].GetPenSize())
301
 
 
302
 
    elements= [('L', ('FIREBRICK', 'fff', ('SHORT_DASH', []), 0.2, (-44, -55, -66, -77))), ('L', ('GREEN YELLOW', 'ggg', ('DOT_DASH', []), 0.3, (-88, -99, 0.1, 0.2))), ('L', ('INDIAN RED', 'hhh', ('TRANSPARENT', []), 0.4, (0.3, 0.4, 0.5, 0.6)))]
303
 
    print('Adding new elements & GetElements():')
304
 
    data.Add(elements)
305
 
    a= data.GetElements()
306
 
    for i in a.keys():
307
 
        print (i, a[i].GetId(), a[i].GetCoords(), a[i].GetColour(), a[i].GetLayer(), a[i].GetPenStyle(), a[i].GetPenSize())
308
 
 
309
 
    print(u'Set new default attributes: #123456, newlayer, SOLID, 1.4')
310
 
    data.SetDefAttr('#123456', 'newlayer', ('SOLID', []), 1.4)
311
 
    print(u'Get new default attributes)', data.GetDefAttr())
312
 
 
313
 
    print(u'Get used attributes'), data.GetUsedAttributes()
 
354
    test= TestParent()