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 <GEOM_Object.hxx>
25 #include <GEOM_Engine.hxx>
26 #include <GEOM_Solver.hxx>
27 #include <TDF_Tool.hxx>
28 #include <TDF_Data.hxx>
29 #include <TDF_Reference.hxx>
30 #include <TDF_LabelSequence.hxx>
31 #include <TDocStd_Owner.hxx>
32 #include <TDocStd_Document.hxx>
33 #include <TDataStd_Integer.hxx>
34 #include <TDataStd_ChildNodeIterator.hxx>
35 #include <TDataStd_UAttribute.hxx>
36 #include <TDataStd_Name.hxx>
37 #include <TDataStd_Comment.hxx>
38 #include <TDataStd_RealArray.hxx>
39 #include <TColStd_HArray1OfReal.hxx>
40 #include <TCollection_AsciiString.hxx>
41 #include <TCollection_ExtendedString.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
45 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
50 #define AUTO_COLOR_LABEL 6
52 //=======================================================================
53 //function : GetObjectID
55 //=======================================================================
56 const Standard_GUID& GEOM_Object::GetObjectID()
58 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
62 //=======================================================================
63 //function : GetSubShapeID
65 //=======================================================================
66 const Standard_GUID& GEOM_Object::GetSubShapeID()
68 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
72 //=============================================================================
76 //=============================================================================
77 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
79 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
81 TCollection_AsciiString anEntry;
82 TDF_Tool::Entry(theLabel, anEntry);
84 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
85 if(aDoc.IsNull()) return NULL;
87 Handle(TDataStd_Integer) anID;
88 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
91 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
92 if(anEngine == NULL) return NULL;
93 return anEngine->GetObject(anID->Get(), (char*) anEntry.ToCString());
98 //=============================================================================
100 * GetReferencedObject
102 //=============================================================================
103 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
105 Handle(TDF_Reference) aRef;
106 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
110 if(aRef.IsNull() || aRef->Get().IsNull()) {
115 // Get TreeNode of a referenced function
116 Handle(TDataStd_TreeNode) aT, aFather;
117 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
122 // Get TreeNode of Object of the referenced function
123 aFather = aT->Father();
124 if (aFather.IsNull()) {
128 // Get label of the referenced object
129 TDF_Label aLabel = aFather->Label();
132 return GEOM_Object::GetObject(aLabel);
135 //=============================================================================
137 * Constructor: private
139 //=============================================================================
140 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
141 : _label(theEntry), _ior("")
143 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
144 _root = TDataStd_TreeNode::Set(theEntry);
147 //=============================================================================
149 * Constructor: public
151 //=============================================================================
152 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
153 : _label(theEntry), _ior("")
155 theEntry.ForgetAllAttributes(Standard_True);
157 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
158 _root = TDataStd_TreeNode::Set(theEntry);
160 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
162 TDataStd_UAttribute::Set(theEntry, GetObjectID());
165 //=============================================================================
169 //=============================================================================
170 int GEOM_Object::GetType()
172 Handle(TDataStd_Integer) aType;
173 if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
178 //=============================================================================
182 //=============================================================================
183 void GEOM_Object::SetType(int theType)
185 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
189 //=============================================================================
191 * Returns modifications counter of this object.
192 * Comparing this value with modifications counters of argument objects
193 * (on which this object depends) we decide whether this object needs to be updated.
195 //=============================================================================
196 int GEOM_Object::GetTic()
198 Handle(TDataStd_Integer) aTicAttr;
199 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
202 return aTicAttr->Get();
205 //=============================================================================
207 * Set another value of modifications counter.
209 * Use this method to update modifications counter of dependent object
210 * to be equal to modifications counter of its argument.
211 * This is commonly done in GEOM_Function::GetValue()
213 //=============================================================================
214 void GEOM_Object::SetTic(int theTic)
216 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
219 //=============================================================================
221 * Increment modifications counter to mark this object as modified.
223 * Commonly called from GEOM_Function::SetValue()
225 //=============================================================================
226 void GEOM_Object::IncrementTic()
228 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
230 Standard_Integer aTic = 0;
231 Handle(TDataStd_Integer) aTicAttr;
232 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
233 aTic = aTicAttr->Get();
235 TDataStd_Integer::Set(aTicLabel, aTic + 1);
239 //=============================================================================
243 //=============================================================================
244 int GEOM_Object::GetDocID()
246 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
247 if(aDoc.IsNull()) return -1;
249 Handle(TDataStd_Integer) anID;
250 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
256 //=============================================================================
260 //=============================================================================
261 TopoDS_Shape GEOM_Object::GetValue()
265 Handle(GEOM_Function) aFunction = GetLastFunction();
267 if (!aFunction.IsNull())
268 aShape = aFunction->GetValue();
273 //=============================================================================
277 //=============================================================================
278 void GEOM_Object::SetName(const char* theName)
280 TDataStd_Name::Set(_label, (char*)theName);
283 //=============================================================================
287 //=============================================================================
288 char* GEOM_Object::GetName()
290 Handle(TDataStd_Name) aNameAttr;
291 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
293 TCollection_AsciiString aName(aNameAttr->Get());
294 // do not return pointer of local variable
295 // return aName.ToCString();
296 // the following code could lead to memory leak, so take care about recieved pointer
297 return strdup(aName.ToCString());
300 //=============================================================================
304 //=============================================================================
305 void GEOM_Object::SetColor(const SALOMEDS::Color& theColor)
307 Handle(TDataStd_RealArray) anArray = new TDataStd_RealArray();
308 anArray->Init( 1, 3 );
309 anArray->SetValue( 1, theColor.R );
310 anArray->SetValue( 2, theColor.G );
311 anArray->SetValue( 3, theColor.B );
313 Handle(TDataStd_RealArray) anAttr =
314 TDataStd_RealArray::Set(_label.FindChild(COLOR_LABEL), anArray->Lower(), anArray->Upper());
315 anAttr->ChangeArray(anArray->Array());
318 //=============================================================================
322 //=============================================================================
323 SALOMEDS::Color GEOM_Object::GetColor()
325 Handle(TDataStd_RealArray) anArray;
326 bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
328 SALOMEDS::Color aColor;
329 aColor.R = isFound ? anArray->Value( 1 ) : 0.f;
330 aColor.G = isFound ? anArray->Value( 2 ) : 0.f;
331 aColor.B = isFound ? anArray->Value( 3 ) : 0.f;
336 //=============================================================================
340 //=============================================================================
341 void GEOM_Object::SetAutoColor(CORBA::Boolean theAutoColor)
343 TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
346 //=============================================================================
350 //=============================================================================
351 CORBA::Boolean GEOM_Object::GetAutoColor()
353 Handle(TDataStd_Integer) anAutoColor;
354 if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
356 return anAutoColor->Get();
359 //=============================================================================
363 //=============================================================================
364 void GEOM_Object::SetAuxData(const char* theData)
366 TDataStd_Comment::Set(_label, (char*)theData);
369 //=============================================================================
373 //=============================================================================
374 TCollection_AsciiString GEOM_Object::GetAuxData()
376 TCollection_AsciiString aData;
378 Handle(TDataStd_Comment) aCommentAttr;
379 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
380 aData = aCommentAttr->Get();
386 //=============================================================================
390 //=============================================================================
391 bool GEOM_Object::IsMainShape()
393 Handle(GEOM_Function) aFunction = GetFunction(1);
394 if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
399 //=============================================================================
403 //=============================================================================
404 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
406 Standard_Integer nb = GetNbFunctions();
407 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
409 TDF_Label aChild = FUNCTION_LABEL(nb);
411 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
412 _root->Append(aNode);
414 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
420 //=============================================================================
424 //=============================================================================
425 int GEOM_Object::GetNbFunctions()
427 Standard_Integer nb = 0;
428 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
432 //=============================================================================
436 //=============================================================================
437 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
439 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
440 return GEOM_Function::GetFunction(aChild);
443 //=============================================================================
447 //=============================================================================
448 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
450 Standard_Integer nb = GetNbFunctions();
451 if(nb) return GetFunction(nb);
457 //=============================================================================
461 //=============================================================================
462 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
464 Handle(TColStd_HSequenceOfTransient) anArray;
465 TDF_LabelSequence aSeq;
466 Standard_Integer nb = GetNbFunctions();
467 if(nb == 0) return anArray;
468 for(Standard_Integer i=1; i<=nb; i++) {
469 Handle(GEOM_Function) aFunction = GetFunction(i);
470 if(aFunction.IsNull()) continue;
471 aFunction->GetDependency(aSeq);
474 Standard_Integer aLength = aSeq.Length();
476 anArray = new TColStd_HSequenceOfTransient;
477 for(Standard_Integer j =1; j<=aLength; j++) {
478 Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
479 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
486 //=============================================================================
490 //=============================================================================
491 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
493 Handle(TColStd_HSequenceOfTransient) anArray;
494 Handle(GEOM_Function) aFunction = GetLastFunction();
495 if (aFunction.IsNull()) return anArray;
497 TDF_LabelSequence aSeq;
498 aFunction->GetDependency(aSeq);
499 Standard_Integer aLength = aSeq.Length();
501 anArray = new TColStd_HSequenceOfTransient;
502 for (Standard_Integer i = 1; i <= aLength; i++)
503 anArray->Append(GetReferencedObject(aSeq(i)));
509 //=============================================================================
513 //=============================================================================
514 TDF_Label GEOM_Object::GetFreeLabel()
516 return _label.FindChild(FREE_LABEL);
519 //=======================================================================
520 //function : GEOM_Object_Type_
522 //=======================================================================
523 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
526 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
527 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
528 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
529 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
532 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
533 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
536 (Standard_Address)_Ancestors,
537 (Standard_Address)NULL);
541 //=======================================================================
542 //function : DownCast
544 //=======================================================================
546 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
548 Handle(GEOM_Object) _anOtherObject;
550 if (!AnObject.IsNull()) {
551 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
552 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
556 return _anOtherObject ;