Salome HOME
46a0298bcb590e0ef5da1a287919e23594372edb
[modules/geom.git] / src / GEOM / GEOM_Object.cxx
1 #include <Standard_Stream.hxx>
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 TCollection_AsciiString GEOM_Object::GetAuxData()
224 {
225   TCollection_AsciiString aData;
226
227   Handle(TDataStd_Comment) aCommentAttr;
228   if (_label.FindAttribute(TDataStd_Comment::GetID(), aCommentAttr))
229     aData = aCommentAttr->Get();
230
231   return aData;
232 }
233
234
235 //=============================================================================
236 /*!
237  *  IsSubShape
238  */
239 //============================================================================= 
240 bool GEOM_Object::IsMainShape()
241 {
242   Handle(GEOM_Function) aFunction = GetFunction(1);
243   if(aFunction == NULL || aFunction->GetDriverGUID() != GetSubShapeID()) return true;
244   return false;
245 }
246
247
248 //=============================================================================
249 /*!
250  *  AddFunction
251  */
252 //=============================================================================
253 Handle(GEOM_Function) GEOM_Object::AddFunction(const Standard_GUID& theGUID, int theFunctionType)
254 {
255   Standard_Integer nb = GetNbFunctions();
256   if(nb == 1 && theGUID == GetSubShapeID()) return NULL; //It's impossible to add a function to sub shape
257   nb++;
258   TDF_Label aChild = FUNCTION_LABEL(nb);
259
260   Handle(TDataStd_TreeNode) aNode = TDataStd_TreeNode::Set(aChild);
261   _root->Append(aNode);
262
263   Handle(GEOM_Function) aFunction = new GEOM_Function(aChild, theGUID, theFunctionType);
264
265   return aFunction;
266
267 }
268
269 //=============================================================================
270 /*!
271  *  GetNbFunctions
272  */
273 //=============================================================================
274 int GEOM_Object::GetNbFunctions()
275 {
276   Standard_Integer nb = 0;
277   for(TDataStd_ChildNodeIterator CI(_root); CI.More(); CI.Next()) nb++;
278   return nb;
279 }
280
281 //=============================================================================
282 /*!
283  *  GetFunction
284  */
285 //=============================================================================
286 Handle(GEOM_Function) GEOM_Object::GetFunction(int theFunctionNumber)
287 {
288   TDF_Label aChild = FUNCTION_LABEL(theFunctionNumber);
289   return GEOM_Function::GetFunction(aChild);
290 }
291
292 //=============================================================================
293 /*!
294  *  GetlastFunction
295  */
296 //=============================================================================
297 Handle(GEOM_Function) GEOM_Object::GetLastFunction()
298 {
299   Standard_Integer nb = GetNbFunctions();
300   if(nb) return GetFunction(nb);
301  
302   return NULL;
303 }
304
305
306 //=============================================================================
307 /*!
308  *  GetAllDependency
309  */
310 //=============================================================================
311 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetAllDependency()
312 {
313   Handle(TColStd_HSequenceOfTransient) anArray;
314   TDF_LabelSequence aSeq;
315   Standard_Integer nb = GetNbFunctions();
316   if(nb == 0) return anArray;
317   for(Standard_Integer i=1; i<=nb; i++) {
318     Handle(GEOM_Function) aFunction = GetFunction(i);
319     if(aFunction.IsNull()) continue;
320     aFunction->GetDependency(aSeq);
321   }
322
323   Standard_Integer aLength = aSeq.Length();
324   if(aLength > 0) {    
325     anArray = new TColStd_HSequenceOfTransient;
326     for(Standard_Integer j =1; j<=aLength; j++)
327       anArray->Append(GetReferencedObject(aSeq(j)));
328   }
329   
330   return anArray;
331 }
332
333 //=============================================================================
334 /*!
335  *  GetLastDependency
336  */
337 //=============================================================================
338 Handle(TColStd_HSequenceOfTransient) GEOM_Object::GetLastDependency()
339 {
340   Handle(TColStd_HSequenceOfTransient) anArray;
341   Handle(GEOM_Function) aFunction = GetLastFunction();
342   if (aFunction.IsNull()) return anArray;
343
344   TDF_LabelSequence aSeq;
345   aFunction->GetDependency(aSeq);
346   Standard_Integer aLength = aSeq.Length();
347   if (aLength > 0) {
348     anArray = new TColStd_HSequenceOfTransient;
349     for (Standard_Integer i = 1; i <= aLength; i++)
350       anArray->Append(GetReferencedObject(aSeq(i)));
351   }
352
353   return anArray;
354 }
355
356 //=============================================================================
357 /*!
358  *  GetFreeLabel
359  */
360 //=============================================================================
361 TDF_Label GEOM_Object::GetFreeLabel()
362 {
363   return _label.FindChild(FREE_LABEL);
364 }
365
366 //=======================================================================
367 //function :  GEOM_Object_Type_
368 //purpose  :
369 //======================================================================= 
370 Standard_EXPORT Handle_Standard_Type& GEOM_Object_Type_()
371 {
372
373   static Handle_Standard_Type aType1 = STANDARD_TYPE(MMgt_TShared);
374   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(MMgt_TShared); 
375   static Handle_Standard_Type aType2 = STANDARD_TYPE(Standard_Transient);
376   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(Standard_Transient);
377  
378
379   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,NULL};
380   static Handle_Standard_Type _aType = new Standard_Type("GEOM_Object",
381                                                          sizeof(GEOM_Object),
382                                                          1,
383                                                          (Standard_Address)_Ancestors,
384                                                          (Standard_Address)NULL);
385   return _aType;
386 }
387
388 //=======================================================================
389 //function : DownCast
390 //purpose  :
391 //======================================================================= 
392
393 const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Transient)& AnObject)
394 {
395   Handle(GEOM_Object) _anOtherObject;
396
397   if (!AnObject.IsNull()) {
398      if (AnObject->IsKind(STANDARD_TYPE(GEOM_Object))) {
399        _anOtherObject = Handle(GEOM_Object)((Handle(GEOM_Object)&)AnObject);
400      }
401   }
402
403   return _anOtherObject ;
404 }
405
406