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 // Retrieving value by index
103 Standard_Real value = realarray->Value(3);
105 // Getting handle to an underlying array TColStd_HArray1OfReal
107 Handle(TColStd_HArray1OfReal) array = realarray->Array();
109 //------------------------- TDataStd_Comment --------------
110 //---------------------------------------------------------
112 // Setting TDataStd_Comment attribute to a label
114 Handle(TDataStd_Comment) comment = TDataStd_Comment::Set(aLabel);
116 // Setting comment string in to the attribute
118 TCollection_ExtendedString message = "This is a remark";
120 comment->Set(message);
122 // Getting comment string stored in the attribute
124 TCollection_ExtendedString string = comment->Get();
126 //------------------------- TDataStd_Name -----------------
127 //---------------------------------------------------------
129 // Setting TDataStd_Name attribute to a label
131 // Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel);
134 // Checking if the label has a name (TDataStd_Name attribute with not empty name field)
136 // Standard_Boolean isempty = name->IsEmpty();
138 // Getting the name of the label
140 //if( !isempty ) TCollection_ExtendedString thename = name->Get();
142 // Erasing the name of the label in TDataStd_Name attribute
146 // Setting a new name string in the attribute
148 TCollection_ExtendedString aname = "Name of the label";
149 Handle(TDataStd_Name) name = TDataStd_Name::Set(aLabel,aname);
153 // Getting comment string stored in the attribute
155 TCollection_ExtendedString namestring = name->Get();
157 // Getting the first father label which has TDataStd_Name attribute
159 //Handle(TDataStd_Name) father;
161 //Standard_Boolean hasfather = name->Father(father);
163 // Find if there exists label with full path "Assembly1:Part3:Prism7"
165 // Converting string to list of names
167 //TCollection_ExtendedString fullpath = "Assembly_1:Part_3:Prism_7";
168 //TDataStd_ListOfExtendedString listofstring;
170 //TDataStd_Name::MakePath(fullpath, listofstring);
172 //Handle(TDataStd_Name) nameattribute;
174 //Standard_Boolean found = TDataStd_Name::Find(DF, listofstring, nameattribute);
177 // Getting the fullpath of the nameattribute (fullpath consists of list of TDataStd_Name attributes situated on
178 // father labels from label where nameattribute is situated to root label
179 //TDF_AttributeList list;
181 //nameattribute->FullPath(list);
184 //TDF_Label currentLabel;
186 // ... Finding currentLabel...
188 // Search under <currentLabel> a label which fits with the given name
190 //Handle(TDataStd_Name) givenname;
192 //found = TDataStd_Name::Find(currentLabel, "Sketch_11", givenname);
196 // Getting all named child labels of the label where givenname attribute situated
197 //TDF_AttributeList listOfChildren;name to the
198 //Standard_Boolean isAtLeastOneFound = givenname->ChildNames(listOfChildren);
202 //------------------------- TDataStd_UAttribute -----------
203 //---------------------------------------------------------
205 // Setting TDataStd_UAttribute to a label
207 Standard_GUID guid("01010101-0101-0101-1010-010101010101");
209 Handle(TDataStd_UAttribute) uattribute = TDataStd_UAttribute::Set(aLabel, guid);
211 // Finding a TDataStd_UAttribute on the label using standard mechanism
213 Handle(TDataStd_UAttribute) theUA;
214 if (aLabel.FindAttribute(guid,theUA)) {
218 // Checking that a TDataStd_UAttribute exist on a given label using standard mechanism
220 if (aLabel.IsAttribute(guid)) {
224 //------------------------- TDF_Reference -----------
225 //---------------------------------------------------------
227 TDF_Label referencedlabel;
229 // ... Finding referencedlabel ...
232 // Setting TDF_Reference attribute to a label
234 Handle(TDF_Reference) reference = TDF_Reference::Set(aLabel, referencedlabel);
236 // Getting a label to TDF_Reference attribute refers to
238 TDF_Label refLabel = reference->Get();
241 //------------------------- TDataXtd_Point ----------------
242 //---------------------------------------------------------
245 // ... Defining point <Pnt> ...
247 // Setting TDataXtd_Point attribute to a label
249 Handle(TDataXtd_Point) P = TDataXtd_Point::Set(aLabel, Pnt);
251 //Retrieve gp_Pnt associated with attribute
254 TDataXtd_Geometry::Point(aLabel, aPnt);
256 //------------------------- TDataXtd_Plane ----------------
257 //---------------------------------------------------------
260 // ... Defining plane <Plane> ...
262 // Setting TDataXtd_Plane attribute to a label
264 Handle(TDataXtd_Plane) Pl = TDataXtd_Plane::Set(aLabel, Plane);
266 //Retrieve gp_Plane associated with attribute
269 TDataXtd_Geometry::Plane(aLabel, aPlane);
271 //------------------------- TDataXtd_Axis ----------------
272 //---------------------------------------------------------
276 // ... Defining axis <Axis> ...
278 // Setting TDataXtd_Axis attribute to a label
280 Handle(TDataXtd_Axis) axis = TDataXtd_Axis::Set(aLabel, Axis);
282 //Retrieve gp_Ax1 associated with attribute
285 TDataXtd_Geometry::Axis(aLabel, anAxis);
288 //------------------------- TDataXtd_Geometry ----------------
289 //---------------------------------------------------------
291 Handle(TNaming_NamedShape) NS;
293 // ... Constructing NS which contains cylinder...
295 // Setting TDataXtd_Geometry attribute to the label where <NS> is situated
297 TDF_Label NSLabel = NS->Label();
299 Handle(TDataXtd_Geometry) geom = TDataXtd_Geometry::Set(NSLabel);
301 // Setting a type of geometry
303 geom->SetType(TDataXtd_CYLINDER);
305 // Retrieving gp_Cylinder stored in associated NamedShape
307 // Checking the type of geometry
309 if( geom->GetType() == TDataXtd_CYLINDER ) {
311 gp_Cylinder Cylinder;
313 TDataXtd_Geometry::Cylinder(geom->Label(), Cylinder);
317 //------------------------- TDataXtd_Constraint -------------
318 //-----------------------------------------------------------
320 // Setting TDataXtd_Constraint to a label
322 Handle(TDataXtd_Constraint) constraint = TDataXtd_Constraint::Set(aLabel);
324 Handle(TNaming_NamedShape) NS1, NS2;
325 Handle(TDataStd_Real) aDistance;
327 // ... Constructing NS1 and NS2 which contain lines and calculating the distance between them ...
329 // Setting DISTANCE Dimension between NS1 and NS2
331 constraint->Set(TDataXtd_DISTANCE, NS1, NS2);
333 constraint->SetValue(aDistance); // <aDistance> should contain the calucalated distance
335 // Checking if <constraint> is a Dimension rather than a Constraint
337 Standard_Boolean isdimension = constraint->IsDimension();
341 // Getting the distance between NS1 and NS2
343 Handle(TDataStd_Real) valOfdistance = constraint->GetValue();
347 // Setting PARALLEL constraint between NS1 and NS2
349 constraint->Set(TDataXtd_PARALLEL, NS1, NS2);
351 // Getting number of geometries which define a constarint
353 Standard_Integer number = constraint->NbGeometries();
355 // Checking if a constraint is verified
357 Standard_Boolean isverified = constraint->Verified();
359 cout << "Constraint is not valid" << endl;
363 //------------------------- TDataStd_Directory --------------
364 //-----------------------------------------------------------
366 // Setting TDataStd_Directory to a label
368 Handle(TDataStd_Directory) directory = TDataStd_Directory::New(aLabel);
370 // Creating a new sub directory of the given directory
372 Handle(TDataStd_Directory) newdirectory = TDataStd_Directory::AddDirectory(directory);
374 // Creating a new label in directory
376 TDF_Label newlabel = TDataStd_Directory::MakeObjectLabel(newdirectory);
379 //------------------------- TDataStd_TreeNode ---------------
380 //-----------------------------------------------------------
382 // Let's create a tree:
390 // --- FirstChildOfSecondChild
394 // Setting a TDataStd_TreeNode attribute to a label.
395 // It becomes a root because it hasn't a father:
397 Handle(TDataStd_TreeNode) Root = TDataStd_TreeNode::Set(aLabel);
399 // Create a child TreeNode:
401 Handle(TDataStd_TreeNode) FirstChild = TDataStd_TreeNode::Set(aLabel.FindChild(1));
402 Root->Append(FirstChild);
404 // Let's add a second child node to the Root:
406 Handle(TDataStd_TreeNode) SecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(2));
407 Root->Append(SecondChild);
409 // Now, it's time to create the a child for the SecondChild node - FirstChildOfSecondChild:
411 Handle(TDataStd_TreeNode) FirstChildOfSecondChild = TDataStd_TreeNode::Set(aLabel.FindChild(3));
412 SecondChild->Append(FirstChildOfSecondChild);
414 // Let's redesign the tree:
422 // --- FirstChildOfSecondChild
424 // Removing of the FirstChild from our tree:
426 FirstChild->Remove();
428 // Setting the FirstChild node as a first child of the SecondChild node:
430 SecondChild->Prepend(FirstChild);