]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Object_i.cc
Salome HOME
Merge from V5_1_main 14/05/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 <strstream>
32 //#include <sstream>
33
34 #include <OpUtil.hxx>
35 #include <Utils_ExceptHandlers.hxx>
36 #include <TDF_Tool.hxx>
37 #include <TDF_Label.hxx>
38 #include <TCollection_AsciiString.hxx>
39
40 #include <BRepTools_ShapeSet.hxx>
41 #include <BRepTools.hxx>
42 #include <TopAbs.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  *  SetName
123  */
124 //=============================================================================
125 void GEOM_Object_i::SetName(const char* theName)
126 {
127   _impl->SetName(theName);
128 }
129
130
131 //=============================================================================
132 /*!
133  *  GetName
134  */
135 //=============================================================================
136 char* GEOM_Object_i::GetName()
137 {
138   char* aName = _impl->GetName();
139   if (aName)
140     return aName; // this is already copy of pointer (see implementation of _impl)
141   return strdup("");
142 }
143
144 //=============================================================================
145 /*!
146  *  SetColor
147  */
148 //=============================================================================
149 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
150 {
151   _impl->SetColor(theColor);
152 }
153
154
155 //=============================================================================
156 /*!
157  *  GetColor
158  */
159 //=============================================================================
160 SALOMEDS::Color GEOM_Object_i::GetColor()
161 {
162   return _impl->GetColor();
163 }
164
165
166 //=============================================================================
167 /*!
168  *  SetAutoColor
169  */
170 //=============================================================================
171 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
172 {
173   _impl->SetAutoColor(theAutoColor);
174 }
175
176
177 //=============================================================================
178 /*!
179  *  GetAutoColor
180  */
181 //=============================================================================
182 CORBA::Boolean GEOM_Object_i::GetAutoColor()
183 {
184   return _impl->GetAutoColor();
185 }
186
187
188 //=============================================================================
189 /*!
190  *  SetMarkerStd
191  */
192 //=============================================================================
193 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
194 {
195   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
196     _impl->UnsetMarker();
197   }
198   else {
199     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
200     double aSize = ((int)theSize+1)*0.5;
201     _impl->SetMarkerStd( aType, aSize );
202   }
203 }
204
205
206 //=============================================================================
207 /*!
208  *  SetMarkerTexture
209  */
210 //=============================================================================
211 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
212 {
213   _impl->SetMarkerTexture( theTextureId );
214 }
215
216
217 //=============================================================================
218 /*!
219  *  GetMarkerType
220  */
221 //=============================================================================
222 GEOM::marker_type GEOM_Object_i::GetMarkerType()
223 {
224   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
225 }
226
227
228 //=============================================================================
229 /*!
230  *  GetMarkerSize
231  */
232 //=============================================================================
233 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
234 {
235   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
236   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
237 }
238
239
240 //=============================================================================
241 /*!
242  *  GetMarkerTexture
243  */
244 //=============================================================================
245 CORBA::Long GEOM_Object_i::GetMarkerTexture()
246 {
247   return _impl->GetMarkerTexture();
248 }
249
250
251 //=============================================================================
252 /*!
253  *  SetStudyEntry
254  */
255 //=============================================================================
256 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
257 {
258   _impl->SetAuxData(theEntry);
259 }
260
261
262 //=============================================================================
263 /*!
264  *  GetStudyEntry
265  */
266 //=============================================================================
267 char* GEOM_Object_i::GetStudyEntry()
268 {
269   TCollection_AsciiString anEntry = _impl->GetAuxData();
270   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
271   return CORBA::string_dup("");
272 }
273
274
275 //=============================================================================
276 /*!
277  *  GetDependency
278  */
279 //=============================================================================
280 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
281 {
282   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
283   aList->length(0);
284
285   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
286   if (aSeq.IsNull()) return aList._retn();
287   int aLength = aSeq->Length();
288   if (aLength == 0) return aList._retn();
289
290   aList->length(aLength);
291
292   TCollection_AsciiString anEntry;
293
294   for (int i = 1; i<=aLength; i++) {
295     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
296     if (anObj.IsNull()) continue;
297     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
298     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
299     aList[i-1] = obj;
300   }
301
302   return aList._retn();
303 }
304
305 //=============================================================================
306 /*!
307  * GetLastDependency
308  */
309 //=============================================================================
310 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
311 {
312   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
313   aList->length(0);
314
315   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
316   if (aSeq.IsNull()) return aList._retn();
317   int aLength = aSeq->Length();
318   if (aLength == 0) return aList._retn();
319
320   aList->length(aLength);
321
322   TCollection_AsciiString anEntry;
323
324   for (int i = 1; i<=aLength; i++) {
325      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
326      if (anObj.IsNull()) continue;
327      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
328      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
329      aList[i-1] = obj;
330   }
331
332   return aList._retn();
333 }
334
335 //=================================================================================
336 // function : GetShapeStream
337 // Transfer resulting shape to client as sequence of bytes
338 //client can extract shape from stream using BrepTools::Read function
339 //=================================================================================
340 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
341 {
342   TopoDS_Shape aShape = _impl->GetValue();
343
344   if(aShape.IsNull()) return NULL;
345
346   std::ostrstream streamShape;
347   //Write TopoDS_Shape in ASCII format to the stream
348   BRepTools::Write(aShape, streamShape);
349   //Returns the number of bytes that have been stored in the stream's buffer.
350   int size = streamShape.pcount();
351   char* buf = new char [size];
352   //Get pointer on internal character array in ostrstream
353   char* valueOfStream = streamShape.str();
354   //Create copy of ostrstream content
355   memcpy(buf, valueOfStream, size);
356   //Allow automatic deletion of ostrstream content
357   streamShape.rdbuf()->freeze(0);
358
359   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
360   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
361   return SeqFile._retn();
362
363 }
364
365
366 //=======================================================================
367 //function : getShape
368 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
369 //=======================================================================
370 CORBA::LongLong GEOM_Object_i::getShape() {
371   _geom = _impl->GetValue();
372   return ((CORBA::LongLong)(&_geom));
373 }
374
375 //=============================================================================
376 /*!
377  *  GetSubShapeIndices
378  */
379 //=============================================================================
380 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
381 {
382   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
383
384   if(!_impl->IsMainShape()) {
385     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
386     if(aFunction.IsNull()) return anIndices._retn();
387     GEOM_ISubShape ISS(aFunction);
388     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
389     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
390     anIndices->length(anArray->Length());
391     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
392   }
393   else {
394     anIndices->length(0);
395   }
396
397   return anIndices._retn();
398 }
399
400
401 //=============================================================================
402 /*!
403  *  GetMainShape
404  */
405 //=============================================================================
406 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
407 {
408   GEOM::GEOM_Object_var obj;
409   if(!_impl->IsMainShape()) {
410     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
411     if(aFunction.IsNull()) return obj._retn();
412     GEOM_ISubShape ISS(aFunction);
413
414     aFunction = ISS.GetMainShape();
415     if(aFunction.IsNull()) return obj._retn();
416     TDF_Label aLabel  = aFunction->GetOwnerEntry();
417     if(aLabel.IsNull()) return obj._retn();
418     TCollection_AsciiString anEntry;
419     TDF_Tool::Entry(aLabel, anEntry);
420     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
421   }
422
423   return obj._retn();
424 }
425
426 bool GEOM_Object_i::IsShape()
427 {
428   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
429 }
430
431 void GEOM_Object_i::SetParameters(const char* theParameters)
432 {
433   _impl->SetParameters((char*)theParameters);
434 }
435
436 char* GEOM_Object_i::GetParameters()
437 {
438   return CORBA::string_dup(_impl->GetParameters().ToCString());
439 }
440