Skip to content

Commit 373252d

Browse files
committed
Change the property system now all property are stored in the db
Simple fix
1 parent f5cb605 commit 373252d

File tree

10 files changed

+125
-67
lines changed

10 files changed

+125
-67
lines changed

PythonCAD/Generic/Kernel/Command/propertycommand.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def changeProp(self, _id):
4545
entity=self.document.getEntity(_id)
4646
style=entity.getInnerStyle()
4747
style.Derived()
48-
for stylePropName,stylePropValue in self.value[1].items():
49-
style.setStyleProp(stylePropName,stylePropValue)
48+
entity.resetProperty()
49+
for PropName,PropValue in self.value[1].get('property',{}).items():
50+
entity.addPropertie(PropName,PropValue)
51+
#style.setStyleProp(stylePropName,stylePropValue)
5052
entity.style=self.document.saveEntity(style)
5153
self.document.saveEntity(entity)
5254

PythonCAD/Generic/Kernel/Db/basedb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def createConnection(self,dbPath=None):
4545
f=tempfile.NamedTemporaryFile(prefix='PyCad_',suffix='.pdr')
4646
dbPath=f.name
4747
f.close()
48-
self.__dbConnection = sql.connect(dbPath)
48+
self.__dbConnection = sql.connect(str(dbPath))
4949
self.dbPath=dbPath
5050

5151
def setConnection(self,dbConnection):

PythonCAD/Generic/Kernel/Db/entitydb.py

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ class EntityDb(BaseDb):
3434
"""
3535
def __init__(self,dbConnection):
3636
BaseDb.__init__(self)
37+
self._entFields={
38+
'pycad_id':'INTEGER PRIMARY KEY',
39+
'pycad_entity_id':'INTEGER',
40+
'pycad_object_type':'TEXT',
41+
'pycad_object_definition':'TEXT',
42+
'pycad_object_style':'TEXT',
43+
'pycad_security_id':'INTEGER',
44+
'pycad_undo_id':'INTEGER',
45+
'pycad_entity_state':'TEXT',
46+
'pycad_index':'NUMERIC',
47+
'pycad_visible':'INTEGER',
48+
'pycad_undo_visible':'INTEGER',
49+
'pycad_locked':'INTEGER',
50+
'pycad_bbox_xmin':'REAL',
51+
'pycad_bbox_ymin':'REAL',
52+
'pycad_bbox_xmax':'REAL',
53+
'pycad_bbox_ymax':'REAL',
54+
'pycad_property':'TEXT'
55+
}
56+
def creationFieldsStr():
57+
outStr=''
58+
for fieldName,fieldValue in self._entFields.items():
59+
outStr=+'%s %s,'%(str(fieldName),str(fieldValue))
60+
return outStr
61+
def addTableField(fieldName,fieldType):
62+
sql="ALTER TABLE pycadent ADD COLUMN %s %s "%(str(fieldName),str(fieldType))
63+
self.makeUpdateInsert(sql)
64+
3765
if dbConnection is None:
3866
self.createConnection()
3967
else:
@@ -42,23 +70,15 @@ def __init__(self,dbConnection):
4270
_table=self.makeSelect(_sqlCheck).fetchone()
4371
if _table is None:
4472
_sqlCreation="""CREATE TABLE pycadent(
45-
pycad_id INTEGER PRIMARY KEY,
46-
pycad_entity_id INTEGER,
47-
pycad_object_type TEXT,
48-
pycad_object_definition TEXT,
49-
pycad_object_style TEXT,
50-
pycad_security_id INTEGER,
51-
pycad_undo_id INTEGER,
52-
pycad_entity_state TEXT,
53-
pycad_index NUMERIC,
54-
pycad_visible INTEGER,
55-
pycad_undo_visible INTEGER,
56-
pycad_locked INTEGER,
57-
pycad_bbox_xmin REAL,
58-
pycad_bbox_ymin REAL,
59-
pycad_bbox_xmax REAL,
60-
pycad_bbox_ymax REAL)"""
61-
self.makeUpdateInsert(_sqlCreation)
73+
%s)"""%creationFieldsStr()
74+
else:
75+
rows=self.makeSelect("pragma table_info('pycadent')")
76+
dbColumns=dict([(row[1],row[2]) for row in rows])
77+
for classColumn in self._entFields.keys():
78+
if not classColumn in dbColumns:
79+
addTableField(classColumn,self._entFields[classColumn])
80+
81+
6282
self.__revisionIndex=self.getRevisionIndex()
6383

