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