]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Function.cxx
Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOM / GEOM_Function.cxx
1 using namespace std;
2
3 #include "GEOM_Function.hxx"
4 #include "GEOM_Object.hxx"
5 #include "GEOM_Solver.hxx"
6
7 #include "utilities.h"
8
9 #include <TDF.hxx>
10 #include <TDF_Data.hxx>
11 #include <TDF_ChildIterator.hxx>
12 #include <TDF_Reference.hxx>
13 #include <TDataStd_Integer.hxx>
14 #include <TDataStd_IntegerArray.hxx>
15 #include <TDataStd_Real.hxx>
16 #include <TDataStd_RealArray.hxx>
17 #include <TDataStd_Comment.hxx>
18 #include <TDataStd_TreeNode.hxx>
19 #include <TDataStd_UAttribute.hxx>
20 #include <TDataStd_ChildNodeIterator.hxx>
21 #include <TDataStd_ExtStringArray.hxx>
22 #include <TDocStd_Owner.hxx>
23 #include <TDocStd_Document.hxx>
24 #include <TFunction_Function.hxx>
25 #include <TNaming_NamedShape.hxx>
26 #include <TNaming_Builder.hxx>
27
28 #include <TColStd_HArray1OfReal.hxx>
29 #include <TColStd_HArray1OfInteger.hxx>
30 #include <TColStd_HSequenceOfTransient.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_ExtendedString.hxx>
33
34 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
35
36 #define ARGUMENT_LABEL 1
37 #define RESULT_LABEL 2
38 #define DESCRIPTION_LABEL 3
39 #define ARGUMENTS _label.FindChild((ARGUMENT_LABEL))
40 #define ARGUMENT(thePosition) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePosition))
41 #define SUB_ARGUMENT(thePos1, thePos2) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePos1)).FindChild((thePos2))
42
43 //=======================================================================
44 //function : GetFunctionTreeID
45 //purpose  :
46 //=======================================================================
47 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
48 {
49   static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
50   return aFunctionTreeID;
51 }
52
53
54 //=======================================================================
55 //function : GetDependencyID
56 //purpose  :
57 //=======================================================================
58 const Standard_GUID& GEOM_Function::GetDependencyID()
59 {
60   static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
61   return aDependencyID;
62 }
63
64 //=============================================================================
65 /*!
66  *  GetFunction:
67  */
68 //=============================================================================
69 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
70 {
71   if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
72
73   return new GEOM_Function(theEntry);
74 }
75
76 //=============================================================================
77 /*!
78  *  Constructor:
79  */
80 //=============================================================================
81 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
82 : _label(theEntry)
83 {
84   TFunction_Function::Set(theEntry, theGUID);
85   TDataStd_Integer::Set(theEntry, theType);
86
87   //Add function to a function tree
88   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theEntry.Data());
89   Handle(TDataStd_TreeNode) aRoot, aNode;
90   if(!aDoc->Main().FindAttribute(GetFunctionTreeID(), aRoot))
91     aRoot = TDataStd_TreeNode::Set(aDoc->Main(), GetFunctionTreeID());
92
93   aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
94   aRoot->Append(aNode);
95 }
96
97 //=============================================================================
98 /*!
99  *  GetOwner
100  */
101 //=============================================================================
102 TDF_Label GEOM_Function::GetOwnerEntry()
103 {
104   TDF_Label aFather = _label.Father();
105   while(!aFather.IsRoot()) {
106     if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
107     aFather = aFather.Father();
108   }
109
110   return TDF_Label();
111 }
112
113 //=============================================================================
114 /*!
115  *  GetType
116  */
117 //=============================================================================
118 int GEOM_Function::GetType()
119 {
120   _isDone = false;
121   Handle(TDataStd_Integer) aType;
122   if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
123   _isDone = true;
124   return aType->Get();
125 }
126
127 //=============================================================================
128 /*!
129  *  GetValue
130  */
131 //=============================================================================
132 TopoDS_Shape GEOM_Function::GetValue()
133 {
134   _isDone = false;
135
136   TopoDS_Shape aShape;
137   TDF_Label aLabel = GetOwnerEntry();
138   if(aLabel.IsRoot()) return aShape;
139   Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
140   if(anObject.IsNull()) return aShape;
141   if(!anObject->IsMainShape()) {
142     try {
143       GEOM_Solver aSolver(GEOM_Engine::GetEngine());
144       if (!aSolver.ComputeFunction(this)) {
145         MESSAGE("GEOM_Object::GetValue Error : Can't build a sub shape");
146         return aShape;
147       }
148     }
149     catch (Standard_Failure) {
150       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
151       MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
152       return aShape;
153     }
154   }
155
156   TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
157   Handle(TNaming_NamedShape) aNS;
158   if(!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
159
160   aShape = aNS->Get();
161
162   _isDone = true;
163   return aShape;
164 }
165
166 //=============================================================================
167 /*!
168  *  GetValue
169  */
170 //=============================================================================
171 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
172 {
173   _isDone = false;
174   TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
175   TNaming_Builder aBuilder(aResultLabel);
176
177   aBuilder.Generated(theShape);
178
179   _isDone = true;
180 }
181
182 //=============================================================================
183 /*!
184  *  GetDriverGUID
185  */
186 //=============================================================================
187 Standard_GUID GEOM_Function::GetDriverGUID()
188 {
189   Handle(TFunction_Function) aFunction;
190   if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
191     return TDF::LowestID();
192   }
193
194   return aFunction->GetDriverGUID();
195 }
196
197 //=============================================================================
198 /*!
199  *  GetDescription
200  */
201 //=============================================================================
202 TCollection_AsciiString GEOM_Function::GetDescription()
203 {
204   Handle(TDataStd_Comment) aComment;
205   TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
206   if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return TCollection_AsciiString();
207   TCollection_AsciiString aDescr(aComment->Get());
208   return aDescr;
209 }
210
211 //=============================================================================
212 /*!
213  *  SetDescription
214  */
215 //=============================================================================
216 void GEOM_Function::SetDescription(TCollection_AsciiString& theDescription)
217 {
218   TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
219   Handle(TDataStd_Comment) aComment = TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
220 }
221
222 //=============================================================================
223 /*!
224  *  SetReal
225  */
226 //=============================================================================
227 void GEOM_Function::SetReal(int thePosition, double theValue)
228 {
229   _isDone = false;
230   if(thePosition <= 0) return;
231   TDF_Label anArgLabel = ARGUMENT(thePosition);
232   TDataStd_Real::Set(anArgLabel, theValue);
233   _isDone = true;
234 }
235
236 //=============================================================================
237 /*!
238  *  SetRealArray
239  */
240 //=============================================================================
241 void GEOM_Function::SetRealArray (int thePosition,
242                                   const Handle(TColStd_HArray1OfReal)& theArray)
243 {
244   _isDone = false;
245   if(thePosition <= 0) return;
246   TDF_Label anArgLabel = ARGUMENT(thePosition);
247   Handle(TDataStd_RealArray) anAttr =
248     TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
249   anAttr->ChangeArray(theArray);
250   _isDone = true;
251 }
252
253 //=============================================================================
254 /*!
255  *  GetReal
256  */
257 //=============================================================================
258 double GEOM_Function::GetReal(int thePosition)
259 {
260   _isDone = false;
261   if(thePosition <= 0) return 0.0;
262   Handle(TDataStd_Real) aReal;
263   TDF_Label anArgLabel = ARGUMENT(thePosition);
264   if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
265
266   _isDone = true;
267   return aReal->Get();
268 }
269
270 //=============================================================================
271 /*!
272  *  GetRealArray
273  */
274 //=============================================================================
275 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
276 {
277   _isDone = false;
278   if(thePosition <= 0) return NULL;
279   Handle(TDataStd_RealArray) aRealArray;
280   TDF_Label anArgLabel = ARGUMENT(thePosition);
281   if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
282
283   _isDone = true;
284   return aRealArray->Array();
285 }
286
287 //=============================================================================
288 /*!
289  *  SetInteger
290  */
291 //=============================================================================
292 void GEOM_Function::SetInteger(int thePosition, int theValue)
293 {
294   _isDone = false;
295   if(thePosition <= 0) return;
296   TDF_Label anArgLabel = ARGUMENT(thePosition);
297   TDataStd_Integer::Set(anArgLabel, theValue);
298   _isDone = true;
299 }
300
301 //=============================================================================
302 /*!
303  *  SetIntegerArray
304  */
305 //=============================================================================
306 void GEOM_Function::SetIntegerArray (int thePosition,
307                                      const Handle(TColStd_HArray1OfInteger)& theArray)
308 {
309   _isDone = false;
310   if(thePosition <= 0) return;
311   TDF_Label anArgLabel = ARGUMENT(thePosition);
312   Handle(TDataStd_IntegerArray) anAttr =
313     TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
314   anAttr->ChangeArray(theArray);
315   _isDone = true;
316 }
317
318 //=============================================================================
319 /*!
320  *  GetInteger
321  */
322 //=============================================================================
323 int GEOM_Function::GetInteger(int thePosition)
324 {
325   _isDone = false;
326   if(thePosition <= 0) return 0;
327   Handle(TDataStd_Integer) anInteger;
328   TDF_Label anArgLabel = ARGUMENT(thePosition);
329   if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
330
331   _isDone = true;
332   return anInteger->Get();
333 }
334
335 //=============================================================================
336 /*!
337  *  GetIntegerArray
338  */
339 //=============================================================================
340 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
341 {
342   _isDone = false;
343   if(thePosition <= 0) return 0;
344   Handle(TDataStd_IntegerArray) anIntegerArray;
345   TDF_Label anArgLabel = ARGUMENT(thePosition);
346   if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
347
348   _isDone = true;
349   return anIntegerArray->Array();
350 }
351
352 //=============================================================================
353 /*!
354  *  SetString
355  */
356 //=============================================================================
357 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
358 {
359   _isDone = false;
360   if(thePosition <= 0) return;
361   TDF_Label anArgLabel = ARGUMENT(thePosition);
362   TDataStd_Comment::Set(anArgLabel, theValue);
363   _isDone = true;
364 }
365
366 //=============================================================================
367 /*!
368  *  GetString
369  */
370 //=============================================================================
371 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
372 {
373   _isDone = false;
374   TCollection_AsciiString aRes;
375   if(thePosition <= 0) return aRes;
376   Handle(TDataStd_Comment) aString;
377   TDF_Label anArgLabel = ARGUMENT(thePosition);
378   if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
379
380   _isDone = true;
381   aRes = TCollection_AsciiString(aString->Get());
382   return aRes;
383 }
384
385 //=============================================================================
386 /*!
387  *  SetReference
388  */
389 //=============================================================================
390 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
391 {
392   _isDone = false;
393   if(thePosition <= 0) return;
394   if(theReference.IsNull()) return;
395   TDF_Label anArgLabel = ARGUMENT(thePosition);
396   TDF_Reference::Set(anArgLabel, theReference->GetEntry());
397   TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
398   _isDone = true;
399   return;
400 }
401
402 //=============================================================================
403 /*!
404  *  GetReference
405  */
406 //=============================================================================
407 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
408 {
409   _isDone = false;
410   if(thePosition <= 0) return NULL;
411   TDF_Label anArgLabel = ARGUMENT(thePosition);
412   Handle(TDF_Reference) aRef;
413   if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
414
415   _isDone = true;
416   return GetFunction(aRef->Get());
417 }
418
419
420 //=============================================================================
421 /*!
422  *  SetStringArray
423  */
424 //=============================================================================
425 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
426 {
427   _isDone = false;
428   if(thePosition <= 0 || theArray.IsNull()) return;
429   TDF_Label anArgLabel = ARGUMENT(thePosition);
430
431   Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
432   anArray->ChangeArray(theArray);
433   anArgLabel.AddAttribute(anArray);
434
435   _isDone = true;
436 }
437
438
439 //=============================================================================
440 /*!
441  *  GetStringArray
442  */
443 //=============================================================================
444 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
445 {
446   _isDone = false;
447   if(thePosition <= 0) return NULL;
448   TDF_Label anArgLabel = ARGUMENT(thePosition);
449   Handle(TDataStd_ExtStringArray) anArray;
450   if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
451
452   _isDone = true;
453   return anArray->Array();
454 }
455
456 //=======================================================================
457 //function : GetReferencesTreeID
458 //purpose  :
459 //=======================================================================
460 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
461 {
462   static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
463   return aReferencesTreeID;
464 }
465
466 //=============================================================================
467 /*!
468  *  SetReferenceList
469  */
470 //=============================================================================
471 void GEOM_Function::SetReferenceList (int thePosition,
472                                       const Handle(TColStd_HSequenceOfTransient)& theRefList)
473 {
474   _isDone = false;
475   if(thePosition <= 0) return;
476
477   // parent label for the list of references
478   TDF_Label anArgLabel = ARGUMENT(thePosition);
479   anArgLabel.ForgetAllAttributes();
480
481   // set TreeNode on the parent label
482   Handle(TDataStd_TreeNode) aRoot, aNode;
483   aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
484
485   // store references on sub-labels of the parent label
486   Handle(GEOM_Function) aFunc;
487   Standard_Integer ind, len = theRefList->Length();
488   for (ind = 1; ind <= len; ind++) {
489     aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
490     if (aFunc.IsNull()) continue;
491     TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
492     TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
493     TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
494
495     // set TreeNode on the child label
496     aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
497     aRoot->Append(aNode);
498   }
499
500   _isDone = true;
501   return;
502 }
503
504 //=============================================================================
505 /*!
506  *  GetReferenceList
507  */
508 //=============================================================================
509 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
510 {
511   Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
512   _isDone = false;
513   if(thePosition <= 0) return aResult;
514
515   // parent label for the list of references
516   TDF_Label anArgLabel = ARGUMENT(thePosition);
517   Handle(TDF_Reference) aRef;
518
519   // get TreeNode on the parent label
520   Handle(TDataStd_TreeNode) aRoot, aNode;
521   if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
522     return aResult;
523
524   // get references, stored on sub-labels of the parent label
525   TDF_Label aLabel_i;
526   TDataStd_ChildNodeIterator anIter (aRoot);
527   for (; anIter.More(); anIter.Next()) {
528     aNode = anIter.Value();
529     aLabel_i = aNode->Label();
530     if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
531     Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
532     if (aFunc_i.IsNull()) continue;
533     aResult->Append(aFunc_i);
534   }
535
536   _isDone = true;
537   return aResult;
538 }
539
540 //=============================================================================
541 /*!
542  *  SetShape
543  */
544 //=============================================================================
545 void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
546 {
547   _isDone = false;
548   if(thePosition <= 0 || theShape.IsNull()) return;
549
550   TDF_Label anArgLabel = ARGUMENT(thePosition);
551   TNaming_Builder aBuilder(anArgLabel);
552   aBuilder.Generated(theShape);
553
554   _isDone = true;
555   return;
556 }
557
558 //=============================================================================
559 /*!
560  *  GetShape
561  */
562 //=============================================================================
563 TopoDS_Shape GEOM_Function::GetShape(int thePosition)
564 {
565   _isDone = false;
566   TopoDS_Shape aShape;
567   if(thePosition <= 0) return aShape;
568
569   TDF_Label anArgLabel = ARGUMENT(thePosition);
570   Handle(TNaming_NamedShape) aNS;
571   if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
572
573   aShape = aNS->Get();
574   _isDone = true;
575   return aShape;
576 }
577
578
579 //=============================================================================
580 /*!
581  *  GetDependency
582  */
583 //=============================================================================
584 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
585 {
586   TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
587   for(; anIterator.More(); anIterator.Next()) {
588     if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
589   }
590 }
591
592 //=======================================================================
593 //function :  GEOM_Function_Type_
594 //purpose  :
595 //=======================================================================
596 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
597 {
598
599   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
600   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
601   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
602   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
603
604
605   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
606   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
607                                                          sizeof(GEOM_Function),
608                                                          1,
609                                                          (Standard_Address)_Ancestors,
610                                                          (Standard_Address)NULL);
611
612   return _aType;
613 }
614
615 //=======================================================================
616 //function : DownCast
617 //purpose  :
618 //=======================================================================
619
620 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
621 {
622   Handle(GEOM_Function) _anOtherObject;
623
624   if (!AnObject.IsNull()) {
625      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
626        _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
627      }
628   }
629
630   return _anOtherObject ;
631 }