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