3 #include "GEOM_Function.hxx"
4 #include "GEOM_Object.hxx"
5 #include "GEOM_Solver.hxx"
10 #include <TDF_Data.hxx>
11 #include <TDF_ChildIterator.hxx>
12 #include <TDF_Reference.hxx>
13 #include <TDataStd_Integer.hxx>
14 #include <TDataStd_IntegerArray.hxx>
15 #include <TDataStd_Real.hxx>
16 #include <TDataStd_RealArray.hxx>
17 #include <TDataStd_Comment.hxx>
18 #include <TDataStd_TreeNode.hxx>
19 #include <TDataStd_UAttribute.hxx>
20 #include <TDataStd_ChildNodeIterator.hxx>
21 #include <TDataStd_ExtStringArray.hxx>
22 #include <TDocStd_Owner.hxx>
23 #include <TDocStd_Document.hxx>
24 #include <TFunction_Function.hxx>
25 #include <TNaming_NamedShape.hxx>
26 #include <TNaming_Builder.hxx>
28 #include <TColStd_HArray1OfReal.hxx>
29 #include <TColStd_HArray1OfInteger.hxx>
30 #include <TColStd_HSequenceOfTransient.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_ExtendedString.hxx>
34 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
36 #define ARGUMENT_LABEL 1
37 #define RESULT_LABEL 2
38 #define DESCRIPTION_LABEL 3
39 #define ARGUMENTS _label.FindChild((ARGUMENT_LABEL))
40 #define ARGUMENT(thePosition) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePosition))
41 #define SUB_ARGUMENT(thePos1, thePos2) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePos1)).FindChild((thePos2))
43 //=======================================================================
44 //function : GetFunctionTreeID
46 //=======================================================================
47 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
49 static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
50 return aFunctionTreeID;
54 //=======================================================================
55 //function : GetDependencyID
57 //=======================================================================
58 const Standard_GUID& GEOM_Function::GetDependencyID()
60 static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
64 //=============================================================================
68 //=============================================================================
69 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
71 if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
73 return new GEOM_Function(theEntry);
76 //=============================================================================
80 //=============================================================================
81 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
84 TFunction_Function::Set(theEntry, theGUID);
85 TDataStd_Integer::Set(theEntry, theType);
87 //Add function to a function tree
88 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theEntry.Data());
89 Handle(TDataStd_TreeNode) aRoot, aNode;
90 if(!aDoc->Main().FindAttribute(GetFunctionTreeID(), aRoot))
91 aRoot = TDataStd_TreeNode::Set(aDoc->Main(), GetFunctionTreeID());
93 aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
97 //=============================================================================
101 //=============================================================================
102 TDF_Label GEOM_Function::GetOwnerEntry()
104 TDF_Label aFather = _label.Father();
105 while(!aFather.IsRoot()) {
106 if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
107 aFather = aFather.Father();
113 //=============================================================================
117 //=============================================================================
118 int GEOM_Function::GetType()
121 Handle(TDataStd_Integer) aType;
122 if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
127 //=============================================================================
131 //=============================================================================
132 TopoDS_Shape GEOM_Function::GetValue()
137 TDF_Label aLabel = GetOwnerEntry();
138 if(aLabel.IsRoot()) return aShape;
139 Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
140 if(anObject.IsNull()) return aShape;
141 if(!anObject->IsMainShape()) {
143 GEOM_Solver aSolver(GEOM_Engine::GetEngine());
144 if (!aSolver.ComputeFunction(this)) {
145 MESSAGE("GEOM_Object::GetValue Error : Can't build a sub shape");
149 catch (Standard_Failure) {
150 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
151 MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
156 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
157 Handle(TNaming_NamedShape) aNS;
158 if(!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
166 //=============================================================================
170 //=============================================================================
171 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
174 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
175 TNaming_Builder aBuilder(aResultLabel);
177 aBuilder.Generated(theShape);
182 //=============================================================================
186 //=============================================================================
187 Standard_GUID GEOM_Function::GetDriverGUID()
189 Handle(TFunction_Function) aFunction;
190 if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
191 return TDF::LowestID();
194 return aFunction->GetDriverGUID();
197 //=============================================================================
201 //=============================================================================
202 TCollection_AsciiString GEOM_Function::GetDescription()
204 Handle(TDataStd_Comment) aComment;
205 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
206 if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return TCollection_AsciiString();
207 TCollection_AsciiString aDescr(aComment->Get());
211 //=============================================================================
215 //=============================================================================
216 void GEOM_Function::SetDescription(TCollection_AsciiString& theDescription)
218 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
219 Handle(TDataStd_Comment) aComment = TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
222 //=============================================================================
226 //=============================================================================
227 void GEOM_Function::SetReal(int thePosition, double theValue)
230 if(thePosition <= 0) return;
231 TDF_Label anArgLabel = ARGUMENT(thePosition);
232 TDataStd_Real::Set(anArgLabel, theValue);
236 //=============================================================================
240 //=============================================================================
241 void GEOM_Function::SetRealArray (int thePosition,
242 const Handle(TColStd_HArray1OfReal)& theArray)
245 if(thePosition <= 0) return;
246 TDF_Label anArgLabel = ARGUMENT(thePosition);
247 Handle(TDataStd_RealArray) anAttr =
248 TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
249 anAttr->ChangeArray(theArray);
253 //=============================================================================
257 //=============================================================================
258 double GEOM_Function::GetReal(int thePosition)
261 if(thePosition <= 0) return 0.0;
262 Handle(TDataStd_Real) aReal;
263 TDF_Label anArgLabel = ARGUMENT(thePosition);
264 if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
270 //=============================================================================
274 //=============================================================================
275 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
278 if(thePosition <= 0) return NULL;
279 Handle(TDataStd_RealArray) aRealArray;
280 TDF_Label anArgLabel = ARGUMENT(thePosition);
281 if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
284 return aRealArray->Array();
287 //=============================================================================
291 //=============================================================================
292 void GEOM_Function::SetInteger(int thePosition, int theValue)
295 if(thePosition <= 0) return;
296 TDF_Label anArgLabel = ARGUMENT(thePosition);
297 TDataStd_Integer::Set(anArgLabel, theValue);
301 //=============================================================================
305 //=============================================================================
306 void GEOM_Function::SetIntegerArray (int thePosition,
307 const Handle(TColStd_HArray1OfInteger)& theArray)
310 if(thePosition <= 0) return;
311 TDF_Label anArgLabel = ARGUMENT(thePosition);
312 Handle(TDataStd_IntegerArray) anAttr =
313 TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
314 anAttr->ChangeArray(theArray);
318 //=============================================================================
322 //=============================================================================
323 int GEOM_Function::GetInteger(int thePosition)
326 if(thePosition <= 0) return 0;
327 Handle(TDataStd_Integer) anInteger;
328 TDF_Label anArgLabel = ARGUMENT(thePosition);
329 if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
332 return anInteger->Get();
335 //=============================================================================
339 //=============================================================================
340 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
343 if(thePosition <= 0) return 0;
344 Handle(TDataStd_IntegerArray) anIntegerArray;
345 TDF_Label anArgLabel = ARGUMENT(thePosition);
346 if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
349 return anIntegerArray->Array();
352 //=============================================================================
356 //=============================================================================
357 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
360 if(thePosition <= 0) return;
361 TDF_Label anArgLabel = ARGUMENT(thePosition);
362 TDataStd_Comment::Set(anArgLabel, theValue);
366 //=============================================================================
370 //=============================================================================
371 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
374 TCollection_AsciiString aRes;
375 if(thePosition <= 0) return aRes;
376 Handle(TDataStd_Comment) aString;
377 TDF_Label anArgLabel = ARGUMENT(thePosition);
378 if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
381 aRes = TCollection_AsciiString(aString->Get());
385 //=============================================================================
389 //=============================================================================
390 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
393 if(thePosition <= 0) return;
394 if(theReference.IsNull()) return;
395 TDF_Label anArgLabel = ARGUMENT(thePosition);
396 TDF_Reference::Set(anArgLabel, theReference->GetEntry());
397 TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
402 //=============================================================================
406 //=============================================================================
407 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
410 if(thePosition <= 0) return NULL;
411 TDF_Label anArgLabel = ARGUMENT(thePosition);
412 Handle(TDF_Reference) aRef;
413 if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
416 return GetFunction(aRef->Get());
420 //=============================================================================
424 //=============================================================================
425 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
428 if(thePosition <= 0 || theArray.IsNull()) return;
429 TDF_Label anArgLabel = ARGUMENT(thePosition);
431 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
432 anArray->ChangeArray(theArray);
433 anArgLabel.AddAttribute(anArray);
439 //=============================================================================
443 //=============================================================================
444 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
447 if(thePosition <= 0) return NULL;
448 TDF_Label anArgLabel = ARGUMENT(thePosition);
449 Handle(TDataStd_ExtStringArray) anArray;
450 if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
453 return anArray->Array();
456 //=======================================================================
457 //function : GetReferencesTreeID
459 //=======================================================================
460 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
462 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
463 return aReferencesTreeID;
466 //=============================================================================
470 //=============================================================================
471 void GEOM_Function::SetReferenceList (int thePosition,
472 const Handle(TColStd_HSequenceOfTransient)& theRefList)
475 if(thePosition <= 0) return;
477 // parent label for the list of references
478 TDF_Label anArgLabel = ARGUMENT(thePosition);
479 anArgLabel.ForgetAllAttributes();
481 // set TreeNode on the parent label
482 Handle(TDataStd_TreeNode) aRoot, aNode;
483 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
485 // store references on sub-labels of the parent label
486 Handle(GEOM_Function) aFunc;
487 Standard_Integer ind, len = theRefList->Length();
488 for (ind = 1; ind <= len; ind++) {
489 aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
490 if (aFunc.IsNull()) continue;
491 TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
492 TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
493 TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
495 // set TreeNode on the child label
496 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
497 aRoot->Append(aNode);
504 //=============================================================================
508 //=============================================================================
509 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
511 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
513 if(thePosition <= 0) return aResult;
515 // parent label for the list of references
516 TDF_Label anArgLabel = ARGUMENT(thePosition);
517 Handle(TDF_Reference) aRef;
519 // get TreeNode on the parent label
520 Handle(TDataStd_TreeNode) aRoot, aNode;
521 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
524 // get references, stored on sub-labels of the parent label
526 TDataStd_ChildNodeIterator anIter (aRoot);
527 for (; anIter.More(); anIter.Next()) {
528 aNode = anIter.Value();
529 aLabel_i = aNode->Label();
530 if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
531 Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
532 if (aFunc_i.IsNull()) continue;
533 aResult->Append(aFunc_i);
540 //=============================================================================
544 //=============================================================================
545 void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
548 if(thePosition <= 0 || theShape.IsNull()) return;
550 TDF_Label anArgLabel = ARGUMENT(thePosition);
551 TNaming_Builder aBuilder(anArgLabel);
552 aBuilder.Generated(theShape);
558 //=============================================================================
562 //=============================================================================
563 TopoDS_Shape GEOM_Function::GetShape(int thePosition)
567 if(thePosition <= 0) return aShape;
569 TDF_Label anArgLabel = ARGUMENT(thePosition);
570 Handle(TNaming_NamedShape) aNS;
571 if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
579 //=============================================================================
583 //=============================================================================
584 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
586 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
587 for(; anIterator.More(); anIterator.Next()) {
588 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
592 //=======================================================================
593 //function : GEOM_Function_Type_
595 //=======================================================================
596 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
599 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
600 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
601 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
602 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
605 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
606 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
607 sizeof(GEOM_Function),
609 (Standard_Address)_Ancestors,
610 (Standard_Address)NULL);
615 //=======================================================================
616 //function : DownCast
618 //=======================================================================
620 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
622 Handle(GEOM_Function) _anOtherObject;
624 if (!AnObject.IsNull()) {
625 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
626 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
630 return _anOtherObject ;