]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Object.cxx
Salome HOME
NPAL18111: Bad shape build by partition.
[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 FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
42 #define TYPE_LABEL 2
43 #define FREE_LABEL 3
44 #define TIC_LABEL  4
45
46 //=======================================================================
47 //function : GetObjectID
48 //purpose  :
49 //=======================================================================
50 const Standard_GUID& GEOM_Object::GetObjectID()
51 {
52   static Standard_GUID anObjectID("FF1BBB01-5D14-4df2-980B-3A668264EA16");
53   return anObjectID;
54 }
55
56 //=======================================================================
57 //function : GetSubShapeID
58 //purpose  :
59 //=======================================================================
60 const Standard_GUID& GEOM_Object::GetSubShapeID()
61 {
62   static Standard_GUID anObjectID("FF1BBB68-5D14-4df2-980B-3A668264EA16");
63   return anObjectID;
64 }
65
66 //=============================================================================
67 /*!
68  *  GetObject
69  */
70 //=============================================================================
71 Handle(GEOM_Object) GEOM_Object::GetObject(TDF_Label& theLabel)
72 {
73   if (!theLabel.IsAttribute(GetObjectID())) return NULL;
74
75   TCollection_AsciiString anEntry;
76   TDF_Tool::Entry(theLabel, anEntry);
77
78   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(theLabel.Data());
79   if(aDoc.IsNull()) return NULL;
80
81   Handle(TDataStd_Integer) anID;
82   if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return NULL;
83
84
85   GEOM_Engine* anEngine=  GEOM_Engine::GetEngine();
86   if(anEngine == NULL) return NULL;
87   return anEngine->GetObject(anID->Get(), anEntry.ToCString());
88 }
89
90 //=============================================================================
91 /*!
92  *  GetReferencedObject
93  */
94 //=============================================================================
95 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(TDF_Label& theLabel)
96 {
97   Handle(TDF_Reference) aRef;
98   if (!theLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
99     return NULL;
100   }
101   
102   if(aRef.IsNull() || aRef->Get().IsNull()) {
103     return NULL;
104   }
105
106
107   // Get TreeNode of a referenced function
108   Handle(TDataStd_TreeNode) aT, aFather;
109   if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
110     return NULL;
111   }
112
113
114   // Get TreeNode of Object of the referenced function
115   aFather = aT->Father();
116   if (aFather.IsNull()) {
117     return NULL;
118   }
119
120   // Get label of the referenced object
121   TDF_Label aLabel = aFather->Label();
122   
123
124   return GEOM_Object::GetObject(aLabel);
125 }
126
127 //=============================================================================
128 /*!
129  *  Constructor: private
130  */
131 //=============================================================================
132 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
133 : _label(theEntry), _ior("")
134 {
135   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
136     _root = TDataStd_TreeNode::Set(theEntry);
137 }
138
139 //=============================================================================
140 /*!
141  *  Constructor: public
142  */
143 //=============================================================================
144 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
145 : _label(theEntry), _ior("")
146 {
147   theEntry.ForgetAllAttributes(Standard_True);
148
149   if(!theEntry.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), _root))
150     _root = TDataStd_TreeNode::Set(theEntry);
151
152   TDataStd_Integer::Set(theEntry.FindChild(TYPE_LABEL), theType);
153
154   TDataStd_UAttribute::Set(theEntry, GetObjectID());
155 }
156
157 //=============================================================================
158 /*!
159  *  GetType
160  */
161 //=============================================================================
162 int GEOM_Object::GetType()
163 {
164   Handle(TDataStd_Integer) aType;
165   if(!_label.FindChild(TYPE_LABEL).FindAttribute(TDataStd_Integer::GetID(), aType)) return -1;
166
167   return aType->Get();
168 }
169
170 //=============================================================================
171 /*!
172  *  SetType
173  */
174 //=============================================================================
175 void GEOM_Object::SetType(int theType)
176 {
177   TDataStd_Integer::Set(_label.FindChild(TYPE_LABEL), theType);
178 }
179
180
181 //=============================================================================
182 /*!
183  *  Returns modifications counter of this object.
184  *  Comparing this value with modifications counters of argument objects
185  *  (on which this object depends) we decide whether this object needs to be updated.
186  */
187 //=============================================================================
188 int GEOM_Object::GetTic()
189 {
190   Handle(TDataStd_Integer) aTicAttr;
191   if (!_label.FindChild(TIC_LABEL).FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
192     return 0;
193
194   return aTicAttr->Get();
195 }
196
197 //=============================================================================
198 /*!
199  *  Set another value of modifications counter.
200  *
201  *  Use this method to update modifications counter of dependent object
202  *  to be equal to modifications counter of its argument.
203  *  This is commonly done in GEOM_Function::GetValue()
204  */
205 //=============================================================================
206 void GEOM_Object::SetTic(int theTic)
207 {
208   TDataStd_Integer::Set(_label.FindChild(TIC_LABEL), theTic);
209 }
210
211 //=============================================================================
212 /*!
213  *  Increment modifications counter to mark this object as modified.
214  *
215  *  Commonly called from GEOM_Function::SetValue()
216  */
217 //=============================================================================
218 void GEOM_Object::IncrementTic()
219 {
220   TDF_Label aTicLabel = _label.FindChild(TIC_LABEL);
221
222   Standard_Integer aTic = 0;
223   Handle(TDataStd_Integer) aTicAttr;
224   if (aTicLabel.FindAttribute(TDataStd_Integer::GetID(), aTicAttr))
225     aTic = aTicAttr->Get();
226
227   TDataStd_Integer::Set(aTicLabel, aTic + 1);
228 }
229
230
231 //=============================================================================
232 /*!
233  *  GetDocID
234  */
235 //=============================================================================
236 int GEOM_Object::GetDocID()
237 {
238   Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(_label.Data());
239   if(aDoc.IsNull()) return -1;
240
241   Handle(TDataStd_Integer) anID;
242   if(!aDoc->Main().FindAttribute(TDataStd_Integer::GetID(), anID)) return -1;
243
244   return anID->Get();
245 }
246
247
248 //=============================================================================
249 /*!
250  *  GetValue
251  */
252 //=============================================================================
253 TopoDS_Shape GEOM_Object::GetValue()
254 {
255   TopoDS_Shape aShape;
256
257   Handle(GEOM_Function) aFunction = GetLastFunction();
258
259   if (!aFunction.IsNull())
260     aShape = aFunction->GetValue();
261
262   return aShape;
263 }
264
265 //=============================================================================
266 /*!
267  *  SetName
268  */
269 //=============================================================================
270 void GEOM_Object::SetName(const char* theName)
271 {
272   TDataStd_Name::Set(_label, (char*)theName);
273 }
274
275 //=============================================================================
276 /*!
277  *  GetName
278  */
279 //=============================================================================
280 char* GEOM_Object::GetName()
281 {
282   Handle(TDataStd_Name) aNameAttr;
283   if(!_label.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) return NULL;
284
285   TCollection_AsciiString aName(aNameAttr->Get());
286   return aName.ToCString();
287 }
288
289 //=============================================================================
290 /*!
291  *  SetAuxData
292  */
293 //=============================================================================
294 void GEOM_Object::SetAuxData(const char* theData)
295 {
296   TDataStd_Comment::Set(_label, (char*)theData);
297 }
298
299 //=============================================================================
300 /*!
301  *  GetAuxData
302  */
303 //=============================================================================
304 TCollection_AsciiString GEOM_Object::GetAuxData()
305 {
306   TCollection_AsciiString aData;
307
308   Handle(TDataStd_Comment) aCommentAttr;
309   if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
310     aData = aCommentAttr->Get();
311
312   return aData;
313 }
314
315
316 //=============================================================================
317 /*!
318  *  IsSubShape
319  */
320 //=============================================================================
321 bool GEOM_Object::IsMainShape()
322 {
323   Handle(GEOM_Function) aFunction = GetFunction(1);
324   if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID()) return true; // mkr : IPAL9921
325   return false;
326 }
327
328
329 //=============================================================================
330 /*!
331  *  AddFunction
332  */
333 //=============================================================================
334 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
335 {
336   Standard_Integer nb = GetNbFunctions();
337   if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
338   nb++;
339   TDF_Label aChild = FUNCTION_LABEL(nb);
340
341   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
342   _root->Append(aNode);
343
344   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
345
346   return aFunction;
347
348 }
349
350 //=============================================================================
351 /*!
352  *  GetNbFunctions
353  */
354 //=============================================================================
355 int GEOM_Object::GetNbFunctions()
356 {
357   Standard_Integer nb = 0;
358   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
359   return nb;
360 }
361
362 //=============================================================================
363 /*!
364  *  GetFunction
365  */
366 //=============================================================================
367 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
368 {
369   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
370   return GEOM_Function::GetFunction(aChild);
371 }
372
373 //=============================================================================
374 /*!
375  *  GetlastFunction
376  */
377 //=============================================================================
378 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
379 {
380   Standard_Integer nb = GetNbFunctions();
381   if(nb) return GetFunction(nb);
382
383   return NULL;
384 }
385
386
387 //=============================================================================
388 /*!
389  *  GetAllDependency
390  */
391 //=============================================================================
392 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
393 {
394   Handle(TColStd_HSequenceOfTransient) anArray;
395   TDF_LabelSequence aSeq;
396   Standard_Integer nb = GetNbFunctions();
397   if(nb == 0) return anArray;
398   for(Standard_Integer i=1; i<=nb; i++) {
399     Handle(GEOM_Function) aFunction = GetFunction(i);
400     if(aFunction.IsNull()) continue;
401     aFunction->GetDependency(aSeq);
402   }
403
404   Standard_Integer aLength = aSeq.Length();
405   if(aLength > 0) {
406     anArray = new TColStd_HSequenceOfTransient;
407     for(Standard_Integer j =1; j<=aLength; j++) {
408       Handle(GEOM_Object) aRefObj = GetReferencedObject(aSeq(j));
409       if(!aRefObj.IsNull()) anArray->Append(aRefObj);
410     }
411   }
412
413   return anArray;
414 }
415
416 //=============================================================================
417 /*!
418  *  GetLastDependency
419  */
420 //=============================================================================
421 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
422 {
423   Handle(TColStd_HSequenceOfTransient) anArray;
424   Handle(GEOM_Function) aFunction = GetLastFunction();
425   if (aFunction.IsNull()) return anArray;
426
427   TDF_LabelSequence aSeq;
428   aFunction->GetDependency(aSeq);
429   Standard_Integer aLength = aSeq.Length();
430   if (aLength > 0) {
431     anArray = new TColStd_HSequenceOfTransient;
432     for (Standard_Integer i = 1; i <= aLength; i++)
433       anArray->Append(GetReferencedObject(aSeq(i)));
434   }
435
436   return anArray;
437 }
438
439 //=============================================================================
440 /*!
441  *  GetFreeLabel
442  */
443 //=============================================================================
444 TDF_Label GEOM_Object::GetFreeLabel()
445 {
446   return _label.FindChild(FREE_LABEL);
447 }
448
449 //=======================================================================
450 //function :  GEOM_Object_Type_
451 //purpose  :
452 //=======================================================================
453 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
454 {
455
456   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
457   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared);
458   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
459   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
460
461
462   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
463   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
464                                                          sizeof(GEOM_Object),
465                                                          1,
466                                                          (Standard_Address)_Ancestors,
467                                                          (Standard_Address)NULL);
468   return _aType;
469 }
470
471 //=======================================================================
472 //function : DownCast
473 //purpose  :
474 //=======================================================================
475
476 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
477 {
478   Handle(GEOM_Object) _anOtherObject;
479
480   if (!AnObject.IsNull()) {
481      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
482        _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
483      }
484   }
485
486   return _anOtherObject ;
487 }
488
489