summaryrefslogtreecommitdiff
path: root/src/DDataStd/DDataStd_Sample.cxx
blob: d8fc8da497b322ad720ba020fbbb8a7418261eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// File:	DDataStd_Sample.cxx
// Created:	Tue Dec 28 09:46:59 1999
// Author:	Sergey RUIN
//		<srn@popox.nnov.matra-dtv.fr>


#include <TDF_Data.hxx>
#include <TDF_Label.hxx>
#include <TDataXtd_Axis.hxx>
#include <TDataStd_Current.hxx>
#include <TDataStd_Comment.hxx>
#include <TDataStd_Integer.hxx>
#include <TDataStd_IntegerArray.hxx>
#include <TDataStd_Real.hxx>
#include <TDataStd_RealArray.hxx>
#include <TDF_Reference.hxx>
#include <TDataStd_UAttribute.hxx>
#include <TDataStd_TreeNode.hxx>
#include <TDataXtd_Constraint.hxx>
#include <TDataXtd_Shape.hxx>
#include <TDataStd_Name.hxx>
#include <TDataStd_Directory.hxx>
#include <TDataXtd_Point.hxx>
#include <TDataXtd_Plane.hxx>
#include <TDataXtd_Geometry.hxx>
#include <TDataXtd_GeometryEnum.hxx>
#include <TDataXtd_ConstraintEnum.hxx>
#include <TNaming_NamedShape.hxx>
#include <gp_Pnt.hxx>
#include <gp_Pln.hxx>
#include <gp_Lin.hxx>
#include <gp_Ax1.hxx>
#include <gp_Cylinder.hxx>
#include <TopoDS_Shape.hxx>
#include <TopLoc_Location.hxx>
#include <TDF_AttributeList.hxx>
#include <TColStd_HArray1OfReal.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TDataStd_ListOfExtendedString.hxx>
#include <TDocStd_Document.hxx>
#include <TDataStd_TreeNode.hxx>

#ifdef DEB

