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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
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>
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>
53 // ====================================================================================
54 // This sample contains templates for typical actions with standard OCAF attributes
55 // ====================================================================================
61 // Starting with data framework
62 Handle(TDF_Data) DF = new TDF_Data();
63 TDF_Label aLabel = DF->Root();
65 //------------------------- TDataStd_Integer (Real) ---------------------------------
66 //-----------------------------------------------------------------------------------
68 // Setting TDataStd_Integer attribute to a label (work with TDataStd_Real is the same)
70 Standard_Integer i = 10;
72 Handle(TDataStd_Integer) I = TDataStd_Integer::Set(aLabel, i);
74 // Getting the value stored in TDataStd_Integer attribute
76 Standard_Integer aValue;
80 // Setting the new value in the attribute
84 //------------------------- TDataStd_RealArray (IntegerArray is analogical) --------------
85 //----------------------------------------------------------------------------------------
87 // Setting TDataStd_RealArray attribute to a label
89 Standard_Integer lower = 1, upper = 20;
91 Handle(TDataStd_RealArray) realarray = TDataStd_RealArray::Set(aLabel, lower, upper);
95 for(Standard_Integer j = realarray->Lower(); j<= realarray->Upper(); j++) {
96 realarray->SetValue(j, M_PI * j);
99 // Retrieving value by index
101 Standard_Real value = realarray->Value(3);
103 // Getting handle to an underlying array TColStd_HArray1OfReal
105 Handle(TColStd_HArray1OfReal) array = realarray->Array();
107 //------------------------- TDataStd_Comment --------------
108 //---------------------------------------------------------
110 // Setting TDataStd_Comment attribute to a label
112 Handle(TDataStd_Comment) comment = TDataStd_Comment::Set(aLabel);
114 // Setting comment string in to the attribute
116 TCollection_ExtendedString message = "This is a remark";
118 comment->Set(message);
120 // Getting comment string stored in the attribute
122 TCollection_ExtendedString string = comment->Get();
124 //------------------------- TDataStd_Name -----------------
125 //---------------------------------------------------------
127 // Setting TDataStd_Name attribute to a label
129 // Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel);
132 // Checking if the label has a name (TDataStd_Name attribute with not empty name field)
134 // Standard_Boolean isempty = name->IsEmpty();
136 // Getting the name of the label
138 //if( !isempty ) TCollection_ExtendedString thename = name->Get();
140 // Erasing the name of the label in TDataStd_Name attribute
144 // Setting a new name string in the attribute
146 TCollection_ExtendedString aname = "Name of the label";
147 Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel,aname);
151 // Getting comment string stored in the attribute
153 TCollection_ExtendedString namestring = name->Get();
155 // Getting the first father label which has TDataStd_Name attribute
157 //Handle(TDataStd_Name) father;
159 //Standard_Boolean hasfather = name->Father(father);
161 // Find if there exists label with full path "Assembly1:Part3:Prism7"
163 // Converting string to list of names
165 //TCollection_ExtendedString fullpath = "Assembly_1:Part_3:Prism_7";
166 //TDataStd_ListOfExtendedString listofstring;
168 //TDataStd_Name::MakePath(fullpath, listofstring);
170 //Handle(TDataStd_Name) nameattribute;
172 //Standard_Boolean found = TDataStd_Name::Find(DF, listofstring, nameattribute);
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;
179 //nameattribute->FullPath(list);
182 //TDF_Label currentLabel;
184 // ... Finding currentLabel...
186 // Search under <currentLabel> a label which fits with the given name
188 //Handle(TDataStd_Name) givenname;
190 //found = TDataStd_Name::Find(currentLabel, "Sketch_11", givenname);
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);
200 //------------------------- TDataStd_UAttribute -----------
201 //---------------------------------------------------------
203 // Setting TDataStd_UAttribute to a label
205 Standard_GUID guid("01010101-0101-0101-1010-010101010101");
207 Handle(TDataStd_UAttribute) uattribute = TDataStd_UAttribute::Set(aLabel, guid);
209 // Finding a TDataStd_UAttribute on the label using standard mechanism
211 Handle(TDataStd_UAttribute) theUA;
212 if (aLabel.FindAttribute(guid,theUA)) {
216 // Checking that a TDataStd_UAttribute exist on a given label using standard mechanism
218 if (aLabel.IsAttribute(guid)) {
222 //------------------------- TDF_Reference -----------
223 //---------------------------------------------------------
225 TDF_Label referencedlabel;
227 // ... Finding referencedlabel ...
230 // Setting TDF_Reference attribute to a label
232 Handle(TDF_Reference) reference = TDF_Reference::Set(aLabel, referencedlabel);
234 // Getting a label to TDF_Reference attribute refers to
236 TDF_Label refLabel = reference->Get();
239 //------------------------- TDataXtd_Point ----------------
240 //---------------------------------------------------------
243 // ... Defining point <Pnt> ...
245 // Setting TDataXtd_Point attribute to a label
247 Handle(TDataXtd_Point) P = TDataXtd_Point::Set(aLabel, Pnt);
249 //Retrieve gp_Pnt associated with attribute
252 TDataXtd_Geometry::Point(aLabel, aPnt);
254 //------------------------- TDataXtd_Plane ----------------
255 //---------------------------------------------------------
258 // ... Defining plane <Plane> ...
260 // Setting TDataXtd_Plane attribute to a label
262 Handle(TDataXtd_Plane) Pl = TDataXtd_Plane::Set(aLabel, Plane);
264 //Retrieve gp_Plane associated with attribute
267 TDataXtd_Geometry::Plane(aLabel, aPlane);
269 //------------------------- TDataXtd_Axis ----------------
270 //---------------------------------------------------------
274 // ... Defining axis <Axis> ...
276 // Setting TDataXtd_Axis attribute to a label
278 Handle(TDataXtd_Axis) axis = TDataXtd_Axis::Set(aLabel, Axis);
280 //Retrieve gp_Ax1 associated with attribute
283 TDataXtd_Geometry::Axis(aLabel, anAxis);
286 //------------------------- TDataXtd_Geometry ----------------
287 //---------------------------------------------------------
289 Handle(TNaming_NamedShape) NS;
291 // ... Constructing NS which contains cylinder...
293 // Setting TDataXtd_Geometry attribute to the label where <NS> is situated
295 TDF_Label NSLabel = NS->Label();
297 Handle(TDataXtd_Geometry) geom = TDataXtd_Geometry::Set(NSLabel);
299 // Setting a type of geometry
301 geom->SetType(TDataXtd_CYLINDER);
303 // Retrieving gp_Cylinder stored in associated NamedShape
305 // Checking the type of geometry
307 if( geom->GetType() == TDataXtd_CYLINDER ) {
309 gp_Cylinder Cylinder;
311 TDataXtd_Geometry::Cylinder(geom->Label(), Cylinder);
315 //------------------------- TDataXtd_Constraint -------------
316 //-----------------------------------------------------------
318 // Setting TDataXtd_Constraint to a label
320 Handle(TDataXtd_Constraint) constraint = TDataXtd_Constraint::Set(aLabel);
322 Handle(TNaming_NamedShape) NS1, NS2;
323 Handle(TDataStd_Real) aDistance;
325 // ... Constructing NS1 and NS2 which contain lines and calculating the distance between them ...
327 // Setting DISTANCE Dimension between NS1 and NS2
329 constraint->Set(TDataXtd_DISTANCE, NS1, NS2);
331 constraint->SetValue(aDistance); // <aDistance> should contain the calucalated distance
333 // Checking if <constraint> is a Dimension rather than a Constraint
335 Standard_Boolean isdimension = constraint->IsDimension();
339 // Getting the distance between NS1 and NS2
341 Handle(TDataStd_Real) valOfdistance = constraint->GetValue();
345 // Setting PARALLEL constraint between NS1 and NS2
347 constraint->Set(TDataXtd_PARALLEL, NS1, NS2);
349 // Getting number of geometries which define a constarint
351 Standard_Integer number = constraint->NbGeometries();
353 // Checking if a constraint is verified
355 Standard_Boolean isverified = constraint->Verified();
357 cout << "Constraint is not valid" << endl;
361 //------------------------- TDataStd_Directory --------------
362 //-----------------------------------------------------------
364 // Setting TDataStd_Directory to a label
366 Handle(TDataStd_Directory) directory = TDataStd_Directory::New(aLabel);
368 // Creating a new sub directory of the given directory
370 Handle(TDataStd_Directory) newdirectory = TDataStd_Directory::AddDirectory(directory);
372 // Creating a new label in directory
374 TDF_Label newlabel = TDataStd_Directory::MakeObjectLabel(newdirectory);
377 //------------------------- TDataStd_TreeNode ---------------
378 //-----------------------------------------------------------
380 // Let's create a tree:
388 // --- FirstChildOfSecondChild
392 // Setting a TDataStd_TreeNode attribute to a label.
393 // It becomes a root because it hasn't a father:
395 Handle(TDataStd_TreeNode) Root = TDataStd_TreeNode::Set(aLabel);
397 // Create a child TreeNode:
399 Handle(TDataStd_TreeNode) FirstChild = TDataStd_TreeNode::Set(aLabel.FindChild(1));
400 Root->Append(FirstChild);
402 // Let's add a second child node to the Root:
404 Handle(TDataStd_TreeNode) SecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(2));
405 Root->Append(SecondChild);
407 // Now, it's time to create the a child for the SecondChild node - FirstChildOfSecondChild:
409 Handle(TDataStd_TreeNode) FirstChildOfSecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(3));
410 SecondChild->Append(FirstChildOfSecondChild);
412 // Let's redesign the tree:
420 // --- FirstChildOfSecondChild
422 // Removing of the FirstChild from our tree:
424 FirstChild->Remove();
426 // Setting the FirstChild node as a first child of the SecondChild node:
428 SecondChild->Prepend(FirstChild);