]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Object.cxx
Salome HOME
Corrections for bug IPAL12999.
[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/ or email : webmaster.salome@opencascade.com
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
276   //if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
277   // change by skl (+srn) for IPAL12999
278   if(nb == 1 && GetFunction(1)->GetDriverGUID() == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
279   nb++;
280   TDF_Label aChild = FUNCTION_LABEL(nb);
281
282   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
283   _root->Append(aNode);
284
285   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
286
287   return aFunction;
288
289 }
290
291 //=============================================================================
292 /*!
293  *  GetNbFunctions
294  */
295 //=============================================================================
296 int GEOM_Object::GetNbFunctions()
297 {
298   Standard_Integer nb = 0;
299   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
300   return nb;
301 }
302
303 //=============================================================================
304 /*!
305  *  GetFunction
306  */
307 //=============================================================================
308 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
309 {
310   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
311   return GEOM_Function::GetFunction(aChild);
312 }
313
314 //=============================================================================
315 /*!
316  *  GetlastFunction
317  */
318 //=============================================================================
319 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
320 {
321   Standard_Integer nb = GetNbFunctions();
322   if(nb) return GetFunction(nb);
323  
324   return NULL;
325 }
326
327
328 //=============================================================================
329 /*!
330  *  GetAllDependency
331  */
332 //=============================================================================
333 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
334 {
335   Handle(TColStd_HSequenceOfTransient) anArray;
336   TDF_LabelSequence aSeq;
337   Standard_Integer nb = GetNbFunctions();
338   if(nb == 0) return anArray;
339   for(Standard_Integer i=1; i<=nb; i++) {
340     Handle(GEOM_Function) aFunction = GetFunction(i);
341     if(aFunction.IsNull()) continue;
342     aFunction->GetDependency(aSeq);
343   }
344
345   Standard_Integer aLength = aSeq.Length();
346   if(aLength > 0) {    
347     anArray = new TColStd_HSequenceOfTransient;
348     for(Standard_Integer j =1; j<=aLength; j++)
349       anArray->Append(GetReferencedObject(aSeq(j)));
350   }
351   
352   return anArray;
353 }
354
355 //=============================================================================
356 /*!
357  *  GetLastDependency
358  */
359 //=============================================================================
360 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
361 {
362   Handle(TColStd_HSequenceOfTransient) anArray;
363   Handle(GEOM_Function) aFunction = GetLastFunction();
364   if (aFunction.IsNull()) return anArray;
365
366   TDF_LabelSequence aSeq;
367   aFunction->GetDependency(aSeq);
368   Standard_Integer aLength = aSeq.Length();
369   if (aLength > 0) {
370     anArray = new TColStd_HSequenceOfTransient;
371     for (Standard_Integer i = 1; i <= aLength; i++)
372       anArray->Append(GetReferencedObject(aSeq(i)));
373   }
374
375   return anArray;
376 }
377
378 //=============================================================================
379 /*!
380  *  GetFreeLabel
381  */
382 //=============================================================================
383 TDF_Label GEOM_Object::GetFreeLabel()
384 {
385   return _label.FindChild(FREE_LABEL);
386 }
387
388 //=======================================================================
389 //function :  GEOM_Object_Type_
390 //purpose  :
391 //======================================================================= 
392 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
393 {
394
395   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
396   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared); 
397   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
398   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
399  
400
401   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
402   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
403                                                          sizeof(GEOM_Object),
404                                                          1,
405                                                          (Standard_Address)_Ancestors,
406                                                          (Standard_Address)NULL);
407   return _aType;
408 }
409
410 //=======================================================================
411 //function : DownCast
412 //purpose  :
413 //======================================================================= 
414
415 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
416 {
417   Handle(GEOM_Object) _anOtherObject;
418
419   if (!AnObject.IsNull()) {
420      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
421        _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
422      }
423   }
424
425   return _anOtherObject ;
426 }
427
428