1 // File: DDataStd_Sample.cxx
2 // Created: Tue Dec 28 09:46:59 1999
4 // <srn@popox.nnov.matra-dtv.fr>
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>
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>
47 // Starting with data framework
48 Handle(TDF_Data) DF = new TDF_Data();
49 TDF_Label aLabel = DF->Root();
51 //------------------------- TDataStd_Integer (Real) ---------------------------------
52 //-----------------------------------------------------------------------------------
54 // Setting TDataStd_Integer attribute to a label (work with TDataStd_Real is the same)
56 Standard_Integer i = 10;
58 Handle(TDataStd_Integer) I = TDataStd_Integer::Set(aLabel, i);
60 // Getting the value stored in TDataStd_Integer attribute
62 Standard_Integer aValue;
66 // Setting the new value in the attribute
70 //------------------------- TDataStd_RealArray (IntegerArray is analogical) --------------
71 //-----------------------------------------------------------------------------------------
73 // Setting TDataStd_RealArray attribute to a label
75 Standard_Integer lower = 1, upper = 20;
77 Handle(TDataStd_RealArray) realarray = TDataStd_RealArray::Set(aLabel, lower, upper);
81 for(Standard_Integer j = realarray->Lower(); j<= realarray->Upper(); j++) {
82 realarray->SetValue(j, M_PI * j);
85 // Getting length of array
87 Standard_Integer length = realarray->Length();
89 // Retrieving value by index
91 Standard_Real value = realarray->Value(3);
93 // Getting handle to an underlying array TColStd_HArray1OfReal
95 Handle(TColStd_HArray1OfReal) array = realarray->Array();
97 //------------------------- TDataStd_Comment --------------
98 //---------------------------------------------------------
100 // Setting TDataStd_Comment attribute to a label
102 Handle(TDataStd_Comment) comment = TDataStd_Comment::Set(aLabel);
104 // Setting comment string in to the attribute
106 TCollection_ExtendedString message = "This is a remark";
108 comment->Set(message);
110 // Getting comment string stored in the attribute
112 TCollection_ExtendedString string = comment->Get();
114 //------------------------- TDataStd_Name -----------------
115 //---------------------------------------------------------
117 // Setting TDataStd_Name attribute to a label
119 // Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel);
122 // Checking if the label has a name (TDataStd_Name attribute with not empty name field)
124 // Standard_Boolean isempty = name->IsEmpty();
126 // Getting the name of the label
128 //if( !isempty ) TCollection_ExtendedString thename = name->Get();
130 // Erasing the name of the label in TDataStd_Name attribute
134 // Setting a new name string in the attribute
136 TCollection_ExtendedString aname = "Name of the label";
137 Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel,aname);
141 // Getting comment string stored in the attribute
143 TCollection_ExtendedString namestring = name->Get();
145 // Getting the first father label which has TDataStd_Name attribute
147 //Handle(TDataStd_Name) father;
149 //Standard_Boolean hasfather = name->Father(father);
151 // Find if there exists label with full path "Assembly1:Part3:Prism7"
153 // Converting string to list of names
155 //TCollection_ExtendedString fullpath = "Assembly_1:Part_3:Prism_7";
156 //TDataStd_ListOfExtendedString listofstring;
158 //TDataStd_Name::MakePath(fullpath, listofstring);
160 //Handle(TDataStd_Name) nameattribute;
162 //Standard_Boolean found = TDataStd_Name::Find(DF, listofstring, nameattribute);
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;
169 //nameattribute->FullPath(list);
172 //TDF_Label currentLabel;
174 // ... Finding currentLabel...
176 // Search under <currentLabel> a label which fits with the given name
178 //Handle(TDataStd_Name) givenname;
180 //found = TDataStd_Name::Find(currentLabel, "Sketch_11", givenname);
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);
190 //------------------------- TDataStd_UAttribute -----------
191 //---------------------------------------------------------
193 // Setting TDataStd_UAttribute to a label
195 Standard_GUID guid("01010101-0101-0101-1010-010101010101");
197 Handle(TDataStd_UAttribute) uattribute = TDataStd_UAttribute::Set(aLabel, guid);
199 // Finding a TDataStd_UAttribute on the label using standard mechanism
201 Handle(TDataStd_UAttribute) theUA;
202 if (aLabel.FindAttribute(guid,theUA)) {
206 // Checking that a TDataStd_UAttribute exist on a given label using standard mechanism
208 if (aLabel.IsAttribute(guid)) {
212 //------------------------- TDF_Reference -----------
213 //---------------------------------------------------------
215 TDF_Label referencedlabel;
217 // ... Finding referencedlabel ...
220 // Setting TDF_Reference attribute to a label
222 Handle(TDF_Reference) reference = TDF_Reference::Set(aLabel, referencedlabel);
224 // Getting a label to TDF_Reference attribute refers to
226 TDF_Label refLabel = reference->Get();
229 //------------------------- TDataXtd_Point ----------------
230 //---------------------------------------------------------
233 // ... Defining point <Pnt> ...
235 // Setting TDataXtd_Point attribute to a label
237 Handle(TDataXtd_Point) P = TDataXtd_Point::Set(aLabel, Pnt);
239 //Retrieve gp_Pnt associated with attribute
242 TDataXtd_Geometry::Point(aLabel, aPnt);
244 //------------------------- TDataXtd_Plane ----------------
245 //---------------------------------------------------------
248 // ... Defining plane <Plane> ...
250 // Setting TDataXtd_Plane attribute to a label
252 Handle(TDataXtd_Plane) Pl = TDataXtd_Plane::Set(aLabel, Plane);
254 //Retrieve gp_Plane associated with attribute
257 TDataXtd_Geometry::Plane(aLabel, aPlane);
259 //------------------------- TDataXtd_Axis ----------------
260 //---------------------------------------------------------
264 // ... Defining axis <Axis> ...
266 // Setting TDataXtd_Axis attribute to a label
268 Handle(TDataXtd_Axis) axis = TDataXtd_Axis::Set(aLabel, Axis);
270 //Retrieve gp_Ax1 associated with attribute
273 TDataXtd_Geometry::Axis(aLabel, anAxis);
276 //------------------------- TDataXtd_Geometry ----------------
277 //---------------------------------------------------------
279 Handle(TNaming_NamedShape) NS;
281 // ... Constructing NS which contains cylinder...
283 // Setting TDataXtd_Geometry attribute to the label where <NS> is situated
285 TDF_Label NSLabel = NS->Label();
287 Handle(TDataXtd_Geometry) geom = TDataXtd_Geometry::Set(NSLabel);
289 // Setting a type of geometry
291 geom->SetType(TDataXtd_CYLINDER);
293 // Retrieving gp_Cylinder stored in associated NamedShape
295 // Checking the type of geometry
297 if( geom->GetType() == TDataXtd_CYLINDER ) {
299 gp_Cylinder Cylinder;
301 TDataXtd_Geometry::Cylinder(geom->Label(), Cylinder);
305 //------------------------- TDataXtd_Constraint -------------
306 //-----------------------------------------------------------
308 // Setting TDataXtd_Constraint to a label
310 Handle(TDataXtd_Constraint) constraint = TDataXtd_Constraint::Set(aLabel);
312 Handle(TNaming_NamedShape) NS1, NS2;
313 Handle(TDataStd_Real) aDistance;
315 // ... Constructing NS1 and NS2 which contain lines and calculating the distance between them ...
317 // Setting DISTANCE Dimension between NS1 and NS2
319 constraint->Set(TDataXtd_DISTANCE, NS1, NS2);
321 constraint->SetValue(aDistance); // <aDistance> should contain the calucalated distance
323 // Checking if <constraint> is a Dimension rather than a Constraint
325 Standard_Boolean isdimension = constraint->IsDimension();
329 // Getting the distance between NS1 and NS2
331 Handle(TDataStd_Real) valOfdistance = constraint->GetValue();
333 Standard_Real distance = valOfdistance->Get();
336 // Setting PARALLEL constraint between NS1 and NS2
338 constraint->Set(TDataXtd_PARALLEL, NS1, NS2);
340 // Getting number of geometries which define a constarint
342 Standard_Integer number = constraint->NbGeometries();
344 // Checking if a constraint is verified
346 Standard_Boolean isverified = constraint->Verified();
348 cout << "Constraint is not valid" << endl;
352 //------------------------- TDataStd_Directory --------------
353 //-----------------------------------------------------------
355 // Setting TDataStd_Directory to a label
357 Handle(TDataStd_Directory) directory = TDataStd_Directory::New(aLabel);
359 // Creating a new sub directory of the given directory
361 Handle(TDataStd_Directory) newdirectory = TDataStd_Directory::AddDirectory(directory);
363 // Creating a new label in directory
365 TDF_Label newlabel = TDataStd_Directory::MakeObjectLabel(newdirectory);
368 //------------------------- TDataStd_TreeNode ---------------
369 //-----------------------------------------------------------
371 // Let's create a tree:
379 // --- FirstChildOfSecondChild
383 // Setting a TDataStd_TreeNode attribute to a label.
384 // It becomes a root because it hasn't a father:
386 Handle(TDataStd_TreeNode) Root = TDataStd_TreeNode::Set(aLabel);
388 // Create a child TreeNode:
390 Handle(TDataStd_TreeNode) FirstChild = TDataStd_TreeNode::Set(aLabel.FindChild(1));
391 Root->Append(FirstChild);
393 // Let's add a second child node to the Root:
395 Handle(TDataStd_TreeNode) SecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(2));
396 Root->Append(SecondChild);
398 // Now, it's time to create the a child for the SecondChild node - FirstChildOfSecondChild:
400 Handle(TDataStd_TreeNode) FirstChildOfSecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(3));
401 SecondChild->Append(FirstChildOfSecondChild);
403 // Let's redesign the tree:
411 // --- FirstChildOfSecondChild
413 // Removing of the FirstChild from our tree:
415 FirstChild->Remove();
417 // Setting the FirstChild node as a first child of the SecondChild node:
419 SecondChild->Prepend(FirstChild);