Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/geom.git] / src / GEOM / GEOM_Object.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/
19 //
20 #include <Standard_Stream.hxx>
21
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 <TCollection_AsciiString.hxx>
37 #include <TCollection_ExtendedString.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopExp.hxx>
40
41 #define TYPE 2
42 #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
43 #define FREE_LABEL 3
44
45 //=======================================================================
46 //function : GetObjectID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOM_Object::GetObjectID()
50 {
51   static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
52   return anObjectID;
53 }    
54
55 //=======================================================================
56 //function : GetSubShapeID
57 //purpose  :
58 //=======================================================================
59 const Standard_GUID& GEOM_Object::GetSubShapeID()
60 {
61   static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
62   return anObjectID;
63 }
64               
65 //=============================================================================
66 /*!
67  *  GetObject
68  */
69 //=============================================================================
70 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
71 {
72   if (!theLabel.IsAttribute(GetObjectID())) return NULL;
73
74   TCollection_AsciiString anEntry;
75   TDF_Tool::Entry(theLabel, anEntry);
76
77   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
78   if(aDoc.IsNull()) return NULL;
79
80   Handle(TDataStd_Integer) anID;
81   if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
82   
83
84   GEOM_Engine* anEngine=  GEOM_Engine::GetEngine();
85   if(anEngine == NULL) return NULL;
86   return anEngine->GetObject(anID->Get(), anEntry.ToCString());
87 }
88
89 //=============================================================================
90 /*!
91  *  GetReferencedObject
92  */
93 //=============================================================================
94 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
95 {
96   Handle(TDF_Reference) aRef;
97   if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) return NULL;
98
99   // Get TreeNode of a referenced function
100   Handle(TDataStd_TreeNode) aT, aFather;
101   if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) return NULL;
102
103   // Get TreeNode of Object of the referenced function
104   aFather = aT->Father();
105   if (aFather.IsNull()) return NULL;
106
107   // Get label of the referenced object
108   TDF_Label aLabel = aFather->Label();
109
110   
111   return GEOM_Object::GetObject(aLabel);
112 }
113
114 //=============================================================================
115 /*!
116  *  Constructor: private
117  */
118 //=============================================================================
119 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
120 : _label(theEntry), _ior("") 
121 {
122   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root)) 
123     _root = TDataStd_TreeNode::Set(theEntry);
124 }
125
126 //=============================================================================
127 /*!
128  *  Constructor: public
129  */
130 //=============================================================================
131 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
132 : _label(theEntry), _ior("") 
133 {
134   theEntry.ForgetAllAttributes(Standard_True);
135
136   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root)) 
137     _root = TDataStd_TreeNode::Set(theEntry);
138
139   TDataStd_Integer::Set(theEntry.FindChild(TYPE), theType);
140
141   TDataStd_UAttribute::Set(theEntry, GetObjectID());
142 }
143
144 //=============================================================================
145 /*!
146  *  GetType
147  */
148 //=============================================================================
149 int GEOM_Object::GetType()
150 {
151   Handle(TDataStd_Integer) aType;
152   if(!_label.FindChild(TYPE).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
153   
154   return aType->Get();
155 }
156
157 //=============================================================================
158 /*!
159  *  SetType
160  */
161 //=============================================================================
162 void GEOM_Object::SetType(int theType)
163 {
164   TDataStd_Integer::Set(_label.FindChild(TYPE), theType);
165   return;
166 }
167
168
169 //=============================================================================
170 /*!
171  *  GetDocID
172  */
173 //=============================================================================
174 int GEOM_Object::GetDocID()
175 {
176   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
177   if(aDoc.IsNull()) return -1;
178
179   Handle(TDataStd_Integer) anID;
180   if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
181   
182   return anID->Get();
183 }
184
185
186 //=============================================================================
187 /*!
188  *  GetValue
189  */
190 //=============================================================================
191 TopoDS_Shape GEOM_Object::GetValue()
192 {
193   TopoDS_Shape aShape;
194
195   Handle(GEOM_Function) aFunction = GetLastFunction();
196
197   if (!aFunction.IsNull())
198     aShape = aFunction->GetValue();
199   
200   return aShape;
201 }
202
203 //=============================================================================
204 /*!
205  *  SetName
206  */
207 //=============================================================================
208 void GEOM_Object::SetName(const char* theName)
209 {
210   TDataStd_Name::Set(_label, (char*)theName);
211 }
212
213 //=============================================================================
214 /*!
215  *  GetName
216  */
217 //=============================================================================
218 char* GEOM_Object::GetName()
219 {
220   Handle(TDataStd_Name) aNameAttr;
221   if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
222   
223   TCollection_AsciiString aName(aNameAttr->Get());
224   return aName.ToCString();
225 }
226
227 //=============================================================================
228 /*!
229  *  SetAuxData
230  */
231 //=============================================================================
232 void GEOM_Object::SetAuxData(const char* theData)
233 {
234   TDataStd_Comment::Set(_label, (char*)theData);
235 }
236
237 //=============================================================================
238 /*!
239  *  GetAuxData
240  */
241 //=============================================================================
242 TCollection_AsciiString GEOM_Object::GetAuxData()
243 {
244   TCollection_AsciiString aData;
245
246   Handle(TDataStd_Comment) aCommentAttr;
247   if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
248     aData = aCommentAttr->Get();
249
250   return aData;
251 }
252
253
254 //=============================================================================
255 /*!
256  *  IsSubShape
257  */
258 //============================================================================= 
259 bool GEOM_Object::IsMainShape()
260 {
261   Handle(GEOM_Function) aFunction = GetFunction(1);
262   if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
263   return false;
264 }
265
266
267 //=============================================================================
268 /*!
269  *  AddFunction
270  */
271 //=============================================================================
272 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
273 {
274   Standard_Integer nb = GetNbFunctions();
275   if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
276   nb++;
277   TDF_Label aChild = FUNCTION_LABEL(nb);
278
279   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
280   _root->Append(aNode);
281
282   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
283
284   return aFunction;
285
286 }
287
288 //=============================================================================
289 /*!
290  *  GetNbFunctions
291  */
292 //=============================================================================
293 int GEOM_Object::GetNbFunctions()
294 {
295   Standard_Integer nb = 0;
296   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
297   return nb;
298 }
299
300 //=============================================================================
301 /*!
302  *  GetFunction
303  */
304 //=============================================================================
305 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
306 {
307   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
308   return GEOM_Function::GetFunction(aChild);
309 }
310
311 //=============================================================================
312 /*!
313  *  GetlastFunction
314  */
315 //=============================================================================
316 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
317 {
318   Standard_Integer nb = GetNbFunctions();
319   if(nb) return GetFunction(nb);
320  
321   return NULL;
322 }
323
324
325 //=============================================================================
326 /*!
327  *  GetAllDependency
328  */
329 //=============================================================================
330 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
331 {
332   Handle(TColStd_HSequenceOfTransient) anArray;
333   TDF_LabelSequence aSeq;
334   Standard_Integer nb = GetNbFunctions();
335   if(nb == 0) return anArray;
336   for(Standard_Integer i=1; i<=nb; i++) {
337     Handle(GEOM_Function) aFunction = GetFunction(i);
338     if(aFunction.IsNull()) continue;
339     aFunction->GetDependency(aSeq);
340   }
341
342   Standard_Integer aLength = aSeq.Length();
343   if(aLength > 0) {    
344     anArray = new TColStd_HSequenceOfTransient;
345     for(Standard_Integer j =1; j<=aLength; j++)
346       anArray->Append(GetReferencedObject(aSeq(j)));
347   }
348   
349   return anArray;
350 }
351
352 //=============================================================================
353 /*!
354  *  GetLastDependency
355  */
356 //=============================================================================
357 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
358 {
359   Handle(TColStd_HSequenceOfTransient) anArray;
360   Handle(GEOM_Function) aFunction = GetLastFunction();
361   if (aFunction.IsNull()) return anArray;
362
363   TDF_LabelSequence aSeq;
364   aFunction->GetDependency(aSeq);
365   Standard_Integer aLength = aSeq.Length();
366   if (aLength > 0) {
367     anArray = new TColStd_HSequenceOfTransient;
368     for (Standard_Integer i = 1; i <= aLength; i++)
369       anArray->Append(GetReferencedObject(aSeq(i)));
370   }
371
372   return anArray;
373 }
374
375 //=============================================================================
376 /*!
377  *  GetFreeLabel
378  */
379 //=============================================================================
380 TDF_Label GEOM_Object::GetFreeLabel()
381 {
382   return _label.FindChild(FREE_LABEL);
383 }
384
385 //=======================================================================
386 //function :  GEOM_Object_Type_
387 //purpose  :
388 //======================================================================= 
389 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
390 {
391
392   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
393   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared); 
394   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
395   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
396  
397
398   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
399   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
400                                                          sizeof(GEOM_Object),
401                                                          1,
402                                                          (Standard_Address)_Ancestors,
403                                                          (Standard_Address)NULL);
404   return _aType;
405 }
406
407 //=======================================================================
408 //function : DownCast
409 //purpose  :
410 //======================================================================= 
411
412 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
413 {
414   Handle(GEOM_Object) _anOtherObject;
415
416   if (!AnObject.IsNull()) {
417      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
418        _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
419      }
420   }
421
422   return _anOtherObject ;
423 }
424
425