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