1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_Stream.hxx>
22 #include <GEOM_Object.hxx>
23 #include <GEOM_Engine.hxx>
24 #include <GEOM_Solver.hxx>
25 #include <TDF_Tool.hxx>
26 #include <TDF_Data.hxx>
27 #include <TDF_Reference.hxx>
28 #include <TDF_LabelSequence.hxx>
29 #include <TDocStd_Owner.hxx>
30 #include <TDocStd_Document.hxx>
31 #include <TDataStd_Integer.hxx>
32 #include <TDataStd_ChildNodeIterator.hxx>
33 #include <TDataStd_UAttribute.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TDataStd_Comment.hxx>
36 #include <TDataStd_RealArray.hxx>
37 #include <TColStd_HArray1OfReal.hxx>
38 #include <TCollection_AsciiString.hxx>
39 #include <TCollection_ExtendedString.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
43 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
48 #define AUTO_COLOR_LABEL 6
50 //=======================================================================
51 //function : GetObjectID
53 //=======================================================================
54 const Standard_GUID& GEOM_Object::GetObjectID()
56 static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
60 //=======================================================================
61 //function : GetSubShapeID
63 //=======================================================================
64 const Standard_GUID& GEOM_Object::GetSubShapeID()
66 static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
70 //=============================================================================
74 //=============================================================================
75 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
77 if (!theLabel.IsAttribute(GetObjectID())) return NULL;
79 TCollection_AsciiString anEntry;
80 TDF_Tool::Entry(theLabel, anEntry);
82 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
83 if(aDoc.IsNull()) return NULL;
85 Handle(TDataStd_Integer) anID;
86 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
89 GEOM_Engine* anEngine= GEOM_Engine::GetEngine();
90 if(anEngine == NULL) return NULL;
91 return anEngine->GetObject(anID->Get(), anEntry.ToCString());
94 //=============================================================================
98 //=============================================================================
99 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
101 Handle(TDF_Reference) aRef;
102 if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
106 if(aRef.IsNull() || aRef->Get().IsNull()) {
111 // Get TreeNode of a referenced function
112 Handle(TDataStd_TreeNode) aT, aFather;
113 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
118 // Get TreeNode of Object of the referenced function
119 aFather = aT->Father();
120 if (aFather.IsNull()) {
124 // Get label of the referenced object
125 TDF_Label aLabel = aFather->Label();
128 return GEOM_Object::GetObject(aLabel);
131 //=============================================================================
133 * Constructor: private
135 //=============================================================================
136 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
137 : _label(theEntry), _ior("")
139 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
140 _root = TDataStd_TreeNode::Set(theEntry);
143 //=============================================================================
145 * Constructor: public
147 //=============================================================================
148 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
149 : _label(theEntry), _ior("")
151 theEntry.ForgetAllAttributes(Standard_True);
153 if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
154 _root = TDataStd_TreeNode::Set(theEntry);
156 TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
158 TDataStd_UAttribute::Set(theEntry, GetObjectID());
161 //=============================================================================
165 //=============================================================================
166 int GEOM_Object::GetType()
168 Handle(TDataStd_Integer) aType;
169 if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
174 //=============================================================================
178 //=============================================================================
179 void GEOM_Object::SetType(int theType)
181 TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
185 //=============================================================================
187 * Returns modifications counter of this object.
188 * Comparing this value with modifications counters of argument objects
189 * (on which this object depends) we decide whether this object needs to be updated.
191 //=============================================================================
192 int GEOM_Object::GetTic()
194 Handle(TDataStd_Integer) aTicAttr;
195 if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
198 return aTicAttr->Get();
201 //=============================================================================
203 * Set another value of modifications counter.
205 * Use this method to update modifications counter of dependent object
206 * to be equal to modifications counter of its argument.
207 * This is commonly done in GEOM_Function::GetValue()
209 //=============================================================================
210 void GEOM_Object::SetTic(int theTic)
212 TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
215 //=============================================================================
217 * Increment modifications counter to mark this object as modified.
219 * Commonly called from GEOM_Function::SetValue()
221 //=============================================================================
222 void GEOM_Object::IncrementTic()
224 TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
226 Standard_Integer aTic = 0;
227 Handle(TDataStd_Integer) aTicAttr;
228 if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
229 aTic = aTicAttr->Get();
231 TDataStd_Integer::Set(aTicLabel, aTic + 1);
235 //=============================================================================
239 //=============================================================================
240 int GEOM_Object::GetDocID()
242 Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
243 if(aDoc.IsNull()) return -1;
245 Handle(TDataStd_Integer) anID;
246 if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
252 //=============================================================================
256 //=============================================================================
257 TopoDS_Shape GEOM_Object::GetValue()
261 Handle(GEOM_Function) aFunction = GetLastFunction();
263 if (!aFunction.IsNull())
264 aShape = aFunction->GetValue();
269 //=============================================================================
273 //=============================================================================
274 void GEOM_Object::SetName(const char* theName)
276 TDataStd_Name::Set(_label, (char*)theName);
279 //=============================================================================
283 //=============================================================================
284 char* GEOM_Object::GetName()
286 Handle(TDataStd_Name) aNameAttr;
287 if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
289 TCollection_AsciiString aName(aNameAttr->Get());
290 // do not return pointer of local variable
291 // return aName.ToCString();
292 // the following code could lead to memory leak, so take care about recieved pointer
293 return strdup(aName.ToCString());
296 //=============================================================================
300 //=============================================================================
301 void GEOM_Object::SetColor(const SALOMEDS::Color& theColor)
303 Handle(TDataStd_RealArray) anArray = new TDataStd_RealArray();
304 anArray->Init( 1, 3 );
305 anArray->SetValue( 1, theColor.R );
306 anArray->SetValue( 2, theColor.G );
307 anArray->SetValue( 3, theColor.B );
309 Handle(TDataStd_RealArray) anAttr =
310 TDataStd_RealArray::Set(_label.FindChild(COLOR_LABEL), anArray->Lower(), anArray->Upper());
311 anAttr->ChangeArray(anArray->Array());
314 //=============================================================================
318 //=============================================================================
319 SALOMEDS::Color GEOM_Object::GetColor()
321 Handle(TDataStd_RealArray) anArray;
322 bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
324 SALOMEDS::Color aColor;
325 aColor.R = isFound ? anArray->Value( 1 ) : 0.f;
326 aColor.G = isFound ? anArray->Value( 2 ) : 0.f;
327 aColor.B = isFound ? anArray->Value( 3 ) : 0.f;
332 //=============================================================================
336 //=============================================================================
337 void GEOM_Object::SetAutoColor(CORBA::Boolean theAutoColor)
339 TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
342 //=============================================================================
346 //=============================================================================
347 CORBA::Boolean GEOM_Object::GetAutoColor()
349 Handle(TDataStd_Integer) anAutoColor;
350 if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
352 return anAutoColor->Get();
355 //=============================================================================
359 //=============================================================================
360 void GEOM_Object::SetAuxData(const char* theData)
362 TDataStd_Comment::Set(_label, (char*)theData);
365 //=============================================================================
369 //=============================================================================
370 TCollection_AsciiString GEOM_Object::GetAuxData()
372 TCollection_AsciiString aData;
374 Handle(TDataStd_Comment) aCommentAttr;
375 if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
376 aData = aCommentAttr->Get();
382 //=============================================================================
386 //=============================================================================
387 bool GEOM_Object::IsMainShape()
389 Handle(GEOM_Function) aFunction = GetFunction(1);
390 if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
395 //=============================================================================
399 //=============================================================================
400 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
402 Standard_Integer nb = GetNbFunctions();
403 if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
405 TDF_Label aChild = FUNCTION_LABEL(nb);
407 Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
408 _root->Append(aNode);
410 Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
416 //=============================================================================
420 //=============================================================================
421 int GEOM_Object::GetNbFunctions()
423 Standard_Integer nb = 0;
424 for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
428 //=============================================================================
432 //=============================================================================
433 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
435 TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
436 return GEOM_Function::GetFunction(aChild);
439 //=============================================================================
443 //=============================================================================
444 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
446 Standard_Integer nb = GetNbFunctions();
447 if(nb) return GetFunction(nb);
453 //=============================================================================
457 //=============================================================================
458 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
460 Handle(TColStd_HSequenceOfTransient) anArray;
461 TDF_LabelSequence aSeq;
462 Standard_Integer nb = GetNbFunctions();
463 if(nb == 0) return anArray;
464 for(Standard_Integer i=1; i<=nb; i++) {
465 Handle(GEOM_Function) aFunction = GetFunction(i);
466 if(aFunction.IsNull()) continue;
467 aFunction->GetDependency(aSeq);
470 Standard_Integer aLength = aSeq.Length();
472 anArray = new TColStd_HSequenceOfTransient;
473 for(Standard_Integer j =1; j<=aLength; j++) {
474 Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
475 if(!aRefObj.IsNull()) anArray->Append(aRefObj);
482 //=============================================================================
486 //=============================================================================
487 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
489 Handle(TColStd_HSequenceOfTransient) anArray;
490 Handle(GEOM_Function) aFunction = GetLastFunction();
491 if (aFunction.IsNull()) return anArray;
493 TDF_LabelSequence aSeq;
494 aFunction->GetDependency(aSeq);
495 Standard_Integer aLength = aSeq.Length();
497 anArray = new TColStd_HSequenceOfTransient;
498 for (Standard_Integer i = 1; i <= aLength; i++)
499 anArray->Append(GetReferencedObject(aSeq(i)));
505 //=============================================================================
509 //=============================================================================
510 TDF_Label GEOM_Object::GetFreeLabel()
512 return _label.FindChild(FREE_LABEL);
515 //=======================================================================
516 //function : GEOM_Object_Type_
518 //=======================================================================
519 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
522 static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
523 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
524 static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
525 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
528 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
529 static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
532 (Standard_Address)_Ancestors,
533 (Standard_Address)NULL);
537 //=======================================================================
538 //function : DownCast
540 //=======================================================================
542 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
544 Handle(GEOM_Object) _anOtherObject;
546 if (!AnObject.IsNull()) {
547 if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
548 _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
552 return _anOtherObject ;