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