Salome HOME
ab05590453424fdd845ab521f1b019b11f5a72b2
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 //  Copyright (C) 2007-2008  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 #include <Standard_OStream.hxx>
23
24 #include <GEOM_Object_i.hh>
25 #include <GEOM_ISubShape.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include "utilities.h"
29 #include <fstream>
30 #include <strstream>
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
43 #ifdef WNT
44 #pragma warning( disable:4786 )
45 #endif
46
47 //=============================================================================
48 /*!
49  *   constructor:
50  */
51 //=============================================================================
52
53 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
54                               Handle(GEOM_Object) theImpl)
55 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
56 {
57 }
58
59 //=============================================================================
60 /*!
61  *  destructor
62  */
63 //=============================================================================
64
65 GEOM_Object_i::~GEOM_Object_i()
66 {
67   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
68   GEOM_Engine::GetEngine()->RemoveObject(_impl);
69 }
70
71
72 //=============================================================================
73 /*!
74  *  GetEntry
75  */
76 //=============================================================================
77 char* GEOM_Object_i::GetEntry()
78 {
79   const TDF_Label& aLabel = _impl->GetEntry();
80   TCollection_AsciiString anEntry;
81   TDF_Tool::Entry(aLabel, anEntry);
82   const char* anEntstr = anEntry.ToCString();
83   return CORBA::string_dup(anEntstr);
84 }
85
86 //=============================================================================
87 /*!
88  *  GetStudyID
89  */
90 //=============================================================================
91 CORBA::Long GEOM_Object_i::GetStudyID()
92 {
93    return _impl->GetDocID();
94 }
95
96
97 //=============================================================================
98 /*!
99  *  GetType
100  */
101 //=============================================================================
102 CORBA::Long GEOM_Object_i::GetType()
103 {
104   return _impl->GetType();
105 }
106
107 //=============================================================================
108 /*!
109  *  GetShapeType
110  */
111 //=============================================================================
112 GEOM::shape_type GEOM_Object_i::GetShapeType()
113 {
114   TopoDS_Shape _geom = _impl->GetValue();
115   if(_geom.IsNull()) return GEOM::SHAPE;
116   return (GEOM::shape_type)_geom.ShapeType();
117 }
118
119 //=============================================================================
120 /*!
121  *  SetName
122  */
123 //=============================================================================
124 void GEOM_Object_i::SetName(const char* theName)
125 {
126   _impl->SetName(theName);
127 }
128
129
130 //=============================================================================
131 /*!
132  *  GetName
133  */
134 //=============================================================================
135 char* GEOM_Object_i::GetName()
136 {
137   char* aName = _impl->GetName();
138   if (aName)
139     return aName; // this is already copy of pointer (see implementation of _impl)
140   return strdup("");
141 }
142
143 //=============================================================================
144 /*!
145  *  SetColor
146  */
147 //=============================================================================
148 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
149 {
150   _impl->SetColor(theColor);
151 }
152
153
154 //=============================================================================
155 /*!
156  *  GetColor
157  */
158 //=============================================================================
159 SALOMEDS::Color GEOM_Object_i::GetColor()
160 {
161   return _impl->GetColor();
162 }
163
164
165 //=============================================================================
166 /*!
167  *  SetAutoColor
168  */
169 //=============================================================================
170 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
171 {
172   _impl->SetAutoColor(theAutoColor);
173 }
174
175
176 //=============================================================================
177 /*!
178  *  GetAutoColor
179  */
180 //=============================================================================
181 CORBA::Boolean GEOM_Object_i::GetAutoColor()
182 {
183   return _impl->GetAutoColor();
184 }
185
186
187 //=============================================================================
188 /*!
189  *  SetMarkerStd
190  */
191 //=============================================================================
192 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
193 {
194   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
195     _impl->UnsetMarker();
196   }
197   else {
198     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
199     double aSize = ((int)theSize+1)*0.5;
200     _impl->SetMarkerStd( aType, aSize );
201   }
202 }
203
204
205 //=============================================================================
206 /*!
207  *  SetMarkerTexture
208  */
209 //=============================================================================
210 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
211 {
212   _impl->SetMarkerTexture( theTextureId );
213 }
214
215
216 //=============================================================================
217 /*!
218  *  GetMarkerType
219  */
220 //=============================================================================
221 GEOM::marker_type GEOM_Object_i::GetMarkerType()
222 {
223   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
224 }
225
226
227 //=============================================================================
228 /*!
229  *  GetMarkerSize
230  */
231 //=============================================================================
232 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
233 {
234   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
235   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
236 }
237
238
239 //=============================================================================
240 /*!
241  *  GetMarkerTexture
242  */
243 //=============================================================================
244 CORBA::Long GEOM_Object_i::GetMarkerTexture()
245 {
246   return _impl->GetMarkerTexture();
247 }
248
249
250 //=============================================================================
251 /*!
252  *  SetStudyEntry
253  */
254 //=============================================================================
255 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
256 {
257   _impl->SetAuxData(theEntry);
258 }
259
260
261 //=============================================================================
262 /*!
263  *  GetStudyEntry
264  */
265 //=============================================================================
266 char* GEOM_Object_i::GetStudyEntry()
267 {
268   TCollection_AsciiString anEntry = _impl->GetAuxData();
269   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
270   return CORBA::string_dup("");
271 }
272
273
274 //=============================================================================
275 /*!
276  *  GetDependency
277  */
278 //=============================================================================
279 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
280 {
281   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
282   aList->length(0);
283
284   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
285   if (aSeq.IsNull()) return aList._retn();
286   int aLength = aSeq->Length();
287   if (aLength == 0) return aList._retn();
288
289   aList->length(aLength);
290
291   TCollection_AsciiString anEntry;
292
293   for (int i = 1; i<=aLength; i++) {
294     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
295     if (anObj.IsNull()) continue;
296     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
297     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
298     aList[i-1] = obj;
299   }
300
301   return aList._retn();
302 }
303
304 //=============================================================================
305 /*!
306  * GetLastDependency
307  */
308 //=============================================================================
309 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
310 {
311   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
312   aList->length(0);
313
314   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
315   if (aSeq.IsNull()) return aList._retn();
316   int aLength = aSeq->Length();
317   if (aLength == 0) return aList._retn();
318
319   aList->length(aLength);
320
321   TCollection_AsciiString anEntry;
322
323   for (int i = 1; i<=aLength; i++) {
324      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
325      if (anObj.IsNull()) continue;
326      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
327      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
328      aList[i-1] = obj;
329   }
330
331   return aList._retn();
332 }
333
334 //=================================================================================
335 // function : GetShapeStream
336 // Transfer resulting shape to client as sequence of bytes
337 //client can extract shape from stream using BrepTools::Read function
338 //=================================================================================
339 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
340 {
341   TopoDS_Shape aShape = _impl->GetValue();
342
343   if(aShape.IsNull()) return NULL;
344
345   std::ostrstream streamShape;
346   //Write TopoDS_Shape in ASCII format to the stream
347   BRepTools::Write(aShape, streamShape);
348   //Returns the number of bytes that have been stored in the stream's buffer.
349   int size = streamShape.pcount();
350   char* buf = new char [size];
351   //Get pointer on internal character array in ostrstream
352   char* valueOfStream = streamShape.str();
353   //Create copy of ostrstream content
354   memcpy(buf, valueOfStream, size);
355   //Allow automatic deletion of ostrstream content
356   streamShape.rdbuf()->freeze(0);
357
358   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
359   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
360   return SeqFile._retn();
361
362 }
363
364
365 //=======================================================================
366 //function : getShape
367 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
368 //=======================================================================
369 CORBA::LongLong GEOM_Object_i::getShape() {
370   _geom = _impl->GetValue();
371   return ((CORBA::LongLong)(&_geom));
372 }
373
374 //=============================================================================
375 /*!
376  *  GetSubShapeIndices
377  */
378 //=============================================================================
379 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
380 {
381   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
382
383   if(!_impl->IsMainShape()) {
384     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
385     if(aFunction.IsNull()) return anIndices._retn();
386     GEOM_ISubShape ISS(aFunction);
387     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
388     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
389     anIndices->length(anArray->Length());
390     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
391   }
392   else {
393     anIndices->length(0);
394   }
395
396   return anIndices._retn();
397 }
398
399
400 //=============================================================================
401 /*!
402  *  GetMainShape
403  */
404 //=============================================================================
405 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
406 {
407   GEOM::GEOM_Object_var obj;
408   if(!_impl->IsMainShape()) {
409     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
410     if(aFunction.IsNull()) return obj._retn();
411     GEOM_ISubShape ISS(aFunction);
412
413     aFunction = ISS.GetMainShape();
414     if(aFunction.IsNull()) return obj._retn();
415     TDF_Label aLabel  = aFunction->GetOwnerEntry();
416     if(aLabel.IsNull()) return obj._retn();
417     TCollection_AsciiString anEntry;
418     TDF_Tool::Entry(aLabel, anEntry);
419     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
420   }
421
422   return obj._retn();
423 }
424
425 bool GEOM_Object_i::IsShape()
426 {
427   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
428 }
429
430 void GEOM_Object_i::SetParameters(const char* theParameters)
431 {
432   _impl->SetParameters((char*)theParameters);
433 }
434
435 char* GEOM_Object_i::GetParameters()
436 {
437   return CORBA::string_dup(_impl->GetParameters().ToCString());
438 }
439