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