6484
def getRevisionIndex(self):
@@ -90,6 +110,7 @@ def saveEntity(self,entityObj,undoId):
90110
_entityId=entityObj.getId()
91111
_entityDump=pickle.dumps(entityObj.getConstructionElements())
92112
_entityType=entityObj.getEntityType()
113+
_property=pickle.dumps(entityObj.properties)
93114
_entityVisible=entityObj.visible
94115
_styleObject=pickle.dumps(entityObj.style)
95116
_xMin,_yMin,_xMax,_yMax=entityObj.getBBox()
@@ -108,8 +129,9 @@ def saveEntity(self,entityObj,undoId):
108129
pycad_bbox_ymax,
109130
pycad_entity_state,
110131
pycad_index,
111-
pycad_visible) VALUES
112-
(?,?,?,?,?,1,?,?,?,?,?,?,?)"""
132+
pycad_visible,
133+
pycad_property) VALUES
134+
(?,?,?,?,?,1,?,?,?,?,?,?,?,?)"""
113135

114136
tupleArg=(
115137
_entityId,
@@ -123,7 +145,8 @@ def saveEntity(self,entityObj,undoId):
123145
_yMax,
124146
_revisionState,
125147
_revisionIndex,
126-
_entityVisible)
148+
_entityVisible,
149+
_property)
127150
self.makeUpdateInsert(_sqlInsert, tupleArg)
128151

129152
def getEntityFromTableId(self,entityTableId):
@@ -137,7 +160,8 @@ def getEntityFromTableId(self,entityTableId):
137160
pycad_object_style,
138161
pycad_entity_state,
139162
pycad_index,
140-
pycad_visible
163+
pycad_visible,
164+
pycad_property
141165
FROM pycadent
142166
WHERE pycad_id=%s"""%str(entityTableId)
143167
_rows=self.makeSelect(_sqlGet)
@@ -150,6 +174,8 @@ def getEntityFromTableId(self,entityTableId):
150174
_outObj.state=_row[4]
151175
_outObj.index=_row[5]
152176
_outObj.visible=_row[6]
177+
for name,value in pickle.loads(str(_row[7])):
178+
_outObj.addPropertie(name, value)
153179
_outObj.updateBBox()
154180
return _outObj
155181

@@ -165,7 +191,8 @@ def getEntityEntityId(self,entityId):
165191
pycad_object_style,
166192
pycad_entity_state,
167193
pycad_index,
168-
pycad_visible
194+
pycad_visible,
195+
pycad_property
169196
FROM pycadent
170197
WHERE pycad_entity_id=%s ORDER BY pycad_id DESC"""%str(entityId)
171198
_dbEntRow=self.makeSelect(_sqlGet)
@@ -177,6 +204,8 @@ def getEntityEntityId(self,entityId):
177204
_entObj.state=_row[5]
178205
_entObj.index=_row[6]
179206
_entObj.visible=_row[7]
207+
for name,value in pickle.loads(str(_row[8])):
208+
_entObj.addPropertie(name, value)
180209
_entObj.updateBBox()
181210
return _entObj
182211

