Salome HOME
Merge from V5_1_main branch 24/11/2010
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_OStream.hxx>
24
25 #include <GEOM_Object_i.hh>
26 #include <GEOM_ISubShape.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include "utilities.h"
30 #include <fstream>
31 #include <sstream>
32
33 #include <OpUtil.hxx>
34 #include <Utils_ExceptHandlers.hxx>
35 #include <TDF_Tool.hxx>
36 #include <TDF_Label.hxx>
37 #include <TCollection_AsciiString.hxx>
38
39 #include <BRepTools_ShapeSet.hxx>
40 #include <BRepTools.hxx>
41 #include <TopAbs.hxx>
42 #include <TopoDS_Iterator.hxx>
43
44 #ifdef WNT
45 #pragma warning( disable:4786 )
46 #endif
47
48 //=============================================================================
49 /*!
50  *   constructor:
51  */
52 //=============================================================================
53
54 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
55                               Handle(GEOM_Object) theImpl)
56 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
57 {
58 }
59
60 //=============================================================================
61 /*!
62  *  destructor
63  */
64 //=============================================================================
65
66 GEOM_Object_i::~GEOM_Object_i()
67 {
68   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
69   GEOM_Engine::GetEngine()->RemoveObject(_impl);
70 }
71
72
73 //=============================================================================
74 /*!
75  *  GetEntry
76  */
77 //=============================================================================
78 char* GEOM_Object_i::GetEntry()
79 {
80   const TDF_Label& aLabel = _impl->GetEntry();
81   TCollection_AsciiString anEntry;
82   TDF_Tool::Entry(aLabel, anEntry);
83   const char* anEntstr = anEntry.ToCString();
84   return CORBA::string_dup(anEntstr);
85 }
86
87 //=============================================================================
88 /*!
89  *  GetStudyID
90  */
91 //=============================================================================
92 CORBA::Long GEOM_Object_i::GetStudyID()
93 {
94    return _impl->GetDocID();
95 }
96
97
98 //=============================================================================
99 /*!
100  *  GetType
101  */
102 //=============================================================================
103 CORBA::Long GEOM_Object_i::GetType()
104 {
105   return _impl->GetType();
106 }
107
108 //=============================================================================
109 /*!
110  *  GetShapeType
111  */
112 //=============================================================================
113 GEOM::shape_type GEOM_Object_i::GetShapeType()
114 {
115   TopoDS_Shape _geom = _impl->GetValue();
116   if(_geom.IsNull()) return GEOM::SHAPE;
117   return (GEOM::shape_type)_geom.ShapeType();
118 }
119
120 //=============================================================================
121 /*!
122  *  GetTopologyType
123  */
124 //=============================================================================
125 GEOM::shape_type GEOM_Object_i::GetTopologyType()
126 {
127   TopoDS_Shape shape = _impl->GetValue();
128   if(shape.IsNull()) return GEOM::SHAPE;
129
130   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
131     TopoDS_Shape shape_i;
132     TopoDS_Iterator It (shape, Standard_True, Standard_False);
133     for (; It.More(); It.Next()) {
134       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
135       shape_i = It.Value();
136     }
137     if ( !shape_i.IsNull() )
138       return (GEOM::shape_type) shape_i.ShapeType();
139   }
140
141   return (GEOM::shape_type)shape.ShapeType();
142 }
143
144 static GEOM::shape_type getMinShapeType( const TopoDS_Shape& shape )
145 {
146   if ( shape.IsNull() )
147     return GEOM::SHAPE;
148
149   GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
150
151   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
152     TopoDS_Iterator it(shape, Standard_True, Standard_False);
153     for (; it.More(); it.Next()) {
154       TopoDS_Shape sub_shape = it.Value();
155       if ( sub_shape.IsNull() ) continue;
156       GEOM::shape_type stype = (GEOM::shape_type)sub_shape.ShapeType();
157       if ( stype != GEOM::SHAPE && stype > ret )
158         ret = stype;
159     }
160   }
161
162   return ret;
163 }
164
165 //=============================================================================
166 /*!
167  *  GetMinShapeType
168  */
169 //=============================================================================
170 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
171 {
172   return getMinShapeType( _impl->GetValue() );
173 }
174
175 //=============================================================================
176 /*!
177  *  SetName
178  */
179 //=============================================================================
180 void GEOM_Object_i::SetName(const char* theName)
181 {
182   _impl->SetName(theName);
183 }
184
185
186 //=============================================================================
187 /*!
188  *  GetName
189  */
190 //=============================================================================
191 char* GEOM_Object_i::GetName()
192 {
193   char* aName = _impl->GetName();
194   if (aName)
195     return aName; // this is already copy of pointer (see implementation of _impl)
196   return strdup("");
197 }
198
199 //=============================================================================
200 /*!
201  *  SetColor
202  */
203 //=============================================================================
204 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
205 {
206   _impl->SetColor(theColor);
207 }
208
209
210 //=============================================================================
211 /*!
212  *  GetColor
213  */
214 //=============================================================================
215 SALOMEDS::Color GEOM_Object_i::GetColor()
216 {
217   return _impl->GetColor();
218 }
219
220
221 //=============================================================================
222 /*!
223  *  SetAutoColor
224  */
225 //=============================================================================
226 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
227 {
228   _impl->SetAutoColor(theAutoColor);
229 }
230
231
232 //=============================================================================
233 /*!
234  *  GetAutoColor
235  */
236 //=============================================================================
237 CORBA::Boolean GEOM_Object_i::GetAutoColor()
238 {
239   return _impl->GetAutoColor();
240 }
241
242
243 //=============================================================================
244 /*!
245  *  SetMarkerStd
246  */
247 //=============================================================================
248 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
249 {
250   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
251     _impl->UnsetMarker();
252   }
253   else {
254     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
255     double aSize = ((int)theSize+1)*0.5;
256     _impl->SetMarkerStd( aType, aSize );
257   }
258 }
259
260
261 //=============================================================================
262 /*!
263  *  SetMarkerTexture
264  */
265 //=============================================================================
266 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
267 {
268   _impl->SetMarkerTexture( theTextureId );
269 }
270
271
272 //=============================================================================
273 /*!
274  *  GetMarkerType
275  */
276 //=============================================================================
277 GEOM::marker_type GEOM_Object_i::GetMarkerType()
278 {
279   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
280 }
281
282
283 //=============================================================================
284 /*!
285  *  GetMarkerSize
286  */
287 //=============================================================================
288 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
289 {
290   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
291   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
292 }
293
294
295 //=============================================================================
296 /*!
297  *  GetMarkerTexture
298  */
299 //=============================================================================
300 CORBA::Long GEOM_Object_i::GetMarkerTexture()
301 {
302   return _impl->GetMarkerTexture();
303 }
304
305
306 //=============================================================================
307 /*!
308  *  SetStudyEntry
309  */
310 //=============================================================================
311 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
312 {
313   _impl->SetAuxData(theEntry);
314 }
315
316
317 //=============================================================================
318 /*!
319  *  GetStudyEntry
320  */
321 //=============================================================================
322 char* GEOM_Object_i::GetStudyEntry()
323 {
324   TCollection_AsciiString anEntry = _impl->GetAuxData();
325   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
326   return CORBA::string_dup("");
327 }
328
329
330 //=============================================================================
331 /*!
332  *  GetDependency
333  */
334 //=============================================================================
335 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
336 {
337   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
338   aList->length(0);
339
340   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
341   if (aSeq.IsNull()) return aList._retn();
342   int aLength = aSeq->Length();
343   if (aLength == 0) return aList._retn();
344
345   aList->length(aLength);
346
347   TCollection_AsciiString anEntry;
348
349   for (int i = 1; i<=aLength; i++) {
350     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
351     if (anObj.IsNull()) continue;
352     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
353     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
354     aList[i-1] = obj;
355   }
356
357   return aList._retn();
358 }
359
360 //=============================================================================
361 /*!
362  * GetLastDependency
363  */
364 //=============================================================================
365 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
366 {
367   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
368   aList->length(0);
369
370   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
371   if (aSeq.IsNull()) return aList._retn();
372   int aLength = aSeq->Length();
373   if (aLength == 0) return aList._retn();
374
375   aList->length(aLength);
376
377   TCollection_AsciiString anEntry;
378
379   for (int i = 1; i<=aLength; i++) {
380      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
381      if (anObj.IsNull()) continue;
382      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
383      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
384      aList[i-1] = obj;
385   }
386
387   return aList._retn();
388 }
389
390 //=================================================================================
391 // function : GetShapeStream
392 // Transfer resulting shape to client as sequence of bytes
393 //client can extract shape from stream using BrepTools::Read function
394 //=================================================================================
395 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
396 {
397   TopoDS_Shape aShape = _impl->GetValue();
398
399   if(aShape.IsNull()) return NULL;
400
401   std::ostringstream streamShape;
402   //Write TopoDS_Shape in ASCII format to the stream
403   BRepTools::Write(aShape, streamShape);
404   //Returns the number of bytes that have been stored in the stream's buffer.
405   int size = streamShape.str().size();
406   //Allocate octect buffer of required size
407   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
408   //Copy ostrstream content to the octect buffer
409   memcpy(OctetBuf, streamShape.str().c_str(), size);
410   //Create and return TMPFile
411   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
412   return SeqFile._retn();
413 }
414
415
416 //=======================================================================
417 //function : getShape
418 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
419 //=======================================================================
420 CORBA::LongLong GEOM_Object_i::getShape() {
421   _geom = _impl->GetValue();
422   return ((CORBA::LongLong)(&_geom));
423 }
424
425 //=============================================================================
426 /*!
427  *  GetSubShapeIndices
428  */
429 //=============================================================================
430 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
431 {
432   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
433
434   if(!_impl->IsMainShape()) {
435     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
436     if(aFunction.IsNull()) return anIndices._retn();
437     GEOM_ISubShape ISS(aFunction);
438     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
439     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
440     anIndices->length(anArray->Length());
441     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
442   }
443   else {
444     anIndices->length(0);
445   }
446
447   return anIndices._retn();
448 }
449
450
451 //=============================================================================
452 /*!
453  *  GetMainShape
454  */
455 //=============================================================================
456 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
457 {
458   GEOM::GEOM_Object_var obj;
459   if(!_impl->IsMainShape()) {
460     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
461     if(aFunction.IsNull()) return obj._retn();
462     GEOM_ISubShape ISS(aFunction);
463
464     aFunction = ISS.GetMainShape();
465     if(aFunction.IsNull()) return obj._retn();
466     TDF_Label aLabel  = aFunction->GetOwnerEntry();
467     if(aLabel.IsNull()) return obj._retn();
468     TCollection_AsciiString anEntry;
469     TDF_Tool::Entry(aLabel, anEntry);
470     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
471   }
472
473   return obj._retn();
474 }
475
476 bool GEOM_Object_i::IsShape()
477 {
478   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
479 }
480
481 void GEOM_Object_i::SetParameters(const char* theParameters)
482 {
483   _impl->SetParameters((char*)theParameters);
484 }
485
486 char* GEOM_Object_i::GetParameters()
487 {
488   return CORBA::string_dup(_impl->GetParameters().ToCString());
489 }
490