Salome HOME
Initial version
[modules/gui.git] / doc / salome / tui / KERNEL / exemple / Example20.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>\r
2 <HEAD>\r
3 <!-- hennerik CVSweb $Revision$ -->\r
4 <TITLE>Exemple</TITLE></HEAD>\r
5 <BODY BGCOLOR="#eeeeee">\r
6 <HR noshade><PRE>\r
7 #^SALOMEDS methods : FindOrCreateAttribute, SaveAs, FindComponent, FindAttribute, NewCommand, GetProperties, Open, CommitCommand, NewBuilder^\r
8 #^Attribute methods : SetLocked, IsLocked, GetCreationMode, SetCreationDate, IsModified, GetUserName, Value, SetUserName, SetValue, GetCreationDate^\r
9 \r
10 batchmode_geompy.myBuilder._set_UndoLimit(20)\r
11 \r
12 #--------------------------------------------------------------------------\r
13 # &nbsp; &nbsp; &nbsp; create AttributeReal &nbsp; &nbsp; &nbsp;\r
14 #===============================================\r
15 A = batchmode_geompy.myBuilder.FindOrCreateAttribute(batchmode_geompy.father, &quot;AttributeReal&quot;)\r
16 if A == None :\r
17  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Can't create AttributeReal attribute&quot;\r
18 A = A._narrow(SALOMEDS.AttributeReal)\r
19 A.SetValue(0.0001)\r
20 if A.Value() != 0.0001:\r
21  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Error : wrong value of &nbsp;AttributeReal&quot;\r
22 \r
23 # &nbsp; &nbsp; &nbsp;create AttributeStudyProperties\r
24 #================================================\r
25 A = batchmode_geompy.myStudy.GetProperties()\r
26 if A == None :\r
27  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Can't create AttributeStudyProperties attribute&quot;\r
28 A = A._narrow(SALOMEDS.AttributeStudyProperties)\r
29 \r
30 batchmode_geompy.myBuilder.NewCommand();\r
31 print &quot;A.GetUserName()= &quot;, A.GetUserName()\r
32 res,mm,hh,dd,mnth,yy=A.GetCreationDate()\r
33 print &quot;A.GetCreationDate() = &quot;, mm,hh,dd,mnth,yy\r
34 print &quot;A.GetCreationMode() = &quot;, A.GetCreationMode()\r
35 print &quot;A.IsModified() = &quot;, A.IsModified()\r
36 print &quot;A.IsLocked() = &quot;, A.IsLocked()\r
37 if A.IsLocked() == 0 :\r
38  &nbsp; &nbsp; &nbsp; &nbsp;A.SetUserName(&quot;tester&quot;); print 'A.SetUserName(&quot;tester&quot;), A.GetUserName() = ', A.GetUserName()\r
39  &nbsp; &nbsp; &nbsp; &nbsp;A.SetCreationDate(11,11,11,11,2002); print 'A.SetCreationDate(11,11,11,11,2002), A.GetCreationDate() =', A.GetCreationDate()\r
40  &nbsp; &nbsp; &nbsp; &nbsp;print &quot;A.IsModified() = &quot;, A.IsModified()\r
41 A.SetLocked(1)\r
42 \r
43 #check the transaction result \r
44 batchmode_geompy.myBuilder.CommitCommand()\r
45 if A.GetUserName() != &quot;tester&quot;:\r
46  &nbsp; &nbsp; &nbsp; &nbsp;print 'Control after transaction close : A.GetUserName() = ', A.GetUserName()\r
47  &nbsp; &nbsp; &nbsp; &nbsp;raise RuntimeError, &quot;Field 'UserName' was not modified but had to!&quot;\r
48 \r
49 # try to make some changes wrapped by transaction \r
50 #================================================\r
51 batchmode_geompy.myBuilder.NewCommand()\r
52 A = batchmode_geompy.myBuilder.FindOrCreateAttribute(batchmode_geompy.father, &quot;AttributeInteger&quot;)\r
53 \r
54 if A == None :\r
55  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Can't create AttributeInteger attribute&quot;\r
56 A = A._narrow(SALOMEDS.AttributeInteger)\r
57 A.SetValue(1000000)\r
58 \r
59 exception_was = None\r
60 try : batchmode_geompy.myBuilder.CommitCommand()\r
61 except Exception: exception_was = 1\r
62 \r
63 if exception_was is None:\r
64  &nbsp; &nbsp; &nbsp; &nbsp;raise RuntimeError, &quot;Study was locked for changes but CommitCommand did not generate an exception !&quot;\r
65 \r
66 # &nbsp; &nbsp; &nbsp;save / restore study &nbsp; &nbsp; &nbsp;\r
67 \r
68 #================================================\r
69 str= os.getenv(&quot;TmpDir&quot;)\r
70 if str == None:\r
71  &nbsp; &nbsp; &nbsp; &nbsp;str = &quot;/tmp&quot;\r
72 file = str+&quot;/test.hdf&quot;\r
73 \r
74 print &quot; ------- &nbsp;We will save to&quot;, file, &quot;-----------&quot;\r
75 \r
76 batchmode_geompy.myStudyManager.SaveAs(file, batchmode_geompy.myStudy)\r
77 \r
78 \r
79 #--------------------------------------------------------------------------#\r
80 #--------------------------- Open file ------------------------------------# \r
81 #--------------------------------------------------------------------------#\r
82 \r
83 print&quot; -------------- Open &nbsp;&quot; + file + &quot;-------------- &quot; \r
84 \r
85 openedStudy = batchmode_geompy.myStudyManager.Open(file)\r
86 if openedStudy == None:\r
87  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Can't open saved study!&quot;\r
88 \r
89 father = openedStudy.FindComponent(&quot;GEOM&quot;)\r
90 if father is None:\r
91  &nbsp; &nbsp; &nbsp; &nbsp; raise &nbsp;RuntimeError, &quot;Geom component is not found! &nbsp;Wrong study is opened.&quot; \r
92 \r
93 \r
94 #1. &nbsp; &nbsp; find AttributeReal\r
95 #================================================\r
96 \r
97 res,A=father.FindAttribute(&quot;AttributeReal&quot;)\r
98 if res == 0 or A == None:\r
99  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Error: &nbsp;not found AttributeReal&quot;\r
100 \r
101 A = A._narrow(SALOMEDS.AttributeReal)\r
102 if A.Value() != 0.0001:\r
103  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Error : wrong value of &nbsp;AttributeReal&quot;\r
104  \r
105 \r
106 #2. &nbsp; &nbsp; find AttributeStudyProperties\r
107 #=================================================\r
108 A=openedStudy.GetProperties()\r
109 if res == 0 or A == None:\r
110  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Error: &nbsp;not found AttributeStudyProperties&quot;\r
111 \r
112 A = A._narrow(SALOMEDS.AttributeStudyProperties)\r
113 if A.IsLocked() == 0 :\r
114  &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Error : AttributeStudyProperties must have Locked flag but have no!&quot;\r
115 \r
116 #get the builder\r
117 myBuilder = openedStudy.NewBuilder()\r
118 \r
119 #3. try to make some changes wrapped by transaction \r
120 #==================================================\r
121 exception_was = None\r
122 try : \r
123  &nbsp; &nbsp; &nbsp; &nbsp;myBuilder.NewCommand()\r
124  &nbsp; &nbsp; &nbsp; &nbsp;A = myBuilder.FindOrCreateAttribute(father, &quot;AttributeInteger&quot;)\r
125 \r
126  &nbsp; &nbsp; &nbsp; &nbsp;if A == None :\r
127  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;RuntimeError, &quot;Can't create AttributeInteger attribute&quot;\r
128  &nbsp; &nbsp; &nbsp; &nbsp;A = A._narrow(SALOMEDS.AttributeInteger)\r
129  &nbsp; &nbsp; &nbsp; &nbsp;A.SetValue(1000000)\r
130  &nbsp; &nbsp; &nbsp; &nbsp;myBuilder.CommitCommand()\r
131 except Exception: exception_was = 1\r
132 \r
133 if exception_was is None:\r
134  &nbsp; &nbsp; &nbsp; &nbsp;raise RuntimeError, &quot;Study was locked for changes but CommitCommand did not generate an exception !&quot;\r
135 \r
136 \r
137 myBuilder.NewCommand()\r
138 A=openedStudy.GetProperties()\r
139 A = A._narrow(SALOMEDS.AttributeStudyProperties)\r
140 A.SetLocked(0)\r
141 myBuilder.CommitCommand()\r
142 \r
143 #4.\r
144 myBuilder.NewCommand()\r
145 A.SetLocked(0);\r
146 print &quot;A.GetUserName()= &quot;, A.GetUserName()\r
147 print &quot;A.GetCreationDate() = &quot;, A.GetCreationDate()\r
148 print &quot;A.GetCreationMode() = &quot;, A.GetCreationMode()\r
149 print &quot;A.IsModified() = &quot;, A.IsModified()\r
150 myBuilder.CommitCommand()\r
151 \r
152 #5.\r
153 myBuilder.NewCommand()\r
154 A.SetUserName(&quot;tester1&quot;)\r
155 myBuilder.CommitCommand()\r
156 print &quot;A.GetUserName()= &quot;, A.GetUserName()\r
157 \r
158 #remove the document file\r
159 os.remove(file)\r
160 </PRE>