1 // Copyright (C) 2007-2010 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
23 #include <Standard_Stream.hxx>
25 #include <GEOM_Function.hxx>
26 #include <GEOM_Object.hxx>
27 #include <GEOM_Solver.hxx>
28 #include <GEOM_ISubShape.hxx>
30 #include "utilities.h"
33 #include <TDF_Tool.hxx>
34 #include <TDF_Data.hxx>
35 #include <TDF_ChildIterator.hxx>
36 #include <TDF_Reference.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 void GEOM_Function::SetReal(int thePosition, double theValue)
311 if(thePosition <= 0) return;
312 TDF_Label anArgLabel = ARGUMENT(thePosition);
313 TDataStd_Real::Set(anArgLabel, theValue);
317 //=============================================================================
321 //=============================================================================
322 void GEOM_Function::SetRealArray (int thePosition,
323 const Handle(TColStd_HArray1OfReal)& theArray)
326 if(thePosition <= 0) return;
327 TDF_Label anArgLabel = ARGUMENT(thePosition);
328 Handle(TDataStd_RealArray) anAttr =
329 TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
330 anAttr->ChangeArray(theArray);
334 //=============================================================================
338 //=============================================================================
339 double GEOM_Function::GetReal(int thePosition)
342 if(thePosition <= 0) return 0.0;
343 Handle(TDataStd_Real) aReal;
344 TDF_Label anArgLabel = ARGUMENT(thePosition);
345 if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
351 //=============================================================================
355 //=============================================================================
356 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
359 if(thePosition <= 0) return NULL;
360 Handle(TDataStd_RealArray) aRealArray;
361 TDF_Label anArgLabel = ARGUMENT(thePosition);
362 if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
365 return aRealArray->Array();
368 //=============================================================================
372 //=============================================================================
373 void GEOM_Function::SetInteger(int thePosition, int theValue)
376 if(thePosition <= 0) return;
377 TDF_Label anArgLabel = ARGUMENT(thePosition);
378 TDataStd_Integer::Set(anArgLabel, theValue);
382 //=============================================================================
386 //=============================================================================
387 void GEOM_Function::SetIntegerArray (int thePosition,
388 const Handle(TColStd_HArray1OfInteger)& theArray)
391 if(thePosition <= 0) return;
392 TDF_Label anArgLabel = ARGUMENT(thePosition);
393 Handle(TDataStd_IntegerArray) anAttr =
394 TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
395 anAttr->ChangeArray(theArray);
399 //=============================================================================
403 //=============================================================================
404 int GEOM_Function::GetInteger(int thePosition)
407 if(thePosition <= 0) return 0;
408 Handle(TDataStd_Integer) anInteger;
409 TDF_Label anArgLabel = ARGUMENT(thePosition);
410 if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
413 return anInteger->Get();
416 //=============================================================================
420 //=============================================================================
421 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
424 if(thePosition <= 0) return 0;
425 Handle(TDataStd_IntegerArray) anIntegerArray;
426 TDF_Label anArgLabel = ARGUMENT(thePosition);
427 if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
430 return anIntegerArray->Array();
433 //=============================================================================
437 //=============================================================================
438 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
441 if(thePosition <= 0) return;
442 TDF_Label anArgLabel = ARGUMENT(thePosition);
443 TDataStd_Comment::Set(anArgLabel, theValue);
447 //=============================================================================
451 //=============================================================================
452 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
455 TCollection_AsciiString aRes;
456 if(thePosition <= 0) return aRes;
457 Handle(TDataStd_Comment) aString;
458 TDF_Label anArgLabel = ARGUMENT(thePosition);
459 if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
462 aRes = TCollection_AsciiString(aString->Get());
466 //=============================================================================
470 //=============================================================================
471 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
474 if(thePosition <= 0) return;
475 if(theReference.IsNull()) return;
476 TDF_Label anArgLabel = ARGUMENT(thePosition);
477 TDF_Reference::Set(anArgLabel, theReference->GetEntry());
478 TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
483 //=============================================================================
487 //=============================================================================
488 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
491 if(thePosition <= 0) return NULL;
492 TDF_Label anArgLabel = ARGUMENT(thePosition);
493 Handle(TDF_Reference) aRef;
494 if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
497 return GetFunction(aRef->Get());
501 //=============================================================================
505 //=============================================================================
506 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
509 if(thePosition <= 0 || theArray.IsNull()) return;
510 TDF_Label anArgLabel = ARGUMENT(thePosition);
512 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
513 anArray->ChangeArray(theArray);
514 anArgLabel.AddAttribute(anArray);
520 //=============================================================================
524 //=============================================================================
525 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
528 if(thePosition <= 0) return NULL;
529 TDF_Label anArgLabel = ARGUMENT(thePosition);
530 Handle(TDataStd_ExtStringArray) anArray;
531 if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
534 return anArray->Array();
537 //=======================================================================
538 //function : GetReferencesTreeID
540 //=======================================================================
541 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
543 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
544 return aReferencesTreeID;
547 //=============================================================================
551 //=============================================================================
552 void GEOM_Function::SetReferenceList (int thePosition,
553 const Handle(TColStd_HSequenceOfTransient)& theRefList)
556 if(thePosition <= 0) return;
558 // parent label for the list of references
559 TDF_Label anArgLabel = ARGUMENT(thePosition);
560 anArgLabel.ForgetAllAttributes();
562 // set TreeNode on the parent label
563 Handle(TDataStd_TreeNode) aRoot, aNode;
564 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
566 // store references on sub-labels of the parent label
567 Handle(GEOM_Function) aFunc;
568 Standard_Integer ind, len = theRefList->Length();
569 for (ind = 1; ind <= len; ind++) {
570 aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
571 if (aFunc.IsNull()) continue;
572 TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
573 TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
574 TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
576 // set TreeNode on the child label
577 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
578 aRoot->Append(aNode);
585 //=============================================================================
589 //=============================================================================
590 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
592 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
594 if(thePosition <= 0) return aResult;
596 // parent label for the list of references
597 TDF_Label anArgLabel = ARGUMENT(thePosition);
598 Handle(TDF_Reference) aRef;
600 // get TreeNode on the parent label
601 Handle(TDataStd_TreeNode) aRoot, aNode;
602 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
605 // get references, stored on sub-labels of the parent label
607 TDataStd_ChildNodeIterator anIter (aRoot);
608 for (; anIter.More(); anIter.Next()) {
609 aNode = anIter.Value();
610 aLabel_i = aNode->Label();
611 if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
612 Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
613 if (aFunc_i.IsNull()) continue;
614 aResult->Append(aFunc_i);
621 //=============================================================================
625 //=============================================================================
626 //void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
629 // if(thePosition <= 0 || theShape.IsNull()) return;
631 // TDF_Label anArgLabel = ARGUMENT(thePosition);
632 // TNaming_Builder aBuilder(anArgLabel);
633 // aBuilder.Generated(theShape);
639 //=============================================================================
643 //=============================================================================
644 //TopoDS_Shape GEOM_Function::GetShape(int thePosition)
647 // TopoDS_Shape aShape;
648 // if(thePosition <= 0) return aShape;
650 // TDF_Label anArgLabel = ARGUMENT(thePosition);
651 // Handle(TNaming_NamedShape) aNS;
652 // if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
654 // aShape = aNS->Get();
660 //=============================================================================
664 //=============================================================================
665 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
667 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
668 for(; anIterator.More(); anIterator.Next()) {
669 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
673 //=============================================================================
677 //=============================================================================
678 TDF_Label GEOM_Function::GetHistoryEntry (const Standard_Boolean create)
680 return _label.FindChild(HISTORY_LABEL, create);
683 //=============================================================================
685 * GetArgumentHistoryEntry
687 //=============================================================================
688 TDF_Label GEOM_Function::GetArgumentHistoryEntry (const TDF_Label& theArgumentRefEntry,
689 const Standard_Boolean create)
691 TColStd_ListOfInteger anArgumentRefTags;
692 TDF_Tool::TagList(theArgumentRefEntry, anArgumentRefTags);
693 Standard_Integer anArgumentRefLabelPos = anArgumentRefTags.Extent();
695 TDF_Label aHistoryLabel = GetHistoryEntry(create);
696 if (aHistoryLabel.IsNull())
697 return aHistoryLabel;
698 Standard_Integer aHistoryLabelPos = aHistoryLabel.Depth() + 1;
700 Standard_Integer itag;
701 TDF_Label aHistoryCurLabel = aHistoryLabel;
702 TColStd_ListIteratorOfListOfInteger aListIter (anArgumentRefTags);
703 for (itag = 1; itag <= aHistoryLabelPos; itag++) {
706 for (; itag <= anArgumentRefLabelPos; itag++) {
707 aHistoryCurLabel = aHistoryCurLabel.FindChild(aListIter.Value(), create);
708 if (aHistoryCurLabel.IsNull())
709 return aHistoryCurLabel;
713 return aHistoryCurLabel;
716 //=======================================================================
717 //function : GEOM_Function_Type_
719 //=======================================================================
720 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
723 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
724 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
725 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
726 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
729 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
730 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
731 sizeof(GEOM_Function),
733 (Standard_Address)_Ancestors,
734 (Standard_Address)NULL);
739 //=======================================================================
740 //function : DownCast
742 //=======================================================================
744 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
746 Handle(GEOM_Function) _anOtherObject;
748 if (!AnObject.IsNull()) {
749 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
750 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
754 return _anOtherObject ;