@@ -197,7 +226,8 @@ def getEntitysFromStyle(self,styleId):
197226
pycad_object_style,
198227
pycad_entity_state,
199228
pycad_index,
200-
pycad_visible
229+
pycad_visible,
230+
pycad_property
201231
FROM pycadent
202232
WHERE PyCad_Id IN (
203233
SELECT max(PyCad_Id)
@@ -213,6 +243,8 @@ def getEntitysFromStyle(self,styleId):
213243
_objEnt.state=_row[5]
214244
_objEnt.index=_row[6]
215245
_objEnt.visible=_dbEntRow[7]
246+
for name,value in pickle.loads(str(_row[9])):
247+
_objEnt.addPropertie(name, value)
216248
_objEnt.updateBBox()
217249
_outObj.append(_objEnt)
218250
return _outObj
@@ -229,7 +261,8 @@ def _getEntInVersion(self, versionIndex):
229261
pycad_object_style,
230262
pycad_entity_state,
231263
pycad_index,
232-
pycad_visible
264+
pycad_visible,
265+
pycad_property
233266
FROM pycadent
234267
WHERE PyCad_Id IN (
235268
SELECT max(PyCad_Id)
@@ -269,7 +302,8 @@ def getMultiFilteredEntity(self, visible=1, entityType='ALL', entityTypeArray=No
269302
pycad_object_style,
270303
pycad_entity_state,
271304
pycad_index,
272-
pycad_visible
305+
pycad_visible,
306+
pycad_property
273307
FROM pycadent
274308
WHERE pycad_id IN (
275309
SELECT max(pycad_id)
@@ -295,7 +329,10 @@ def getEntityFromType(self,entityType):
295329
_objEnt.state=_row[5]
296330
_objEnt.index=_row[6]
297331
_objEnt.visible=_row[7]
298-
_objEnt.updateBBox()
332+
if _row[8]!=None:
333+
for name,value in pickle.loads(str(_row[8])):
334+
_objEnt.addPropertie(name, value)
335+
_objEnt.updateBBox()
299336
_outObj.append(_objEnt)
300337
return _outObj
301338

@@ -321,7 +358,8 @@ def convertRowToDbEnt(self, row):
321358
pycad_object_style,
322359
pycad_entity_state,
323360
pycad_index,
324-
pycad_visible
361+
pycad_visible,
362+
pycad_property
325363
FROM pycadent
326364
"""
327365
_style=pickle.loads(str(row[4]))
@@ -330,6 +368,9 @@ def convertRowToDbEnt(self, row):
330368
_objEnt.state=row[5]
331369
_objEnt.index=row[6]
332370
_objEnt.visible=row[7]
371+
if row[8]!=None:
372+
for name,value in pickle.loads(str(row[8])):
373+
_objEnt.addPropertie(name, value)
333374
_objEnt.updateBBox()
334375
return _objEnt
335376

@@ -450,6 +491,7 @@ def uptateEntity(self, entityObj):
450491
_xMin,_yMin,_xMax,_yMax=entityObj.getBBox()
451492
_revisionIndex=entityObj.index
452493
_revisionState=entityObj.state
494+
_property=pickle.dumps(entityObj.property)
453495
_sqlInsert="""UPDATE pycadent set
454496
pycad_object_type="%s",
455497
pycad_object_definition="%s",
@@ -460,7 +502,8 @@ def uptateEntity(self, entityObj):
460502
pycad_bbox_ymax=%s,
461503
pycad_entity_state="%s",
462504
pycad_index=%s,
463-
pycad_visible=%s
505+
pycad_visible=%s,
506+
pycad_property=%s
464507
WHERE PyCad_Id IN (
465508
SELECT max(PyCad_Id)
466509
FROM pycadent
@@ -478,7 +521,8 @@ def uptateEntity(self, entityObj):
478521
str(_revisionState),
479522
str(_revisionIndex),
480523
str(_entityVisible),
481-
str(_entityId))
524+
str(_entityId),
525+
str(_property))
482526
#**************************************
483527
#**************Attention***************
484528
#**************************************

PythonCAD/Generic/Kernel/Db/pycadobject.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
#
2121
# This module provide basic pythoncadObject
2222
#
23-
from Kernel.GeoEntity.style import Style
23+
from Kernel.GeoEntity.style import Style
24+
from Kernel.exception import EntityMissing
2425

2526
class PyCadObject(object):
2627
"""
27-
This class provide basic information usefoul for the
28-
db like id for exsample
28+
This class provide basic information for all the pythoncad object
2929
"""
30-
def __init__(self,objId,style,eType):
30+
def __init__(self,objId,style,eType,properties={}):
3131
from Kernel.initsetting import OBJECT_STATE
3232
self.OBJECT_STATE=OBJECT_STATE
3333
self.__entityId=objId
@@ -36,7 +36,36 @@ def __init__(self,objId,style,eType):
3636
self.__visible=1
3737
self.__style=style
3838
self.__entType=eType
39+
self.__properties=properties
40+
41+
42+
def addPropertie(self,name,value):
43+
"""
44+
add a properties to the object
45+
"""
46+
self.__properties[name]=value
3947

48+
def getPropertie(self,name):
49+
"""
50+
get the properties with a given name
51+
"""
52+
if name in self.__properties:
53+
return self.__properties[name]
54+
raise EntityMissing("No entity with name %s"%str(name))
55+
56+
def resetProperty(self):
57+
"""
58+
reset the property
59+
"""
60+
self.__properties={}
61+
62+
@property
63+
def properties(self):
64+
"""
65+
get all the properties from the entity
66+
"""
67+
return self.__properties
68+
4069
def setVisible(self, visible):
4170
"""
4271
set the visible value
@@ -83,7 +112,7 @@ def getNewIndex(self):
83112
"""
84113
Get the new index of the current entity
85114
"""
86-
if index :
115+
if self._index:
87116
self._index+=self._index
88117
self.__state=self.OBJECT_STATE[0]
89118
else:

PythonCAD/Generic/Kernel/GeoEntity/point.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ def __init__(self, x, y=None):
5858
raise SyntaxError, "Invalid call to Point()."
5959
self.__x = _x
6060
self.__y = _y
61+
6162
def getPoint(self):
6263
return self
64+
6365
def __str__(self):
6466
return "Point : (%g,%g)" % (self.__x, self.__y)
67+
6568
@property
6669
def info(self):
6770
return "Point : (%g,%g)" % (self.__x, self.__y)
71+
6872
def __sub__(self, p):
6973
"""
7074
Return the separation between two points.

PythonCAD/Generic/Kernel/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def __init__(self,dbPath=None):
9898
self.__RelationDb=RelationDb(self.getConnection())
9999
# Some inizialization parameter
100100
self.__bulkCommit=False
101-
self.__bulkUndoIndex=-1 # undo index are alweys positive so we do not breke in case missing entity id
101+
self.__bulkUndoIndex=-1 # undo index are always positive so we do not brake in case missing entity id
102102
self.__entId=self.__EntityDb.getNewEntId()
103103
# set the default style
104104
self.__logger.debug('Set Style')
@@ -118,7 +118,7 @@ def __init__(self,dbPath=None):
118118

119119
def addPropertie(self,name,value):
120120
"""
121-
add a propertys to the object
121+
add a properties to the object
122122
"""
123123
self.__property[name]=value
124124

PythonCAD/Generic/Kernel/entity.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# This module provide basic DB class for storing entity in pythoncad
2222
#
2323

24-
from Kernel.exception import EntityMissing
24+
2525
from Kernel.Db.pycadobject import *
2626
from Kernel.GeoEntity.point import Point
2727
from Kernel.GeoEntity.style import Style
@@ -38,29 +38,9 @@ def __init__(self,entType,constructionElements,style,objId):
3838
raise TypeError,'type error in dictionary'
3939
PyCadObject.__init__(self,eType=entType, objId=objId,style=style)
4040
self.setConstructionElements(constructionElements)
41-
self._properties={}
42-
self._snapPoints=[]
43-
44-
def addPropertie(self,name,value):
45-
"""
46-
add a propertys to the object
47-
"""
48-
self._properties[name]=value
4941

50-
def getPropertie(self,name):
51-
"""
52-
get the properties with a given name
53-
"""
54-
if name in self._properties:
55-
return self._properties[name]
56-
raise EntityMissing("No entity with name %s"%str(name))
42+
self._snapPoints=[]
5743

58-
@property
59-
def properties(self):
60-
"""
61-
get all the properties from the entity
62-
"""
63-
return self._properties
6444

6545
def __str__(self):
6646
return 'Entity : %s'%self.eType

0 commit comments

Comments
 (0)