static void Sample()
{
  // Starting with data framework 
  Handle(TDF_Data) DF = new TDF_Data();
  TDF_Label aLabel = DF->Root();

  //------------------------- TDataStd_Integer (Real) ---------------------------------
  //-----------------------------------------------------------------------------------
  
  // Setting TDataStd_Integer attribute to a label (work with TDataStd_Real is the same)

  Standard_Integer i = 10;

  Handle(TDataStd_Integer) I = TDataStd_Integer::Set(aLabel, i);

  // Setting the new value in the attribute 
    
  I->Set( 25 );

  //------------------------- TDataStd_RealArray (IntegerArray is analogical) --------------
  //-----------------------------------------------------------------------------------------

  // Setting TDataStd_RealArray attribute to a label 

  Standard_Integer lower = 1, upper = 20;

  Handle(TDataStd_RealArray) realarray = TDataStd_RealArray::Set(aLabel, lower, upper);
 
  // Filling array

  for(Standard_Integer j = realarray->Lower(); j<= realarray->Upper(); j++) {
    realarray->SetValue(j, Standard_PI*j); 
  }

  // Getting length of array

  Standard_Integer length = realarray->Length();

  // Retrieving value by index

  Standard_Real value = realarray->Value(3);

  // Getting handle to an underlying array TColStd_HArray1OfReal

  Handle(TColStd_HArray1OfReal) array = realarray->Array();

  //------------------------- TDataStd_Comment --------------
  //---------------------------------------------------------

  // Setting TDataStd_Comment attribute to a label

  Handle(TDataStd_Comment) comment = TDataStd_Comment::Set(aLabel);

  // Setting comment string in to the attribute
 
  TCollection_ExtendedString message = "This is a remark";

  comment->Set(message);

  // Getting comment string stored in the attribute

  TCollection_ExtendedString string = comment->Get();  

  //------------------------- TDataStd_Name -----------------
  //---------------------------------------------------------
 
  // Setting TDataStd_Name attribute to a label

//  Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel);


  // Checking if the label has a name (TDataStd_Name attribute with not empty name field)

  // Standard_Boolean isempty = name->IsEmpty();

  // Getting the name of the label

  //if( !isempty ) TCollection_ExtendedString thename = name->Get();

  // Erasing the name of the label in TDataStd_Name attribute

  // name->SetEmpty(); 
  
  // Setting a new name string in the attribute
  
  TCollection_ExtendedString aname = "Name of the label";
  Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel,aname);

  name->Set(aname); 

  // Getting comment string stored in the attribute

  TCollection_ExtendedString namestring = name->Get();

  // Getting the first father label which has TDataStd_Name attribute

  //Handle(TDataStd_Name) father;

  //Standard_Boolean hasfather = name->Father(father);

  // Find if there exists label with full path "Assembly1:Part3:Prism7"

  // Converting string to list of names
 
  //TCollection_ExtendedString fullpath = "Assembly_1:Part_3:Prism_7";
  //TDataStd_ListOfExtendedString listofstring;
  
  //TDataStd_Name::MakePath(fullpath, listofstring);

  //Handle(TDataStd_Name) nameattribute;
  
  //Standard_Boolean found = TDataStd_Name::Find(DF, listofstring, nameattribute);
   
  //if( found ) {
    // Getting the fullpath of the nameattribute (fullpath consists of list of TDataStd_Name attributes situated on 
    // father labels from label where nameattribute is situated to root label
    //TDF_AttributeList list;
   
    //nameattribute->FullPath(list);
  //}

  //TDF_Label currentLabel;

  // ... Finding currentLabel...

  // Search  under <currentLabel>  a  label which fits with the given name
   
  //Handle(TDataStd_Name) givenname;

  //found = TDataStd_Name::Find(currentLabel, "Sketch_11", givenname);

  //if( found ) {
    
    // Getting all named child labels of the label where givenname attribute situated 
    //TDF_AttributeList listOfChildren;name to the 
    //Standard_Boolean isAtLeastOneFound =  givenname->ChildNames(listOfChildren);
  //}


  //------------------------- TDataStd_UAttribute -----------
  //---------------------------------------------------------

  // Setting TDataStd_UAttribute to a label

  Standard_GUID guid("01010101-0101-0101-1010-010101010101");

  Handle(TDataStd_UAttribute) uattribute = TDataStd_UAttribute::Set(aLabel, guid);

  // Finding a TDataStd_UAttribute on the label using standard mechanism
  
  Handle(TDataStd_UAttribute) theUA;
  if (aLabel.FindAttribute(guid,theUA)) {
    // do something
  }
  
  // Checking that a  TDataStd_UAttribute exist on a given label using standard mechanism
  
  if (aLabel.IsAttribute(guid)) {
    // do something
  }
  
  //------------------------- TDF_Reference  -----------
  //---------------------------------------------------------

  TDF_Label referencedlabel;

  // ... Finding referencedlabel ...

  
  // Setting TDF_Reference attribute to a label
  
  Handle(TDF_Reference) reference = TDF_Reference::Set(aLabel, referencedlabel);

  //------------------------- TDataXtd_Point ----------------
  //---------------------------------------------------------
  gp_Pnt Pnt;

  // ... Defining point <Pnt> ...

  // Setting TDataXtd_Point attribute to a label

  Handle(TDataXtd_Point) P = TDataXtd_Point::Set(aLabel, Pnt);

  //Retrieve gp_Pnt associated with attribute
     
  gp_Pnt aPnt;
  TDataXtd_Geometry::Point(aLabel, aPnt);

  //------------------------- TDataXtd_Plane ----------------
  //---------------------------------------------------------
  gp_Pln Plane;

  // ... Defining plane <Plane> ...

  // Setting TDataXtd_Plane attribute to a label  

  Handle(TDataXtd_Plane) Pl = TDataXtd_Plane::Set(aLabel, Plane);

  //Retrieve gp_Plane associated with attribute
     
  gp_Pln aPlane;
  TDataXtd_Geometry::Plane(aLabel, aPlane);

  //------------------------- TDataXtd_Axis ----------------
  //---------------------------------------------------------

  gp_Lin Axis;

  // ... Defining axis <Axis> ...

  // Setting TDataXtd_Axis attribute to a label

  Handle(TDataXtd_Axis) axis = TDataXtd_Axis::Set(aLabel, Axis);
  
  //Retrieve gp_Ax1 associated with attribute
  
  gp_Ax1 anAxis;
  TDataXtd_Geometry::Axis(aLabel, anAxis); 


  //------------------------- TDataXtd_Geometry ----------------
  //---------------------------------------------------------

   Handle(TNaming_NamedShape) NS;

   // ... Constructing NS which contains cylinder...

   // Setting TDataXtd_Geometry attribute to the label where <NS> is situated

   TDF_Label NSLabel = NS->Label();

   Handle(TDataXtd_Geometry) geom = TDataXtd_Geometry::Set(NSLabel);

   // Setting a type of geometry   

   geom->SetType(TDataXtd_CYLINDER); 

   // Retrieving gp_Cylinder stored in associated NamedShape

   // Checking the type of geometry

   if( geom->GetType() == TDataXtd_CYLINDER ) {
   
     gp_Cylinder Cylinder;

     TDataXtd_Geometry::Cylinder(geom->Label(), Cylinder);     

   }

  //------------------------- TDataXtd_Constraint -------------
  //-----------------------------------------------------------

  // Setting TDataXtd_Constraint to a label

  Handle(TDataXtd_Constraint) constraint = TDataXtd_Constraint::Set(aLabel);

  Handle(TNaming_NamedShape) NS1, NS2;
  Handle(TDataStd_Real) aDistance;

  // ... Constructing NS1 and NS2 which contain lines and calculating the distance between them ...

  // Setting DISTANCE Dimension  between NS1 and NS2

  constraint->Set(TDataXtd_DISTANCE, NS1, NS2);

  constraint->SetValue(aDistance);     // <aDistance> should contain the calucalated distance

  // Checking if <constraint> is a Dimension rather than a Constraint

  Standard_Boolean isdimension = constraint->IsDimension();

  if(isdimension) {

   // Getting the distance between NS1 and NS2

   Handle(TDataStd_Real) valOfdistance = constraint->GetValue();

   Standard_Real distance = valOfdistance->Get();
  }

  // Setting PARALLEL constraint between NS1 and NS2

  constraint->Set(TDataXtd_PARALLEL, NS1, NS2);

  // Getting number of geometries which define a constarint
  
  Standard_Integer number = constraint->NbGeometries();

  // Checking if a constraint is verified

  Standard_Boolean isverified =  constraint->Verified();
  if( !isverified ) {
   cout << "Constraint is not valid"   << endl;
  }


  //------------------------- TDataStd_Directory --------------
  //-----------------------------------------------------------

  // Setting TDataStd_Directory to a label

  Handle(TDataStd_Directory) directory = TDataStd_Directory::New(aLabel);

  // Creating a new sub directory of the given directory

  Handle(TDataStd_Directory) newdirectory = TDataStd_Directory::AddDirectory(directory);

  // Creating a new label in directory

  TDataStd_Directory::MakeObjectLabel(newdirectory);

   
  //------------------------- TDataStd_TreeNode ---------------
  //-----------------------------------------------------------

  // Let's create a tree:

  //  Root
  //   |
  //   --- FirstChild
  //   |
  //   --- SecondChild
  //           |
  //           --- FirstChildOfSecondChild



  // Setting a TDataStd_TreeNode attribute to a label. 
  // It becomes a root because it hasn't a father:
  
  Handle(TDataStd_TreeNode) Root = TDataStd_TreeNode::Set(aLabel);

  // Create a child TreeNode:
  
  Handle(TDataStd_TreeNode) FirstChild = TDataStd_TreeNode::Set(aLabel.FindChild(1));
  Root->Append(FirstChild);

  // Let's add a second child node to the Root:
  
  Handle(TDataStd_TreeNode) SecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(2));
  Root->Append(SecondChild);
  
  // Now, it's time to create the a child for the SecondChild node - FirstChildOfSecondChild:
  
  Handle(TDataStd_TreeNode) FirstChildOfSecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(3));
  SecondChild->Append(FirstChildOfSecondChild);

  // Let's redesign the tree:

  //  Root
  //   |
  //   --- SecondChild
  //           |
  //           --- FirstChild
  //           |
  //           --- FirstChildOfSecondChild
  
  // Removing of the FirstChild from our tree:
  
  FirstChild->Remove();
  
  // Setting the FirstChild node as a first child of the SecondChild node:
  
  SecondChild->Prepend(FirstChild);
}

#endif