3 #include "GEOM_Object.hxx"
4 #include "GEOM_Engine.hxx"
5 #include "GEOM_Solver.hxx"
6 #include <TDF_Tool.hxx>
7 #include <TDF_Data.hxx>
8 #include <TDF_Reference.hxx>
9 #include <TDF_LabelSequence.hxx>
10 #include <TDocStd_Owner.hxx>
11 #include <TDocStd_Document.hxx>
12 #include <TDataStd_Integer.hxx>
13 #include <TDataStd_ChildNodeIterator.hxx>
14 #include <TDataStd_UAttribute.hxx>
15 #include <TDataStd_Name.hxx>
16 #include <TDataStd_Comment.hxx>
17 #include <TCollection_AsciiString.hxx>
18 #include <TCollection_ExtendedString.hxx>
19 #include <TopTools_IndexedMapOfShape.hxx>
23 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
26 //=======================================================================
27 //function : GetObjectID
29 //=======================================================================
30 const Standard_GUID& GEOM_Object::GetObjectID()
32 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
36 //=======================================================================
37 //function : GetSubShapeID
39 //=======================================================================
40 const Standard_GUID& GEOM_Object::GetSubShapeID()
42 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
46 //=============================================================================
50 //=============================================================================
51 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
53 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
55 TCollection_AsciiString anEntry;
56 TDF_Tool::Entry(theLabel, anEntry);
58 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
59 if(aDoc.IsNull()) return NULL;
61 Handle(TDataStd_Integer) anID;
62 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
65 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
66 if(anEngine == NULL) return NULL;
67 return anEngine->GetObject(anID->Get(), anEntry.ToCString());
70 //=============================================================================
74 //=============================================================================
75 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
77 Handle(TDF_Reference) aRef;
78 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
80 // Get TreeNode of a referenced function
81 Handle(TDataStd_TreeNode) aT, aFather;
82 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) return NULL;
84 // Get TreeNode of Object of the referenced function
85 aFather = aT->Father();
86 if (aFather.IsNull()) return NULL;
88 // Get label of the referenced object
89 TDF_Label aLabel = aFather->Label();
92 return GEOM_Object::GetObject(aLabel);
95 //=============================================================================
97 * Constructor: private
99 //=============================================================================
100 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
101 : _label(theEntry), _ior("")
103 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
104 _root = TDataStd_TreeNode::Set(theEntry);
107 //=============================================================================
109 * Constructor: public
111 //=============================================================================
112 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
113 : _label(theEntry), _ior("")
115 theEntry.ForgetAllAttributes(Standard_True);
117 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
118 _root = TDataStd_TreeNode::Set(theEntry);
120 TDataStd_Integer::Set(theEntry.FindChild(TYPE), theType);
122 TDataStd_UAttribute::Set(theEntry, GetObjectID());
125 //=============================================================================
129 //=============================================================================
130 int GEOM_Object::GetType()
132 Handle(TDataStd_Integer) aType;
133 if(!_label.FindChild(TYPE).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
138 //=============================================================================
142 //=============================================================================
143 void GEOM_Object::SetType(int theType)
145 TDataStd_Integer::Set(_label.FindChild(TYPE), theType);
150 //=============================================================================
154 //=============================================================================
155 int GEOM_Object::GetDocID()
157 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
158 if(aDoc.IsNull()) return -1;
160 Handle(TDataStd_Integer) anID;
161 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
167 //=============================================================================
171 //=============================================================================
172 TopoDS_Shape GEOM_Object::GetValue()
176 Handle(GEOM_Function) aFunction = GetLastFunction();
178 if (!aFunction.IsNull())
179 aShape = aFunction->GetValue();
184 //=============================================================================
188 //=============================================================================
189 void GEOM_Object::SetName(const char* theName)
191 TDataStd_Name::Set(_label, (char*)theName);
194 //=============================================================================
198 //=============================================================================
199 char* GEOM_Object::GetName()
201 Handle(TDataStd_Name) aNameAttr;
202 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
204 TCollection_AsciiString aName(aNameAttr->Get());
205 return aName.ToCString();
208 //=============================================================================
212 //=============================================================================
213 void GEOM_Object::SetAuxData(const char* theData)
215 TDataStd_Comment::Set(_label, (char*)theData);
218 //=============================================================================
222 //=============================================================================
223 TCollection_AsciiString GEOM_Object::GetAuxData()
225 TCollection_AsciiString aData;
227 Handle(TDataStd_Comment) aCommentAttr;
228 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
229 aData = aCommentAttr->Get();
235 //=============================================================================
239 //=============================================================================
240 bool GEOM_Object::IsMainShape()
242 Handle(GEOM_Function) aFunction = GetFunction(1);
243 if(aFunction == NULL || aFunction->GetDriverGUID() != GetSubShapeID()) return true;
248 //=============================================================================
252 //=============================================================================
253 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
255 Standard_Integer nb = GetNbFunctions();
256 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
258 TDF_Label aChild = FUNCTION_LABEL(nb);
260 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
261 _root->Append(aNode);
263 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
269 //=============================================================================
273 //=============================================================================
274 int GEOM_Object::GetNbFunctions()
276 Standard_Integer nb = 0;
277 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
281 //=============================================================================
285 //=============================================================================
286 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
288 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
289 return GEOM_Function::GetFunction(aChild);
292 //=============================================================================
296 //=============================================================================
297 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
299 Standard_Integer nb = GetNbFunctions();
300 if(nb) return GetFunction(nb);
306 //=============================================================================
310 //=============================================================================
311 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
313 Handle(TColStd_HSequenceOfTransient) anArray;
314 TDF_LabelSequence aSeq;
315 Standard_Integer nb = GetNbFunctions();
316 if(nb == 0) return anArray;
317 for(Standard_Integer i=1; i<=nb; i++) {
318 Handle(GEOM_Function) aFunction = GetFunction(i);
319 if(aFunction.IsNull()) continue;
320 aFunction->GetDependency(aSeq);
323 Standard_Integer aLength = aSeq.Length();
325 anArray = new TColStd_HSequenceOfTransient;
326 for(Standard_Integer j =1; j<=aLength; j++)
327 anArray->Append(GetReferencedObject(aSeq(j)));
333 //=============================================================================
337 //=============================================================================
338 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
340 Handle(TColStd_HSequenceOfTransient) anArray;
341 Handle(GEOM_Function) aFunction = GetLastFunction();
342 if (aFunction.IsNull()) return anArray;
344 TDF_LabelSequence aSeq;
345 aFunction->GetDependency(aSeq);
346 Standard_Integer aLength = aSeq.Length();
348 anArray = new TColStd_HSequenceOfTransient;
349 for (Standard_Integer i = 1; i <= aLength; i++)
350 anArray->Append(GetReferencedObject(aSeq(i)));
356 //=============================================================================
360 //=============================================================================
361 TDF_Label GEOM_Object::GetFreeLabel()
363 return _label.FindChild(FREE_LABEL);
366 //=======================================================================
367 //function : GEOM_Object_Type_
369 //=======================================================================
370 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
373 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
374 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
375 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
376 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
379 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
380 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
383 (Standard_Address)_Ancestors,
384 (Standard_Address)NULL);
388 //=======================================================================
389 //function : DownCast
391 //=======================================================================
393 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
395 Handle(GEOM_Object) _anOtherObject;
397 if (!AnObject.IsNull()) {
398 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
399 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
403 return _anOtherObject ;