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