3 #include "GEOM_Function.hxx"
4 #include "GEOM_Object.hxx"
5 #include "GEOM_Solver.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>
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>
37 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
39 #define ARGUMENT_LABEL 1
40 #define RESULT_LABEL 2
41 #define DESCRIPTION_LABEL 3
42 #define HISTORY_LABEL 4
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))
48 //=======================================================================
49 //function : GetFunctionTreeID
51 //=======================================================================
52 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
54 static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
55 return aFunctionTreeID;
59 //=======================================================================
60 //function : GetDependencyID
62 //=======================================================================
63 const Standard_GUID& GEOM_Function::GetDependencyID()
65 static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
69 //=============================================================================
73 //=============================================================================
74 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
76 if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
78 return new GEOM_Function(theEntry);
81 //=============================================================================
85 //=============================================================================
86 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
89 TFunction_Function::Set(theEntry, theGUID);
90 TDataStd_Integer::Set(theEntry, theType);
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());
98 aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
102 //=============================================================================
106 //=============================================================================
107 TDF_Label GEOM_Function::GetOwnerEntry()
109 TDF_Label aFather = _label.Father();
110 while(!aFather.IsRoot()) {
111 if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
112 aFather = aFather.Father();
118 //=============================================================================
122 //=============================================================================
123 int GEOM_Function::GetType()
126 Handle(TDataStd_Integer) aType;
127 if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
132 //=============================================================================
136 //=============================================================================
137 TopoDS_Shape GEOM_Function::GetValue()
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()) {
148 GEOM_Solver aSolver(GEOM_Engine::GetEngine());
149 if (!aSolver.ComputeFunction(this)) {
150 MESSAGE("GEOM_Object::GetValue Error : Can't build a sub shape");
154 catch (Standard_Failure) {
155 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
156 MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
161 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
162 Handle(TNaming_NamedShape) aNS;
163 if(!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
171 //=============================================================================
175 //=============================================================================
176 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
179 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
180 TNaming_Builder aBuilder(aResultLabel);
182 aBuilder.Generated(theShape);
187 //=============================================================================
191 //=============================================================================
192 Standard_GUID GEOM_Function::GetDriverGUID()
194 Handle(TFunction_Function) aFunction;
195 if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
196 return TDF::LowestID();
199 return aFunction->GetDriverGUID();
202 //=============================================================================
206 //=============================================================================
207 TCollection_AsciiString GEOM_Function::GetDescription()
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());
216 //=============================================================================
220 //=============================================================================
221 void GEOM_Function::SetDescription(const TCollection_AsciiString& theDescription)
223 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
224 Handle(TDataStd_Comment) aComment =
225 TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
228 //=============================================================================
232 //=============================================================================
233 void GEOM_Function::SetReal(int thePosition, double theValue)
236 if(thePosition <= 0) return;
237 TDF_Label anArgLabel = ARGUMENT(thePosition);
238 TDataStd_Real::Set(anArgLabel, theValue);
242 //=============================================================================
246 //=============================================================================
247 void GEOM_Function::SetRealArray (int thePosition,
248 const Handle(TColStd_HArray1OfReal)& theArray)
251 if(thePosition <= 0) return;
252 TDF_Label anArgLabel = ARGUMENT(thePosition);
253 Handle(TDataStd_RealArray) anAttr =
254 TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
255 anAttr->ChangeArray(theArray);
259 //=============================================================================
263 //=============================================================================
264 double GEOM_Function::GetReal(int thePosition)
267 if(thePosition <= 0) return 0.0;
268 Handle(TDataStd_Real) aReal;
269 TDF_Label anArgLabel = ARGUMENT(thePosition);
270 if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
276 //=============================================================================
280 //=============================================================================
281 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
284 if(thePosition <= 0) return NULL;
285 Handle(TDataStd_RealArray) aRealArray;
286 TDF_Label anArgLabel = ARGUMENT(thePosition);
287 if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
290 return aRealArray->Array();
293 //=============================================================================
297 //=============================================================================
298 void GEOM_Function::SetInteger(int thePosition, int theValue)
301 if(thePosition <= 0) return;
302 TDF_Label anArgLabel = ARGUMENT(thePosition);
303 TDataStd_Integer::Set(anArgLabel, theValue);
307 //=============================================================================
311 //=============================================================================
312 void GEOM_Function::SetIntegerArray (int thePosition,
313 const Handle(TColStd_HArray1OfInteger)& theArray)
316 if(thePosition <= 0) return;
317 TDF_Label anArgLabel = ARGUMENT(thePosition);
318 Handle(TDataStd_IntegerArray) anAttr =
319 TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
320 anAttr->ChangeArray(theArray);
324 //=============================================================================
328 //=============================================================================
329 int GEOM_Function::GetInteger(int thePosition)
332 if(thePosition <= 0) return 0;
333 Handle(TDataStd_Integer) anInteger;
334 TDF_Label anArgLabel = ARGUMENT(thePosition);
335 if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
338 return anInteger->Get();
341 //=============================================================================
345 //=============================================================================
346 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
349 if(thePosition <= 0) return 0;
350 Handle(TDataStd_IntegerArray) anIntegerArray;
351 TDF_Label anArgLabel = ARGUMENT(thePosition);
352 if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
355 return anIntegerArray->Array();
358 //=============================================================================
362 //=============================================================================
363 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
366 if(thePosition <= 0) return;
367 TDF_Label anArgLabel = ARGUMENT(thePosition);
368 TDataStd_Comment::Set(anArgLabel, theValue);
372 //=============================================================================
376 //=============================================================================
377 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
380 TCollection_AsciiString aRes;
381 if(thePosition <= 0) return aRes;
382 Handle(TDataStd_Comment) aString;
383 TDF_Label anArgLabel = ARGUMENT(thePosition);
384 if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
387 aRes = TCollection_AsciiString(aString->Get());
391 //=============================================================================
395 //=============================================================================
396 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
399 if(thePosition <= 0) return;
400 if(theReference.IsNull()) return;
401 TDF_Label anArgLabel = ARGUMENT(thePosition);
402 TDF_Reference::Set(anArgLabel, theReference->GetEntry());
403 TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
408 //=============================================================================
412 //=============================================================================
413 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
416 if(thePosition <= 0) return NULL;
417 TDF_Label anArgLabel = ARGUMENT(thePosition);
418 Handle(TDF_Reference) aRef;
419 if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
422 return GetFunction(aRef->Get());
426 //=============================================================================
430 //=============================================================================
431 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
434 if(thePosition <= 0 || theArray.IsNull()) return;
435 TDF_Label anArgLabel = ARGUMENT(thePosition);
437 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
438 anArray->ChangeArray(theArray);
439 anArgLabel.AddAttribute(anArray);
445 //=============================================================================
449 //=============================================================================
450 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
453 if(thePosition <= 0) return NULL;
454 TDF_Label anArgLabel = ARGUMENT(thePosition);
455 Handle(TDataStd_ExtStringArray) anArray;
456 if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
459 return anArray->Array();
462 //=======================================================================
463 //function : GetReferencesTreeID
465 //=======================================================================
466 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
468 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
469 return aReferencesTreeID;
472 //=============================================================================
476 //=============================================================================
477 void GEOM_Function::SetReferenceList (int thePosition,
478 const Handle(TColStd_HSequenceOfTransient)& theRefList)
481 if(thePosition <= 0) return;
483 // parent label for the list of references
484 TDF_Label anArgLabel = ARGUMENT(thePosition);
485 anArgLabel.ForgetAllAttributes();
487 // set TreeNode on the parent label
488 Handle(TDataStd_TreeNode) aRoot, aNode;
489 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
491 // store references on sub-labels of the parent label
492 Handle(GEOM_Function) aFunc;
493 Standard_Integer ind, len = theRefList->Length();
494 for (ind = 1; ind <= len; ind++) {
495 aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
496 if (aFunc.IsNull()) continue;
497 TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
498 TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
499 TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
501 // set TreeNode on the child label
502 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
503 aRoot->Append(aNode);
510 //=============================================================================
514 //=============================================================================
515 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
517 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
519 if(thePosition <= 0) return aResult;
521 // parent label for the list of references
522 TDF_Label anArgLabel = ARGUMENT(thePosition);
523 Handle(TDF_Reference) aRef;
525 // get TreeNode on the parent label
526 Handle(TDataStd_TreeNode) aRoot, aNode;
527 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
530 // get references, stored on sub-labels of the parent label
532 TDataStd_ChildNodeIterator anIter (aRoot);
533 for (; anIter.More(); anIter.Next()) {
534 aNode = anIter.Value();
535 aLabel_i = aNode->Label();
536 if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
537 Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
538 if (aFunc_i.IsNull()) continue;
539 aResult->Append(aFunc_i);
546 //=============================================================================
550 //=============================================================================
551 //void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
554 // if(thePosition <= 0 || theShape.IsNull()) return;
556 // TDF_Label anArgLabel = ARGUMENT(thePosition);
557 // TNaming_Builder aBuilder(anArgLabel);
558 // aBuilder.Generated(theShape);
564 //=============================================================================
568 //=============================================================================
569 //TopoDS_Shape GEOM_Function::GetShape(int thePosition)
572 // TopoDS_Shape aShape;
573 // if(thePosition <= 0) return aShape;
575 // TDF_Label anArgLabel = ARGUMENT(thePosition);
576 // Handle(TNaming_NamedShape) aNS;
577 // if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
579 // aShape = aNS->Get();
585 //=============================================================================
589 //=============================================================================
590 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
592 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
593 for(; anIterator.More(); anIterator.Next()) {
594 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
598 //=============================================================================
602 //=============================================================================
603 TDF_Label GEOM_Function::GetHistoryEntry (const Standard_Boolean create)
605 return _label.FindChild(HISTORY_LABEL, create);
608 //=============================================================================
610 * GetArgumentHistoryEntry
612 //=============================================================================
613 TDF_Label GEOM_Function::GetArgumentHistoryEntry (const TDF_Label& theArgumentRefEntry,
614 const Standard_Boolean create)
616 TColStd_ListOfInteger anArgumentRefTags;
617 TDF_Tool::TagList(theArgumentRefEntry, anArgumentRefTags);
618 Standard_Integer anArgumentRefLabelPos = anArgumentRefTags.Extent();
620 TDF_Label aHistoryLabel = GetHistoryEntry(create);
621 if (aHistoryLabel.IsNull())
622 return aHistoryLabel;
623 Standard_Integer aHistoryLabelPos = aHistoryLabel.Depth() + 1;
625 Standard_Integer itag;
626 TDF_Label aHistoryCurLabel = aHistoryLabel;
627 TColStd_ListIteratorOfListOfInteger aListIter (anArgumentRefTags);
628 for (itag = 1; itag <= aHistoryLabelPos; itag++) {
631 for (; itag <= anArgumentRefLabelPos; itag++) {
632 aHistoryCurLabel = aHistoryCurLabel.FindChild(aListIter.Value(), create);
633 if (aHistoryCurLabel.IsNull())
634 return aHistoryCurLabel;
638 return aHistoryCurLabel;
641 //=======================================================================
642 //function : GEOM_Function_Type_
644 //=======================================================================
645 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
648 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
649 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
650 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
651 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
654 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
655 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
656 sizeof(GEOM_Function),
658 (Standard_Address)_Ancestors,
659 (Standard_Address)NULL);
664 //=======================================================================
665 //function : DownCast
667 //=======================================================================
669 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
671 Handle(GEOM_Function) _anOtherObject;
673 if (!AnObject.IsNull()) {
674 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
675 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
679 return _anOtherObject ;