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