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
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.
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.
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.
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>
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>
63 // Starting with data framework
64 Handle(TDF_Data) DF = new TDF_Data();
65 TDF_Label aLabel = DF->Root();
67 //------------------------- TDataStd_Integer (Real) ---------------------------------
68 //-----------------------------------------------------------------------------------
70 // Setting TDataStd_Integer attribute to a label (work with TDataStd_Real is the same)
72 Standard_Integer i = 10;
74 Handle(TDataStd_Integer) I = TDataStd_Integer::Set(aLabel, i);
76 // Getting the value stored in TDataStd_Integer attribute
78 Standard_Integer aValue;
82 // Setting the new value in the attribute
86 //------------------------- TDataStd_RealArray (IntegerArray is analogical) --------------
87 //-----------------------------------------------------------------------------------------
89 // Setting TDataStd_RealArray attribute to a label
91 Standard_Integer lower = 1, upper = 20;
93 Handle(TDataStd_RealArray) realarray = TDataStd_RealArray::Set(aLabel, lower, upper);
97 for(Standard_Integer j = realarray->Lower(); j<= realarray->Upper(); j++) {
98 realarray->SetValue(j, M_PI * j);
101 // Getting length of array
103 Standard_Integer length = realarray->Length();
105 // Retrieving value by index
107 Standard_Real value = realarray->Value(3);
109 // Getting handle to an underlying array TColStd_HArray1OfReal
111 Handle(TColStd_HArray1OfReal) array = realarray->Array();
113 //------------------------- TDataStd_Comment --------------
114 //---------------------------------------------------------
116 // Setting TDataStd_Comment attribute to a label
118 Handle(TDataStd_Comment) comment = TDataStd_Comment::Set(aLabel);
120 // Setting comment string in to the attribute
122 TCollection_ExtendedString message = "This is a remark";
124 comment->Set(message);
126 // Getting comment string stored in the attribute
128 TCollection_ExtendedString string = comment->Get();
130 //------------------------- TDataStd_Name -----------------
131 //---------------------------------------------------------
133 // Setting TDataStd_Name attribute to a label
135 // Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel);
138 // Checking if the label has a name (TDataStd_Name attribute with not empty name field)
140 // Standard_Boolean isempty = name->IsEmpty();
142 // Getting the name of the label
144 //if( !isempty ) TCollection_ExtendedString thename = name->Get();
146 // Erasing the name of the label in TDataStd_Name attribute
150 // Setting a new name string in the attribute
152 TCollection_ExtendedString aname = "Name of the label";
153 Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel,aname);
157 // Getting comment string stored in the attribute
159 TCollection_ExtendedString namestring = name->Get();
161 // Getting the first father label which has TDataStd_Name attribute
163 //Handle(TDataStd_Name) father;
165 //Standard_Boolean hasfather = name->Father(father);
167 // Find if there exists label with full path "Assembly1:Part3:Prism7"
169 // Converting string to list of names
171 //TCollection_ExtendedString fullpath = "Assembly_1:Part_3:Prism_7";
172 //TDataStd_ListOfExtendedString listofstring;
174 //TDataStd_Name::MakePath(fullpath, listofstring);
176 //Handle(TDataStd_Name) nameattribute;
178 //Standard_Boolean found = TDataStd_Name::Find(DF, listofstring, nameattribute);
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;
185 //nameattribute->FullPath(list);
188 //TDF_Label currentLabel;
190 // ... Finding currentLabel...
192 // Search under <currentLabel> a label which fits with the given name
194 //Handle(TDataStd_Name) givenname;
196 //found = TDataStd_Name::Find(currentLabel, "Sketch_11", givenname);
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);
206 //------------------------- TDataStd_UAttribute -----------
207 //---------------------------------------------------------
209 // Setting TDataStd_UAttribute to a label
211 Standard_GUID guid("01010101-0101-0101-1010-010101010101");
213 Handle(TDataStd_UAttribute) uattribute = TDataStd_UAttribute::Set(aLabel, guid);
215 // Finding a TDataStd_UAttribute on the label using standard mechanism
217 Handle(TDataStd_UAttribute) theUA;
218 if (aLabel.FindAttribute(guid,theUA)) {
222 // Checking that a TDataStd_UAttribute exist on a given label using standard mechanism
224 if (aLabel.IsAttribute(guid)) {
228 //------------------------- TDF_Reference -----------
229 //---------------------------------------------------------
231 TDF_Label referencedlabel;
233 // ... Finding referencedlabel ...
236 // Setting TDF_Reference attribute to a label
238 Handle(TDF_Reference) reference = TDF_Reference::Set(aLabel, referencedlabel);
240 // Getting a label to TDF_Reference attribute refers to
242 TDF_Label refLabel = reference->Get();
245 //------------------------- TDataXtd_Point ----------------
246 //---------------------------------------------------------
249 // ... Defining point <Pnt> ...
251 // Setting TDataXtd_Point attribute to a label
253 Handle(TDataXtd_Point) P = TDataXtd_Point::Set(aLabel, Pnt);
255 //Retrieve gp_Pnt associated with attribute
258 TDataXtd_Geometry::Point(aLabel, aPnt);
260 //------------------------- TDataXtd_Plane ----------------
261 //---------------------------------------------------------
264 // ... Defining plane <Plane> ...
266 // Setting TDataXtd_Plane attribute to a label
268 Handle(TDataXtd_Plane) Pl = TDataXtd_Plane::Set(aLabel, Plane);
270 //Retrieve gp_Plane associated with attribute
273 TDataXtd_Geometry::Plane(aLabel, aPlane);
275 //------------------------- TDataXtd_Axis ----------------
276 //---------------------------------------------------------
280 // ... Defining axis <Axis> ...
282 // Setting TDataXtd_Axis attribute to a label
284 Handle(TDataXtd_Axis) axis = TDataXtd_Axis::Set(aLabel, Axis);
286 //Retrieve gp_Ax1 associated with attribute
289 TDataXtd_Geometry::Axis(aLabel, anAxis);
292 //------------------------- TDataXtd_Geometry ----------------
293 //---------------------------------------------------------
295 Handle(TNaming_NamedShape) NS;
297 // ... Constructing NS which contains cylinder...
299 // Setting TDataXtd_Geometry attribute to the label where <NS> is situated
301 TDF_Label NSLabel = NS->Label();
303 Handle(TDataXtd_Geometry) geom = TDataXtd_Geometry::Set(NSLabel);
305 // Setting a type of geometry
307 geom->SetType(TDataXtd_CYLINDER);
309 // Retrieving gp_Cylinder stored in associated NamedShape
311 // Checking the type of geometry
313 if( geom->GetType() == TDataXtd_CYLINDER ) {
315 gp_Cylinder Cylinder;
317 TDataXtd_Geometry::Cylinder(geom->Label(), Cylinder);
321 //------------------------- TDataXtd_Constraint -------------
322 //-----------------------------------------------------------
324 // Setting TDataXtd_Constraint to a label
326 Handle(TDataXtd_Constraint) constraint = TDataXtd_Constraint::Set(aLabel);
328 Handle(TNaming_NamedShape) NS1, NS2;
329 Handle(TDataStd_Real) aDistance;
331 // ... Constructing NS1 and NS2 which contain lines and calculating the distance between them ...
333 // Setting DISTANCE Dimension between NS1 and NS2
335 constraint->Set(TDataXtd_DISTANCE, NS1, NS2);
337 constraint->SetValue(aDistance); // <aDistance> should contain the calucalated distance
339 // Checking if <constraint> is a Dimension rather than a Constraint
341 Standard_Boolean isdimension = constraint->IsDimension();
345 // Getting the distance between NS1 and NS2
347 Handle(TDataStd_Real) valOfdistance = constraint->GetValue();
349 Standard_Real distance = valOfdistance->Get();
352 // Setting PARALLEL constraint between NS1 and NS2
354 constraint->Set(TDataXtd_PARALLEL, NS1, NS2);
356 // Getting number of geometries which define a constarint
358 Standard_Integer number = constraint->NbGeometries();
360 // Checking if a constraint is verified
362 Standard_Boolean isverified = constraint->Verified();
364 cout << "Constraint is not valid" << endl;
368 //------------------------- TDataStd_Directory --------------
369 //-----------------------------------------------------------
371 // Setting TDataStd_Directory to a label
373 Handle(TDataStd_Directory) directory = TDataStd_Directory::New(aLabel);
375 // Creating a new sub directory of the given directory
377 Handle(TDataStd_Directory) newdirectory = TDataStd_Directory::AddDirectory(directory);
379 // Creating a new label in directory
381 TDF_Label newlabel = TDataStd_Directory::MakeObjectLabel(newdirectory);
384 //------------------------- TDataStd_TreeNode ---------------
385 //-----------------------------------------------------------
387 // Let's create a tree:
395 // --- FirstChildOfSecondChild
399 // Setting a TDataStd_TreeNode attribute to a label.
400 // It becomes a root because it hasn't a father:
402 Handle(TDataStd_TreeNode) Root = TDataStd_TreeNode::Set(aLabel);
404 // Create a child TreeNode:
406 Handle(TDataStd_TreeNode) FirstChild = TDataStd_TreeNode::Set(aLabel.FindChild(1));
407 Root->Append(FirstChild);
409 // Let's add a second child node to the Root:
411 Handle(TDataStd_TreeNode) SecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(2));
412 Root->Append(SecondChild);
414 // Now, it's time to create the a child for the SecondChild node - FirstChildOfSecondChild:
416 Handle(TDataStd_TreeNode) FirstChildOfSecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(3));
417 SecondChild->Append(FirstChildOfSecondChild);
419 // Let's redesign the tree:
427 // --- FirstChildOfSecondChild
429 // Removing of the FirstChild from our tree:
431 FirstChild->Remove();
433 // Setting the FirstChild node as a first child of the SecondChild node:
435 SecondChild->Prepend(FirstChild);