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 "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 SALOMEDS::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 SALOMEDS::Color GEOM_Object::GetColor()
351 Handle(TDataStd_RealArray) anArray;
352 bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
354 SALOMEDS::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(CORBA::Boolean theAutoColor)
369 TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
372 //=============================================================================
376 //=============================================================================
377 CORBA::Boolean GEOM_Object::GetAutoColor()
379 Handle(TDataStd_Integer) anAutoColor;
380 if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
382 return 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, int theFunctionType)
547 Standard_Integer nb = GetNbFunctions();
548 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
550 TDF_Label aChild = FUNCTION_LABEL(nb);
552 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
553 _root->Append(aNode);
555 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
561 //=============================================================================
565 //=============================================================================
566 int GEOM_Object::GetNbFunctions()
568 Standard_Integer nb = 0;
569 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
573 //=============================================================================
577 //=============================================================================
578 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
580 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
581 return GEOM_Function::GetFunction(aChild);
584 //=============================================================================
588 //=============================================================================
589 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
591 Standard_Integer nb = GetNbFunctions();
592 if(nb) return GetFunction(nb);
598 //=============================================================================
602 //=============================================================================
603 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
605 Handle(TColStd_HSequenceOfTransient) anArray;
606 TDF_LabelSequence aSeq;
607 Standard_Integer nb = GetNbFunctions();
608 if(nb == 0) return anArray;
609 for(Standard_Integer i=1; i<=nb; i++) {
610 Handle(GEOM_Function) aFunction = GetFunction(i);
611 if(aFunction.IsNull()) continue;
612 aFunction->GetDependency(aSeq);
615 Standard_Integer aLength = aSeq.Length();
617 anArray = new TColStd_HSequenceOfTransient;
618 for(Standard_Integer j =1; j<=aLength; j++) {
619 Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
620 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
627 //=============================================================================
631 //=============================================================================
632 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
634 Handle(TColStd_HSequenceOfTransient) anArray;
635 Handle(GEOM_Function) aFunction = GetLastFunction();
636 if (aFunction.IsNull()) return anArray;
638 TDF_LabelSequence aSeq;
639 aFunction->GetDependency(aSeq);
640 Standard_Integer aLength = aSeq.Length();
642 anArray = new TColStd_HSequenceOfTransient;
643 for (Standard_Integer i = 1; i <= aLength; i++)
644 anArray->Append(GetReferencedObject(aSeq(i)));
650 //=============================================================================
654 //=============================================================================
655 TDF_Label GEOM_Object::GetFreeLabel()
657 return _label.FindChild(FREE_LABEL);
660 //=======================================================================
661 //function : GEOM_Object_Type_
663 //=======================================================================
664 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
667 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
668 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
669 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
670 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
673 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
674 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
677 (Standard_Address)_Ancestors,
678 (Standard_Address)NULL);
682 //=======================================================================
683 //function : DownCast
685 //=======================================================================
687 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
689 Handle(GEOM_Object) _anOtherObject;
691 if (!AnObject.IsNull()) {
692 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
693 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
697 return _anOtherObject ;