2
# -*- coding: utf-8 -*-
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
8
# This file is part of “LORZE erasandcad“
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.
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.
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/>.
16
from lrzdrawline import DrawLine
20
# Drawing object for LORZE erasandcad
22
def __init__(self, attribute, log, geohelp):
24
self.__attribute= attribute
29
# geometry helper object
30
self.__geohelp= geohelp
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
38
# dictionairy: drawing elements, key= id, value= drawing element
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
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}
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)
67
# get outline rectangle (float)
68
return(self.__recx, self.__recy, self.__recw, self.__rech)
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 ..)
75
# get typ & attributes
77
color, layer, style, width, coords= attributes
80
self.__logtext= 'LorzeDrawing.Add([('+ typ+ ', ('+ color+ ', '+ layer+ ', '+ style+ ', '+ width+ ', ('
83
self.__logtext+= ', '+ str(i)
85
self.__logtext+= str(i)
86
self.__logtext+= '))]'
110
line= DrawLine(self.__geohelp)
111
lrzid= self.GetFreeId('L')
112
self.__logtext+= '|New ID: '+ lrzid
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
118
self.__log.AddEntry(self.__logtext)
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 ..)
144
# get id & attributes
146
color, layer, style, width, coords= attributes
149
self.__logtext= 'LorzeDrawing.Update([('+ lrzid+ ', ('+ color+ ', '+ layer+ ', '+ style+ ', '+ width+ ', ('
152
self.__logtext+= ', '+ str(i)
154
self.__logtext+= str(i)
155
self.__logtext+= '))]'
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
186
self.__log.AddEntry(self.__logtext)
209
def Del(self, idlist):
210
# delete drawing elements (list)
212
if i in self.__elements:
213
# element with id is in the dictionairy
214
del self.__elements[i]
216
self.__log.AddEntry('OK|LorzeDrawing.Del('+ i+ ')')
218
# element with not in the dictionairy
219
self.__log.AddEntry('FAIL|Id not in dictionary|LorzeDrawing.Del('+ i+ ')')
222
def Get(self, idlist):
223
# get drawing elements (list)
226
elements.append(self.__elements[i])
230
def GetElements(self):
231
# get dictionairy with elements (dictionairy)
232
return(self.__elements)
235
def GetFreeId(self, typ):
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
242
# a few ids: reading id, then delete
243
lrzid= self.__newids[typ][0]
244
del self.__newids[typ][0]
246
# 1st char is the type, the other are the number
247
return(typ+ str(lrzid))
250
def SetFreeId(self, lrzid):
251
# mark id as free (string), 1st char is the type
254
# the other chars ar a number
255
value= int(lrzid[1:])
258
self.__newids[typ].insert(0, value)
261
def SetBaseAttributes(self, element, lrzid, color, layer, style, width):
262
# set basis attributes (LorzeDrawElement, attributes)
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()
273
if color in colorkeys:
274
element.SetColor(color)
276
element.SetColor(drawdefcolor)
277
self.__logtext+= '|color not in dictionairy, set default'
280
if layer in layerkeys:
281
element.SetLayer(layer)
283
element.SetLayer(drawdeflayer)
284
self.__logtext+= '|layer not in dictionairy, set default'
287
if style in stylekeys:
288
element.SetStyle(style)
290
element.SetStyle(drawdefstyle)
291
self.__logtext+= '|style not in dictionairy, set default'
294
if width in widthkeys:
295
element.SetWidth(width)
297
element.SetWidth(drawdefwidth)
298
self.__logtext+= '|width not in dictionairy, set default'
300
self.__logtext= 'OK|'+ self.__logtext
305
def GetUsedAttributes(self):
306
# get amounts of used attributes, define amounts
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())
319
return(color, layer, style, width)
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()
330
for lrzid, lrzelem in self.__elements.items():
332
color= lrzelem.GetColor()
333
layer= lrzelem.GetLayer()
334
style= lrzelem.GetStyle()
335
width= lrzelem.GetWidth()
337
# set default color, string for lorzelog
338
logtext= 'LorzeDrawing.CheckAttrDict()'
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)
345
if layer not in layerkeys:
346
lrzelem.SetLayer(drawdeflayer)
347
self.__log.AddEntry('FAIL|layer not in dictionairy: '+ layer+ '|'+ logtext+ '|Set default: '+ drawdeflayer)
349
if style not in stylekeys:
350
lrzelem.SetStyle(drawdefstyle)
351
self.__log.AddEntry('FAIL|style not in dictionairy: '+ style+ '|'+ logtext+ '|Set default: '+ drawdefstyle)
353
if width not in widthkeys:
354
lrzelem.SetWidth(drawdefwidth)
355
self.__log.AddEntry('FAIL|width not in dictionairy: '+ width+ '|'+ logtext+ '|Set default: '+ drawdefwidth)
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
365
options= LorzeOptions()
367
valchck= LorzeValueCheck()
368
attribute= LorzeAttribute(options, log, valchck)
369
geohelp= LorzeGeoHelp(options)
371
drawing= LorzeDrawing(attribute, log, geohelp)
372
drawing.SetRec(1, 2, 3, 4)
373
print('Rec', drawing.GetRec())
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()
381
print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
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())
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()
393
print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
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()
403
print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
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()
410
print (i, a[i].GetId(), a[i].GetColor(), a[i].GetLayer(), a[i].GetStyle(), a[i].GetWidth(), a[i].GetCoords())
413
print('Trying to delete element L5')
415
print('Get used attributes')
416
print(drawing.GetUsedAttributes())
418
print('check attribute dictionairy')
419
drawing.CheckAttrDict()
422
for i in log.GetLog():