1 // Copyright (C) 2007-2024 CEA, EDF, 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, or (at your option) any later version.
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_DriverTable.hxx>
41 #include "utilities.h"
44 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
48 // #define COLOR_LABEL 5 -- Labels used by GEOM_Object
49 // #define AUTO_COLOR_LABEL 6
50 // #define MARKER_LABEL 7
53 //=======================================================================
54 //function : GetObjectID
56 //=======================================================================
58 const Standard_GUID& GEOM_BaseObject::GetObjectID()
60 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
64 //=======================================================================
65 //function : GetSubShapeID
67 //=======================================================================
69 const Standard_GUID& GEOM_BaseObject::GetSubShapeID()
71 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
75 //=============================================================================
79 //=============================================================================
81 Handle(GEOM_BaseObject) GEOM_BaseObject::GetObject(const TDF_Label& theLabel)
83 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
85 TCollection_AsciiString anEntry;
86 TDF_Tool::Entry(theLabel, anEntry);
88 GEOM_Engine* anEngine = GEOM_Engine::GetEngine();
89 if(anEngine == NULL) return NULL;
90 return anEngine->GetObject(anEntry.ToCString());
93 //=============================================================================
97 //=============================================================================
98 Handle(GEOM_BaseObject) GEOM_BaseObject::GetReferencedObject(const TDF_Label& theLabel)
100 Handle(TDF_Reference) aRef;
101 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
105 if(aRef.IsNull() || aRef->Get().IsNull()) {
110 // Get TreeNode of a referenced function
111 Handle(TDataStd_TreeNode) aT, aFather;
112 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
117 // Get TreeNode of Object of the referenced function
118 aFather = aT->Father();
119 if (aFather.IsNull()) {
123 // Get label of the referenced object
124 TDF_Label aLabel = aFather->Label();
127 return GEOM_BaseObject::GetObject(aLabel);
130 //=======================================================================
131 //function : GetEntryString
132 //purpose : Returns an entry of this GEOM_BaseObject
133 //=======================================================================
135 TCollection_AsciiString GEOM_BaseObject::GetEntryString()
137 TCollection_AsciiString anEntry;
138 TDF_Tool::Entry( GetEntry(), anEntry );
142 //=======================================================================
144 //purpose : Returns type of an object (GEOM_POINT, GEOM_VECTOR...) on theLabel,
145 // -1 if no object is there
146 //=======================================================================
148 int GEOM_BaseObject::GetType(const TDF_Label& theLabel)
150 Handle(TDataStd_Integer) aType;
151 if(theLabel.IsNull() ||
152 !theLabel.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
158 //=============================================================================
160 * Constructor: private
162 //=============================================================================
163 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry)
164 : _label(theEntry), _ior("")
166 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
167 _root = TDataStd_TreeNode::Set(theEntry);
170 //=============================================================================
172 * Constructor: public
174 //=============================================================================
175 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry, int theType)
176 : _label(theEntry), _ior("")
178 theEntry.ForgetAllAttributes(Standard_True);
180 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
181 _root = TDataStd_TreeNode::Set(theEntry);
183 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
185 TDataStd_UAttribute::Set(theEntry, GetObjectID());
188 //=============================================================================
192 //=============================================================================
193 GEOM_BaseObject::~GEOM_BaseObject()
195 //MESSAGE("GEOM_BaseObject::~GEOM_BaseObject()");
198 //=============================================================================
202 //=============================================================================
203 int GEOM_BaseObject::GetType()
206 Handle(TDataStd_Integer) aType;
207 if(_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
213 //=============================================================================
217 //=============================================================================
218 void GEOM_BaseObject::SetType(int theType)
220 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
224 //=============================================================================
226 * Returns modifications counter of this object.
227 * Comparing this value with modifications counters of argument objects
228 * (on which this object depends) we decide whether this object needs to be updated.
230 //=============================================================================
231 int GEOM_BaseObject::GetTic()
233 Handle(TDataStd_Integer) aTicAttr;
234 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
237 return aTicAttr->Get();
240 //=============================================================================
242 * Set another value of modifications counter.
244 * Use this method to update modifications counter of dependent object
245 * to be equal to modifications counter of its argument.
246 * This is commonly done in GEOM_Function::GetValue()
248 //=============================================================================
249 void GEOM_BaseObject::SetTic(int theTic)
251 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
254 //=============================================================================
256 * Increment modifications counter to mark this object as modified.
258 * Commonly called from GEOM_Function::SetValue()
260 //=============================================================================
261 void GEOM_BaseObject::IncrementTic()
263 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
265 Standard_Integer aTic = 0;
266 Handle(TDataStd_Integer) aTicAttr;
267 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
268 aTic = aTicAttr->Get();
270 TDataStd_Integer::Set(aTicLabel, aTic + 1);
273 //=============================================================================
277 //=============================================================================
278 void GEOM_BaseObject::SetName(const char* theName)
280 TDataStd_Name::Set(_label, (char*)theName);
283 //=============================================================================
287 //=============================================================================
288 TCollection_AsciiString GEOM_BaseObject::GetName()
290 TCollection_AsciiString aName;
291 Handle(TDataStd_Name) aNameAttr;
292 if(_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr))
293 aName = aNameAttr->Get();
294 // do not return pointer of local variable
295 // return aName.ToCString();
296 // the following code could lead to memory leak, so take care about received pointer
300 //=============================================================================
304 //=============================================================================
305 void GEOM_BaseObject::SetAuxData(const char* theData)
307 TDataStd_Comment::Set(_label, (char*)theData);
310 //=============================================================================
314 //=============================================================================
315 TCollection_AsciiString GEOM_BaseObject::GetAuxData()
317 TCollection_AsciiString aData;
319 Handle(TDataStd_Comment) aCommentAttr;
320 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
321 aData = aCommentAttr->Get();
326 //=============================================================================
330 //=============================================================================
331 void GEOM_BaseObject::SetParameters(const TCollection_AsciiString& theParameters)
333 if( _parameters.IsEmpty() )
334 _parameters = theParameters;
337 _parameters += theParameters;
341 //=============================================================================
345 //=============================================================================
346 TCollection_AsciiString GEOM_BaseObject::GetParameters() const
351 //=============================================================================
355 //=============================================================================
356 Handle(GEOM_Function) GEOM_BaseObject::AddFunction(const Standard_GUID& theGUID,
360 Standard_Integer nb = GetNbFunctions();
361 if(!allowSubShape && nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub-shape
362 TDF_Label aChild = FUNCTION_LABEL(++nb);
364 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
365 _root->Append(aNode);
367 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
372 //=============================================================================
376 //=============================================================================
377 int GEOM_BaseObject::GetNbFunctions()
379 Standard_Integer nb = 0;
380 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
384 //=============================================================================
388 //=============================================================================
389 Handle(GEOM_Function) GEOM_BaseObject::GetFunction(int theFunctionNumber)
391 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
392 return GEOM_Function::GetFunction(aChild);
395 //=============================================================================
399 //=============================================================================
400 Handle(GEOM_Function) GEOM_BaseObject::GetLastFunction()
402 Standard_Integer nb = GetNbFunctions();
403 if(nb) return GetFunction(nb);
408 //=============================================================================
412 //=============================================================================
413 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetAllDependency()
415 Handle(TColStd_HSequenceOfTransient) anArray;
416 TDF_LabelSequence aSeq;
417 Standard_Integer nb = GetNbFunctions();
418 if(nb == 0) return anArray;
419 for(Standard_Integer i=1; i<=nb; i++) {
420 Handle(GEOM_Function) aFunction = GetFunction(i);
421 if(aFunction.IsNull()) continue;
422 aFunction->GetDependency(aSeq);
425 Standard_Integer aLength = aSeq.Length();
427 anArray = new TColStd_HSequenceOfTransient;
428 for(Standard_Integer j =1; j<=aLength; j++) {
429 Handle(GEOM_BaseObject) aRefObj = GetReferencedObject(aSeq(j));
430 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
437 //=============================================================================
441 //=============================================================================
442 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetLastDependency()
444 Handle(TColStd_HSequenceOfTransient) anArray;
445 Handle(GEOM_Function) aFunction = GetLastFunction();
446 if (aFunction.IsNull()) return anArray;
448 TDF_LabelSequence aSeq;
449 aFunction->GetDependency(aSeq);
450 Standard_Integer aLength = aSeq.Length();
452 anArray = new TColStd_HSequenceOfTransient;
453 for (Standard_Integer i = 1; i <= aLength; i++)
454 anArray->Append(GetReferencedObject(aSeq(i)));
460 //================================================================================
462 * \brief Returns a driver creator of this object
464 //================================================================================
466 Handle(TFunction_Driver) GEOM_BaseObject::GetCreationDriver(int funNb)
468 Handle(TFunction_Driver) driver;
469 Handle(GEOM_Function) function = GetFunction(funNb);
470 if ( !function.IsNull() )
472 Standard_GUID aGUID = function->GetDriverGUID();
473 if ( TFunction_DriverTable::Get()->FindDriver(aGUID, driver ))
474 driver->Init( function->GetEntry() );
479 //=============================================================================
483 //=============================================================================
484 TDF_Label GEOM_BaseObject::GetFreeLabel()
486 return _label.FindChild(FREE_LABEL);
489 IMPLEMENT_STANDARD_RTTIEXT(GEOM_BaseObject, Standard_Transient )