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