]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Object.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / GEOM / GEOM_Object.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <Standard_Stream.hxx>
21
22 #include <GEOM_Object.hxx>
23 #include <GEOM_Engine.hxx>
24 #include <GEOM_Solver.hxx>
25 #include <TDF_Tool.hxx>
26 #include <TDF_Data.hxx>
27 #include <TDF_Reference.hxx>
28 #include <TDF_LabelSequence.hxx>
29 #include <TDocStd_Owner.hxx>
30 #include <TDocStd_Document.hxx>
31 #include <TDataStd_Integer.hxx>
32 #include <TDataStd_ChildNodeIterator.hxx>
33 #include <TDataStd_UAttribute.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TDataStd_Comment.hxx>
36 #include <TDataStd_RealArray.hxx>
37 #include <TColStd_HArray1OfReal.hxx>
38 #include <TCollection_AsciiString.hxx>
39 #include <TCollection_ExtendedString.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopExp.hxx>
42
43 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
44 #define TYPE_LABEL 2
45 #define FREE_LABEL 3
46 #define TIC_LABEL  4
47 #define COLOR_LABEL      5
48 #define AUTO_COLOR_LABEL 6
49
50 //=======================================================================
51 //function : GetObjectID
52 //purpose  :
53 //=======================================================================
54 const Standard_GUID& GEOM_Object::GetObjectID()
55 {
56   static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
57   return anObjectID;
58 }
59
60 //=======================================================================
61 //function : GetSubShapeID
62 //purpose  :
63 //=======================================================================
64 const Standard_GUID& GEOM_Object::GetSubShapeID()
65 {
66   static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
67   return anObjectID;
68 }
69
70 //=============================================================================
71 /*!
72  *  GetObject
73  */
74 //=============================================================================
75 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
76 {
77   if (!theLabel.IsAttribute(GetObjectID())) return NULL;
78
79   TCollection_AsciiString anEntry;
80   TDF_Tool::Entry(theLabel, anEntry);
81
82   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
83   if(aDoc.IsNull()) return NULL;
84
85   Handle(TDataStd_Integer) anID;
86   if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
87
88
89   GEOM_Engine* anEngine=  GEOM_Engine::GetEngine();
90   if(anEngine == NULL) return NULL;
91   return anEngine->GetObject(anID->Get(), anEntry.ToCString());
92 }
93
94 //=============================================================================
95 /*!
96  *  GetReferencedObject
97  */
98 //=============================================================================
99 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
100 {
101   Handle(TDF_Reference) aRef;
102   if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
103     return NULL;
104   }
105   
106   if(aRef.IsNull() || aRef->Get().IsNull()) {
107     return NULL;
108   }
109
110
111   // Get TreeNode of a referenced function
112   Handle(TDataStd_TreeNode) aT, aFather;
113   if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
114     return NULL;
115   }
116
117
118   // Get TreeNode of Object of the referenced function
119   aFather = aT->Father();
120   if (aFather.IsNull()) {
121     return NULL;
122   }
123
124   // Get label of the referenced object
125   TDF_Label aLabel = aFather->Label();
126   
127
128   return GEOM_Object::GetObject(aLabel);
129 }
130
131 //=============================================================================
132 /*!
133  *  Constructor: private
134  */
135 //=============================================================================
136 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
137 : _label(theEntry), _ior("")
138 {
139   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
140     _root = TDataStd_TreeNode::Set(theEntry);
141 }
142
143 //=============================================================================
144 /*!
145  *  Constructor: public
146  */
147 //=============================================================================
148 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
149 : _label(theEntry), _ior("")
150 {
151   theEntry.ForgetAllAttributes(Standard_True);
152
153   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
154     _root = TDataStd_TreeNode::Set(theEntry);
155
156   TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
157
158   TDataStd_UAttribute::Set(theEntry, GetObjectID());
159 }
160
161 //=============================================================================
162 /*!
163  *  GetType
164  */
165 //=============================================================================
166 int GEOM_Object::GetType()
167 {
168   Handle(TDataStd_Integer) aType;
169   if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
170
171   return aType->Get();
172 }
173
174 //=============================================================================
175 /*!
176  *  SetType
177  */
178 //=============================================================================
179 void GEOM_Object::SetType(int theType)
180 {
181   TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
182 }
183
184
185 //=============================================================================
186 /*!
187  *  Returns modifications counter of this object.
188  *  Comparing this value with modifications counters of argument objects
189  *  (on which this object depends) we decide whether this object needs to be updated.
190  */
191 //=============================================================================
192 int GEOM_Object::GetTic()
193 {
194   Handle(TDataStd_Integer) aTicAttr;
195   if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
196     return 0;
197
198   return aTicAttr->Get();
199 }
200
201 //=============================================================================
202 /*!
203  *  Set another value of modifications counter.
204  *
205  *  Use this method to update modifications counter of dependent object
206  *  to be equal to modifications counter of its argument.
207  *  This is commonly done in GEOM_Function::GetValue()
208  */
209 //=============================================================================
210 void GEOM_Object::SetTic(int theTic)
211 {
212   TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
213 }
214
215 //=============================================================================
216 /*!
217  *  Increment modifications counter to mark this object as modified.
218  *
219  *  Commonly called from GEOM_Function::SetValue()
220  */
221 //=============================================================================
222 void GEOM_Object::IncrementTic()
223 {
224   TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
225
226   Standard_Integer aTic = 0;
227   Handle(TDataStd_Integer) aTicAttr;
228   if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
229     aTic = aTicAttr->Get();
230
231   TDataStd_Integer::Set(aTicLabel, aTic + 1);
232 }
233
234
235 //=============================================================================
236 /*!
237  *  GetDocID
238  */
239 //=============================================================================
240 int GEOM_Object::GetDocID()
241 {
242   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
243   if(aDoc.IsNull()) return -1;
244
245   Handle(TDataStd_Integer) anID;
246   if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
247
248   return anID->Get();
249 }
250
251
252 //=============================================================================
253 /*!
254  *  GetValue
255  */
256 //=============================================================================
257 TopoDS_Shape GEOM_Object::GetValue()
258 {
259   TopoDS_Shape aShape;
260
261   Handle(GEOM_Function) aFunction = GetLastFunction();
262
263   if (!aFunction.IsNull())
264     aShape = aFunction->GetValue();
265
266   return aShape;
267 }
268
269 //=============================================================================
270 /*!
271  *  SetName
272  */
273 //=============================================================================
274 void GEOM_Object::SetName(const char* theName)
275 {
276   TDataStd_Name::Set(_label, (char*)theName);
277 }
278
279 //=============================================================================
280 /*!
281  *  GetName
282  */
283 //=============================================================================
284 char* GEOM_Object::GetName()
285 {
286   Handle(TDataStd_Name) aNameAttr;
287   if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
288
289   TCollection_AsciiString aName(aNameAttr->Get());
290   // do not return pointer of local variable
291   // return aName.ToCString();
292   // the following code could lead to memory leak, so take care about recieved pointer
293   return strdup(aName.ToCString());
294 }
295
296 //=============================================================================
297 /*!
298  *  SetColor
299  */
300 //=============================================================================
301 void GEOM_Object::SetColor(const SALOMEDS::Color& theColor)
302 {
303   Handle(TDataStd_RealArray) anArray = new TDataStd_RealArray();
304   anArray->Init( 1, 3 );
305   anArray->SetValue( 1, theColor.R );
306   anArray->SetValue( 2, theColor.G );
307   anArray->SetValue( 3, theColor.B );
308
309   Handle(TDataStd_RealArray) anAttr =
310     TDataStd_RealArray::Set(_label.FindChild(COLOR_LABEL), anArray->Lower(), anArray->Upper());
311   anAttr->ChangeArray(anArray->Array());
312 }
313
314 //=============================================================================
315 /*!
316  *  GetColor
317  */
318 //=============================================================================
319 SALOMEDS::Color GEOM_Object::GetColor()
320 {
321   Handle(TDataStd_RealArray) anArray;
322   bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
323
324   SALOMEDS::Color aColor;
325   aColor.R = isFound ? anArray->Value( 1 ) : 0.f;
326   aColor.G = isFound ? anArray->Value( 2 ) : 0.f;
327   aColor.B = isFound ? anArray->Value( 3 ) : 0.f;
328
329   return aColor;
330 }
331
332 //=============================================================================
333 /*!
334  *  SetAutoColor
335  */
336 //=============================================================================
337 void GEOM_Object::SetAutoColor(CORBA::Boolean theAutoColor)
338 {
339   TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
340 }
341
342 //=============================================================================
343 /*!
344  *  GetAutoColor
345  */
346 //=============================================================================
347 CORBA::Boolean GEOM_Object::GetAutoColor()
348 {
349   Handle(TDataStd_Integer) anAutoColor;
350   if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
351
352   return anAutoColor->Get();
353 }
354
355 //=============================================================================
356 /*!
357  *  SetAuxData
358  */
359 //=============================================================================
360 void GEOM_Object::SetAuxData(const char* theData)
361 {
362   TDataStd_Comment::Set(_label, (char*)theData);
363 }
364
365 //=============================================================================
366 /*!
367  *  GetAuxData
368  */
369 //=============================================================================
370 TCollection_AsciiString GEOM_Object::GetAuxData()
371 {
372   TCollection_AsciiString aData;
373
374   Handle(TDataStd_Comment) aCommentAttr;
375   if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
376     aData = aCommentAttr->Get();
377
378   return aData;
379 }
380
381
382 //=============================================================================
383 /*!
384  *  IsSubShape
385  */
386 //=============================================================================
387 bool GEOM_Object::IsMainShape()
388 {
389   Handle(GEOM_Function) aFunction = GetFunction(1);
390   if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
391   return false;
392 }
393
394
395 //=============================================================================
396 /*!
397  *  AddFunction
398  */
399 //=============================================================================
400 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
401 {
402   Standard_Integer nb = GetNbFunctions();
403   if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
404   nb++;
405   TDF_Label aChild = FUNCTION_LABEL(nb);
406
407   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
408   _root->Append(aNode);
409
410   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
411
412   return aFunction;
413
414 }
415
416 //=============================================================================
417 /*!
418  *  GetNbFunctions
419  */
420 //=============================================================================
421 int GEOM_Object::GetNbFunctions()
422 {
423   Standard_Integer nb = 0;
424   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
425   return nb;
426 }
427
428 //=============================================================================
429 /*!
430  *  GetFunction
431  */
432 //=============================================================================
433 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
434 {
435   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
436   return GEOM_Function::GetFunction(aChild);
437 }
438
439 //=============================================================================
440 /*!
441  *  GetlastFunction
442  */
443 //=============================================================================
444 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
445 {
446   Standard_Integer nb = GetNbFunctions();
447   if(nb) return GetFunction(nb);
448
449   return NULL;
450 }
451
452
453 //=============================================================================
454 /*!
455  *  GetAllDependency
456  */
457 //=============================================================================
458 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
459 {
460   Handle(TColStd_HSequenceOfTransient) anArray;
461   TDF_LabelSequence aSeq;
462   Standard_Integer nb = GetNbFunctions();
463   if(nb == 0) return anArray;
464   for(Standard_Integer i=1; i<=nb; i++) {
465     Handle(GEOM_Function) aFunction = GetFunction(i);
466     if(aFunction.IsNull()) continue;
467     aFunction->GetDependency(aSeq);
468   }
469
470   Standard_Integer aLength = aSeq.Length();
471   if(aLength > 0) {
472     anArray = new TColStd_HSequenceOfTransient;
473     for(Standard_Integer j =1; j<=aLength; j++) {
474       Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
475       if(!aRefObj.IsNull()) anArray->Append(aRefObj);
476     }
477   }
478
479   return anArray;
480 }
481
482 //=============================================================================
483 /*!
484  *  GetLastDependency
485  */
486 //=============================================================================
487 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
488 {
489   Handle(TColStd_HSequenceOfTransient) anArray;
490   Handle(GEOM_Function) aFunction = GetLastFunction();
491   if (aFunction.IsNull()) return anArray;
492
493   TDF_LabelSequence aSeq;
494   aFunction->GetDependency(aSeq);
495   Standard_Integer aLength = aSeq.Length();
496   if (aLength > 0) {
497     anArray = new TColStd_HSequenceOfTransient;
498     for (Standard_Integer i = 1; i <= aLength; i++)
499       anArray->Append(GetReferencedObject(aSeq(i)));
500   }
501
502   return anArray;
503 }
504
505 //=============================================================================
506 /*!
507  *  GetFreeLabel
508  */
509 //=============================================================================
510 TDF_Label GEOM_Object::GetFreeLabel()
511 {
512   return _label.FindChild(FREE_LABEL);
513 }
514
515 //=======================================================================
516 //function :  GEOM_Object_Type_
517 //purpose  :
518 //=======================================================================
519 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
520 {
521
522   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
523   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
524   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
525   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
526
527
528   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
529   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
530                                                          sizeof(GEOM_Object),
531                                                          1,
532                                                          (Standard_Address)_Ancestors,
533                                                          (Standard_Address)NULL);
534   return _aType;
535 }
536
537 //=======================================================================
538 //function : DownCast
539 //purpose  :
540 //=======================================================================
541
542 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
543 {
544   Handle(GEOM_Object) _anOtherObject;
545
546   if (!AnObject.IsNull()) {
547      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
548        _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
549      }
550   }
551
552   return _anOtherObject ;
553 }
554
555