1 // Copyright (C) 2007-2012 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 "utilities.h"
27 #include <GEOM_Object.hxx>
28 #include <GEOM_Engine.hxx>
29 #include <GEOM_Solver.hxx>
30 #include <TDF_Tool.hxx>
31 #include <TDF_Data.hxx>
32 #include <TDF_Reference.hxx>
33 #include <TDF_LabelSequence.hxx>
34 #include <TDocStd_Owner.hxx>
35 #include <TDocStd_Document.hxx>
36 #include <TDataStd_Integer.hxx>
37 #include <TDataStd_Real.hxx>
38 #include <TDataStd_ChildNodeIterator.hxx>
39 #include <TDataStd_UAttribute.hxx>
40 #include <TDataStd_Name.hxx>
41 #include <TDataStd_Comment.hxx>
42 #include <TDataStd_RealArray.hxx>
43 #include <TDataStd_ByteArray.hxx>
44 #include <TColStd_HArray1OfReal.hxx>
45 #include <TCollection_AsciiString.hxx>
46 #include <TCollection_ExtendedString.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
50 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
55 #define AUTO_COLOR_LABEL 6
56 #define MARKER_LABEL 7
58 #define MARKER_LABEL_TYPE 1
59 #define MARKER_LABEL_SIZE 2
60 #define MARKER_LABEL_ID 3
62 //=======================================================================
63 //function : GetObjectID
65 //=======================================================================
66 const Standard_GUID& GEOM_Object::GetObjectID()
68 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
72 //=======================================================================
73 //function : GetSubShapeID
75 //=======================================================================
76 const Standard_GUID& GEOM_Object::GetSubShapeID()
78 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
82 //=============================================================================
86 //=============================================================================
87 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
89 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
91 TCollection_AsciiString anEntry;
92 TDF_Tool::Entry(theLabel, anEntry);
94 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
95 if(aDoc.IsNull()) return NULL;
97 Handle(TDataStd_Integer) anID;
98 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
101 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
102 if(anEngine == NULL) return NULL;
103 return anEngine->GetObject(anID->Get(), (char*) anEntry.ToCString());
108 //=============================================================================
110 * GetReferencedObject
112 //=============================================================================
113 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
115 Handle(TDF_Reference) aRef;
116 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
120 if(aRef.IsNull() || aRef->Get().IsNull()) {
125 // Get TreeNode of a referenced function
126 Handle(TDataStd_TreeNode) aT, aFather;
127 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
132 // Get TreeNode of Object of the referenced function
133 aFather = aT->Father();
134 if (aFather.IsNull()) {
138 // Get label of the referenced object
139 TDF_Label aLabel = aFather->Label();
142 return GEOM_Object::GetObject(aLabel);
145 //=============================================================================
147 * Constructor: private
149 //=============================================================================
150 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
151 : _label(theEntry), _ior(""), _docID(-1)
153 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
155 Handle(TDataStd_Integer) anID;
156 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
159 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
160 _root = TDataStd_TreeNode::Set(theEntry);
163 //=============================================================================
165 * Constructor: public
167 //=============================================================================
168 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
169 : _label(theEntry), _ior(""), _docID(-1)
171 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
173 Handle(TDataStd_Integer) anID;
174 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
177 theEntry.ForgetAllAttributes(Standard_True);
179 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
180 _root = TDataStd_TreeNode::Set(theEntry);
182 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
184 TDataStd_UAttribute::Set(theEntry, GetObjectID());
187 //=============================================================================
191 //=============================================================================
192 GEOM_Object::~GEOM_Object()
194 MESSAGE("GEOM_Object::~GEOM_Object()");
197 //=============================================================================
201 //=============================================================================
202 int GEOM_Object::GetType()
204 Handle(TDataStd_Integer) aType;
205 if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
210 //=============================================================================
214 //=============================================================================
215 void GEOM_Object::SetType(int theType)
217 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
221 //=============================================================================
223 * Returns modifications counter of this object.
224 * Comparing this value with modifications counters of argument objects
225 * (on which this object depends) we decide whether this object needs to be updated.
227 //=============================================================================
228 int GEOM_Object::GetTic()
230 Handle(TDataStd_Integer) aTicAttr;
231 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
234 return aTicAttr->Get();
237 //=============================================================================
239 * Set another value of modifications counter.
241 * Use this method to update modifications counter of dependent object
242 * to be equal to modifications counter of its argument.
243 * This is commonly done in GEOM_Function::GetValue()
245 //=============================================================================
246 void GEOM_Object::SetTic(int theTic)
248 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
251 //=============================================================================
253 * Increment modifications counter to mark this object as modified.
255 * Commonly called from GEOM_Function::SetValue()
257 //=============================================================================
258 void GEOM_Object::IncrementTic()
260 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
262 Standard_Integer aTic = 0;
263 Handle(TDataStd_Integer) aTicAttr;
264 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
265 aTic = aTicAttr->Get();
267 TDataStd_Integer::Set(aTicLabel, aTic + 1);
271 //=============================================================================
275 //=============================================================================
276 int GEOM_Object::GetDocID()
282 //=============================================================================
286 //=============================================================================
287 TopoDS_Shape GEOM_Object::GetValue()
291 Handle(GEOM_Function) aFunction = GetLastFunction();
293 if (!aFunction.IsNull())
294 aShape = aFunction->GetValue();
299 //=============================================================================
303 //=============================================================================
304 void GEOM_Object::SetName(const char* theName)
306 TDataStd_Name::Set(_label, (char*)theName);
309 //=============================================================================
313 //=============================================================================
314 char* GEOM_Object::GetName()
316 Handle(TDataStd_Name) aNameAttr;
317 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
319 TCollection_AsciiString aName(aNameAttr->Get());
320 // do not return pointer of local variable
321 // return aName.ToCString();
322 // the following code could lead to memory leak, so take care about recieved pointer
323 return strdup(aName.ToCString());
326 //=============================================================================
330 //=============================================================================
331 void GEOM_Object::SetColor(const GEOM_Object::Color& theColor)
333 Handle(TDataStd_RealArray) anArray = new TDataStd_RealArray();
334 anArray->Init( 1, 3 );
335 anArray->SetValue( 1, theColor.R );
336 anArray->SetValue( 2, theColor.G );
337 anArray->SetValue( 3, theColor.B );
339 Handle(TDataStd_RealArray) anAttr =
340 TDataStd_RealArray::Set(_label.FindChild(COLOR_LABEL), anArray->Lower(), anArray->Upper());
341 anAttr->ChangeArray(anArray->Array());
344 //=============================================================================
348 //=============================================================================
349 GEOM_Object::Color GEOM_Object::GetColor()
351 Handle(TDataStd_RealArray) anArray;
352 bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
354 GEOM_Object::Color aColor;
355 aColor.R = isFound ? anArray->Value( 1 ) : -1;
356 aColor.G = isFound ? anArray->Value( 2 ) : -1;
357 aColor.B = isFound ? anArray->Value( 3 ) : -1;
362 //=============================================================================
366 //=============================================================================
367 void GEOM_Object::SetAutoColor(bool theAutoColor)
369 TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
372 //=============================================================================
376 //=============================================================================
377 bool GEOM_Object::GetAutoColor()
379 Handle(TDataStd_Integer) anAutoColor;
380 if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
382 return bool(anAutoColor->Get());
385 //=============================================================================
389 //=============================================================================
390 void GEOM_Object::SetMarkerStd(const Aspect_TypeOfMarker theType, double theSize)
392 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL);
393 TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_TYPE), (int)theType);
394 TDataStd_Real::Set(aMarkerLabel.FindChild(MARKER_LABEL_SIZE), theSize);
397 //=============================================================================
401 //=============================================================================
402 void GEOM_Object::SetMarkerTexture(int theTextureId)
404 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL);
405 TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_TYPE), (int)Aspect_TOM_USERDEFINED);
406 TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_ID), theTextureId);
409 //=============================================================================
413 //=============================================================================
414 Aspect_TypeOfMarker GEOM_Object::GetMarkerType()
416 Standard_Integer aType = -1;
417 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
418 if(!aMarkerLabel.IsNull()) {
419 TDF_Label aTypeLabel = aMarkerLabel.FindChild(MARKER_LABEL_TYPE, Standard_False);
420 Handle(TDataStd_Integer) aTypeAttr;
421 if (!aTypeLabel.IsNull() && aTypeLabel.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr))
422 aType = aTypeAttr->Get();
424 return (Aspect_TypeOfMarker)aType;
427 //=============================================================================
431 //=============================================================================
432 double GEOM_Object::GetMarkerSize()
434 Standard_Real aSize = 0.;
435 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
436 if(!aMarkerLabel.IsNull()) {
437 TDF_Label aSizeLabel = aMarkerLabel.FindChild(MARKER_LABEL_SIZE, Standard_False);
438 Handle(TDataStd_Real) aSizeAttr;
439 if (!aSizeLabel.IsNull() && aSizeLabel.FindAttribute(TDataStd_Real::GetID(), aSizeAttr))
440 aSize = aSizeAttr->Get();
445 //=============================================================================
449 //=============================================================================
450 int GEOM_Object::GetMarkerTexture()
452 Standard_Integer anId = 0;
453 if ( GetMarkerType() == Aspect_TOM_USERDEFINED) {
454 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
455 if(!aMarkerLabel.IsNull()) {
456 TDF_Label aTypeLabel = aMarkerLabel.FindChild(MARKER_LABEL_ID, Standard_False);
457 Handle(TDataStd_Integer) anIdAttr;
458 if (!aTypeLabel.IsNull() && aTypeLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr))
459 anId = anIdAttr->Get();
465 //=============================================================================
469 //=============================================================================
470 void GEOM_Object::UnsetMarker()
472 SetMarkerStd((Aspect_TypeOfMarker)-1, 0.);
475 //=============================================================================
479 //=============================================================================
480 void GEOM_Object::SetAuxData(const char* theData)
482 TDataStd_Comment::Set(_label, (char*)theData);
485 //=============================================================================
489 //=============================================================================
490 TCollection_AsciiString GEOM_Object::GetAuxData()
492 TCollection_AsciiString aData;
494 Handle(TDataStd_Comment) aCommentAttr;
495 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
496 aData = aCommentAttr->Get();
501 //=============================================================================
505 //=============================================================================
506 void GEOM_Object::SetParameters(const TCollection_AsciiString& theParameters)
508 if( _parameters.IsEmpty() )
509 _parameters = theParameters;
512 _parameters += theParameters;
516 //=============================================================================
520 //=============================================================================
521 TCollection_AsciiString GEOM_Object::GetParameters() const
527 //=============================================================================
531 //=============================================================================
532 bool GEOM_Object::IsMainShape()
534 Handle(GEOM_Function) aFunction = GetFunction(1);
535 if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
540 //=============================================================================
544 //=============================================================================
545 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID,
549 Standard_Integer nb = GetNbFunctions();
550 if(!allowSubShape && nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub-shape
552 TDF_Label aChild = FUNCTION_LABEL(nb);
554 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
555 _root->Append(aNode);
557 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
563 //=============================================================================
567 //=============================================================================
568 int GEOM_Object::GetNbFunctions()
570 Standard_Integer nb = 0;
571 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
575 //=============================================================================
579 //=============================================================================
580 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
582 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
583 return GEOM_Function::GetFunction(aChild);
586 //=============================================================================
590 //=============================================================================
591 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
593 Standard_Integer nb = GetNbFunctions();
594 if(nb) return GetFunction(nb);
600 //=============================================================================
604 //=============================================================================
605 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
607 Handle(TColStd_HSequenceOfTransient) anArray;
608 TDF_LabelSequence aSeq;
609 Standard_Integer nb = GetNbFunctions();
610 if(nb == 0) return anArray;
611 for(Standard_Integer i=1; i<=nb; i++) {
612 Handle(GEOM_Function) aFunction = GetFunction(i);
613 if(aFunction.IsNull()) continue;
614 aFunction->GetDependency(aSeq);
617 Standard_Integer aLength = aSeq.Length();
619 anArray = new TColStd_HSequenceOfTransient;
620 for(Standard_Integer j =1; j<=aLength; j++) {
621 Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
622 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
629 //=============================================================================
633 //=============================================================================
634 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
636 Handle(TColStd_HSequenceOfTransient) anArray;
637 Handle(GEOM_Function) aFunction = GetLastFunction();
638 if (aFunction.IsNull()) return anArray;
640 TDF_LabelSequence aSeq;
641 aFunction->GetDependency(aSeq);
642 Standard_Integer aLength = aSeq.Length();
644 anArray = new TColStd_HSequenceOfTransient;
645 for (Standard_Integer i = 1; i <= aLength; i++)
646 anArray->Append(GetReferencedObject(aSeq(i)));
652 //=============================================================================
656 //=============================================================================
657 TDF_Label GEOM_Object::GetFreeLabel()
659 return _label.FindChild(FREE_LABEL);
662 //=======================================================================
663 //function : GEOM_Object_Type_
665 //=======================================================================
666 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
669 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
670 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
671 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
672 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
675 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
676 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
679 (Standard_Address)_Ancestors,
680 (Standard_Address)NULL);
684 //=======================================================================
685 //function : DownCast
687 //=======================================================================
689 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
691 Handle(GEOM_Object) _anOtherObject;
693 if (!AnObject.IsNull()) {
694 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
695 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
699 return _anOtherObject ;