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