Salome HOME
71b460dfdebd9fca6e135e78b2613e3e20eab665
[modules/geom.git] / src / GEOM / GEOM_BaseObject.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "GEOM_BaseObject.hxx"
24 #include "GEOM_Engine.hxx"
25
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>
40
41 #include "utilities.h"
42
43
44 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
45 #define TYPE_LABEL       2
46 #define FREE_LABEL       3
47 #define TIC_LABEL        4
48 // #define COLOR_LABEL      5 -- Labels used by GEOM_Object
49 // #define AUTO_COLOR_LABEL 6
50 // #define MARKER_LABEL     7
51
52
53 //=======================================================================
54 //function : GetObjectID
55 //purpose  :
56 //=======================================================================
57
58 const Standard_GUID& GEOM_BaseObject::GetObjectID()
59 {
60   static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
61   return anObjectID;
62 }
63
64 //=======================================================================
65 //function : GetSubShapeID
66 //purpose  :
67 //=======================================================================
68
69 const Standard_GUID& GEOM_BaseObject::GetSubShapeID()
70 {
71   static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
72   return anObjectID;
73 }
74
75 //=============================================================================
76 /*!
77  *  GetObject
78  */
79 //=============================================================================
80
81 Handle(GEOM_BaseObject) GEOM_BaseObject::GetObject(const TDF_Label& theLabel)
82 {
83   if (!theLabel.IsAttribute(GetObjectID())) return NULL;
84
85   TCollection_AsciiString anEntry;
86   TDF_Tool::Entry(theLabel, anEntry);
87
88   GEOM_Engine* anEngine = GEOM_Engine::GetEngine();
89   if(anEngine == NULL) return NULL;
90   return anEngine->GetObject(anEntry.ToCString());
91 }
92
93 //=============================================================================
94 /*!
95  *  GetReferencedObject
96  */
97 //=============================================================================
98 Handle(GEOM_BaseObject) GEOM_BaseObject::GetReferencedObject(const TDF_Label& theLabel)
99 {
100   Handle(TDF_Reference) aRef;
101   if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
102     return NULL;
103   }
104   
105   if(aRef.IsNull() || aRef->Get().IsNull()) {
106     return NULL;
107   }
108
109
110   // Get TreeNode of a referenced function
111   Handle(TDataStd_TreeNode) aT, aFather;
112   if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
113     return NULL;
114   }
115
116
117   // Get TreeNode of Object of the referenced function
118   aFather = aT->Father();
119   if (aFather.IsNull()) {
120     return NULL;
121   }
122
123   // Get label of the referenced object
124   TDF_Label aLabel = aFather->Label();
125   
126
127   return GEOM_BaseObject::GetObject(aLabel);
128 }
129
130 //=======================================================================
131 //function : GetEntryString
132 //purpose  : Returns an entry of this GEOM_BaseObject
133 //=======================================================================
134
135 TCollection_AsciiString GEOM_BaseObject::GetEntryString()
136 {
137   TCollection_AsciiString anEntry;
138   TDF_Tool::Entry( GetEntry(), anEntry );
139   return anEntry;
140 }
141
142 //=======================================================================
143 //function : GetType
144 //purpose  : Returns type of an object (GEOM_POINT, GEOM_VECTOR...) on theLabel,
145 //           -1 if no object is there
146 //=======================================================================
147
148 int GEOM_BaseObject::GetType(const TDF_Label& theLabel)
149 {
150   Handle(TDataStd_Integer) aType;
151   if(theLabel.IsNull() ||
152      !theLabel.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
153     return -1;
154
155   return aType->Get();
156 }
157
158 //=============================================================================
159 /*!
160  *  Constructor: private
161  */
162 //=============================================================================
163 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry)
164   : _label(theEntry), _ior("")
165 {
166   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
167     _root = TDataStd_TreeNode::Set(theEntry);
168 }
169
170 //=============================================================================
171 /*!
172  *  Constructor: public
173  */
174 //=============================================================================
175 GEOM_BaseObject::GEOM_BaseObject(const TDF_Label& theEntry, int theType)
176 : _label(theEntry), _ior("")
177 {
178   theEntry.ForgetAllAttributes(Standard_True);
179
180   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
181     _root = TDataStd_TreeNode::Set(theEntry);
182
183   TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
184
185   TDataStd_UAttribute::Set(theEntry, GetObjectID());
186 }
187
188 //=============================================================================
189 /*!
190  *  Destructor
191  */
192 //=============================================================================
193 GEOM_BaseObject::~GEOM_BaseObject()
194 {
195   //MESSAGE("GEOM_BaseObject::~GEOM_BaseObject()");
196 }
197
198 //=============================================================================
199 /*!
200  *  GetType
201  */
202 //=============================================================================
203 int GEOM_BaseObject::GetType()
204 {
205   int type = -1;
206   Handle(TDataStd_Integer) aType;
207   if(_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType))
208     type = aType->Get();
209
210   return type;
211 }
212
213 //=============================================================================
214 /*!
215  *  SetType
216  */
217 //=============================================================================
218 void GEOM_BaseObject::SetType(int theType)
219 {
220   TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
221 }
222
223
224 //=============================================================================
225 /*!
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.
229  */
230 //=============================================================================
231 int GEOM_BaseObject::GetTic()
232 {
233   Handle(TDataStd_Integer) aTicAttr;
234   if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
235     return 0;
236
237   return aTicAttr->Get();
238 }
239
240 //=============================================================================
241 /*!
242  *  Set another value of modifications counter.
243  *
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()
247  */
248 //=============================================================================
249 void GEOM_BaseObject::SetTic(int theTic)
250 {
251   TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
252 }
253
254 //=============================================================================
255 /*!
256  *  Increment modifications counter to mark this object as modified.
257  *
258  *  Commonly called from GEOM_Function::SetValue()
259  */
260 //=============================================================================
261 void GEOM_BaseObject::IncrementTic()
262 {
263   TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
264
265   Standard_Integer aTic = 0;
266   Handle(TDataStd_Integer) aTicAttr;
267   if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
268     aTic = aTicAttr->Get();
269
270   TDataStd_Integer::Set(aTicLabel, aTic + 1);
271 }
272
273 //=============================================================================
274 /*!
275  *  SetName
276  */
277 //=============================================================================
278 void GEOM_BaseObject::SetName(const char* theName)
279 {
280   TDataStd_Name::Set(_label, (char*)theName);
281 }
282
283 //=============================================================================
284 /*!
285  *  GetName
286  */
287 //=============================================================================
288 TCollection_AsciiString GEOM_BaseObject::GetName()
289 {
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
297   return aName;
298 }
299
300 //=============================================================================
301 /*!
302  *  SetAuxData
303  */
304 //=============================================================================
305 void GEOM_BaseObject::SetAuxData(const char* theData)
306 {
307   TDataStd_Comment::Set(_label, (char*)theData);
308 }
309
310 //=============================================================================
311 /*!
312  *  GetAuxData
313  */
314 //=============================================================================
315 TCollection_AsciiString GEOM_BaseObject::GetAuxData()
316 {
317   TCollection_AsciiString aData;
318
319   Handle(TDataStd_Comment) aCommentAttr;
320   if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
321     aData = aCommentAttr->Get();
322
323   return aData;
324 }
325
326 //=============================================================================
327 /*!
328  *  SetParameters
329  */
330 //=============================================================================
331 void GEOM_BaseObject::SetParameters(const TCollection_AsciiString& theParameters)
332 {
333   if( _parameters.IsEmpty() )
334     _parameters = theParameters;
335   else {
336     _parameters += "|";
337     _parameters += theParameters;
338   }
339 }
340
341 //=============================================================================
342 /*!
343  *  GetParameters
344  */
345 //=============================================================================
346 TCollection_AsciiString GEOM_BaseObject::GetParameters() const
347 {
348   return _parameters;
349 }
350
351 //=============================================================================
352 /*!
353  *  AddFunction
354  */
355 //=============================================================================
356 Handle(GEOM_Function) GEOM_BaseObject::AddFunction(const Standard_GUID& theGUID,
357                                                    int                  theFunctionType,
358                                                    bool                 allowSubShape)
359 {
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);
363
364   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
365   _root->Append(aNode);
366
367   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
368
369   return aFunction;
370 }
371
372 //=============================================================================
373 /*!
374  *  GetNbFunctions
375  */
376 //=============================================================================
377 int GEOM_BaseObject::GetNbFunctions()
378 {
379   Standard_Integer nb = 0;
380   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
381   return nb;
382 }
383
384 //=============================================================================
385 /*!
386  *  GetFunction
387  */
388 //=============================================================================
389 Handle(GEOM_Function) GEOM_BaseObject::GetFunction(int theFunctionNumber)
390 {
391   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
392   return GEOM_Function::GetFunction(aChild);
393 }
394
395 //=============================================================================
396 /*!
397  *  GetlastFunction
398  */
399 //=============================================================================
400 Handle(GEOM_Function) GEOM_BaseObject::GetLastFunction()
401 {
402   Standard_Integer nb = GetNbFunctions();
403   if(nb) return GetFunction(nb);
404
405   return NULL;
406 }
407
408 //=============================================================================
409 /*!
410  *  GetAllDependency
411  */
412 //=============================================================================
413 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetAllDependency()
414 {
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);
423   }
424
425   Standard_Integer aLength = aSeq.Length();
426   if(aLength > 0) {
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);
431     }
432   }
433
434   return anArray;
435 }
436
437 //=============================================================================
438 /*!
439  *  GetLastDependency
440  */
441 //=============================================================================
442 Handle(TColStd_HSequenceOfTransient) GEOM_BaseObject::GetLastDependency()
443 {
444   Handle(TColStd_HSequenceOfTransient) anArray;
445   Handle(GEOM_Function) aFunction = GetLastFunction();
446   if (aFunction.IsNull()) return anArray;
447
448   TDF_LabelSequence aSeq;
449   aFunction->GetDependency(aSeq);
450   Standard_Integer aLength = aSeq.Length();
451   if (aLength > 0) {
452     anArray = new TColStd_HSequenceOfTransient;
453     for (Standard_Integer i = 1; i <= aLength; i++)
454       anArray->Append(GetReferencedObject(aSeq(i)));
455   }
456
457   return anArray;
458 }
459
460 //================================================================================
461 /*!
462  * \brief Returns a driver creator of this object
463  */
464 //================================================================================
465
466 Handle(TFunction_Driver) GEOM_BaseObject::GetCreationDriver(int funNb)
467 {
468   Handle(TFunction_Driver) driver;
469   Handle(GEOM_Function) function = GetFunction(funNb);
470   if ( !function.IsNull() )
471   {
472     Standard_GUID aGUID = function->GetDriverGUID();
473     if ( TFunction_DriverTable::Get()->FindDriver(aGUID, driver ))
474       driver->Init( function->GetEntry() );
475   }
476   return driver;
477 }
478
479 //=============================================================================
480 /*!
481  *  GetFreeLabel
482  */
483 //=============================================================================
484 TDF_Label GEOM_BaseObject::GetFreeLabel()
485 {
486   return _label.FindChild(FREE_LABEL);
487 }
488
489 IMPLEMENT_STANDARD_RTTIEXT(GEOM_BaseObject, Standard_Transient )