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>
41 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
46 //=======================================================================
47 //function : GetObjectID
49 //=======================================================================
50 const Standard_GUID& GEOM_Object::GetObjectID()
52 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
56 //=======================================================================
57 //function : GetSubShapeID
59 //=======================================================================
60 const Standard_GUID& GEOM_Object::GetSubShapeID()
62 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
66 //=============================================================================
70 //=============================================================================
71 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
73 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
75 TCollection_AsciiString anEntry;
76 TDF_Tool::Entry(theLabel, anEntry);
78 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
79 if(aDoc.IsNull()) return NULL;
81 Handle(TDataStd_Integer) anID;
82 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
85 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
86 if(anEngine == NULL) return NULL;
87 return anEngine->GetObject(anID->Get(), anEntry.ToCString());
90 //=============================================================================
94 //=============================================================================
95 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
97 Handle(TDF_Reference) aRef;
98 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
102 if(aRef.IsNull() || aRef->Get().IsNull()) {
107 // Get TreeNode of a referenced function
108 Handle(TDataStd_TreeNode) aT, aFather;
109 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
114 // Get TreeNode of Object of the referenced function
115 aFather = aT->Father();
116 if (aFather.IsNull()) {
120 // Get label of the referenced object
121 TDF_Label aLabel = aFather->Label();
124 return GEOM_Object::GetObject(aLabel);
127 //=============================================================================
129 * Constructor: private
131 //=============================================================================
132 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
133 : _label(theEntry), _ior("")
135 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
136 _root = TDataStd_TreeNode::Set(theEntry);
139 //=============================================================================
141 * Constructor: public
143 //=============================================================================
144 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
145 : _label(theEntry), _ior("")
147 theEntry.ForgetAllAttributes(Standard_True);
149 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
150 _root = TDataStd_TreeNode::Set(theEntry);
152 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
154 TDataStd_UAttribute::Set(theEntry, GetObjectID());
157 //=============================================================================
161 //=============================================================================
162 int GEOM_Object::GetType()
164 Handle(TDataStd_Integer) aType;
165 if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
170 //=============================================================================
174 //=============================================================================
175 void GEOM_Object::SetType(int theType)
177 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
181 //=============================================================================
183 * Returns modifications counter of this object.
184 * Comparing this value with modifications counters of argument objects
185 * (on which this object depends) we decide whether this object needs to be updated.
187 //=============================================================================
188 int GEOM_Object::GetTic()
190 Handle(TDataStd_Integer) aTicAttr;
191 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
194 return aTicAttr->Get();
197 //=============================================================================
199 * Set another value of modifications counter.
201 * Use this method to update modifications counter of dependent object
202 * to be equal to modifications counter of its argument.
203 * This is commonly done in GEOM_Function::GetValue()
205 //=============================================================================
206 void GEOM_Object::SetTic(int theTic)
208 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
211 //=============================================================================
213 * Increment modifications counter to mark this object as modified.
215 * Commonly called from GEOM_Function::SetValue()
217 //=============================================================================
218 void GEOM_Object::IncrementTic()
220 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
222 Standard_Integer aTic = 0;
223 Handle(TDataStd_Integer) aTicAttr;
224 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
225 aTic = aTicAttr->Get();
227 TDataStd_Integer::Set(aTicLabel, aTic + 1);
231 //=============================================================================
235 //=============================================================================
236 int GEOM_Object::GetDocID()
238 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
239 if(aDoc.IsNull()) return -1;
241 Handle(TDataStd_Integer) anID;
242 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
248 //=============================================================================
252 //=============================================================================
253 TopoDS_Shape GEOM_Object::GetValue()
257 Handle(GEOM_Function) aFunction = GetLastFunction();
259 if (!aFunction.IsNull())
260 aShape = aFunction->GetValue();
265 //=============================================================================
269 //=============================================================================
270 void GEOM_Object::SetName(const char* theName)
272 TDataStd_Name::Set(_label, (char*)theName);
275 //=============================================================================
279 //=============================================================================
280 char* GEOM_Object::GetName()
282 Handle(TDataStd_Name) aNameAttr;
283 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
285 TCollection_AsciiString aName(aNameAttr->Get());
286 // do not return pointer of local variable
287 // return aName.ToCString();
288 // the following code could lead to memory leak, so take care about recieved pointer
289 return strdup(aName.ToCString());
292 //=============================================================================
296 //=============================================================================
297 void GEOM_Object::SetAuxData(const char* theData)
299 TDataStd_Comment::Set(_label, (char*)theData);
302 //=============================================================================
306 //=============================================================================
307 TCollection_AsciiString GEOM_Object::GetAuxData()
309 TCollection_AsciiString aData;
311 Handle(TDataStd_Comment) aCommentAttr;
312 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
313 aData = aCommentAttr->Get();
319 //=============================================================================
323 //=============================================================================
324 bool GEOM_Object::IsMainShape()
326 Handle(GEOM_Function) aFunction = GetFunction(1);
327 if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
332 //=============================================================================
336 //=============================================================================
337 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
339 Standard_Integer nb = GetNbFunctions();
340 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
342 TDF_Label aChild = FUNCTION_LABEL(nb);
344 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
345 _root->Append(aNode);
347 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
353 //=============================================================================
357 //=============================================================================
358 int GEOM_Object::GetNbFunctions()
360 Standard_Integer nb = 0;
361 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
365 //=============================================================================
369 //=============================================================================
370 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
372 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
373 return GEOM_Function::GetFunction(aChild);
376 //=============================================================================
380 //=============================================================================
381 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
383 Standard_Integer nb = GetNbFunctions();
384 if(nb) return GetFunction(nb);
390 //=============================================================================
394 //=============================================================================
395 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
397 Handle(TColStd_HSequenceOfTransient) anArray;
398 TDF_LabelSequence aSeq;
399 Standard_Integer nb = GetNbFunctions();
400 if(nb == 0) return anArray;
401 for(Standard_Integer i=1; i<=nb; i++) {
402 Handle(GEOM_Function) aFunction = GetFunction(i);
403 if(aFunction.IsNull()) continue;
404 aFunction->GetDependency(aSeq);
407 Standard_Integer aLength = aSeq.Length();
409 anArray = new TColStd_HSequenceOfTransient;
410 for(Standard_Integer j =1; j<=aLength; j++) {
411 Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
412 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
419 //=============================================================================
423 //=============================================================================
424 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
426 Handle(TColStd_HSequenceOfTransient) anArray;
427 Handle(GEOM_Function) aFunction = GetLastFunction();
428 if (aFunction.IsNull()) return anArray;
430 TDF_LabelSequence aSeq;
431 aFunction->GetDependency(aSeq);
432 Standard_Integer aLength = aSeq.Length();
434 anArray = new TColStd_HSequenceOfTransient;
435 for (Standard_Integer i = 1; i <= aLength; i++)
436 anArray->Append(GetReferencedObject(aSeq(i)));
442 //=============================================================================
446 //=============================================================================
447 TDF_Label GEOM_Object::GetFreeLabel()
449 return _label.FindChild(FREE_LABEL);
452 //=======================================================================
453 //function : GEOM_Object_Type_
455 //=======================================================================
456 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
459 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
460 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
461 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
462 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
465 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
466 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
469 (Standard_Address)_Ancestors,
470 (Standard_Address)NULL);
474 //=======================================================================
475 //function : DownCast
477 //=======================================================================
479 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
481 Handle(GEOM_Object) _anOtherObject;
483 if (!AnObject.IsNull()) {
484 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
485 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
489 return _anOtherObject ;