1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "GEOM_BaseObject.hxx"
24 #include "GEOM_Engine.hxx"
26 #include <TCollection_ExtendedString.hxx>
27 #include <TDF_Data.hxx>
28 #include <TDF_LabelSequence.hxx>
29 #include <TDF_Reference.hxx>
30 #include <TDF_Tool.hxx>
31 #include <TDataStd_ChildNodeIterator.hxx>
32 #include <TDataStd_Comment.hxx>
33 #include <TDataStd_Integer.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TDataStd_Real.hxx>
36 #include <TDataStd_UAttribute.hxx>
37 #include <TDocStd_Document.hxx>
38 #include <TDocStd_Owner.hxx>
39 #include <TFunction_Driver.hxx>
40 #include <TFunction_DriverTable.hxx>
42 #include "utilities.h"
45 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
49 // #define COLOR_LABEL 5 -- Labels used by GEOM_Object
50 // #define AUTO_COLOR_LABEL 6
51 // #define MARKER_LABEL 7
54 //=======================================================================
55 //function : GetObjectID
57 //=======================================================================
59 const Standard_GUID& GEOM_BaseObject::GetObjectID()
61 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
65 //=======================================================================
66 //function : GetSubShapeID
68 //=======================================================================
70 const Standard_GUID& GEOM_BaseObject::GetSubShapeID()
72 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
76 //=============================================================================
80 //=============================================================================
82 Handle(GEOM_BaseObject) GEOM_BaseObject::GetObject(const TDF_Label& theLabel)
84 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
86 TCollection_AsciiString anEntry;
87 TDF_Tool::Entry(theLabel, anEntry);
89 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
90 if(aDoc.IsNull()) return NULL;
92 Handle(TDataStd_Integer) anID;
93 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
95 GEOM_Engine* anEngine = GEOM_Engine::GetEngine();
96 if(anEngine == NULL) return NULL;
97 return anEngine->GetObject(anID->Get(), anEntry.ToCString());
100 //=============================================================================
102 * GetReferencedObject
104 //=============================================================================
105 Handle(GEOM_BaseObject) GEOM_BaseObject::GetReferencedObject(const TDF_Label& theLabel)
107 Handle(TDF_Reference) aRef;
108 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
112 if(aRef.IsNull() || aRef->Get().IsNull()) {
117 // Get TreeNode of a referenced function
118 Handle(TDataStd_TreeNode) aT, aFather;
119 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
124 // Get TreeNode of Object of the referenced function
125 aFather = aT->Father();
126 if (aFather.IsNull()) {
130 // Get label of the referenced object
131 TDF_Label aLabel = aFather->Label();
134 return GEOM_BaseObject::GetObject(aLabel);
137 //=======================================================================
138 //function : GetEntryString
139 //purpose : Returns an entry of this GEOM_BaseObject
140 //=======================================================================
142 TCollection_AsciiString GEOM_BaseObject::GetEntryString()
144 TCollection_AsciiString anEntry;
145 TDF_Tool::Entry( GetEntry(), anEntry );
149 //=======================================================================
151 //purpose : Returns type of an object (GEOM_POINT, GEOM_VECTOR...) on theLabel,
152 // -1 if no object is there
153 //=======================================================================
155 int GEOM_BaseObject::GetType(const TDF_Label& theLabel)
157 Handle(TDataStd_Integer) aType;
158 if(theLabel.IsNull() ||
159 !theLabel.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
165 //=============================================================================
167 * Constructor: private
169 //=============================================================================
170 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry)
171 : _label(theEntry), _ior(""), _docID(-1)
173 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
175 Handle(TDataStd_Integer) anID;
176 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
179 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
180 _root = TDataStd_TreeNode::Set(theEntry);
183 //=============================================================================
185 * Constructor: public
187 //=============================================================================
188 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry, int theType)
189 : _label(theEntry), _ior(""), _docID(-1)
191 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
193 Handle(TDataStd_Integer) anID;
194 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
197 theEntry.ForgetAllAttributes(Standard_True);
199 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
200 _root = TDataStd_TreeNode::Set(theEntry);
202 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
204 TDataStd_UAttribute::Set(theEntry, GetObjectID());
207 //=============================================================================
211 //=============================================================================
212 GEOM_BaseObject::~GEOM_BaseObject()
214 MESSAGE("GEOM_BaseObject::~GEOM_BaseObject()");
217 //=============================================================================
221 //=============================================================================
222 int GEOM_BaseObject::GetType()
225 Handle(TDataStd_Integer) aType;
226 if(_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
232 //=============================================================================
236 //=============================================================================
237 void GEOM_BaseObject::SetType(int theType)
239 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
243 //=============================================================================
245 * Returns modifications counter of this object.
246 * Comparing this value with modifications counters of argument objects
247 * (on which this object depends) we decide whether this object needs to be updated.
249 //=============================================================================
250 int GEOM_BaseObject::GetTic()
252 Handle(TDataStd_Integer) aTicAttr;
253 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
256 return aTicAttr->Get();
259 //=============================================================================
261 * Set another value of modifications counter.
263 * Use this method to update modifications counter of dependent object
264 * to be equal to modifications counter of its argument.
265 * This is commonly done in GEOM_Function::GetValue()
267 //=============================================================================
268 void GEOM_BaseObject::SetTic(int theTic)
270 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
273 //=============================================================================
275 * Increment modifications counter to mark this object as modified.
277 * Commonly called from GEOM_Function::SetValue()
279 //=============================================================================
280 void GEOM_BaseObject::IncrementTic()
282 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
284 Standard_Integer aTic = 0;
285 Handle(TDataStd_Integer) aTicAttr;
286 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
287 aTic = aTicAttr->Get();
289 TDataStd_Integer::Set(aTicLabel, aTic + 1);
293 //=============================================================================
297 //=============================================================================
298 int GEOM_BaseObject::GetDocID()
303 //=============================================================================
307 //=============================================================================
308 void GEOM_BaseObject::SetName(const char* theName)
310 TDataStd_Name::Set(_label, (char*)theName);
313 //=============================================================================
317 //=============================================================================
318 TCollection_AsciiString GEOM_BaseObject::GetName()
320 TCollection_AsciiString aName;
321 Handle(TDataStd_Name) aNameAttr;
322 if(_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr))
323 aName = aNameAttr->Get();
324 // do not return pointer of local variable
325 // return aName.ToCString();
326 // the following code could lead to memory leak, so take care about recieved pointer
330 //=============================================================================
334 //=============================================================================
335 void GEOM_BaseObject::SetAuxData(const char* theData)
337 TDataStd_Comment::Set(_label, (char*)theData);
340 //=============================================================================
344 //=============================================================================
345 TCollection_AsciiString GEOM_BaseObject::GetAuxData()
347 TCollection_AsciiString aData;
349 Handle(TDataStd_Comment) aCommentAttr;
350 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
351 aData = aCommentAttr->Get();
356 //=============================================================================
360 //=============================================================================
361 void GEOM_BaseObject::SetParameters(const TCollection_AsciiString& theParameters)
363 if( _parameters.IsEmpty() )
364 _parameters = theParameters;
367 _parameters += theParameters;
371 //=============================================================================
375 //=============================================================================
376 TCollection_AsciiString GEOM_BaseObject::GetParameters() const
381 //=============================================================================
385 //=============================================================================
386 Handle(GEOM_Function) GEOM_BaseObject::AddFunction(const Standard_GUID& theGUID,
390 Standard_Integer nb = GetNbFunctions();
391 if(!allowSubShape && nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub-shape
392 TDF_Label aChild = FUNCTION_LABEL(++nb);
394 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
395 _root->Append(aNode);
397 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
402 //=============================================================================
406 //=============================================================================
407 int GEOM_BaseObject::GetNbFunctions()
409 Standard_Integer nb = 0;
410 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
414 //=============================================================================
418 //=============================================================================
419 Handle(GEOM_Function) GEOM_BaseObject::GetFunction(int theFunctionNumber)
421 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
422 return GEOM_Function::GetFunction(aChild);
425 //=============================================================================
429 //=============================================================================
430 Handle(GEOM_Function) GEOM_BaseObject::GetLastFunction()
432 Standard_Integer nb = GetNbFunctions();
433 if(nb) return GetFunction(nb);
438 //=============================================================================
442 //=============================================================================
443 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetAllDependency()
445 Handle(TColStd_HSequenceOfTransient) anArray;
446 TDF_LabelSequence aSeq;
447 Standard_Integer nb = GetNbFunctions();
448 if(nb == 0) return anArray;
449 for(Standard_Integer i=1; i<=nb; i++) {
450 Handle(GEOM_Function) aFunction = GetFunction(i);
451 if(aFunction.IsNull()) continue;
452 aFunction->GetDependency(aSeq);
455 Standard_Integer aLength = aSeq.Length();
457 anArray = new TColStd_HSequenceOfTransient;
458 for(Standard_Integer j =1; j<=aLength; j++) {
459 Handle(GEOM_BaseObject) aRefObj = GetReferencedObject(aSeq(j));
460 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
467 //=============================================================================
471 //=============================================================================
472 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetLastDependency()
474 Handle(TColStd_HSequenceOfTransient) anArray;
475 Handle(GEOM_Function) aFunction = GetLastFunction();
476 if (aFunction.IsNull()) return anArray;
478 TDF_LabelSequence aSeq;
479 aFunction->GetDependency(aSeq);
480 Standard_Integer aLength = aSeq.Length();
482 anArray = new TColStd_HSequenceOfTransient;
483 for (Standard_Integer i = 1; i <= aLength; i++)
484 anArray->Append(GetReferencedObject(aSeq(i)));
490 //================================================================================
492 * \brief Returns a driver creator of this object
494 //================================================================================
496 Handle(TFunction_Driver) GEOM_BaseObject::GetCreationDriver()
498 Handle(TFunction_Driver) aDriver;
500 Handle(GEOM_Function) function = GetFunction(1);
501 if ( !function.IsNull() )
503 Standard_GUID aGUID = function->GetDriverGUID();
504 if ( TFunction_DriverTable::Get()->FindDriver(aGUID, aDriver))
505 aDriver->Init( function->GetEntry() );
510 //=============================================================================
514 //=============================================================================
515 TDF_Label GEOM_BaseObject::GetFreeLabel()
517 return _label.FindChild(FREE_LABEL);
520 IMPLEMENT_STANDARD_HANDLE (GEOM_BaseObject, Standard_Transient );
521 IMPLEMENT_STANDARD_RTTIEXT(GEOM_BaseObject, Standard_Transient );