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 char* GEOM_Object::GetAuxData()
225 Handle(TDataStd_Comment) aCommentAttr;
226 if(!_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr)) return NULL;
228 TCollection_AsciiString aData(aCommentAttr->Get());
229 return aData.ToCString();
233 //=============================================================================
237 //=============================================================================
238 bool GEOM_Object::IsMainShape()
240 Handle(GEOM_Function) aFunction = GetFunction(1);
241 if(aFunction == NULL || aFunction->GetDriverGUID() != GetSubShapeID()) return true;
246 //=============================================================================
250 //=============================================================================
251 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
253 Standard_Integer nb = GetNbFunctions();
254 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
256 TDF_Label aChild = FUNCTION_LABEL(nb);
258 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
259 _root->Append(aNode);
261 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
267 //=============================================================================
271 //=============================================================================
272 int GEOM_Object::GetNbFunctions()
274 Standard_Integer nb = 0;
275 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
279 //=============================================================================
283 //=============================================================================
284 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
286 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
287 return GEOM_Function::GetFunction(aChild);
290 //=============================================================================
294 //=============================================================================
295 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
297 Standard_Integer nb = GetNbFunctions();
298 if(nb) return GetFunction(nb);
304 //=============================================================================
308 //=============================================================================
309 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
311 Handle(TColStd_HSequenceOfTransient) anArray;
312 TDF_LabelSequence aSeq;
313 Standard_Integer nb = GetNbFunctions();
314 if(nb == 0) return anArray;
315 for(Standard_Integer i=1; i<=nb; i++) {
316 Handle(GEOM_Function) aFunction = GetFunction(i);
317 if(aFunction.IsNull()) continue;
318 aFunction->GetDependency(aSeq);
321 Standard_Integer aLength = aSeq.Length();
323 anArray = new TColStd_HSequenceOfTransient;
324 for(Standard_Integer j =1; j<=aLength; j++)
325 anArray->Append(GetReferencedObject(aSeq(j)));
331 //=============================================================================
335 //=============================================================================
336 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
338 Handle(TColStd_HSequenceOfTransient) anArray;
339 Handle(GEOM_Function) aFunction = GetLastFunction();
340 if (aFunction.IsNull()) return anArray;
342 TDF_LabelSequence aSeq;
343 aFunction->GetDependency(aSeq);
344 Standard_Integer aLength = aSeq.Length();
346 anArray = new TColStd_HSequenceOfTransient;
347 for (Standard_Integer i = 1; i <= aLength; i++)
348 anArray->Append(GetReferencedObject(aSeq(i)));
354 //=============================================================================
358 //=============================================================================
359 TDF_Label GEOM_Object::GetFreeLabel()
361 return _label.FindChild(FREE_LABEL);
364 //=======================================================================
365 //function : GEOM_Object_Type_
367 //=======================================================================
368 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
371 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
372 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
373 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
374 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
377 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
378 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
381 (Standard_Address)_Ancestors,
382 (Standard_Address)NULL);
386 //=======================================================================
387 //function : DownCast
389 //=======================================================================
391 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
393 Handle(GEOM_Object) _anOtherObject;
395 if (!AnObject.IsNull()) {
396 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
397 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
401 return _anOtherObject ;