1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_Stream.hxx>
24 #include <GEOM_Function.hxx>
25 #include <GEOM_Object.hxx>
26 #include <GEOM_Solver.hxx>
27 #include <GEOM_ISubShape.hxx>
29 #include "utilities.h"
32 #include <TDF_Tool.hxx>
33 #include <TDF_Data.hxx>
34 #include <TDF_ChildIterator.hxx>
35 #include <TDF_Reference.hxx>
36 #include <TDataStd_AsciiString.hxx>
37 #include <TDataStd_Integer.hxx>
38 #include <TDataStd_IntegerArray.hxx>
39 #include <TDataStd_Real.hxx>
40 #include <TDataStd_RealArray.hxx>
41 #include <TDataStd_Comment.hxx>
42 #include <TDataStd_TreeNode.hxx>
43 #include <TDataStd_UAttribute.hxx>
44 #include <TDataStd_ChildNodeIterator.hxx>
45 #include <TDataStd_ExtStringArray.hxx>
46 #include <TDocStd_Owner.hxx>
47 #include <TDocStd_Document.hxx>
48 #include <TFunction_Function.hxx>
49 #include <TNaming_NamedShape.hxx>
50 #include <TNaming_Builder.hxx>
52 #include <TColStd_ListOfInteger.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54 #include <TColStd_HArray1OfReal.hxx>
55 #include <TColStd_HArray1OfInteger.hxx>
56 #include <TColStd_HSequenceOfTransient.hxx>
57 #include <TCollection_AsciiString.hxx>
58 #include <TCollection_ExtendedString.hxx>
60 #include <Standard_Failure.hxx>
61 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
63 #define ARGUMENT_LABEL 1
64 #define RESULT_LABEL 2
65 #define DESCRIPTION_LABEL 3
66 #define HISTORY_LABEL 4
68 #define ARGUMENTS _label.FindChild((ARGUMENT_LABEL))
69 #define ARGUMENT(thePosition) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePosition))
70 #define SUB_ARGUMENT(thePos1, thePos2) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePos1)).FindChild((thePos2))
72 //=======================================================================
73 //function : GetFunctionTreeID
75 //=======================================================================
76 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
78 static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
79 return aFunctionTreeID;
83 //=======================================================================
84 //function : GetDependencyID
86 //=======================================================================
87 const Standard_GUID& GEOM_Function::GetDependencyID()
89 static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
93 //=============================================================================
97 //=============================================================================
98 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
100 if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
102 return new GEOM_Function(theEntry);
105 //=============================================================================
109 //=============================================================================
110 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
113 TFunction_Function::Set(theEntry, theGUID);
114 TDataStd_Integer::Set(theEntry, theType);
116 //Add function to a function tree
117 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theEntry.Data());
118 Handle(TDataStd_TreeNode) aRoot, aNode;
119 if(!aDoc->Main().FindAttribute(GetFunctionTreeID(), aRoot))
120 aRoot = TDataStd_TreeNode::Set(aDoc->Main(), GetFunctionTreeID());
122 aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
123 aRoot->Append(aNode);
126 //=============================================================================
130 //=============================================================================
131 TDF_Label GEOM_Function::GetOwnerEntry()
133 TDF_Label aFather = _label.Father();
134 while(!aFather.IsRoot()) {
135 if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
136 aFather = aFather.Father();
142 //=============================================================================
146 //=============================================================================
147 int GEOM_Function::GetType()
150 Handle(TDataStd_Integer) aType;
151 if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
156 //=============================================================================
160 //=============================================================================
161 TopoDS_Shape GEOM_Function::GetValue()
166 TDF_Label aLabel = GetOwnerEntry();
167 if (aLabel.IsRoot()) return aShape;
168 Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
169 if (anObject.IsNull()) return aShape;
171 if (!anObject->IsMainShape()) {
172 bool isResult = false;
173 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
174 if (!aResultLabel.IsNull()) {
175 Handle(TNaming_NamedShape) aNS;
176 if (aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS))
183 Standard_Integer aTic = anObject->GetTic();
186 GEOM_ISubShape aCI (this);
187 TDF_Label aLabelObjMainSh = aCI.GetMainShape()->GetOwnerEntry();
188 if (aLabelObjMainSh.IsRoot()) return aShape;
189 Handle(GEOM_Object) anObjMainSh = GEOM_Object::GetObject(aLabelObjMainSh);
190 if (anObjMainSh.IsNull()) return aShape;
191 Standard_Integer aTicMainSh = anObjMainSh->GetTic();
194 isResult = ((aTic == aTicMainSh) ? true : false);
199 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
202 GEOM_Solver aSolver(GEOM_Engine::GetEngine());
203 if (!aSolver.ComputeFunction(this)) {
204 MESSAGE("GEOM_Object::GetValue Error : Can't build a sub shape");
208 catch (Standard_Failure) {
209 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
210 MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
216 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
217 Handle(TNaming_NamedShape) aNS;
218 if (!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
226 //=============================================================================
230 //=============================================================================
231 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
234 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
235 TNaming_Builder aBuilder(aResultLabel);
237 aBuilder.Generated(theShape);
239 // synchronisation between main shape and its sub-shapes
240 TDF_Label aLabel = GetOwnerEntry();
241 if (aLabel.IsRoot()) return;
242 Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
243 if (anObject.IsNull()) return;
244 if (anObject->IsMainShape()) {
245 // increase modifications counter of this (main) shape
246 anObject->IncrementTic();
249 // update modifications counter of this (sub-) shape to be the same as on main shape
250 GEOM_ISubShape aCI (this);
251 TDF_Label aLabelObjMainSh = aCI.GetMainShape()->GetOwnerEntry();
252 if (aLabelObjMainSh.IsRoot()) return;
253 Handle(GEOM_Object) anObjMainSh = GEOM_Object::GetObject(aLabelObjMainSh);
254 if (anObjMainSh.IsNull()) return;
256 anObject->SetTic(anObjMainSh->GetTic());
262 //=============================================================================
266 //=============================================================================
267 Standard_GUID GEOM_Function::GetDriverGUID()
269 Handle(TFunction_Function) aFunction;
270 if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
271 return TDF::LowestID();
274 return aFunction->GetDriverGUID();
277 //=============================================================================
281 //=============================================================================
282 TCollection_AsciiString GEOM_Function::GetDescription()
284 Handle(TDataStd_Comment) aComment;
285 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
286 if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return TCollection_AsciiString();
287 TCollection_AsciiString aDescr(aComment->Get());
291 //=============================================================================
295 //=============================================================================
296 void GEOM_Function::SetDescription(const TCollection_AsciiString& theDescription)
298 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
299 Handle(TDataStd_Comment) aComment =
300 TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
303 //=============================================================================
307 //=============================================================================
308 int GEOM_Function::GetArgsCount()
310 return _label.FindChild( ARGUMENT_LABEL ).NbChildren();
313 //=============================================================================
317 //=============================================================================
318 void GEOM_Function::SetParam( int thePosition, const TCollection_AsciiString& theParamName )
321 if( thePosition <= 0 )
324 TDF_Label anArgLabel = ARGUMENT( thePosition );
325 TDataStd_AsciiString::Set( anArgLabel, theParamName );
329 //=============================================================================
333 //=============================================================================
334 TCollection_AsciiString GEOM_Function::GetParam(int thePosition)
337 if( thePosition <= 0 )
340 Handle( TDataStd_AsciiString ) aParam;
341 TDF_Label anArgLabel = ARGUMENT( thePosition );
342 if( !anArgLabel.FindAttribute( TDataStd_AsciiString::GetID(), aParam ) )
346 return aParam->Get();
349 //=============================================================================
353 //=============================================================================
354 void GEOM_Function::SetReal(int thePosition, double theValue)
357 if(thePosition <= 0) return;
358 TDF_Label anArgLabel = ARGUMENT(thePosition);
359 TDataStd_Real::Set(anArgLabel, theValue);
363 //=============================================================================
367 //=============================================================================
368 void GEOM_Function::SetRealArray (int thePosition,
369 const Handle(TColStd_HArray1OfReal)& theArray)
372 if(thePosition <= 0) return;
373 TDF_Label anArgLabel = ARGUMENT(thePosition);
374 Handle(TDataStd_RealArray) anAttr =
375 TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
376 anAttr->ChangeArray(theArray);
380 //=============================================================================
384 //=============================================================================
385 double GEOM_Function::GetReal(int thePosition)
388 if(thePosition <= 0) return 0.0;
389 Handle(TDataStd_Real) aReal;
390 TDF_Label anArgLabel = ARGUMENT(thePosition);
391 if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
397 //=============================================================================
401 //=============================================================================
402 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
405 if(thePosition <= 0) return NULL;
406 Handle(TDataStd_RealArray) aRealArray;
407 TDF_Label anArgLabel = ARGUMENT(thePosition);
408 if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
411 return aRealArray->Array();
414 //=============================================================================
418 //=============================================================================
419 void GEOM_Function::SetInteger(int thePosition, int theValue)
422 if(thePosition <= 0) return;
423 TDF_Label anArgLabel = ARGUMENT(thePosition);
424 TDataStd_Integer::Set(anArgLabel, theValue);
428 //=============================================================================
432 //=============================================================================
433 void GEOM_Function::SetIntegerArray (int thePosition,
434 const Handle(TColStd_HArray1OfInteger)& theArray)
437 if(thePosition <= 0) return;
438 TDF_Label anArgLabel = ARGUMENT(thePosition);
439 Handle(TDataStd_IntegerArray) anAttr =
440 TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
441 anAttr->ChangeArray(theArray);
445 //=============================================================================
449 //=============================================================================
450 int GEOM_Function::GetInteger(int thePosition)
453 if(thePosition <= 0) return 0;
454 Handle(TDataStd_Integer) anInteger;
455 TDF_Label anArgLabel = ARGUMENT(thePosition);
456 if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
459 return anInteger->Get();
462 //=============================================================================
466 //=============================================================================
467 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
470 if(thePosition <= 0) return 0;
471 Handle(TDataStd_IntegerArray) anIntegerArray;
472 TDF_Label anArgLabel = ARGUMENT(thePosition);
473 if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
476 return anIntegerArray->Array();
479 //=============================================================================
483 //=============================================================================
484 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
487 if(thePosition <= 0) return;
488 TDF_Label anArgLabel = ARGUMENT(thePosition);
489 TDataStd_Comment::Set(anArgLabel, theValue);
493 //=============================================================================
497 //=============================================================================
498 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
501 TCollection_AsciiString aRes;
502 if(thePosition <= 0) return aRes;
503 Handle(TDataStd_Comment) aString;
504 TDF_Label anArgLabel = ARGUMENT(thePosition);
505 if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
508 aRes = TCollection_AsciiString(aString->Get());
512 //=============================================================================
516 //=============================================================================
517 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
520 if(thePosition <= 0) return;
521 if(theReference.IsNull()) return;
522 TDF_Label anArgLabel = ARGUMENT(thePosition);
523 TDF_Reference::Set(anArgLabel, theReference->GetEntry());
524 TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
529 //=============================================================================
533 //=============================================================================
534 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
537 if(thePosition <= 0) return NULL;
538 TDF_Label anArgLabel = ARGUMENT(thePosition);
539 Handle(TDF_Reference) aRef;
540 if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
543 return GetFunction(aRef->Get());
547 //=============================================================================
551 //=============================================================================
552 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
555 if(thePosition <= 0 || theArray.IsNull()) return;
556 TDF_Label anArgLabel = ARGUMENT(thePosition);
558 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
559 anArray->ChangeArray(theArray);
560 anArgLabel.AddAttribute(anArray);
566 //=============================================================================
570 //=============================================================================
571 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
574 if(thePosition <= 0) return NULL;
575 TDF_Label anArgLabel = ARGUMENT(thePosition);
576 Handle(TDataStd_ExtStringArray) anArray;
577 if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
580 return anArray->Array();
583 //=======================================================================
584 //function : GetReferencesTreeID
586 //=======================================================================
587 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
589 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
590 return aReferencesTreeID;
593 //=============================================================================
597 //=============================================================================
598 void GEOM_Function::SetReferenceList (int thePosition,
599 const Handle(TColStd_HSequenceOfTransient)& theRefList)
602 if(thePosition <= 0) return;
604 // parent label for the list of references
605 TDF_Label anArgLabel = ARGUMENT(thePosition);
606 anArgLabel.ForgetAllAttributes();
608 // set TreeNode on the parent label
609 Handle(TDataStd_TreeNode) aRoot, aNode;
610 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
612 // store references on sub-labels of the parent label
613 Handle(GEOM_Function) aFunc;
614 Standard_Integer ind, len = theRefList->Length();
615 for (ind = 1; ind <= len; ind++) {
616 aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
617 if (aFunc.IsNull()) continue;
618 TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
619 TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
620 TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
622 // set TreeNode on the child label
623 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
624 aRoot->Append(aNode);
631 //=============================================================================
635 //=============================================================================
636 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
638 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
640 if(thePosition <= 0) return aResult;
642 // parent label for the list of references
643 TDF_Label anArgLabel = ARGUMENT(thePosition);
644 Handle(TDF_Reference) aRef;
646 // get TreeNode on the parent label
647 Handle(TDataStd_TreeNode) aRoot, aNode;
648 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
651 // get references, stored on sub-labels of the parent label
653 TDataStd_ChildNodeIterator anIter (aRoot);
654 for (; anIter.More(); anIter.Next()) {
655 aNode = anIter.Value();
656 aLabel_i = aNode->Label();
657 if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
658 Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
659 if (aFunc_i.IsNull()) continue;
660 aResult->Append(aFunc_i);
667 //=============================================================================
671 //=============================================================================
672 //void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
675 // if(thePosition <= 0 || theShape.IsNull()) return;
677 // TDF_Label anArgLabel = ARGUMENT(thePosition);
678 // TNaming_Builder aBuilder(anArgLabel);
679 // aBuilder.Generated(theShape);
685 //=============================================================================
689 //=============================================================================
690 //TopoDS_Shape GEOM_Function::GetShape(int thePosition)
693 // TopoDS_Shape aShape;
694 // if(thePosition <= 0) return aShape;
696 // TDF_Label anArgLabel = ARGUMENT(thePosition);
697 // Handle(TNaming_NamedShape) aNS;
698 // if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
700 // aShape = aNS->Get();
706 //=============================================================================
710 //=============================================================================
711 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
713 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
714 for(; anIterator.More(); anIterator.Next()) {
715 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
719 //=============================================================================
723 //=============================================================================
724 TDF_Label GEOM_Function::GetHistoryEntry (const Standard_Boolean create)
726 return _label.FindChild(HISTORY_LABEL, create);
729 //=============================================================================
731 * GetArgumentHistoryEntry
733 //=============================================================================
734 TDF_Label GEOM_Function::GetArgumentHistoryEntry (const TDF_Label& theArgumentRefEntry,
735 const Standard_Boolean create)
737 TColStd_ListOfInteger anArgumentRefTags;
738 TDF_Tool::TagList(theArgumentRefEntry, anArgumentRefTags);
739 Standard_Integer anArgumentRefLabelPos = anArgumentRefTags.Extent();
741 TDF_Label aHistoryLabel = GetHistoryEntry(create);
742 if (aHistoryLabel.IsNull())
743 return aHistoryLabel;
744 Standard_Integer aHistoryLabelPos = aHistoryLabel.Depth() + 1;
746 Standard_Integer itag;
747 TDF_Label aHistoryCurLabel = aHistoryLabel;
748 TColStd_ListIteratorOfListOfInteger aListIter (anArgumentRefTags);
749 for (itag = 1; itag <= aHistoryLabelPos; itag++) {
752 for (; itag <= anArgumentRefLabelPos; itag++) {
753 aHistoryCurLabel = aHistoryCurLabel.FindChild(aListIter.Value(), create);
754 if (aHistoryCurLabel.IsNull())
755 return aHistoryCurLabel;
759 return aHistoryCurLabel;
762 //=======================================================================
763 //function : GEOM_Function_Type_
765 //=======================================================================
766 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
769 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
770 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
771 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
772 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
775 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
776 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
777 sizeof(GEOM_Function),
779 (Standard_Address)_Ancestors,
780 (Standard_Address)NULL);
785 //=======================================================================
786 //function : DownCast
788 //=======================================================================
790 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
792 Handle(GEOM_Function) _anOtherObject;
794 if (!AnObject.IsNull()) {
795 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
796 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
800 return _anOtherObject ;