0022792: Globally defined symbol PI conflicts with VTK definition (Intel compiler)
[occt.git] / src / DDataStd / DDataStd_Sample.cxx
1 // File:        DDataStd_Sample.cxx
2 // Created:     Tue Dec 28 09:46:59 1999
3 // Author:      Sergey RUIN
4 //              <srn@popox.nnov.matra-dtv.fr>
5
6
7 #include <TDF_Data.hxx>
8 #include <TDF_Label.hxx>
9 #include <TDataXtd_Axis.hxx>
10 #include <TDataStd_Current.hxx>
11 #include <TDataStd_Comment.hxx>
12 #include <TDataStd_Integer.hxx>
13 #include <TDataStd_IntegerArray.hxx>
14 #include <TDataStd_Real.hxx>
15 #include <TDataStd_RealArray.hxx>
16 #include <TDF_Reference.hxx>
17 #include <TDataStd_UAttribute.hxx>
18 #include <TDataStd_TreeNode.hxx>
19 #include <TDataXtd_Constraint.hxx>
20 #include <TDataXtd_Shape.hxx>
21 #include <TDataStd_Name.hxx>
22 #include <TDataStd_Directory.hxx>
23 #include <TDataXtd_Point.hxx>
24 #include <TDataXtd_Plane.hxx>
25 #include <TDataXtd_Geometry.hxx>
26 #include <TDataXtd_GeometryEnum.hxx>
27 #include <TDataXtd_ConstraintEnum.hxx>
28 #include <TNaming_NamedShape.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Pln.hxx>
31 #include <gp_Lin.hxx>
32 #include <gp_Ax1.hxx>
33 #include <gp_Cylinder.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopLoc_Location.hxx>
36 #include <TDF_AttributeList.hxx>
37 #include <TColStd_HArray1OfReal.hxx>
38 #include <TCollection_ExtendedString.hxx>
39 #include <TDataStd_ListOfExtendedString.hxx>
40 #include <TDocStd_Document.hxx>
41 #include <TDataStd_TreeNode.hxx>
42
43 #ifdef DEB
44
45 static void Sample()
46 {
47   // Starting with data framework 
48   Handle(TDF_Data) DF = new TDF_Data();
49   TDF_Label aLabel = DF->Root();
50
51   //------------------------- TDataStd_Integer (Real) ---------------------------------
52   //-----------------------------------------------------------------------------------
53   
54   // Setting TDataStd_Integer attribute to a label (work with TDataStd_Real is the same)
55
56   Standard_Integer i = 10;
57
58   Handle(TDataStd_Integer) I = TDataStd_Integer::Set(aLabel, i);
59
60   // Getting the value stored in TDataStd_Integer attribute
61
62   Standard_Integer aValue;
63
64   aValue = I->Get();
65
66   // Setting the new value in the attribute 
67     
68   I->Set( 25 );
69
70   //------------------------- TDataStd_RealArray (IntegerArray is analogical) --------------
71   //-----------------------------------------------------------------------------------------
72
73   // Setting TDataStd_RealArray attribute to a label 
74
75   Standard_Integer lower = 1, upper = 20;
76
77   Handle(TDataStd_RealArray) realarray = TDataStd_RealArray::Set(aLabel, lower, upper);
78  
79   // Filling array
80
81   for(Standard_Integer j = realarray->Lower(); j<= realarray->Upper(); j++) {
82     realarray->SetValue(j, M_PI * j); 
83   }
84
85   // Getting length of array
86
87   Standard_Integer length = realarray->Length();
88
89   // Retrieving value by index
90
91   Standard_Real value = realarray->Value(3);
92
93   // Getting handle to an underlying array TColStd_HArray1OfReal
94
95   Handle(TColStd_HArray1OfReal) array = realarray->Array();
96
97   //------------------------- TDataStd_Comment --------------
98   //---------------------------------------------------------
99
100   // Setting TDataStd_Comment attribute to a label
101
102   Handle(TDataStd_Comment) comment = TDataStd_Comment::Set(aLabel);
103
104   // Setting comment string in to the attribute
105  
106   TCollection_ExtendedString message = "This is a remark";
107
108   comment->Set(message);
109
110   // Getting comment string stored in the attribute
111
112   TCollection_ExtendedString string = comment->Get();  
113
114   //------------------------- TDataStd_Name -----------------
115   //---------------------------------------------------------
116  
117   // Setting TDataStd_Name attribute to a label
118
119 //  Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel);
120
121
122   // Checking if the label has a name (TDataStd_Name attribute with not empty name field)
123
124   // Standard_Boolean isempty = name->IsEmpty();
125
126   // Getting the name of the label
127
128   //if( !isempty ) TCollection_ExtendedString thename = name->Get();
129
130   // Erasing the name of the label in TDataStd_Name attribute
131
132   // name->SetEmpty(); 
133   
134   // Setting a new name string in the attribute
135   
136   TCollection_ExtendedString aname = "Name of the label";
137   Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel,aname);
138
139   name->Set(aname); 
140
141   // Getting comment string stored in the attribute
142
143   TCollection_ExtendedString namestring = name->Get();
144
145   // Getting the first father label which has TDataStd_Name attribute
146
147   //Handle(TDataStd_Name) father;
148
149   //Standard_Boolean hasfather = name->Father(father);
150
151   // Find if there exists label with full path "Assembly1:Part3:Prism7"
152
153   // Converting string to list of names
154  
155   //TCollection_ExtendedString fullpath = "Assembly_1:Part_3:Prism_7";
156   //TDataStd_ListOfExtendedString listofstring;
157   
158   //TDataStd_Name::MakePath(fullpath, listofstring);
159
160   //Handle(TDataStd_Name) nameattribute;
161   
162   //Standard_Boolean found = TDataStd_Name::Find(DF, listofstring, nameattribute);
163    
164   //if( found ) {
165     // Getting the fullpath of the nameattribute (fullpath consists of list of TDataStd_Name attributes situated on 
166     // father labels from label where nameattribute is situated to root label
167     //TDF_AttributeList list;
168    
169     //nameattribute->FullPath(list);
170   //}
171
172   //TDF_Label currentLabel;
173
174   // ... Finding currentLabel...
175
176   // Search  under <currentLabel>  a  label which fits with the given name
177    
178   //Handle(TDataStd_Name) givenname;
179
180   //found = TDataStd_Name::Find(currentLabel, "Sketch_11", givenname);
181
182   //if( found ) {
183     
184     // Getting all named child labels of the label where givenname attribute situated 
185     //TDF_AttributeList listOfChildren;name to the 
186     //Standard_Boolean isAtLeastOneFound =  givenname->ChildNames(listOfChildren);
187   //}
188
189
190   //------------------------- TDataStd_UAttribute -----------
191   //---------------------------------------------------------
192
193   // Setting TDataStd_UAttribute to a label
194
195   Standard_GUID guid("01010101-0101-0101-1010-010101010101");
196
197   Handle(TDataStd_UAttribute) uattribute = TDataStd_UAttribute::Set(aLabel, guid);
198
199   // Finding a TDataStd_UAttribute on the label using standard mechanism
200   
201   Handle(TDataStd_UAttribute) theUA;
202   if (aLabel.FindAttribute(guid,theUA)) {
203     // do something
204   }
205   
206   // Checking that a  TDataStd_UAttribute exist on a given label using standard mechanism
207   
208   if (aLabel.IsAttribute(guid)) {
209     // do something
210   }
211   
212   //------------------------- TDF_Reference  -----------
213   //---------------------------------------------------------
214
215   TDF_Label referencedlabel;
216
217   // ... Finding referencedlabel ...
218
219   
220   // Setting TDF_Reference attribute to a label
221   
222   Handle(TDF_Reference) reference = TDF_Reference::Set(aLabel, referencedlabel);
223
224   // Getting a label to TDF_Reference attribute refers to
225    
226   TDF_Label refLabel = reference->Get();
227
228
229   //------------------------- TDataXtd_Point ----------------
230   //---------------------------------------------------------
231   gp_Pnt Pnt;
232
233   // ... Defining point <Pnt> ...
234
235   // Setting TDataXtd_Point attribute to a label
236
237   Handle(TDataXtd_Point) P = TDataXtd_Point::Set(aLabel, Pnt);
238
239   //Retrieve gp_Pnt associated with attribute
240      
241   gp_Pnt aPnt;
242   TDataXtd_Geometry::Point(aLabel, aPnt);
243
244   //------------------------- TDataXtd_Plane ----------------
245   //---------------------------------------------------------
246   gp_Pln Plane;
247
248   // ... Defining plane <Plane> ...
249
250   // Setting TDataXtd_Plane attribute to a label  
251
252   Handle(TDataXtd_Plane) Pl = TDataXtd_Plane::Set(aLabel, Plane);
253
254   //Retrieve gp_Plane associated with attribute
255      
256   gp_Pln aPlane;
257   TDataXtd_Geometry::Plane(aLabel, aPlane);
258
259   //------------------------- TDataXtd_Axis ----------------
260   //---------------------------------------------------------
261
262   gp_Lin Axis;
263
264   // ... Defining axis <Axis> ...
265
266   // Setting TDataXtd_Axis attribute to a label
267
268   Handle(TDataXtd_Axis) axis = TDataXtd_Axis::Set(aLabel, Axis);
269   
270   //Retrieve gp_Ax1 associated with attribute
271   
272   gp_Ax1 anAxis;
273   TDataXtd_Geometry::Axis(aLabel, anAxis); 
274
275
276   //------------------------- TDataXtd_Geometry ----------------
277   //---------------------------------------------------------
278
279    Handle(TNaming_NamedShape) NS;
280
281    // ... Constructing NS which contains cylinder...
282
283    // Setting TDataXtd_Geometry attribute to the label where <NS> is situated
284
285    TDF_Label NSLabel = NS->Label();
286
287    Handle(TDataXtd_Geometry) geom = TDataXtd_Geometry::Set(NSLabel);
288
289    // Setting a type of geometry   
290
291    geom->SetType(TDataXtd_CYLINDER); 
292
293    // Retrieving gp_Cylinder stored in associated NamedShape
294
295    // Checking the type of geometry
296
297    if( geom->GetType() == TDataXtd_CYLINDER ) {
298    
299      gp_Cylinder Cylinder;
300
301      TDataXtd_Geometry::Cylinder(geom->Label(), Cylinder);     
302
303    }
304
305   //------------------------- TDataXtd_Constraint -------------
306   //-----------------------------------------------------------
307
308   // Setting TDataXtd_Constraint to a label
309
310   Handle(TDataXtd_Constraint) constraint = TDataXtd_Constraint::Set(aLabel);
311
312   Handle(TNaming_NamedShape) NS1, NS2;
313   Handle(TDataStd_Real) aDistance;
314
315   // ... Constructing NS1 and NS2 which contain lines and calculating the distance between them ...
316
317   // Setting DISTANCE Dimension  between NS1 and NS2
318
319   constraint->Set(TDataXtd_DISTANCE, NS1, NS2);
320
321   constraint->SetValue(aDistance);     // <aDistance> should contain the calucalated distance
322
323   // Checking if <constraint> is a Dimension rather than a Constraint
324
325   Standard_Boolean isdimension = constraint->IsDimension();
326
327   if(isdimension) {
328
329    // Getting the distance between NS1 and NS2
330
331    Handle(TDataStd_Real) valOfdistance = constraint->GetValue();
332
333    Standard_Real distance = valOfdistance->Get();
334   }
335
336   // Setting PARALLEL constraint between NS1 and NS2
337
338   constraint->Set(TDataXtd_PARALLEL, NS1, NS2);
339
340   // Getting number of geometries which define a constarint
341   
342   Standard_Integer number = constraint->NbGeometries();
343
344   // Checking if a constraint is verified
345
346   Standard_Boolean isverified =  constraint->Verified();
347   if( !isverified ) {
348    cout << "Constraint is not valid"   << endl;
349   }
350
351
352   //------------------------- TDataStd_Directory --------------
353   //-----------------------------------------------------------
354
355   // Setting TDataStd_Directory to a label
356
357   Handle(TDataStd_Directory) directory = TDataStd_Directory::New(aLabel);
358
359   // Creating a new sub directory of the given directory
360
361   Handle(TDataStd_Directory) newdirectory = TDataStd_Directory::AddDirectory(directory);
362
363   // Creating a new label in directory
364
365   TDF_Label newlabel = TDataStd_Directory::MakeObjectLabel(newdirectory);
366
367    
368   //------------------------- TDataStd_TreeNode ---------------
369   //-----------------------------------------------------------
370
371   // Let's create a tree:
372
373   //  Root
374   //   |
375   //   --- FirstChild
376   //   |
377   //   --- SecondChild
378   //           |
379   //           --- FirstChildOfSecondChild
380
381
382
383   // Setting a TDataStd_TreeNode attribute to a label. 
384   // It becomes a root because it hasn't a father:
385   
386   Handle(TDataStd_TreeNode) Root = TDataStd_TreeNode::Set(aLabel);
387
388   // Create a child TreeNode:
389   
390   Handle(TDataStd_TreeNode) FirstChild = TDataStd_TreeNode::Set(aLabel.FindChild(1));
391   Root->Append(FirstChild);
392
393   // Let's add a second child node to the Root:
394   
395   Handle(TDataStd_TreeNode) SecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(2));
396   Root->Append(SecondChild);
397   
398   // Now, it's time to create the a child for the SecondChild node - FirstChildOfSecondChild:
399   
400   Handle(TDataStd_TreeNode) FirstChildOfSecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(3));
401   SecondChild->Append(FirstChildOfSecondChild);
402
403   // Let's redesign the tree:
404
405   //  Root
406   //   |
407   //   --- SecondChild
408   //           |
409   //           --- FirstChild
410   //           |
411   //           --- FirstChildOfSecondChild
412   
413   // Removing of the FirstChild from our tree:
414   
415   FirstChild->Remove();
416   
417   // Setting the FirstChild node as a first child of the SecondChild node:
418   
419   SecondChild->Prepend(FirstChild);
420 }
421
422 #endif