DM_get_by_id.py
1 ## @package python.demo.DM_get_by_id
2 #
3 # Each object that is inserted into odm gets a unique 64bit id that can be used for directly accessing the object.
4 #
5 # The following example creates an odm that contains two points, a polygon and a polygon. First the ids of the objects
6 # are collected and then used to access them by the getGeometry function
7 # (script works in python 2 and 3)
8 #
9 from __future__ import print_function # print function syntax as in python 3
10 
11 from opals import pyDM
12 
13 odm = pyDM.Datamanager.create("get_by_id_test.odm", False)
14 
15 # add two points to odm
16 pt1 = pyDM.Point(15.00, 85.00, 30.00)
17 pt2 = pyDM.Point(90.00, 10.00, 55.00)
18 odm.addPoint(pt1)
19 odm.addPoint(pt2)
20 
21 # add polyline to odm
22 lf = pyDM.PolylineFactory()
23 lf.addPoint(5.00, 5.00, 10.00)
24 lf.addPoint(20.00, 60.00, 25.00)
25 lf.addPoint(95.00, 92.00, 88.00)
26 line = lf.getPolyline()
27 odm.addPolyline(line)
28 
29 # add polygon to odm
30 pf = pyDM.PolygonFactory()
31 pf.addPoint(35.00, 35.00, 40.00)
32 pf.addPoint(45.00, 50.00, 60.00)
33 pf.addPoint(55.00, 40.00, 75.00)
34 pf.closePart()
35 polygon = pf.getPolygon()
36 odm.addPolygon(polygon)
37 
38 #save odm
39 odm.save()
40 
41 lf = pyDM.AddInfoLayoutFactory()
42 lf.addColumn(pyDM.ColumnSemantic.Id)
43 layout = lf.getLayout()
44 
45 ids = []
46 
47 print("List objects within odm:")
48 for obj in odm.geometries(layout):
49  print("\tType:",obj.type(),"\tId:",obj.info().get(0))
50  ids.append(obj.info().get(0))
51 
52 print("\nAccess objects by id:")
53 for id in ids:
54  obj = odm.getGeometry(id)
55  obj.cloneAddInfoView(layout,False)
56  print("\tType:", obj.type(),"\tId:",id,"(check id:",obj.info().get(0),")")
57