1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOM_Function.hxx>
24 #include <GEOM_Object.hxx>
25 #include <GEOM_Solver.hxx>
27 #include "utilities.h"
30 #include <TDF_Tool.hxx>
31 #include <TDF_Data.hxx>
32 #include <TDF_ChildIterator.hxx>
33 #include <TDF_Reference.hxx>
34 #include <TDataStd_Integer.hxx>
35 #include <TDataStd_IntegerArray.hxx>
36 #include <TDataStd_Real.hxx>
37 #include <TDataStd_RealArray.hxx>
38 #include <TDataStd_Comment.hxx>
39 #include <TDataStd_TreeNode.hxx>
40 #include <TDataStd_UAttribute.hxx>
41 #include <TDataStd_ChildNodeIterator.hxx>
42 #include <TDataStd_ExtStringArray.hxx>
43 #include <TDocStd_Owner.hxx>
44 #include <TDocStd_Document.hxx>
45 #include <TFunction_Function.hxx>
46 #include <TNaming_NamedShape.hxx>
47 #include <TNaming_Builder.hxx>
49 #include <TColStd_ListOfInteger.hxx>
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <TColStd_HArray1OfReal.hxx>
52 #include <TColStd_HArray1OfInteger.hxx>
53 #include <TColStd_HSequenceOfTransient.hxx>
54 #include <TCollection_AsciiString.hxx>
55 #include <TCollection_ExtendedString.hxx>
57 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
59 #define ARGUMENT_LABEL 1
60 #define RESULT_LABEL 2
61 #define DESCRIPTION_LABEL 3
62 #define HISTORY_LABEL 4
64 #define ARGUMENTS _label.FindChild((ARGUMENT_LABEL))
65 #define ARGUMENT(thePosition) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePosition))
66 #define SUB_ARGUMENT(thePos1, thePos2) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePos1)).FindChild((thePos2))
68 //=======================================================================
69 //function : GetFunctionTreeID
71 //=======================================================================
72 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
74 static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
75 return aFunctionTreeID;
79 //=======================================================================
80 //function : GetDependencyID
82 //=======================================================================
83 const Standard_GUID& GEOM_Function::GetDependencyID()
85 static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
89 //=============================================================================
93 //=============================================================================
94 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
96 if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
98 return new GEOM_Function(theEntry);
101 //=============================================================================
105 //=============================================================================
106 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
109 TFunction_Function::Set(theEntry, theGUID);
110 TDataStd_Integer::Set(theEntry, theType);
112 //Add function to a function tree
113 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theEntry.Data());
114 Handle(TDataStd_TreeNode) aRoot, aNode;
115 if(!aDoc->Main().FindAttribute(GetFunctionTreeID(), aRoot))
116 aRoot = TDataStd_TreeNode::Set(aDoc->Main(), GetFunctionTreeID());
118 aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
119 aRoot->Append(aNode);
122 //=============================================================================
126 //=============================================================================
127 TDF_Label GEOM_Function::GetOwnerEntry()
129 TDF_Label aFather = _label.Father();
130 while(!aFather.IsRoot()) {
131 if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
132 aFather = aFather.Father();
138 //=============================================================================
142 //=============================================================================
143 int GEOM_Function::GetType()
146 Handle(TDataStd_Integer) aType;
147 if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
152 //=============================================================================
156 //=============================================================================
157 TopoDS_Shape GEOM_Function::GetValue()
162 TDF_Label aLabel = GetOwnerEntry();
163 if(aLabel.IsRoot()) return aShape;
164 Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
165 if(anObject.IsNull()) return aShape;
166 if(!anObject->IsMainShape()) {
168 GEOM_Solver aSolver(GEOM_Engine::GetEngine());
169 if (!aSolver.ComputeFunction(this)) {
170 MESSAGE("GEOM_Object::GetValue Error : Can't build a sub shape");
174 catch (Standard_Failure) {
175 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
176 MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
181 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
182 Handle(TNaming_NamedShape) aNS;
183 if(!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
191 //=============================================================================
195 //=============================================================================
196 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
199 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
200 TNaming_Builder aBuilder(aResultLabel);
202 aBuilder.Generated(theShape);
207 //=============================================================================
211 //=============================================================================
212 Standard_GUID GEOM_Function::GetDriverGUID()
214 Handle(TFunction_Function) aFunction;
215 if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
216 return TDF::LowestID();
219 return aFunction->GetDriverGUID();
222 //=============================================================================
226 //=============================================================================
227 TCollection_AsciiString GEOM_Function::GetDescription()
229 Handle(TDataStd_Comment) aComment;
230 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
231 if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return TCollection_AsciiString();
232 TCollection_AsciiString aDescr(aComment->Get());
236 //=============================================================================
240 //=============================================================================
241 void GEOM_Function::SetDescription(const TCollection_AsciiString& theDescription)
243 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
244 Handle(TDataStd_Comment) aComment =
245 TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
248 //=============================================================================
252 //=============================================================================
253 void GEOM_Function::SetReal(int thePosition, double theValue)
256 if(thePosition <= 0) return;
257 TDF_Label anArgLabel = ARGUMENT(thePosition);
258 TDataStd_Real::Set(anArgLabel, theValue);
262 //=============================================================================
266 //=============================================================================
267 void GEOM_Function::SetRealArray (int thePosition,
268 const Handle(TColStd_HArray1OfReal)& theArray)
271 if(thePosition <= 0) return;
272 TDF_Label anArgLabel = ARGUMENT(thePosition);
273 Handle(TDataStd_RealArray) anAttr =
274 TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
275 anAttr->ChangeArray(theArray);
279 //=============================================================================
283 //=============================================================================
284 double GEOM_Function::GetReal(int thePosition)
287 if(thePosition <= 0) return 0.0;
288 Handle(TDataStd_Real) aReal;
289 TDF_Label anArgLabel = ARGUMENT(thePosition);
290 if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
296 //=============================================================================
300 //=============================================================================
301 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
304 if(thePosition <= 0) return NULL;
305 Handle(TDataStd_RealArray) aRealArray;
306 TDF_Label anArgLabel = ARGUMENT(thePosition);
307 if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
310 return aRealArray->Array();
313 //=============================================================================
317 //=============================================================================
318 void GEOM_Function::SetInteger(int thePosition, int theValue)
321 if(thePosition <= 0) return;
322 TDF_Label anArgLabel = ARGUMENT(thePosition);
323 TDataStd_Integer::Set(anArgLabel, theValue);
327 //=============================================================================
331 //=============================================================================
332 void GEOM_Function::SetIntegerArray (int thePosition,
333 const Handle(TColStd_HArray1OfInteger)& theArray)
336 if(thePosition <= 0) return;
337 TDF_Label anArgLabel = ARGUMENT(thePosition);
338 Handle(TDataStd_IntegerArray) anAttr =
339 TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
340 anAttr->ChangeArray(theArray);
344 //=============================================================================
348 //=============================================================================
349 int GEOM_Function::GetInteger(int thePosition)
352 if(thePosition <= 0) return 0;
353 Handle(TDataStd_Integer) anInteger;
354 TDF_Label anArgLabel = ARGUMENT(thePosition);
355 if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
358 return anInteger->Get();
361 //=============================================================================
365 //=============================================================================
366 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
369 if(thePosition <= 0) return 0;
370 Handle(TDataStd_IntegerArray) anIntegerArray;
371 TDF_Label anArgLabel = ARGUMENT(thePosition);
372 if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
375 return anIntegerArray->Array();
378 //=============================================================================
382 //=============================================================================
383 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
386 if(thePosition <= 0) return;
387 TDF_Label anArgLabel = ARGUMENT(thePosition);
388 TDataStd_Comment::Set(anArgLabel, theValue);
392 //=============================================================================
396 //=============================================================================
397 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
400 TCollection_AsciiString aRes;
401 if(thePosition <= 0) return aRes;
402 Handle(TDataStd_Comment) aString;
403 TDF_Label anArgLabel = ARGUMENT(thePosition);
404 if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
407 aRes = TCollection_AsciiString(aString->Get());
411 //=============================================================================
415 //=============================================================================
416 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
419 if(thePosition <= 0) return;
420 if(theReference.IsNull()) return;
421 TDF_Label anArgLabel = ARGUMENT(thePosition);
422 TDF_Reference::Set(anArgLabel, theReference->GetEntry());
423 TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
428 //=============================================================================
432 //=============================================================================
433 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
436 if(thePosition <= 0) return NULL;
437 TDF_Label anArgLabel = ARGUMENT(thePosition);
438 Handle(TDF_Reference) aRef;
439 if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
442 return GetFunction(aRef->Get());
446 //=============================================================================
450 //=============================================================================
451 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
454 if(thePosition <= 0 || theArray.IsNull()) return;
455 TDF_Label anArgLabel = ARGUMENT(thePosition);
457 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
458 anArray->ChangeArray(theArray);
459 anArgLabel.AddAttribute(anArray);
465 //=============================================================================
469 //=============================================================================
470 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
473 if(thePosition <= 0) return NULL;
474 TDF_Label anArgLabel = ARGUMENT(thePosition);
475 Handle(TDataStd_ExtStringArray) anArray;
476 if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
479 return anArray->Array();
482 //=======================================================================
483 //function : GetReferencesTreeID
485 //=======================================================================
486 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
488 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
489 return aReferencesTreeID;
492 //=============================================================================
496 //=============================================================================
497 void GEOM_Function::SetReferenceList (int thePosition,
498 const Handle(TColStd_HSequenceOfTransient)& theRefList)
501 if(thePosition <= 0) return;
503 // parent label for the list of references
504 TDF_Label anArgLabel = ARGUMENT(thePosition);
505 anArgLabel.ForgetAllAttributes();
507 // set TreeNode on the parent label
508 Handle(TDataStd_TreeNode) aRoot, aNode;
509 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
511 // store references on sub-labels of the parent label
512 Handle(GEOM_Function) aFunc;
513 Standard_Integer ind, len = theRefList->Length();
514 for (ind = 1; ind <= len; ind++) {
515 aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
516 if (aFunc.IsNull()) continue;
517 TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
518 TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
519 TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
521 // set TreeNode on the child label
522 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
523 aRoot->Append(aNode);
530 //=============================================================================
534 //=============================================================================
535 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
537 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
539 if(thePosition <= 0) return aResult;
541 // parent label for the list of references
542 TDF_Label anArgLabel = ARGUMENT(thePosition);
543 Handle(TDF_Reference) aRef;
545 // get TreeNode on the parent label
546 Handle(TDataStd_TreeNode) aRoot, aNode;
547 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
550 // get references, stored on sub-labels of the parent label
552 TDataStd_ChildNodeIterator anIter (aRoot);
553 for (; anIter.More(); anIter.Next()) {
554 aNode = anIter.Value();
555 aLabel_i = aNode->Label();
556 if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
557 Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
558 if (aFunc_i.IsNull()) continue;
559 aResult->Append(aFunc_i);
566 //=============================================================================
570 //=============================================================================
571 //void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
574 // if(thePosition <= 0 || theShape.IsNull()) return;
576 // TDF_Label anArgLabel = ARGUMENT(thePosition);
577 // TNaming_Builder aBuilder(anArgLabel);
578 // aBuilder.Generated(theShape);
584 //=============================================================================
588 //=============================================================================
589 //TopoDS_Shape GEOM_Function::GetShape(int thePosition)
592 // TopoDS_Shape aShape;
593 // if(thePosition <= 0) return aShape;
595 // TDF_Label anArgLabel = ARGUMENT(thePosition);
596 // Handle(TNaming_NamedShape) aNS;
597 // if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
599 // aShape = aNS->Get();
605 //=============================================================================
609 //=============================================================================
610 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
612 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
613 for(; anIterator.More(); anIterator.Next()) {
614 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
618 //=============================================================================
622 //=============================================================================
623 TDF_Label GEOM_Function::GetHistoryEntry (const Standard_Boolean create)
625 return _label.FindChild(HISTORY_LABEL, create);
628 //=============================================================================
630 * GetArgumentHistoryEntry
632 //=============================================================================
633 TDF_Label GEOM_Function::GetArgumentHistoryEntry (const TDF_Label& theArgumentRefEntry,
634 const Standard_Boolean create)
636 TColStd_ListOfInteger anArgumentRefTags;
637 TDF_Tool::TagList(theArgumentRefEntry, anArgumentRefTags);
638 Standard_Integer anArgumentRefLabelPos = anArgumentRefTags.Extent();
640 TDF_Label aHistoryLabel = GetHistoryEntry(create);
641 if (aHistoryLabel.IsNull())
642 return aHistoryLabel;
643 Standard_Integer aHistoryLabelPos = aHistoryLabel.Depth() + 1;
645 Standard_Integer itag;
646 TDF_Label aHistoryCurLabel = aHistoryLabel;
647 TColStd_ListIteratorOfListOfInteger aListIter (anArgumentRefTags);
648 for (itag = 1; itag <= aHistoryLabelPos; itag++) {
651 for (; itag <= anArgumentRefLabelPos; itag++) {
652 aHistoryCurLabel = aHistoryCurLabel.FindChild(aListIter.Value(), create);
653 if (aHistoryCurLabel.IsNull())
654 return aHistoryCurLabel;
658 return aHistoryCurLabel;
661 //=======================================================================
662 //function : GEOM_Function_Type_
664 //=======================================================================
665 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
668 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
669 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
670 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
671 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
674 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
675 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
676 sizeof(GEOM_Function),
678 (Standard_Address)_Ancestors,
679 (Standard_Address)NULL);
684 //=======================================================================
685 //function : DownCast
687 //=======================================================================
689 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
691 Handle(GEOM_Function) _anOtherObject;
693 if (!AnObject.IsNull()) {
694 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
695 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
699 return _anOtherObject ;