1 // Copyright (C) 2007-2008 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
22 #include <Standard_Stream.hxx>
24 #include "utilities.h"
26 #include <GEOM_Object.hxx>
27 #include <GEOM_Engine.hxx>
28 #include <GEOM_Solver.hxx>
29 #include <TDF_Tool.hxx>
30 #include <TDF_Data.hxx>
31 #include <TDF_Reference.hxx>
32 #include <TDF_LabelSequence.hxx>
33 #include <TDocStd_Owner.hxx>
34 #include <TDocStd_Document.hxx>
35 #include <TDataStd_Integer.hxx>
36 #include <TDataStd_Real.hxx>
37 #include <TDataStd_ChildNodeIterator.hxx>
38 #include <TDataStd_UAttribute.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <TDataStd_Comment.hxx>
41 #include <TDataStd_RealArray.hxx>
42 #include <TDataStd_ByteArray.hxx>
43 #include <TColStd_HArray1OfReal.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <TCollection_ExtendedString.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
49 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
54 #define AUTO_COLOR_LABEL 6
55 #define MARKER_LABEL 7
57 #define MARKER_LABEL_TYPE 1
58 #define MARKER_LABEL_SIZE 2
59 #define MARKER_LABEL_ID 3
61 //=======================================================================
62 //function : GetObjectID
64 //=======================================================================
65 const Standard_GUID& GEOM_Object::GetObjectID()
67 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
71 //=======================================================================
72 //function : GetSubShapeID
74 //=======================================================================
75 const Standard_GUID& GEOM_Object::GetSubShapeID()
77 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
81 //=============================================================================
85 //=============================================================================
86 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
88 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
90 TCollection_AsciiString anEntry;
91 TDF_Tool::Entry(theLabel, anEntry);
93 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
94 if(aDoc.IsNull()) return NULL;
96 Handle(TDataStd_Integer) anID;
97 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
100 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
101 if(anEngine == NULL) return NULL;
102 return anEngine->GetObject(anID->Get(), (char*) anEntry.ToCString());
107 //=============================================================================
109 * GetReferencedObject
111 //=============================================================================
112 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
114 Handle(TDF_Reference) aRef;
115 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
119 if(aRef.IsNull() || aRef->Get().IsNull()) {
124 // Get TreeNode of a referenced function
125 Handle(TDataStd_TreeNode) aT, aFather;
126 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
131 // Get TreeNode of Object of the referenced function
132 aFather = aT->Father();
133 if (aFather.IsNull()) {
137 // Get label of the referenced object
138 TDF_Label aLabel = aFather->Label();
141 return GEOM_Object::GetObject(aLabel);
144 //=============================================================================
146 * Constructor: private
148 //=============================================================================
149 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
150 : _label(theEntry), _ior(""), _docID(-1)
152 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
154 Handle(TDataStd_Integer) anID;
155 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
158 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
159 _root = TDataStd_TreeNode::Set(theEntry);
162 //=============================================================================
164 * Constructor: public
166 //=============================================================================
167 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
168 : _label(theEntry), _ior(""), _docID(-1)
170 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
172 Handle(TDataStd_Integer) anID;
173 if(aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) _docID = anID->Get();
176 theEntry.ForgetAllAttributes(Standard_True);
178 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
179 _root = TDataStd_TreeNode::Set(theEntry);
181 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
183 TDataStd_UAttribute::Set(theEntry, GetObjectID());
186 //=============================================================================
190 //=============================================================================
191 GEOM_Object::~GEOM_Object()
193 MESSAGE("GEOM_Object::~GEOM_Object()");
196 //=============================================================================
200 //=============================================================================
201 int GEOM_Object::GetType()
203 Handle(TDataStd_Integer) aType;
204 if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
209 //=============================================================================
213 //=============================================================================
214 void GEOM_Object::SetType(int theType)
216 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
220 //=============================================================================
222 * Returns modifications counter of this object.
223 * Comparing this value with modifications counters of argument objects
224 * (on which this object depends) we decide whether this object needs to be updated.
226 //=============================================================================
227 int GEOM_Object::GetTic()
229 Handle(TDataStd_Integer) aTicAttr;
230 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
233 return aTicAttr->Get();
236 //=============================================================================
238 * Set another value of modifications counter.
240 * Use this method to update modifications counter of dependent object
241 * to be equal to modifications counter of its argument.
242 * This is commonly done in GEOM_Function::GetValue()
244 //=============================================================================
245 void GEOM_Object::SetTic(int theTic)
247 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
250 //=============================================================================
252 * Increment modifications counter to mark this object as modified.
254 * Commonly called from GEOM_Function::SetValue()
256 //=============================================================================
257 void GEOM_Object::IncrementTic()
259 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
261 Standard_Integer aTic = 0;
262 Handle(TDataStd_Integer) aTicAttr;
263 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
264 aTic = aTicAttr->Get();
266 TDataStd_Integer::Set(aTicLabel, aTic + 1);
270 //=============================================================================
274 //=============================================================================
275 int GEOM_Object::GetDocID()
281 //=============================================================================
285 //=============================================================================
286 TopoDS_Shape GEOM_Object::GetValue()
290 Handle(GEOM_Function) aFunction = GetLastFunction();
292 if (!aFunction.IsNull())
293 aShape = aFunction->GetValue();
298 //=============================================================================
302 //=============================================================================
303 void GEOM_Object::SetName(const char* theName)
305 TDataStd_Name::Set(_label, (char*)theName);
308 //=============================================================================
312 //=============================================================================
313 char* GEOM_Object::GetName()
315 Handle(TDataStd_Name) aNameAttr;
316 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
318 TCollection_AsciiString aName(aNameAttr->Get());
319 // do not return pointer of local variable
320 // return aName.ToCString();
321 // the following code could lead to memory leak, so take care about recieved pointer
322 return strdup(aName.ToCString());
325 //=============================================================================
329 //=============================================================================
330 void GEOM_Object::SetColor(const SALOMEDS::Color& theColor)
332 Handle(TDataStd_RealArray) anArray = new TDataStd_RealArray();
333 anArray->Init( 1, 3 );
334 anArray->SetValue( 1, theColor.R );
335 anArray->SetValue( 2, theColor.G );
336 anArray->SetValue( 3, theColor.B );
338 Handle(TDataStd_RealArray) anAttr =
339 TDataStd_RealArray::Set(_label.FindChild(COLOR_LABEL), anArray->Lower(), anArray->Upper());
340 anAttr->ChangeArray(anArray->Array());
343 //=============================================================================
347 //=============================================================================
348 SALOMEDS::Color GEOM_Object::GetColor()
350 Handle(TDataStd_RealArray) anArray;
351 bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
353 SALOMEDS::Color aColor;
354 aColor.R = isFound ? anArray->Value( 1 ) : -1;
355 aColor.G = isFound ? anArray->Value( 2 ) : -1;
356 aColor.B = isFound ? anArray->Value( 3 ) : -1;
361 //=============================================================================
365 //=============================================================================
366 void GEOM_Object::SetAutoColor(CORBA::Boolean theAutoColor)
368 TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
371 //=============================================================================
375 //=============================================================================
376 CORBA::Boolean GEOM_Object::GetAutoColor()
378 Handle(TDataStd_Integer) anAutoColor;
379 if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
381 return anAutoColor->Get();
384 //=============================================================================
388 //=============================================================================
389 void GEOM_Object::SetMarkerStd(const Aspect_TypeOfMarker theType, double theSize)
391 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL);
392 TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_TYPE), (int)theType);
393 TDataStd_Real::Set(aMarkerLabel.FindChild(MARKER_LABEL_SIZE), theSize);
396 //=============================================================================
400 //=============================================================================
401 void GEOM_Object::SetMarkerTexture(int theTextureId)
403 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL);
404 TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_TYPE), (int)Aspect_TOM_USERDEFINED);
405 TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_ID), theTextureId);
408 //=============================================================================
412 //=============================================================================
413 Aspect_TypeOfMarker GEOM_Object::GetMarkerType()
415 Standard_Integer aType = -1;
416 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
417 if(!aMarkerLabel.IsNull()) {
418 TDF_Label aTypeLabel = aMarkerLabel.FindChild(MARKER_LABEL_TYPE, Standard_False);
419 Handle(TDataStd_Integer) aTypeAttr;
420 if (!aTypeLabel.IsNull() && aTypeLabel.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr))
421 aType = aTypeAttr->Get();
423 return (Aspect_TypeOfMarker)aType;
426 //=============================================================================
430 //=============================================================================
431 double GEOM_Object::GetMarkerSize()
433 Standard_Real aSize = 0.;
434 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
435 if(!aMarkerLabel.IsNull()) {
436 TDF_Label aSizeLabel = aMarkerLabel.FindChild(MARKER_LABEL_SIZE, Standard_False);
437 Handle(TDataStd_Real) aSizeAttr;
438 if (!aSizeLabel.IsNull() && aSizeLabel.FindAttribute(TDataStd_Real::GetID(), aSizeAttr))
439 aSize = aSizeAttr->Get();
444 //=============================================================================
448 //=============================================================================
449 int GEOM_Object::GetMarkerTexture()
451 Standard_Integer anId = 0;
452 if ( GetMarkerType() == Aspect_TOM_USERDEFINED) {
453 TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
454 if(!aMarkerLabel.IsNull()) {
455 TDF_Label aTypeLabel = aMarkerLabel.FindChild(MARKER_LABEL_ID, Standard_False);
456 Handle(TDataStd_Integer) anIdAttr;
457 if (!aTypeLabel.IsNull() && aTypeLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr))
458 anId = anIdAttr->Get();
464 //=============================================================================
468 //=============================================================================
469 void GEOM_Object::UnsetMarker()
471 SetMarkerStd((Aspect_TypeOfMarker)-1, 0.);
474 //=============================================================================
478 //=============================================================================
479 void GEOM_Object::SetAuxData(const char* theData)
481 TDataStd_Comment::Set(_label, (char*)theData);
484 //=============================================================================
488 //=============================================================================
489 TCollection_AsciiString GEOM_Object::GetAuxData()
491 TCollection_AsciiString aData;
493 Handle(TDataStd_Comment) aCommentAttr;
494 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
495 aData = aCommentAttr->Get();
500 //=============================================================================
504 //=============================================================================
505 void GEOM_Object::SetParameters(const TCollection_AsciiString& theParameters)
507 if( _parameters.IsEmpty() )
508 _parameters = theParameters;
511 _parameters += theParameters;
515 //=============================================================================
519 //=============================================================================
520 TCollection_AsciiString GEOM_Object::GetParameters() const
526 //=============================================================================
530 //=============================================================================
531 bool GEOM_Object::IsMainShape()
533 Handle(GEOM_Function) aFunction = GetFunction(1);
534 if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
539 //=============================================================================
543 //=============================================================================
544 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
546 Standard_Integer nb = GetNbFunctions();
547 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
549 TDF_Label aChild = FUNCTION_LABEL(nb);
551 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
552 _root->Append(aNode);
554 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
560 //=============================================================================
564 //=============================================================================
565 int GEOM_Object::GetNbFunctions()
567 Standard_Integer nb = 0;
568 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
572 //=============================================================================
576 //=============================================================================
577 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
579 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
580 return GEOM_Function::GetFunction(aChild);
583 //=============================================================================
587 //=============================================================================
588 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
590 Standard_Integer nb = GetNbFunctions();
591 if(nb) return GetFunction(nb);
597 //=============================================================================
601 //=============================================================================
602 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
604 Handle(TColStd_HSequenceOfTransient) anArray;
605 TDF_LabelSequence aSeq;
606 Standard_Integer nb = GetNbFunctions();
607 if(nb == 0) return anArray;
608 for(Standard_Integer i=1; i<=nb; i++) {
609 Handle(GEOM_Function) aFunction = GetFunction(i);
610 if(aFunction.IsNull()) continue;
611 aFunction->GetDependency(aSeq);
614 Standard_Integer aLength = aSeq.Length();
616 anArray = new TColStd_HSequenceOfTransient;
617 for(Standard_Integer j =1; j<=aLength; j++) {
618 Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
619 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
626 //=============================================================================
630 //=============================================================================
631 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
633 Handle(TColStd_HSequenceOfTransient) anArray;
634 Handle(GEOM_Function) aFunction = GetLastFunction();
635 if (aFunction.IsNull()) return anArray;
637 TDF_LabelSequence aSeq;
638 aFunction->GetDependency(aSeq);
639 Standard_Integer aLength = aSeq.Length();
641 anArray = new TColStd_HSequenceOfTransient;
642 for (Standard_Integer i = 1; i <= aLength; i++)
643 anArray->Append(GetReferencedObject(aSeq(i)));
649 //=============================================================================
653 //=============================================================================
654 TDF_Label GEOM_Object::GetFreeLabel()
656 return _label.FindChild(FREE_LABEL);
659 //=======================================================================
660 //function : GEOM_Object_Type_
662 //=======================================================================
663 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
666 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
667 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
668 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
669 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
672 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
673 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
676 (Standard_Address)_Ancestors,
677 (Standard_Address)NULL);
681 //=======================================================================
682 //function : DownCast
684 //=======================================================================
686 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
688 Handle(GEOM_Object) _anOtherObject;
690 if (!AnObject.IsNull()) {
691 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
692 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
696 return _anOtherObject ;