1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_Stream.hxx>
22 #include <GEOM_Object.hxx>
23 #include <GEOM_Engine.hxx>
24 #include <GEOM_Solver.hxx>
25 #include <TDF_Tool.hxx>
26 #include <TDF_Data.hxx>
27 #include <TDF_Reference.hxx>
28 #include <TDF_LabelSequence.hxx>
29 #include <TDocStd_Owner.hxx>
30 #include <TDocStd_Document.hxx>
31 #include <TDataStd_Integer.hxx>
32 #include <TDataStd_ChildNodeIterator.hxx>
33 #include <TDataStd_UAttribute.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TDataStd_Comment.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TCollection_ExtendedString.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
42 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
45 //=======================================================================
46 //function : GetObjectID
48 //=======================================================================
49 const Standard_GUID& GEOM_Object::GetObjectID()
51 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
55 //=======================================================================
56 //function : GetSubShapeID
58 //=======================================================================
59 const Standard_GUID& GEOM_Object::GetSubShapeID()
61 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
65 //=============================================================================
69 //=============================================================================
70 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
72 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
74 TCollection_AsciiString anEntry;
75 TDF_Tool::Entry(theLabel, anEntry);
77 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
78 if(aDoc.IsNull()) return NULL;
80 Handle(TDataStd_Integer) anID;
81 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
84 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
85 if(anEngine == NULL) return NULL;
86 return anEngine->GetObject(anID->Get(), anEntry.ToCString());
89 //=============================================================================
93 //=============================================================================
94 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
96 Handle(TDF_Reference) aRef;
97 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
99 // Get TreeNode of a referenced function
100 Handle(TDataStd_TreeNode) aT, aFather;
101 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) return NULL;
103 // Get TreeNode of Object of the referenced function
104 aFather = aT->Father();
105 if (aFather.IsNull()) return NULL;
107 // Get label of the referenced object
108 TDF_Label aLabel = aFather->Label();
111 return GEOM_Object::GetObject(aLabel);
114 //=============================================================================
116 * Constructor: private
118 //=============================================================================
119 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
120 : _label(theEntry), _ior("")
122 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
123 _root = TDataStd_TreeNode::Set(theEntry);
126 //=============================================================================
128 * Constructor: public
130 //=============================================================================
131 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
132 : _label(theEntry), _ior("")
134 theEntry.ForgetAllAttributes(Standard_True);
136 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
137 _root = TDataStd_TreeNode::Set(theEntry);
139 TDataStd_Integer::Set(theEntry.FindChild(TYPE), theType);
141 TDataStd_UAttribute::Set(theEntry, GetObjectID());
144 //=============================================================================
148 //=============================================================================
149 int GEOM_Object::GetType()
151 Handle(TDataStd_Integer) aType;
152 if(!_label.FindChild(TYPE).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
157 //=============================================================================
161 //=============================================================================
162 void GEOM_Object::SetType(int theType)
164 TDataStd_Integer::Set(_label.FindChild(TYPE), theType);
169 //=============================================================================
173 //=============================================================================
174 int GEOM_Object::GetDocID()
176 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
177 if(aDoc.IsNull()) return -1;
179 Handle(TDataStd_Integer) anID;
180 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
186 //=============================================================================
190 //=============================================================================
191 TopoDS_Shape GEOM_Object::GetValue()
195 Handle(GEOM_Function) aFunction = GetLastFunction();
197 if (!aFunction.IsNull())
198 aShape = aFunction->GetValue();
203 //=============================================================================
207 //=============================================================================
208 void GEOM_Object::SetName(const char* theName)
210 TDataStd_Name::Set(_label, (char*)theName);
213 //=============================================================================
217 //=============================================================================
218 char* GEOM_Object::GetName()
220 Handle(TDataStd_Name) aNameAttr;
221 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
223 TCollection_AsciiString aName(aNameAttr->Get());
224 return aName.ToCString();
227 //=============================================================================
231 //=============================================================================
232 void GEOM_Object::SetAuxData(const char* theData)
234 TDataStd_Comment::Set(_label, (char*)theData);
237 //=============================================================================
241 //=============================================================================
242 TCollection_AsciiString GEOM_Object::GetAuxData()
244 TCollection_AsciiString aData;
246 Handle(TDataStd_Comment) aCommentAttr;
247 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
248 aData = aCommentAttr->Get();
254 //=============================================================================
258 //=============================================================================
259 bool GEOM_Object::IsMainShape()
261 Handle(GEOM_Function) aFunction = GetFunction(1);
262 if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
267 //=============================================================================
271 //=============================================================================
272 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
274 Standard_Integer nb = GetNbFunctions();
275 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
277 TDF_Label aChild = FUNCTION_LABEL(nb);
279 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
280 _root->Append(aNode);
282 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
288 //=============================================================================
292 //=============================================================================
293 int GEOM_Object::GetNbFunctions()
295 Standard_Integer nb = 0;
296 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
300 //=============================================================================
304 //=============================================================================
305 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
307 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
308 return GEOM_Function::GetFunction(aChild);
311 //=============================================================================
315 //=============================================================================
316 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
318 Standard_Integer nb = GetNbFunctions();
319 if(nb) return GetFunction(nb);
325 //=============================================================================
329 //=============================================================================
330 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
332 Handle(TColStd_HSequenceOfTransient) anArray;
333 TDF_LabelSequence aSeq;
334 Standard_Integer nb = GetNbFunctions();
335 if(nb == 0) return anArray;
336 for(Standard_Integer i=1; i<=nb; i++) {
337 Handle(GEOM_Function) aFunction = GetFunction(i);
338 if(aFunction.IsNull()) continue;
339 aFunction->GetDependency(aSeq);
342 Standard_Integer aLength = aSeq.Length();
344 anArray = new TColStd_HSequenceOfTransient;
345 for(Standard_Integer j =1; j<=aLength; j++)
346 anArray->Append(GetReferencedObject(aSeq(j)));
352 //=============================================================================
356 //=============================================================================
357 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
359 Handle(TColStd_HSequenceOfTransient) anArray;
360 Handle(GEOM_Function) aFunction = GetLastFunction();
361 if (aFunction.IsNull()) return anArray;
363 TDF_LabelSequence aSeq;
364 aFunction->GetDependency(aSeq);
365 Standard_Integer aLength = aSeq.Length();
367 anArray = new TColStd_HSequenceOfTransient;
368 for (Standard_Integer i = 1; i <= aLength; i++)
369 anArray->Append(GetReferencedObject(aSeq(i)));
375 //=============================================================================
379 //=============================================================================
380 TDF_Label GEOM_Object::GetFreeLabel()
382 return _label.FindChild(FREE_LABEL);
385 //=======================================================================
386 //function : GEOM_Object_Type_
388 //=======================================================================
389 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
392 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
393 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
394 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
395 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
398 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
399 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
402 (Standard_Address)_Ancestors,
403 (Standard_Address)NULL);
407 //=======================================================================
408 //function : DownCast
410 //=======================================================================
412 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
414 Handle(GEOM_Object) _anOtherObject;
416 if (!AnObject.IsNull()) {
417 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
418 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
422 return _anOtherObject ;