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(TCollection_AsciiString& theDescription)
223 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
224 Handle(TDataStd_Comment) aComment = TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
227 //=============================================================================
231 //=============================================================================
232 void GEOM_Function::SetReal(int thePosition, double theValue)
235 if(thePosition <= 0) return;
236 TDF_Label anArgLabel = ARGUMENT(thePosition);
237 TDataStd_Real::Set(anArgLabel, theValue);
241 //=============================================================================
245 //=============================================================================
246 void GEOM_Function::SetRealArray (int thePosition,
247 const Handle(TColStd_HArray1OfReal)& theArray)
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);
258 //=============================================================================
262 //=============================================================================
263 double GEOM_Function::GetReal(int thePosition)
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;
275 //=============================================================================
279 //=============================================================================
280 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
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;
289 return aRealArray->Array();
292 //=============================================================================
296 //=============================================================================
297 void GEOM_Function::SetInteger(int thePosition, int theValue)
300 if(thePosition <= 0) return;
301 TDF_Label anArgLabel = ARGUMENT(thePosition);
302 TDataStd_Integer::Set(anArgLabel, theValue);
306 //=============================================================================
310 //=============================================================================
311 void GEOM_Function::SetIntegerArray (int thePosition,
312 const Handle(TColStd_HArray1OfInteger)& theArray)
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);
323 //=============================================================================
327 //=============================================================================
328 int GEOM_Function::GetInteger(int thePosition)
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;
337 return anInteger->Get();
340 //=============================================================================
344 //=============================================================================
345 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
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;
354 return anIntegerArray->Array();
357 //=============================================================================
361 //=============================================================================
362 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
365 if(thePosition <= 0) return;
366 TDF_Label anArgLabel = ARGUMENT(thePosition);
367 TDataStd_Comment::Set(anArgLabel, theValue);
371 //=============================================================================
375 //=============================================================================
376 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
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;
386 aRes = TCollection_AsciiString(aString->Get());
390 //=============================================================================
394 //=============================================================================
395 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
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());
407 //=============================================================================
411 //=============================================================================
412 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
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;
421 return GetFunction(aRef->Get());
425 //=============================================================================
429 //=============================================================================
430 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
433 if(thePosition <= 0 || theArray.IsNull()) return;
434 TDF_Label anArgLabel = ARGUMENT(thePosition);
436 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
437 anArray->ChangeArray(theArray);
438 anArgLabel.AddAttribute(anArray);
444 //=============================================================================
448 //=============================================================================
449 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
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;
458 return anArray->Array();
461 //=======================================================================
462 //function : GetReferencesTreeID
464 //=======================================================================
465 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
467 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
468 return aReferencesTreeID;
471 //=============================================================================
475 //=============================================================================
476 void GEOM_Function::SetReferenceList (int thePosition,
477 const Handle(TColStd_HSequenceOfTransient)& theRefList)
480 if(thePosition <= 0) return;
482 // parent label for the list of references
483 TDF_Label anArgLabel = ARGUMENT(thePosition);
484 anArgLabel.ForgetAllAttributes();
486 // set TreeNode on the parent label
487 Handle(TDataStd_TreeNode) aRoot, aNode;
488 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
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());
500 // set TreeNode on the child label
501 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
502 aRoot->Append(aNode);
509 //=============================================================================
513 //=============================================================================
514 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
516 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
518 if(thePosition <= 0) return aResult;
520 // parent label for the list of references
521 TDF_Label anArgLabel = ARGUMENT(thePosition);
522 Handle(TDF_Reference) aRef;
524 // get TreeNode on the parent label
525 Handle(TDataStd_TreeNode) aRoot, aNode;
526 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
529 // get references, stored on sub-labels of the parent label
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);
545 //=============================================================================
549 //=============================================================================
550 //void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
553 // if(thePosition <= 0 || theShape.IsNull()) return;
555 // TDF_Label anArgLabel = ARGUMENT(thePosition);
556 // TNaming_Builder aBuilder(anArgLabel);
557 // aBuilder.Generated(theShape);
563 //=============================================================================
567 //=============================================================================
568 //TopoDS_Shape GEOM_Function::GetShape(int thePosition)
571 // TopoDS_Shape aShape;
572 // if(thePosition <= 0) return aShape;
574 // TDF_Label anArgLabel = ARGUMENT(thePosition);
575 // Handle(TNaming_NamedShape) aNS;
576 // if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
578 // aShape = aNS->Get();
584 //=============================================================================
588 //=============================================================================
589 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
591 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
592 for(; anIterator.More(); anIterator.Next()) {
593 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
597 //=============================================================================
601 //=============================================================================
602 TDF_Label GEOM_Function::GetHistoryEntry (const Standard_Boolean create)
604 return _label.FindChild(HISTORY_LABEL, create);
607 //=============================================================================
609 * GetArgumentHistoryEntry
611 //=============================================================================
612 TDF_Label GEOM_Function::GetArgumentHistoryEntry (const TDF_Label& theArgumentRefEntry,
613 const Standard_Boolean create)
615 TColStd_ListOfInteger anArgumentRefTags;
616 TDF_Tool::TagList(theArgumentRefEntry, anArgumentRefTags);
617 Standard_Integer anArgumentRefLabelPos = anArgumentRefTags.Extent();
619 TDF_Label aHistoryLabel = GetHistoryEntry(create);
620 if (aHistoryLabel.IsNull())
621 return aHistoryLabel;
622 Standard_Integer aHistoryLabelPos = aHistoryLabel.Depth() + 1;
624 Standard_Integer itag;
625 TDF_Label aHistoryCurLabel = aHistoryLabel;
626 TColStd_ListIteratorOfListOfInteger aListIter (anArgumentRefTags);
627 for (itag = 1; itag <= aHistoryLabelPos; itag++) {
630 for (; itag <= anArgumentRefLabelPos; itag++) {
631 aHistoryCurLabel = aHistoryCurLabel.FindChild(aListIter.Value(), create);
632 if (aHistoryCurLabel.IsNull())
633 return aHistoryCurLabel;
637 return aHistoryCurLabel;
640 //=======================================================================
641 //function : GEOM_Function_Type_
643 //=======================================================================
644 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
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);
653 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
654 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
655 sizeof(GEOM_Function),
657 (Standard_Address)_Ancestors,
658 (Standard_Address)NULL);
663 //=======================================================================
664 //function : DownCast
666 //=======================================================================
668 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
670 Handle(GEOM_Function) _anOtherObject;
672 if (!AnObject.IsNull()) {
673 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
674 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
678 return _anOtherObject ;