1 // Copyright (C) 2007-2016 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, 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 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
89 if(aDoc.IsNull()) return NULL;
91 Handle(TDataStd_Integer) anID;
92 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
94 GEOM_Engine* anEngine = GEOM_Engine::GetEngine();
95 if(anEngine == NULL) return NULL;
96 return anEngine->GetObject(anID->Get(), anEntry.ToCString());
99 //=============================================================================
101 * GetReferencedObject
103 //=============================================================================
104 Handle(GEOM_BaseObject) GEOM_BaseObject::GetReferencedObject(const TDF_Label& theLabel)
106 Handle(TDF_Reference) aRef;
107 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
111 if(aRef.IsNull() || aRef->Get().IsNull()) {
116 // Get TreeNode of a referenced function
117 Handle(TDataStd_TreeNode) aT, aFather;
118 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
123 // Get TreeNode of Object of the referenced function
124 aFather = aT->Father();
125 if (aFather.IsNull()) {
129 // Get label of the referenced object
130 TDF_Label aLabel = aFather->Label();
133 return GEOM_BaseObject::GetObject(aLabel);
136 //=======================================================================
137 //function : GetEntryString
138 //purpose : Returns an entry of this GEOM_BaseObject
139 //=======================================================================
141 TCollection_AsciiString GEOM_BaseObject::GetEntryString()
143 TCollection_AsciiString anEntry;
144 TDF_Tool::Entry( GetEntry(), anEntry );
148 //=======================================================================
150 //purpose : Returns type of an object (GEOM_POINT, GEOM_VECTOR...) on theLabel,
151 // -1 if no object is there
152 //=======================================================================
154 int GEOM_BaseObject::GetType(const TDF_Label& theLabel)
156 Handle(TDataStd_Integer) aType;
157 if(theLabel.IsNull() ||
158 !theLabel.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
164 //=============================================================================
166 * Constructor: private
168 //=============================================================================
169 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry)
170 : _label(theEntry), _ior(""), _docID(-1)
172 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
174 Handle(TDataStd_Integer) anID;
175 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
178 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
179 _root = TDataStd_TreeNode::Set(theEntry);
182 //=============================================================================
184 * Constructor: public
186 //=============================================================================
187 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry, int theType)
188 : _label(theEntry), _ior(""), _docID(-1)
190 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
192 Handle(TDataStd_Integer) anID;
193 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
196 theEntry.ForgetAllAttributes(Standard_True);
198 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
199 _root = TDataStd_TreeNode::Set(theEntry);
201 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
203 TDataStd_UAttribute::Set(theEntry, GetObjectID());
206 //=============================================================================
210 //=============================================================================
211 GEOM_BaseObject::~GEOM_BaseObject()
213 //MESSAGE("GEOM_BaseObject::~GEOM_BaseObject()");
216 //=============================================================================
220 //=============================================================================
221 int GEOM_BaseObject::GetType()
224 Handle(TDataStd_Integer) aType;
225 if(_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
231 //=============================================================================
235 //=============================================================================
236 void GEOM_BaseObject::SetType(int theType)
238 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
242 //=============================================================================
244 * Returns modifications counter of this object.
245 * Comparing this value with modifications counters of argument objects
246 * (on which this object depends) we decide whether this object needs to be updated.
248 //=============================================================================
249 int GEOM_BaseObject::GetTic()
251 Handle(TDataStd_Integer) aTicAttr;
252 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
255 return aTicAttr->Get();
258 //=============================================================================
260 * Set another value of modifications counter.
262 * Use this method to update modifications counter of dependent object
263 * to be equal to modifications counter of its argument.
264 * This is commonly done in GEOM_Function::GetValue()
266 //=============================================================================
267 void GEOM_BaseObject::SetTic(int theTic)
269 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
272 //=============================================================================
274 * Increment modifications counter to mark this object as modified.
276 * Commonly called from GEOM_Function::SetValue()
278 //=============================================================================
279 void GEOM_BaseObject::IncrementTic()
281 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
283 Standard_Integer aTic = 0;
284 Handle(TDataStd_Integer) aTicAttr;
285 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
286 aTic = aTicAttr->Get();
288 TDataStd_Integer::Set(aTicLabel, aTic + 1);
292 //=============================================================================
296 //=============================================================================
297 int GEOM_BaseObject::GetDocID()
302 //=============================================================================
306 //=============================================================================
307 void GEOM_BaseObject::SetName(const char* theName)
309 TDataStd_Name::Set(_label, (char*)theName);
312 //=============================================================================
316 //=============================================================================
317 TCollection_AsciiString GEOM_BaseObject::GetName()
319 TCollection_AsciiString aName;
320 Handle(TDataStd_Name) aNameAttr;
321 if(_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr))
322 aName = aNameAttr->Get();
323 // do not return pointer of local variable
324 // return aName.ToCString();
325 // the following code could lead to memory leak, so take care about received pointer
329 //=============================================================================
333 //=============================================================================
334 void GEOM_BaseObject::SetAuxData(const char* theData)
336 TDataStd_Comment::Set(_label, (char*)theData);
339 //=============================================================================
343 //=============================================================================
344 TCollection_AsciiString GEOM_BaseObject::GetAuxData()
346 TCollection_AsciiString aData;
348 Handle(TDataStd_Comment) aCommentAttr;
349 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
350 aData = aCommentAttr->Get();
355 //=============================================================================
359 //=============================================================================
360 void GEOM_BaseObject::SetParameters(const TCollection_AsciiString& theParameters)
362 if( _parameters.IsEmpty() )
363 _parameters = theParameters;
366 _parameters += theParameters;
370 //=============================================================================
374 //=============================================================================
375 TCollection_AsciiString GEOM_BaseObject::GetParameters() const
380 //=============================================================================
384 //=============================================================================
385 Handle(GEOM_Function) GEOM_BaseObject::AddFunction(const Standard_GUID& theGUID,
389 Standard_Integer nb = GetNbFunctions();
390 if(!allowSubShape && nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub-shape
391 TDF_Label aChild = FUNCTION_LABEL(++nb);
393 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
394 _root->Append(aNode);
396 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
401 //=============================================================================
405 //=============================================================================
406 int GEOM_BaseObject::GetNbFunctions()
408 Standard_Integer nb = 0;
409 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
413 //=============================================================================
417 //=============================================================================
418 Handle(GEOM_Function) GEOM_BaseObject::GetFunction(int theFunctionNumber)
420 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
421 return GEOM_Function::GetFunction(aChild);
424 //=============================================================================
428 //=============================================================================
429 Handle(GEOM_Function) GEOM_BaseObject::GetLastFunction()
431 Standard_Integer nb = GetNbFunctions();
432 if(nb) return GetFunction(nb);
437 //=============================================================================
441 //=============================================================================
442 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetAllDependency()
444 Handle(TColStd_HSequenceOfTransient) anArray;
445 TDF_LabelSequence aSeq;
446 Standard_Integer nb = GetNbFunctions();
447 if(nb == 0) return anArray;
448 for(Standard_Integer i=1; i<=nb; i++) {
449 Handle(GEOM_Function) aFunction = GetFunction(i);
450 if(aFunction.IsNull()) continue;
451 aFunction->GetDependency(aSeq);
454 Standard_Integer aLength = aSeq.Length();
456 anArray = new TColStd_HSequenceOfTransient;
457 for(Standard_Integer j =1; j<=aLength; j++) {
458 Handle(GEOM_BaseObject) aRefObj = GetReferencedObject(aSeq(j));
459 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
466 //=============================================================================
470 //=============================================================================
471 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetLastDependency()
473 Handle(TColStd_HSequenceOfTransient) anArray;
474 Handle(GEOM_Function) aFunction = GetLastFunction();
475 if (aFunction.IsNull()) return anArray;
477 TDF_LabelSequence aSeq;
478 aFunction->GetDependency(aSeq);
479 Standard_Integer aLength = aSeq.Length();
481 anArray = new TColStd_HSequenceOfTransient;
482 for (Standard_Integer i = 1; i <= aLength; i++)
483 anArray->Append(GetReferencedObject(aSeq(i)));
489 //================================================================================
491 * \brief Returns a driver creator of this object
493 //================================================================================
495 Handle(TFunction_Driver) GEOM_BaseObject::GetCreationDriver(int funNb)
497 Handle(TFunction_Driver) driver;
498 Handle(GEOM_Function) function = GetFunction(funNb);
499 if ( !function.IsNull() )
501 Standard_GUID aGUID = function->GetDriverGUID();
502 if ( TFunction_DriverTable::Get()->FindDriver(aGUID, driver ))
503 driver->Init( function->GetEntry() );
508 //=============================================================================
512 //=============================================================================
513 TDF_Label GEOM_BaseObject::GetFreeLabel()
515 return _label.FindChild(FREE_LABEL);
518 OCCT_IMPLEMENT_STANDARD_RTTIEXT(GEOM_BaseObject, Standard_Transient );