]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Object.cxx
Salome HOME
0020504: EDF 1078 GEOM: Point color is not good at study loading
[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 ) : -1;
330   aColor.G = isFound ? anArray->Value( 2 ) : -1;
331   aColor.B = isFound ? anArray->Value( 3 ) : -1;
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 /*!
388  *  IsSubShape
389  */
390 //=============================================================================
391 bool GEOM_Object::IsMainShape()
392 {
393   Handle(GEOM_Function) aFunction = GetFunction(1);
394   if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
395   return false;
396 }
397
398
399 //=============================================================================
400 /*!
401  *  AddFunction
402  */
403 //=============================================================================
404 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
405 {
406   Standard_Integer nb = GetNbFunctions();
407   if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
408   nb++;
409   TDF_Label aChild = FUNCTION_LABEL(nb);
410
411   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
412   _root->Append(aNode);
413
414   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
415
416   return aFunction;
417
418 }
419
420 //=============================================================================
421 /*!
422  *  GetNbFunctions
423  */
424 //=============================================================================
425 int GEOM_Object::GetNbFunctions()
426 {
427   Standard_Integer nb = 0;
428   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
429   return nb;
430 }
431
432 //=============================================================================
433 /*!
434  *  GetFunction
435  */
436 //=============================================================================
437 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
438 {
439   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
440   return GEOM_Function::GetFunction(aChild);
441 }
442
443 //=============================================================================
444 /*!
445  *  GetlastFunction
446  */
447 //=============================================================================
448 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
449 {
450   Standard_Integer nb = GetNbFunctions();
451   if(nb) return GetFunction(nb);
452
453   return NULL;
454 }
455
456
457 //=============================================================================
458 /*!
459  *  GetAllDependency
460  */
461 //=============================================================================
462 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
463 {
464   Handle(TColStd_HSequenceOfTransient) anArray;
465   TDF_LabelSequence aSeq;
466   Standard_Integer nb = GetNbFunctions();
467   if(nb == 0) return anArray;
468   for(Standard_Integer i=1; i<=nb; i++) {
469     Handle(GEOM_Function) aFunction = GetFunction(i);
470     if(aFunction.IsNull()) continue;
471     aFunction->GetDependency(aSeq);
472   }
473
474   Standard_Integer aLength = aSeq.Length();
475   if(aLength > 0) {
476     anArray = new TColStd_HSequenceOfTransient;
477     for(Standard_Integer j =1; j<=aLength; j++) {
478       Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
479       if(!aRefObj.IsNull()) anArray->Append(aRefObj);
480     }
481   }
482
483   return anArray;
484 }
485
486 //=============================================================================
487 /*!
488  *  GetLastDependency
489  */
490 //=============================================================================
491 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
492 {
493   Handle(TColStd_HSequenceOfTransient) anArray;
494   Handle(GEOM_Function) aFunction = GetLastFunction();
495   if (aFunction.IsNull()) return anArray;
496
497   TDF_LabelSequence aSeq;
498   aFunction->GetDependency(aSeq);
499   Standard_Integer aLength = aSeq.Length();
500   if (aLength > 0) {
501     anArray = new TColStd_HSequenceOfTransient;
502     for (Standard_Integer i = 1; i <= aLength; i++)
503       anArray->Append(GetReferencedObject(aSeq(i)));
504   }
505
506   return anArray;
507 }
508
509 //=============================================================================
510 /*!
511  *  GetFreeLabel
512  */
513 //=============================================================================
514 TDF_Label GEOM_Object::GetFreeLabel()
515 {
516   return _label.FindChild(FREE_LABEL);
517 }
518
519 //=======================================================================
520 //function :  GEOM_Object_Type_
521 //purpose  :
522 //=======================================================================
523 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
524 {
525
526   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
527   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
528   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
529   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
530
531
532   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
533   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
534                                                          sizeof(GEOM_Object),
535                                                          1,
536                                                          (Standard_Address)_Ancestors,
537                                                          (Standard_Address)NULL);
538   return _aType;
539 }
540
541 //=======================================================================
542 //function : DownCast
543 //purpose  :
544 //=======================================================================
545
546 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
547 {
548   Handle(GEOM_Object) _anOtherObject;
549
550   if (!AnObject.IsNull()) {
551      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
552        _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
553      }
554   }
555
556   return _anOtherObject ;
557 }
558
559