3 #include "GEOM_Function.hxx"
4 #include "GEOM_Object.hxx"
5 #include "GEOM_Solver.hxx"
7 #include <TDF_Data.hxx>
8 #include <TDF_ChildIterator.hxx>
9 #include <TDF_Reference.hxx>
10 #include <TDataStd_Integer.hxx>
11 #include <TDataStd_IntegerArray.hxx>
12 #include <TDataStd_Real.hxx>
13 #include <TDataStd_RealArray.hxx>
14 #include <TDataStd_Comment.hxx>
15 #include <TDataStd_TreeNode.hxx>
16 #include <TDataStd_UAttribute.hxx>
17 #include <TDataStd_ChildNodeIterator.hxx>
18 #include <TDataStd_ExtStringArray.hxx>
19 #include <TDocStd_Owner.hxx>
20 #include <TDocStd_Document.hxx>
21 #include <TFunction_Function.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <TNaming_Builder.hxx>
25 #include <TColStd_HArray1OfReal.hxx>
26 #include <TColStd_HArray1OfInteger.hxx>
27 #include <TColStd_HSequenceOfTransient.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <TCollection_ExtendedString.hxx>
31 #include "utilities.h"
33 #define ARGUMENT_LABEL 1
34 #define RESULT_LABEL 2
35 #define DESCRIPTION_LABEL 3
36 #define ARGUMENTS _label.FindChild((ARGUMENT_LABEL))
37 #define ARGUMENT(thePosition) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePosition))
38 #define SUB_ARGUMENT(thePos1, thePos2) _label.FindChild((ARGUMENT_LABEL)).FindChild((thePos1)).FindChild((thePos2))
40 //=======================================================================
41 //function : GetFunctionTreeID
43 //=======================================================================
44 const Standard_GUID& GEOM_Function::GetFunctionTreeID()
46 static Standard_GUID aFunctionTreeID("FF1BBB00-5D14-4df2-980B-3A668264EA16");
47 return aFunctionTreeID;
51 //=======================================================================
52 //function : GetDependencyID
54 //=======================================================================
55 const Standard_GUID& GEOM_Function::GetDependencyID()
57 static Standard_GUID aDependencyID("E2620650-2354-41bd-8C2C-210CFCD00948");
61 //=============================================================================
65 //=============================================================================
66 Handle(GEOM_Function) GEOM_Function::GetFunction(const TDF_Label& theEntry)
68 if(!theEntry.IsAttribute(TFunction_Function::GetID())) return NULL;
70 return new GEOM_Function(theEntry);
73 //=============================================================================
77 //=============================================================================
78 GEOM_Function::GEOM_Function(const TDF_Label& theEntry, const Standard_GUID& theGUID, int theType)
81 TFunction_Function::Set(theEntry, theGUID);
82 TDataStd_Integer::Set(theEntry, theType);
84 //Add function to a function tree
85 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theEntry.Data());
86 Handle(TDataStd_TreeNode) aRoot, aNode;
87 if(!aDoc->Main().FindAttribute(GetFunctionTreeID(), aRoot))
88 aRoot = TDataStd_TreeNode::Set(aDoc->Main(), GetFunctionTreeID());
90 aNode = TDataStd_TreeNode::Set(theEntry, GetFunctionTreeID());
94 //=============================================================================
98 //=============================================================================
99 TDF_Label GEOM_Function::GetOwnerEntry()
101 TDF_Label aFather = _label.Father();
102 while(!aFather.IsRoot()) {
103 if(aFather.IsAttribute(GEOM_Object::GetObjectID())) return aFather;
104 aFather = aFather.Father();
110 //=============================================================================
114 //=============================================================================
115 int GEOM_Function::GetType()
118 Handle(TDataStd_Integer) aType;
119 if(!_label.FindAttribute(TDataStd_Integer::GetID(), aType)) return 0;
124 //=============================================================================
128 //=============================================================================
129 TopoDS_Shape GEOM_Function::GetValue()
134 TDF_Label aLabel = GetOwnerEntry();
135 if(aLabel.IsRoot()) return aShape;
136 Handle(GEOM_Object) anObject = GEOM_Object::GetObject(aLabel);
137 if(anObject.IsNull()) return aShape;
138 if(!anObject->IsMainShape()) {
140 GEOM_Solver aSolver(GEOM_Engine::GetEngine());
141 if (!aSolver.ComputeFunction(this)) {
142 MESSAGE("GEOM_Object::GetValue Error : Can't build a sub shape");
146 catch (Standard_Failure) {
147 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
148 MESSAGE("GEOM_Function::GetValue Error: " << aFail->GetMessageString());
153 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
154 Handle(TNaming_NamedShape) aNS;
155 if(!aResultLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
163 //=============================================================================
167 //=============================================================================
168 void GEOM_Function::SetValue(TopoDS_Shape& theShape)
171 TDF_Label aResultLabel = _label.FindChild(RESULT_LABEL);
172 TNaming_Builder aBuilder(aResultLabel);
174 aBuilder.Generated(theShape);
179 //=============================================================================
183 //=============================================================================
184 Standard_GUID GEOM_Function::GetDriverGUID()
186 Handle(TFunction_Function) aFunction;
187 if(!_label.FindAttribute(TFunction_Function::GetID(), aFunction)) {
188 return TDF::LowestID();
191 return aFunction->GetDriverGUID();
194 //=============================================================================
198 //=============================================================================
199 TCollection_AsciiString GEOM_Function::GetDescription()
201 Handle(TDataStd_Comment) aComment;
202 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
203 if(!aChild.FindAttribute(TDataStd_Comment::GetID(), aComment)) return TCollection_AsciiString();
204 TCollection_AsciiString aDescr(aComment->Get());
208 //=============================================================================
212 //=============================================================================
213 void GEOM_Function::SetDescription(TCollection_AsciiString& theDescription)
215 TDF_Label aChild = _label.FindChild(DESCRIPTION_LABEL);
216 Handle(TDataStd_Comment) aComment = TDataStd_Comment::Set(aChild, TCollection_ExtendedString(theDescription));
219 //=============================================================================
223 //=============================================================================
224 void GEOM_Function::SetReal(int thePosition, double theValue)
227 if(thePosition <= 0) return;
228 TDF_Label anArgLabel = ARGUMENT(thePosition);
229 TDataStd_Real::Set(anArgLabel, theValue);
233 //=============================================================================
237 //=============================================================================
238 void GEOM_Function::SetRealArray (int thePosition,
239 const Handle(TColStd_HArray1OfReal)& theArray)
242 if(thePosition <= 0) return;
243 TDF_Label anArgLabel = ARGUMENT(thePosition);
244 Handle(TDataStd_RealArray) anAttr =
245 TDataStd_RealArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
246 anAttr->ChangeArray(theArray);
250 //=============================================================================
254 //=============================================================================
255 double GEOM_Function::GetReal(int thePosition)
258 if(thePosition <= 0) return 0.0;
259 Handle(TDataStd_Real) aReal;
260 TDF_Label anArgLabel = ARGUMENT(thePosition);
261 if(!anArgLabel.FindAttribute(TDataStd_Real::GetID(), aReal)) return 0.0;
267 //=============================================================================
271 //=============================================================================
272 Handle(TColStd_HArray1OfReal) GEOM_Function::GetRealArray(int thePosition)
275 if(thePosition <= 0) return NULL;
276 Handle(TDataStd_RealArray) aRealArray;
277 TDF_Label anArgLabel = ARGUMENT(thePosition);
278 if(!anArgLabel.FindAttribute(TDataStd_RealArray::GetID(), aRealArray)) return NULL;
281 return aRealArray->Array();
284 //=============================================================================
288 //=============================================================================
289 void GEOM_Function::SetInteger(int thePosition, int theValue)
292 if(thePosition <= 0) return;
293 TDF_Label anArgLabel = ARGUMENT(thePosition);
294 TDataStd_Integer::Set(anArgLabel, theValue);
298 //=============================================================================
302 //=============================================================================
303 void GEOM_Function::SetIntegerArray (int thePosition,
304 const Handle(TColStd_HArray1OfInteger)& theArray)
307 if(thePosition <= 0) return;
308 TDF_Label anArgLabel = ARGUMENT(thePosition);
309 Handle(TDataStd_IntegerArray) anAttr =
310 TDataStd_IntegerArray::Set(anArgLabel, theArray->Lower(), theArray->Upper());
311 anAttr->ChangeArray(theArray);
315 //=============================================================================
319 //=============================================================================
320 int GEOM_Function::GetInteger(int thePosition)
323 if(thePosition <= 0) return 0;
324 Handle(TDataStd_Integer) anInteger;
325 TDF_Label anArgLabel = ARGUMENT(thePosition);
326 if(!anArgLabel.FindAttribute(TDataStd_Integer::GetID(), anInteger)) return 0;
329 return anInteger->Get();
332 //=============================================================================
336 //=============================================================================
337 Handle(TColStd_HArray1OfInteger) GEOM_Function::GetIntegerArray(int thePosition)
340 if(thePosition <= 0) return 0;
341 Handle(TDataStd_IntegerArray) anIntegerArray;
342 TDF_Label anArgLabel = ARGUMENT(thePosition);
343 if(!anArgLabel.FindAttribute(TDataStd_IntegerArray::GetID(), anIntegerArray)) return 0;
346 return anIntegerArray->Array();
349 //=============================================================================
353 //=============================================================================
354 void GEOM_Function::SetString(int thePosition, const TCollection_AsciiString& theValue)
357 if(thePosition <= 0) return;
358 TDF_Label anArgLabel = ARGUMENT(thePosition);
359 TDataStd_Comment::Set(anArgLabel, theValue);
363 //=============================================================================
367 //=============================================================================
368 TCollection_AsciiString GEOM_Function::GetString(int thePosition)
371 TCollection_AsciiString aRes;
372 if(thePosition <= 0) return aRes;
373 Handle(TDataStd_Comment) aString;
374 TDF_Label anArgLabel = ARGUMENT(thePosition);
375 if(!anArgLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) return aRes;
378 aRes = TCollection_AsciiString(aString->Get());
382 //=============================================================================
386 //=============================================================================
387 void GEOM_Function::SetReference(int thePosition, Handle(GEOM_Function) theReference)
390 if(thePosition <= 0) return;
391 if(theReference.IsNull()) return;
392 TDF_Label anArgLabel = ARGUMENT(thePosition);
393 TDF_Reference::Set(anArgLabel, theReference->GetEntry());
394 TDataStd_UAttribute::Set(anArgLabel, GetDependencyID());
399 //=============================================================================
403 //=============================================================================
404 Handle(GEOM_Function) GEOM_Function::GetReference(int thePosition)
407 if(thePosition <= 0) return NULL;
408 TDF_Label anArgLabel = ARGUMENT(thePosition);
409 Handle(TDF_Reference) aRef;
410 if(!anArgLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
413 return GetFunction(aRef->Get());
417 //=============================================================================
421 //=============================================================================
422 void GEOM_Function::SetStringArray(int thePosition, const Handle(TColStd_HArray1OfExtendedString)& theArray)
425 if(thePosition <= 0 || theArray.IsNull()) return;
426 TDF_Label anArgLabel = ARGUMENT(thePosition);
428 Handle(TDataStd_ExtStringArray) anArray = new TDataStd_ExtStringArray;
429 anArray->ChangeArray(theArray);
430 anArgLabel.AddAttribute(anArray);
436 //=============================================================================
440 //=============================================================================
441 Handle(TColStd_HArray1OfExtendedString) GEOM_Function::GetStringArray(int thePosition)
444 if(thePosition <= 0) return NULL;
445 TDF_Label anArgLabel = ARGUMENT(thePosition);
446 Handle(TDataStd_ExtStringArray) anArray;
447 if(!anArgLabel.FindAttribute(TDataStd_ExtStringArray::GetID(), anArray)) return NULL;
450 return anArray->Array();
453 //=======================================================================
454 //function : GetReferencesTreeID
456 //=======================================================================
457 const Standard_GUID& GEOM_Function::GetReferencesTreeID()
459 static Standard_GUID aReferencesTreeID("FF1BBB10-5D14-4df2-980B-3A668264EA16");
460 return aReferencesTreeID;
463 //=============================================================================
467 //=============================================================================
468 void GEOM_Function::SetReferenceList (int thePosition,
469 const Handle(TColStd_HSequenceOfTransient)& theRefList)
472 if(thePosition <= 0) return;
474 // parent label for the list of references
475 TDF_Label anArgLabel = ARGUMENT(thePosition);
476 anArgLabel.ForgetAllAttributes();
478 // set TreeNode on the parent label
479 Handle(TDataStd_TreeNode) aRoot, aNode;
480 aRoot = TDataStd_TreeNode::Set(anArgLabel, GetReferencesTreeID());
482 // store references on sub-labels of the parent label
483 Handle(GEOM_Function) aFunc;
484 Standard_Integer ind, len = theRefList->Length();
485 for (ind = 1; ind <= len; ind++) {
486 aFunc = Handle(GEOM_Function)::DownCast(theRefList->Value(ind));
487 if (aFunc.IsNull()) continue;
488 TDF_Label anArgLabel_i = SUB_ARGUMENT(thePosition, ind);
489 TDF_Reference::Set(anArgLabel_i, aFunc->GetEntry());
490 TDataStd_UAttribute::Set(anArgLabel_i, GetDependencyID());
492 // set TreeNode on the child label
493 aNode = TDataStd_TreeNode::Set(anArgLabel_i, GetReferencesTreeID());
494 aRoot->Append(aNode);
501 //=============================================================================
505 //=============================================================================
506 Handle(TColStd_HSequenceOfTransient) GEOM_Function::GetReferenceList(int thePosition)
508 Handle(TColStd_HSequenceOfTransient) aResult = new TColStd_HSequenceOfTransient;
510 if(thePosition <= 0) return aResult;
512 // parent label for the list of references
513 TDF_Label anArgLabel = ARGUMENT(thePosition);
514 Handle(TDF_Reference) aRef;
516 // get TreeNode on the parent label
517 Handle(TDataStd_TreeNode) aRoot, aNode;
518 if(!anArgLabel.FindAttribute(GetReferencesTreeID(), aRoot))
521 // get references, stored on sub-labels of the parent label
523 TDataStd_ChildNodeIterator anIter (aRoot);
524 for (; anIter.More(); anIter.Next()) {
525 aNode = anIter.Value();
526 aLabel_i = aNode->Label();
527 if (!aLabel_i.FindAttribute(TDF_Reference::GetID(), aRef)) continue;
528 Handle(GEOM_Function) aFunc_i = GetFunction(aRef->Get());
529 if (aFunc_i.IsNull()) continue;
530 aResult->Append(aFunc_i);
537 //=============================================================================
541 //=============================================================================
542 void GEOM_Function::SetShape(int thePosition, const TopoDS_Shape& theShape)
545 if(thePosition <= 0 || theShape.IsNull()) return;
547 TDF_Label anArgLabel = ARGUMENT(thePosition);
548 TNaming_Builder aBuilder(anArgLabel);
549 aBuilder.Generated(theShape);
555 //=============================================================================
559 //=============================================================================
560 TopoDS_Shape GEOM_Function::GetShape(int thePosition)
564 if(thePosition <= 0) return aShape;
566 TDF_Label anArgLabel = ARGUMENT(thePosition);
567 Handle(TNaming_NamedShape) aNS;
568 if(!anArgLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return aShape;
576 //=============================================================================
580 //=============================================================================
581 void GEOM_Function::GetDependency(TDF_LabelSequence& theSeq)
583 TDF_ChildIterator anIterator(ARGUMENTS, Standard_True);
584 for(; anIterator.More(); anIterator.Next()) {
585 if(anIterator.Value().IsAttribute(GetDependencyID())) theSeq.Append(anIterator.Value());
589 //=======================================================================
590 //function : GEOM_Function_Type_
592 //=======================================================================
593 Standard_EXPORT Handle_Standard_Type& GEOM_Function_Type_()
596 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
597 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
598 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
599 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
602 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
603 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Function",
604 sizeof(GEOM_Function),
606 (Standard_Address)_Ancestors,
607 (Standard_Address)NULL);
612 //=======================================================================
613 //function : DownCast
615 //=======================================================================
617 const Handle(GEOM_Function) Handle(GEOM_Function)::DownCast(const Handle(Standard_Transient)& AnObject)
619 Handle(GEOM_Function) _anOtherObject;
621 if (!AnObject.IsNull()) {
622 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Function))) {
623 _anOtherObject = Handle(GEOM_Function)((Handle(GEOM_Function)&)AnObject);
627 return _anOtherObject ;