]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Function.cxx
Salome HOME
ab8e57fd6bc0172297297a54324fdd90ac54e971
[modules/geom.git] / src / GEOM / GEOM_Function.cxx
1 // Copyright (C) 2007-2014  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 <Standard_Stream.hxx>
24
25 #include <GEOM_Function.hxx>
26 #include <GEOM_Object.hxx>
27 #include <GEOM_Solver.hxx>
28 #include <GEOM_ISubShape.hxx>
29
30 #include <Basics_OCCTVersion.hxx>
31
32 #include "utilities.h"
33
34 #include <TDF.hxx>
35 #include <TDF_Tool.hxx>
36 #include <TDF_Data.hxx>
37 #include <TDF_ChildIterator.hxx>
38 #include <TDF_Reference.hxx>
39 #include <TDataStd_BooleanArray.hxx>
40 #include <TDataStd_ByteArray.hxx>
41 #include <TDataStd_Integer.hxx>
42 #include <TDataStd_IntegerArray.hxx>
43 #include <TDataStd_Real.hxx>
44 #include <TDataStd_RealArray.hxx>
45 #include <TDataStd_Comment.hxx>
46 #include <TDataStd_TreeNode.hxx>
47 #include <TDataStd_UAttribute.hxx>
48 #include <TDataStd_ChildNodeIterator.hxx>
49 #include <TDataStd_ExtStringArray.hxx>
50 #include <TDataStd_ExtStringList.hxx>
51 #include <TDocStd_Owner.hxx>
52 #include <TDocStd_Document.hxx>
53 #include <TFunction_Function.hxx>
54 #include <TNaming_NamedShape.hxx>
55 #include <TNaming_Builder.hxx>
56
57 #include <TColStd_ListOfInteger.hxx>
58 #include <TColStd_ListIteratorOfListOfInteger.hxx>
59 #include <TColStd_HArray1OfReal.hxx>
60 #include <TColStd_HArray1OfInteger.hxx>
61 #include <TColStd_HSequenceOfTransient.hxx>
62 #include <TCollection_AsciiString.hxx>
63 #include <TCollection_ExtendedString.hxx>
64
65 #include <cstdlib>
66
67 #include <Standard_Failure.hxx>
68 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
69
70 // This modification was introduced in frame of Mantis issue 0021251.
71 // This line allows to keep shape orientation together with the shape itself.
72 // Otherwise orientation can be lost in some cases.
73 #define KEEP_ORIENTATION_0021251
74
75 #define ARGUMENT_LABEL 1
76 #define RESULT_LABEL 2
77 #define DESCRIPTION_LABEL 3
78 #define HISTORY_LABEL 4
79 #define SUBSHAPES_LABEL 5 // 0020756: GetGroups
80 #define NAMING_LABEL 6 // 0020750: Naming during STEP import
81 #define CALLBACK_LABEL 1 // TDataStd_Comment
82
83 #ifdef KEEP_ORIENTATION_0021251
84 #define ORIENTATION_LABEL 7 // 0021251: TNaming_NamedShape doesn't store orientation
85 #endif
86
87 #define ARGUMENTS _label.FindChild((ARGUMENT_LABEL))
88 #define ARGUMENT(thePosition) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePosition))
89 #define SUB_ARGUMENT(thePos1, thePos2) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePos1)).FindChild((thePos2))
90
91 //=======================================================================
92 //function : GetFunctionTreeID
93 //purpose  :
94 //=======================================================================
95 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
96 {
97   static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
98   return aFunctionTreeID;
99 }
100
101
102 //=======================================================================
103 //function : GetDependencyID
104 //purpose  :
105 //=======================================================================
106 const Standard_GUID& GEOM_Function::GetDependencyID()
107 {
108   static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
109   return aDependencyID;
110 }
111
112 //=============================================================================
113 /*!
114  *  GetFunction:
115  */
116 //=============================================================================
117 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
118 {
119   if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
120
121   return new GEOM_Function(theEntry);
122 }
123
124 //=============================================================================
125 /*!
126  *  Constructor:
127  */
128 //=============================================================================
129 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
130   : _label(theEntry), _isCallBackData(false)
131 {
132   TFunction_Function::Set(theEntry, theGUID);
133   TDataStd_Integer::Set(theEntry, theType);
134
135   //Add function to a function tree
136   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theEntry.Data());
137   Handle(TDataStd_TreeNode) aRoot, aNode;
138   if(!aDoc->Main().FindAttribute(GetFunctionTreeID(), aRoot))
139     aRoot = TDataStd_TreeNode::Set(aDoc->Main(), GetFunctionTreeID());
140
141   aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
142   aRoot->Append(aNode);
143 }
144
145 //================================================================================
146 /*!
147  * \brief 
148  * 
149  * 
150  */
151 //================================================================================
152
153 GEOM_Function::~GEOM_Function()
154 {
155   if ( _isCallBackData ) {
156     _label.FindChild( CALLBACK_LABEL ).ForgetAttribute( TDataStd_Comment::GetID() );
157   }
158 }
159
160 //================================================================================
161 /*!
162  * \brief Retuns true if this function is the last one in the study
163  */
164 //================================================================================
165
166 bool GEOM_Function::IsLastFuntion()
167 {
168   bool isLast = false;
169
170   Handle(TDataStd_TreeNode) aNode;
171   if (_label.FindAttribute(GetFunctionTreeID(), aNode))
172     isLast = !aNode->HasNext();
173
174   return isLast;
175 }
176
177 //=============================================================================
178 /*!
179  *  GetOwner
180  */
181 //=============================================================================
182 TDF_Label GEOM_Function::GetOwnerEntry()
183 {
184   TDF_Label aFather = _label.Father();
185   while(!aFather.IsRoot()) {
186     if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
187     aFather = aFather.Father();
188   }
189
190   return TDF_Label();
191 }
192
193 //=============================================================================
194 /*!
195  *  GetType
196  */
197 //=============================================================================
198 int GEOM_Function::GetType()
199 {
200   _isDone = false;
201   Handle(TDataStd_Integer) aType;
202   if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
203   _isDone = true;
204   return aType->Get();
205 }
206
207 //=============================================================================
208 /*!
209  *  GetValue
210  */
211 //=============================================================================
212 TopoDS_Shape GEOM_Function::GetValue()
213 {
214   _isDone = false;
215
216   TopoDS_Shape aShape;
217   TDF_Label aLabel = GetOwnerEntry();
218   if (aLabel.IsRoot()) return aShape;
219   Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
220   if (anObject.IsNull()) return aShape;
221
222   if (!anObject->IsMainShape()) {
223     bool isResult = false;
224     TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
225     if (!aResultLabel.IsNull()) {
226       Handle(TNaming_NamedShape) aNS;
227       if (aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS))
228         isResult = true;
229     }
230
231     // compare tics
232     if (isResult) {
233       // tic of this
234       Standard_Integer aTic = anObject->GetTic();
235
236       // tic of main shape
237       GEOM_ISubShape aCI (this);
238       TDF_Label aLabelObjMainSh = aCI.GetMainShape()->GetOwnerEntry();
239       if (aLabelObjMainSh.IsRoot()) return aShape;
240       Handle(GEOM_Object) anObjMainSh = GEOM_Object::GetObject(aLabelObjMainSh);
241       if (anObjMainSh.IsNull()) return aShape;
242       Standard_Integer aTicMainSh = anObjMainSh->GetTic();
243
244       // compare
245       isResult = ((aTic == aTicMainSh) ? true : false);
246     }
247
248     if (!isResult) {
249       try {
250         OCC_CATCH_SIGNALS;
251         GEOM_Solver aSolver(GEOM_Engine::GetEngine());
252         if (!aSolver.ComputeFunction(this)) {
253           MESSAGE("GEOM_Object::GetValue Error : Can't build a sub-shape");
254           return aShape;
255         }
256       }
257       catch (Standard_Failure) {
258         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
259         MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
260         return aShape;
261       }
262     }
263   }
264
265   TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
266   Handle(TNaming_NamedShape) aNS;
267   if (!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
268
269   aShape = aNS->Get();
270
271 #ifdef KEEP_ORIENTATION_0021251
272   // 0021251: TNaming_NamedShape doesn't store orientation
273   TDF_Label anOrientationLabel = _label.FindChild(ORIENTATION_LABEL);
274   Handle(TDataStd_Integer) anInteger;
275   if (anOrientationLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) {
276     aShape.Orientation((TopAbs_Orientation)anInteger->Get());
277   }
278 #endif
279
280   _isDone = true;
281   return aShape;
282 }
283
284 //=============================================================================
285 /*!
286  *  SetValue
287  */
288 //=============================================================================
289 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
290 {
291   _isDone = false;
292   TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
293   TNaming_Builder aBuilder (aResultLabel);
294
295   aBuilder.Generated(theShape);
296
297 #ifdef KEEP_ORIENTATION_0021251
298   // 0021251: TNaming_NamedShape doesn't store orientation
299   TDF_Label anOrientationLabel = _label.FindChild(ORIENTATION_LABEL);
300   TDataStd_Integer::Set(anOrientationLabel, (int)theShape.Orientation());
301 #endif
302
303   // synchronisation between main shape and its sub-shapes
304   TDF_Label aLabel = GetOwnerEntry();
305   if (aLabel.IsRoot()) return;
306   Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
307   if (anObject.IsNull()) return;
308   if (anObject->IsMainShape()) {
309     // increase modifications counter of this (main) shape
310     anObject->IncrementTic();
311   }
312   else {
313     // update modifications counter of this (sub-) shape to be the same as on main shape
314     GEOM_ISubShape aCI (this);
315     TDF_Label aLabelObjMainSh = aCI.GetMainShape()->GetOwnerEntry();
316     if (aLabelObjMainSh.IsRoot()) return;
317     Handle(GEOM_Object) anObjMainSh = GEOM_Object::GetObject(aLabelObjMainSh);
318     if (anObjMainSh.IsNull()) return;
319
320     anObject->SetTic(anObjMainSh->GetTic());
321   }
322
323   _isDone = true;
324 }
325
326 //=============================================================================
327 /*!
328  *  GetDriverGUID
329  */
330 //=============================================================================
331 Standard_GUID GEOM_Function::GetDriverGUID()
332 {
333   Handle(TFunction_Function) aFunction;
334   if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
335     return TDF::LowestID();
336   }
337
338   return aFunction->GetDriverGUID();
339 }
340
341 //=============================================================================
342 /*!
343  *  GetDescription
344  */
345 //=============================================================================
346 TCollection_AsciiString GEOM_Function::GetDescription()
347 {
348   Handle(TDataStd_Comment) aComment;
349   TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
350   if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return TCollection_AsciiString();
351   TCollection_AsciiString aDescr(aComment->Get());
352   return aDescr;
353 }
354
355 //=============================================================================
356 /*!
357  *  SetDescription
358  */
359 //=============================================================================
360 void GEOM_Function::SetDescription(const TCollection_AsciiString& theDescription)
361 {
362   TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
363   Handle(TDataStd_Comment) aComment =
364     TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
365 }
366
367 //=============================================================================
368 /*!
369  *  SetReal
370  */
371 //=============================================================================
372 void GEOM_Function::SetReal(int thePosition, double theValue)
373 {
374   _isDone = false;
375   if(thePosition <= 0) return;
376   TDF_Label anArgLabel = ARGUMENT(thePosition);
377   TDataStd_Real::Set(anArgLabel, theValue);
378   _isDone = true;
379 }
380
381 //=============================================================================
382 /*!
383  *  SetRealArray
384  */
385 //=============================================================================
386 void GEOM_Function::SetRealArray (int thePosition,
387                                   const Handle(TColStd_HArray1OfReal)& theArray)
388 {
389   _isDone = false;
390   if(thePosition <= 0) return;
391   TDF_Label anArgLabel = ARGUMENT(thePosition);
392   Handle(TDataStd_RealArray) anAttr =
393     TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
394   anAttr->ChangeArray(theArray);
395   _isDone = true;
396 }
397
398 //=============================================================================
399 /*!
400  *  GetReal
401  */
402 //=============================================================================
403 double GEOM_Function::GetReal(int thePosition)
404 {
405   _isDone = false;
406   if(thePosition <= 0) return 0.0;
407   Handle(TDataStd_Real) aReal;
408   TDF_Label anArgLabel = ARGUMENT(thePosition);
409   if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
410
411   _isDone = true;
412   return aReal->Get();
413 }
414
415 //=============================================================================
416 /*!
417  *  GetRealArray
418  */
419 //=============================================================================
420 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
421 {
422   _isDone = false;
423   if(thePosition <= 0) return NULL;
424   Handle(TDataStd_RealArray) aRealArray;
425   TDF_Label anArgLabel = ARGUMENT(thePosition);
426   if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
427
428   _isDone = true;
429   return aRealArray->Array();
430 }
431
432 //=============================================================================
433 /*!
434  *  SetInteger
435  */
436 //=============================================================================
437 void GEOM_Function::SetInteger(int thePosition, int theValue)
438 {
439   _isDone = false;
440   if(thePosition <= 0) return;
441   TDF_Label anArgLabel = ARGUMENT(thePosition);
442   TDataStd_Integer::Set(anArgLabel, theValue);
443   _isDone = true;
444 }
445
446 //=============================================================================
447 /*!
448  *  SetIntegerArray
449  */
450 //=============================================================================
451 void GEOM_Function::SetIntegerArray (int thePosition,
452                                      const Handle(TColStd_HArray1OfInteger)& theArray)
453 {
454   _isDone = false;
455   if(thePosition <= 0) return;
456   TDF_Label anArgLabel = ARGUMENT(thePosition);
457   Handle(TDataStd_IntegerArray) anAttr =
458     TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
459   anAttr->ChangeArray(theArray);
460   _isDone = true;
461 }
462
463 //=============================================================================
464 /*!
465  *  GetInteger
466  */
467 //=============================================================================
468 int GEOM_Function::GetInteger(int thePosition)
469 {
470   _isDone = false;
471   if(thePosition <= 0) return 0;
472   Handle(TDataStd_Integer) anInteger;
473   TDF_Label anArgLabel = ARGUMENT(thePosition);
474   if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
475
476   _isDone = true;
477   return anInteger->Get();
478 }
479
480 //=============================================================================
481 /*!
482  *  GetIntegerArray
483  */
484 //=============================================================================
485 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
486 {
487   _isDone = false;
488   if(thePosition <= 0) return 0;
489   Handle(TDataStd_IntegerArray) anIntegerArray;
490   TDF_Label anArgLabel = ARGUMENT(thePosition);
491   if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
492
493   _isDone = true;
494   return anIntegerArray->Array();
495 }
496
497 //=============================================================================
498 /*!
499  *  SetByteArray
500  */
501 //=============================================================================
502 void GEOM_Function::SetByteArray (int thePosition,
503                                   const Handle(TColStd_HArray1OfByte)& theArray)
504 {
505   _isDone = false;
506   if(thePosition <= 0) return;
507   TDF_Label anArgLabel = ARGUMENT(thePosition);
508   Handle(TDataStd_ByteArray) anAttr =
509     TDataStd_ByteArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
510   anAttr->ChangeArray(theArray);
511   _isDone = true;
512 }
513
514 //=============================================================================
515 /*!
516  *  GetByteArray
517  */
518 //=============================================================================
519 Handle(TColStd_HArray1OfByte) GEOM_Function::GetByteArray(int thePosition)
520 {
521   _isDone = false;
522   if(thePosition <= 0) return 0;
523   Handle(TDataStd_ByteArray) aByteArray;
524   TDF_Label anArgLabel = ARGUMENT(thePosition);
525   if(!anArgLabel.FindAttribute(TDataStd_ByteArray::GetID(), aByteArray)) return 0;
526
527   _isDone = true;
528   return aByteArray->InternalArray();
529 }
530
531 //=============================================================================
532 /*!
533  *  SetBooleanArray
534  */
535 //=============================================================================
536 void GEOM_Function::SetBooleanArray (int thePosition,
537                                      const Handle(TColStd_HArray1OfByte)& theArray)
538 {
539   _isDone = false;
540   if(thePosition <= 0) return;
541   TDF_Label anArgLabel = ARGUMENT(thePosition);
542   Handle(TDataStd_BooleanArray) anAttr =
543     TDataStd_BooleanArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
544   anAttr->SetInternalArray(theArray);
545   _isDone = true;
546 }
547
548 //=============================================================================
549 /*!
550  *  GetBooleanArray
551  */
552 //=============================================================================
553 Handle(TColStd_HArray1OfByte) GEOM_Function::GetBooleanArray(int thePosition)
554 {
555   _isDone = false;
556   if(thePosition <= 0) return 0;
557   Handle(TDataStd_BooleanArray) aBooleanArray;
558   TDF_Label anArgLabel = ARGUMENT(thePosition);
559   if(!anArgLabel.FindAttribute(TDataStd_BooleanArray::GetID(), aBooleanArray)) return 0;
560
561   _isDone = true;
562   return aBooleanArray->InternalArray();
563 }
564
565 //=============================================================================
566 /*!
567  *  SetString
568  */
569 //=============================================================================
570 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
571 {
572   _isDone = false;
573   if(thePosition <= 0) return;
574   TDF_Label anArgLabel = ARGUMENT(thePosition);
575   TDataStd_Comment::Set(anArgLabel, theValue);
576   _isDone = true;
577 }
578
579 //=============================================================================
580 /*!
581  *  GetString
582  */
583 //=============================================================================
584 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
585 {
586   _isDone = false;
587   TCollection_AsciiString aRes;
588   if(thePosition <= 0) return aRes;
589   Handle(TDataStd_Comment) aString;
590   TDF_Label anArgLabel = ARGUMENT(thePosition);
591   if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
592
593   _isDone = true;
594   aRes = TCollection_AsciiString(aString->Get());
595   return aRes;
596 }
597
598 //=============================================================================
599 /*!
600  *  SetReference
601  */
602 //=============================================================================
603 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
604 {
605   _isDone = false;
606   if (thePosition <= 0) return;
607   if (theReference.IsNull()) return;
608   TDF_Label anArgLabel = ARGUMENT(thePosition);
609   TDF_Reference::Set(anArgLabel, theReference->GetEntry());
610   TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
611   _isDone = true;
612   return;
613 }
614
615 //=============================================================================
616 /*!
617  *  GetReference
618  */
619 //=============================================================================
620 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
621 {
622   _isDone = false;
623   if(thePosition <= 0) return NULL;
624   TDF_Label anArgLabel = ARGUMENT(thePosition);
625   Handle(TDF_Reference) aRef;
626   if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
627
628   _isDone = true;
629   return GetFunction(aRef->Get());
630 }
631
632
633 //=============================================================================
634 /*!
635  *  SetStringArray
636  */
637 //=============================================================================
638 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
639 {
640   _isDone = false;
641   if(thePosition <= 0 || theArray.IsNull()) return;
642   TDF_Label anArgLabel = ARGUMENT(thePosition);
643
644   Handle(TDataStd_ExtStringArray) anArray =
645     TDataStd_ExtStringArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
646   anArray->ChangeArray(theArray);
647
648   _isDone = true;
649 }
650
651
652 //=============================================================================
653 /*!
654  *  GetStringArray
655  */
656 //=============================================================================
657 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
658 {
659   _isDone = false;
660   if(thePosition <= 0) return NULL;
661   TDF_Label anArgLabel = ARGUMENT(thePosition);
662   Handle(TDataStd_ExtStringArray) anArray;
663   if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
664
665   _isDone = true;
666   return anArray->Array();
667 }
668
669 //=======================================================================
670 //function : HasData
671 //purpose  : Returns true if data of given type already exists
672 //=======================================================================
673
674 bool GEOM_Function::HasData(int thePosition, const Standard_GUID& dataID)
675 {
676   if(thePosition <= 0) return false;
677   TDF_Label anArgLabel = ARGUMENT(thePosition);
678   return anArgLabel.IsAttribute( dataID );
679 }
680
681 //=======================================================================
682 //function : GetReferencesTreeID
683 //purpose  :
684 //=======================================================================
685 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
686 {
687   static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
688   return aReferencesTreeID;
689 }
690
691 //=============================================================================
692 /*!
693  *  SetReferenceList
694  */
695 //=============================================================================
696 void GEOM_Function::SetReferenceList (int thePosition,
697                                       const Handle(TColStd_HSequenceOfTransient)& theRefList)
698 {
699   _isDone = false;
700   if(thePosition <= 0) return;
701
702   // parent label for the list of references
703   TDF_Label anArgLabel = ARGUMENT(thePosition);
704   anArgLabel.ForgetAllAttributes();
705
706   // set TreeNode on the parent label
707   Handle(TDataStd_TreeNode) aRoot, aNode;
708   aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
709
710   // store references on sub-labels of the parent label
711   Handle(GEOM_Function) aFunc;
712   Standard_Integer ind, len = theRefList->Length();
713   for (ind = 1; ind <= len; ind++) {
714     aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
715     if (aFunc.IsNull()) continue;
716     TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
717     TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
718     TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
719
720     // set TreeNode on the child label
721     aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
722     aRoot->Append(aNode);
723   }
724
725   _isDone = true;
726   return;
727 }
728
729 //=============================================================================
730 /*!
731  *  GetReferenceList
732  */
733 //=============================================================================
734 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
735 {
736   Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
737   _isDone = false;
738   if(thePosition <= 0) return aResult;
739
740   // parent label for the list of references
741   TDF_Label anArgLabel = ARGUMENT(thePosition);
742   Handle(TDF_Reference) aRef;
743
744   // get TreeNode on the parent label
745   Handle(TDataStd_TreeNode) aRoot, aNode;
746   if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
747     return aResult;
748
749   // get references, stored on sub-labels of the parent label
750   TDF_Label aLabel_i;
751   TDataStd_ChildNodeIterator anIter (aRoot);
752   for (; anIter.More(); anIter.Next()) {
753     aNode = anIter.Value();
754     aLabel_i = aNode->Label();
755     if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
756     Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
757     if (aFunc_i.IsNull()) continue;
758     aResult->Append(aFunc_i);
759   }
760
761   _isDone = true;
762   return aResult;
763 }
764
765 //=============================================================================
766 /*!
767  *  SetShape
768  */
769 //=============================================================================
770 //void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
771 //{
772 //  _isDone = false;
773 //  if(thePosition <= 0 || theShape.IsNull()) return;
774 //
775 //  TDF_Label anArgLabel = ARGUMENT(thePosition);
776 //  TNaming_Builder aBuilder(anArgLabel);
777 //  aBuilder.Generated(theShape);
778 //
779 //  _isDone = true;
780 //  return;
781 //}
782 //
783 //=============================================================================
784 /*!
785  *  GetShape
786  */
787 //=============================================================================
788 //TopoDS_Shape GEOM_Function::GetShape(int thePosition)
789 //{
790 //  _isDone = false;
791 //  TopoDS_Shape aShape;
792 //  if(thePosition <= 0) return aShape;
793 //
794 //  TDF_Label anArgLabel = ARGUMENT(thePosition);
795 //  Handle(TNaming_NamedShape) aNS;
796 //  if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
797 //
798 //  aShape = aNS->Get();
799 //  _isDone = true;
800 //  return aShape;
801 //}
802
803
804 //=============================================================================
805 /*!
806  *  GetDependency
807  */
808 //=============================================================================
809 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
810 {
811   TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
812   for(; anIterator.More(); anIterator.Next()) {
813     if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
814   }
815 }
816
817 //=============================================================================
818 /*!
819  *  AddSubShapeReference
820  */
821 //=============================================================================
822 void GEOM_Function::AddSubShapeReference(Handle(GEOM_Function) theSubShape)
823 {
824   _isDone = false;
825
826   TDF_Label aSubShapesLabel = _label.FindChild(SUBSHAPES_LABEL);
827
828   Handle(TDataStd_ExtStringList) aList;
829   if (!aSubShapesLabel.FindAttribute(TDataStd_ExtStringList::GetID(), aList)) {
830     aList = new TDataStd_ExtStringList;
831     aSubShapesLabel.AddAttribute(aList);
832   }
833
834   TCollection_AsciiString anEntry;
835   TDF_Tool::Entry(theSubShape->GetOwnerEntry(), anEntry);
836   aList->Append(anEntry);
837
838   _isDone = true;
839 }
840
841 //=============================================================================
842 /*!
843  *  RemoveSubShapeReference
844  */
845 //=============================================================================
846 void GEOM_Function::RemoveSubShapeReference(Handle(GEOM_Function) theSubShape)
847 {
848   _isDone = false;
849
850   TDF_Label aSubShapesLabel = _label.FindChild(SUBSHAPES_LABEL);
851
852   Handle(TDataStd_ExtStringList) aList;
853   if (aSubShapesLabel.FindAttribute(TDataStd_ExtStringList::GetID(), aList)) {
854     TCollection_AsciiString anEntry;
855     TDF_Tool::Entry(theSubShape->GetOwnerEntry(), anEntry);
856     aList->Remove(anEntry);
857   }
858
859   _isDone = true;
860 }
861
862 //=============================================================================
863 /*!
864  *  HasSubShapeReferences
865  */
866 //=============================================================================
867 bool GEOM_Function::HasSubShapeReferences()
868 {
869   _isDone = true;
870
871   TDF_Label aSubShapesLabel = _label.FindChild(SUBSHAPES_LABEL);
872   return aSubShapesLabel.IsAttribute(TDataStd_ExtStringList::GetID());
873 }
874
875 //=============================================================================
876 /*!
877  *  GetSubShapeReferences
878  */
879 //=============================================================================
880 const TDataStd_ListOfExtendedString& GEOM_Function::GetSubShapeReferences()
881 {
882   _isDone = false;
883
884   TDF_Label aSubShapesLabel = _label.FindChild(SUBSHAPES_LABEL);
885
886   Handle(TDataStd_ExtStringList) aList;
887   if (!aSubShapesLabel.FindAttribute(TDataStd_ExtStringList::GetID(), aList)) {
888     aList = new TDataStd_ExtStringList;
889     aSubShapesLabel.AddAttribute(aList);
890   }
891
892   _isDone = true;
893   return aList->List();
894 }
895
896 //=============================================================================
897 /*!
898  *  GetHistoryEntry
899  */
900 //=============================================================================
901 TDF_Label GEOM_Function::GetHistoryEntry (const Standard_Boolean create)
902 {
903   return _label.FindChild(HISTORY_LABEL, create);
904 }
905
906 //=============================================================================
907 /*!
908  *  GetArgumentHistoryEntry
909  */
910 //=============================================================================
911 TDF_Label GEOM_Function::GetArgumentHistoryEntry (const TDF_Label&       theArgumentRefEntry,
912                                                   const Standard_Boolean create)
913 {
914   TColStd_ListOfInteger anArgumentRefTags;
915   TDF_Tool::TagList(theArgumentRefEntry, anArgumentRefTags);
916   Standard_Integer anArgumentRefLabelPos = anArgumentRefTags.Extent();
917
918   TDF_Label aHistoryLabel = GetHistoryEntry(create);
919   if (aHistoryLabel.IsNull())
920     return aHistoryLabel;
921   Standard_Integer aHistoryLabelPos = aHistoryLabel.Depth() + 1;
922
923   Standard_Integer itag;
924   TDF_Label aHistoryCurLabel = aHistoryLabel;
925   TColStd_ListIteratorOfListOfInteger aListIter (anArgumentRefTags);
926   for (itag = 1; itag <= aHistoryLabelPos; itag++) {
927     aListIter.Next();
928   }
929   for (; itag <= anArgumentRefLabelPos; itag++) {
930     aHistoryCurLabel = aHistoryCurLabel.FindChild(aListIter.Value(), create);
931     if (aHistoryCurLabel.IsNull())
932       return aHistoryCurLabel;
933     aListIter.Next();
934   }
935
936   return aHistoryCurLabel;
937 }
938
939 //=============================================================================
940 /*!
941  *  GetNamingEntry
942  */
943 //=============================================================================
944 TDF_Label GEOM_Function::GetNamingEntry (const Standard_Boolean create)
945 {
946   return _label.FindChild(NAMING_LABEL, create);
947 }
948
949 //================================================================================
950 /*!
951  * Save a pointer to a data holder intended to pass temporary data Driver -> Operation.
952  * This method should be called by Operation to set the data holder.
953  * An instance of GEOM_Function that sets the data holder will remove the
954  * corresponding OCAF attribute at it's destruction
955  */
956 //================================================================================
957
958 void GEOM_Function::SetCallBackData( void* data )
959 {
960   std::ostringstream strm;
961   strm << (long long) data;
962   TCollection_ExtendedString string( strm.str().c_str() );
963
964   TDF_Label aChild = _label.FindChild(CALLBACK_LABEL);
965   TDataStd_Comment::Set(aChild, string);
966
967   _isCallBackData = true; // I will remove TDataStd_Comment at destruction
968 }
969
970 //================================================================================
971 /*!
972  * Returns a pointer to a data holder intended to pass data Driver -> Operation.
973  * This method should be called by Driver to get the data holder to fill it in.
974  * Returns NULL if the Operation have not set the data holder.
975  */
976 //================================================================================
977
978 void* GEOM_Function::GetCallBackData()
979 {
980   Handle(TDataStd_Comment) aComment;
981   TDF_Label aChild = _label.FindChild( CALLBACK_LABEL );
982   if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return NULL;
983   TCollection_AsciiString string( aComment->Get() );
984
985   long long address;
986 #ifndef WIN32
987   address = atoll ( string.ToCString() );
988 #else
989   address = _strtoi64 ( string.ToCString(), NULL, 10 );
990 #endif
991
992   return reinterpret_cast<void*> ( address );
993 }
994
995 IMPLEMENT_STANDARD_HANDLE (GEOM_Function, Standard_Transient);
996 IMPLEMENT_STANDARD_RTTIEXT(GEOM_Function, Standard_Transient );