Hi,

Recently I had enough of storing OCC's cad as files.
Sometimes all you need is a database.
Turns out that its really easy to do so. Perhaps its worth sharing how this
can be done...
Though this might be pretty trivial technically speaking, it can open up
interesting ways of collaborating on projects.

-jelle


================================================
from OCC.Utils.Topology import Topo

__author__ = 'jdf'


from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session

from OCC.Utils.Construct import make_cube
#from OCC.TopoDS import TopoDS_Shape
#
#TopoDS_Shape.__eq__ = lambda x: TopoDS_Shape.IsEqual(x)

cube = make_cube(1,1,1)

engine = create_engine('sqlite:///jelle.db')
Base = declarative_base(bind=engine)
Session = scoped_session(sessionmaker(engine))

def comp_brep(brepA, brepB):
    import ipdb; ipdb.set_trace()
    print 'jajajaj',brepB, brepA
    return brepA.IsEqual(brepB)

class Individual(Base):
    '''used to store data about an individual
    a row forms a generation
    '''
    __tablename__ = 'individual'

    id = Column(Integer, primary_key=True)
    #name = Column(String)
    #fullname = Column(String)
    #password = Column(String)
    generation = Column(Integer)
    ancestor_a = Column(PickleType())
    ancestor_b = Column(PickleType())
    brep = Column(PickleType(mutable=False)) #comparator=comp_brep)) #) )
    fitness = Column(Float)

Base.metadata.create_all()


indy = Individual()
indy.ancestor_a = 'parent_a'
indy.fitness = 12.
indy.ancestor_b = 'parent_a'
indy.generation = 1
#indy.id = 0
indy.brep = cube

'''
to INSERT many rows very quickly, use the "executemany" style of
insertion:


connection.execute(table.insert(), [{'foo':'row1'}, {'foo':'row2'},
{'foo':'row3'}, ...])
'''

s = Session()
# s.begin()
s.add(indy)
s.commit()
s.close()

print('closed the previous db session\nlet\'s see if we can read back the
objects, that be cool...')

engine = create_engine('sqlite:///jelle.db', echo=True)
Base = declarative_base(bind=engine)
Session = scoped_session(sessionmaker(engine))
s = Session()

from OCC.BRep import BRep_Tool
bt = BRep_Tool().Pnt

for indy in s.query(Individual):
print indy.id
for i in Topo(indy.brep).vertices():
    print bt(i).Coord()
================================================
_______________________________________________
Pythonocc-users mailing list
Pythonocc-users@gna.org
https://mail.gna.org/listinfo/pythonocc-users

Reply via email to