Salome HOME
16731b97ac41289da26c2eb2c54b5a15d7783ad1
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 // Copyright (C) 2007-2013  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 "GEOM_Object_i.hh"
24
25 #include "GEOM_ISubShape.hxx"
26 #include "GEOMImpl_Types.hxx"
27 #include "GEOM_BaseDriver.hxx"
28
29 #include <utilities.h>
30 #include <OpUtil.hxx>
31 #include <Utils_ExceptHandlers.hxx>
32
33 #include <BRepTools.hxx>
34 #include <BRepTools_ShapeSet.hxx>
35 #include <Standard_OStream.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TDF_Label.hxx>
38 #include <TDF_Tool.hxx>
39 #include <TopAbs.hxx>
40 #include <TopoDS_Iterator.hxx>
41
42 #include <fstream>
43 #include <sstream>
44
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx>
47
48 #ifdef _DEBUG_
49 #include <typeinfo>
50 #endif
51
52 #ifdef WIN32
53 #pragma warning( disable:4786 )
54 #endif
55
56 //=============================================================================
57 /*!
58  *   constructor:
59  */
60 //=============================================================================
61
62 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
63                               Handle(GEOM_Object) theImpl) :
64   SALOME::GenericObj_i( thePOA ),
65   GEOM_BaseObject_i( thePOA, theEngine, theImpl ),
66   _impl( theImpl )
67 {
68 }
69
70 //=============================================================================
71 /*!
72  *  destructor
73  */
74 //=============================================================================
75
76 GEOM_Object_i::~GEOM_Object_i()
77 {
78   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
79 }
80
81 //=============================================================================
82 /*!
83  *  GetShapeType
84  */
85 //=============================================================================
86 GEOM::shape_type GEOM_Object_i::GetShapeType()
87 {
88   TopoDS_Shape _geom = _impl->GetValue();
89   if(_geom.IsNull()) return GEOM::SHAPE;
90   return (GEOM::shape_type)_geom.ShapeType();
91 }
92
93 //=============================================================================
94 /*!
95  *  GetTopologyType
96  */
97 //=============================================================================
98 GEOM::shape_type GEOM_Object_i::GetTopologyType()
99 {
100   TopoDS_Shape shape = _impl->GetValue();
101   if(shape.IsNull()) return GEOM::SHAPE;
102
103   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
104     TopoDS_Shape shape_i;
105     TopoDS_Iterator It (shape, Standard_True, Standard_False);
106     for (; It.More(); It.Next()) {
107       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
108       shape_i = It.Value();
109     }
110     if ( !shape_i.IsNull() )
111       return (GEOM::shape_type) shape_i.ShapeType();
112   }
113
114   return (GEOM::shape_type)shape.ShapeType();
115 }
116
117 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
118 {
119   if ( shape.IsNull() )
120     return GEOM::SHAPE;
121
122   GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
123
124   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
125     TopoDS_Iterator it(shape, Standard_True, Standard_False);
126     for (; it.More(); it.Next()) {
127       TopoDS_Shape sub_shape = it.Value();
128       if ( sub_shape.IsNull() ) continue;
129       GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
130       if ( stype == GEOM::SHAPE ) continue;
131       if ( ismin && stype > ret )
132         ret = stype;
133       else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
134         ret = stype;
135     }
136   }
137
138   return ret;
139 }
140
141 //=============================================================================
142 /*!
143  *  GetMinShapeType
144  */
145 //=============================================================================
146 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
147 {
148   return getMinMaxShapeType( _impl->GetValue(), true );
149 }
150
151 //=============================================================================
152 /*!
153  *  GetMaxShapeType
154  */
155 //=============================================================================
156 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
157 {
158   return getMinMaxShapeType( _impl->GetValue(), false );
159 }
160
161 //=============================================================================
162 /*!
163  *  SetColor
164  */
165 //=============================================================================
166 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
167 {
168   ::GEOM_Object::Color aColor;
169   aColor.R = theColor.R;
170   aColor.G = theColor.G;
171   aColor.B = theColor.B;
172   _impl->SetColor(aColor);
173 }
174
175
176 //=============================================================================
177 /*!
178  *  GetColor
179  */
180 //=============================================================================
181 SALOMEDS::Color GEOM_Object_i::GetColor()
182 {
183   SALOMEDS::Color aColor;
184   aColor.R = _impl->GetColor().R;
185   aColor.G = _impl->GetColor().G;
186   aColor.B = _impl->GetColor().B;
187   return aColor;
188 }
189
190
191 //=============================================================================
192 /*!
193  *  SetAutoColor
194  */
195 //=============================================================================
196 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
197 {
198   _impl->SetAutoColor(theAutoColor);
199 }
200
201
202 //=============================================================================
203 /*!
204  *  GetAutoColor
205  */
206 //=============================================================================
207 CORBA::Boolean GEOM_Object_i::GetAutoColor()
208 {
209   return _impl->GetAutoColor();
210 }
211
212
213 //=============================================================================
214 /*!
215  *  SetMarkerStd
216  */
217 //=============================================================================
218 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
219 {
220   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
221     _impl->UnsetMarker();
222   }
223   else {
224     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
225     double aSize = ((int)theSize+1)*0.5;
226     _impl->SetMarkerStd( aType, aSize );
227   }
228 }
229
230
231 //=============================================================================
232 /*!
233  *  SetMarkerTexture
234  */
235 //=============================================================================
236 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
237 {
238   _impl->SetMarkerTexture( theTextureId );
239 }
240
241
242 //=============================================================================
243 /*!
244  *  GetMarkerType
245  */
246 //=============================================================================
247 GEOM::marker_type GEOM_Object_i::GetMarkerType()
248 {
249   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
250 }
251
252
253 //=============================================================================
254 /*!
255  *  GetMarkerSize
256  */
257 //=============================================================================
258 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
259 {
260   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
261   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
262 }
263
264
265 //=============================================================================
266 /*!
267  *  GetMarkerTexture
268  */
269 //=============================================================================
270 CORBA::Long GEOM_Object_i::GetMarkerTexture()
271 {
272   return _impl->GetMarkerTexture();
273 }
274
275 //=================================================================================
276 // function : GetShapeStream
277 // Transfer resulting shape to client as sequence of bytes
278 //client can extract shape from stream using BrepTools::Read function
279 //=================================================================================
280 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
281 {
282   TopoDS_Shape aShape = _impl->GetValue();
283
284   if(aShape.IsNull()) return NULL;
285
286   std::ostringstream streamShape;
287   //Write TopoDS_Shape in ASCII format to the stream
288   BRepTools::Write(aShape, streamShape);
289   //Returns the number of bytes that have been stored in the stream's buffer.
290   int size = streamShape.str().size();
291   //Allocate octect buffer of required size
292   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
293   //Copy ostrstream content to the octect buffer
294   memcpy(OctetBuf, streamShape.str().c_str(), size);
295   //Create and return TMPFile
296   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
297   return SeqFile._retn();
298 }
299
300
301 //=======================================================================
302 //function : getShape
303 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
304 //=======================================================================
305 CORBA::LongLong GEOM_Object_i::getShape() {
306   _geom = _impl->GetValue();
307   return ((CORBA::LongLong)(&_geom));
308 }
309
310 //=============================================================================
311 /*!
312  *  GetSubShapeIndices
313  */
314 //=============================================================================
315 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
316 {
317   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
318
319   if(!_impl->IsMainShape()) {
320     Handle(GEOM_Function) aFunction = _impl->GetLastFunction(); //Get Sub-shape function (always the first (and last)  one)
321     if(aFunction.IsNull()) return anIndices._retn();
322     GEOM_ISubShape ISS(aFunction);
323     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
324     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
325     anIndices->length(anArray->Length());
326     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
327   }
328   else {
329     anIndices->length(0);
330   }
331
332   return anIndices._retn();
333 }
334
335
336 //=============================================================================
337 /*!
338  *  GetMainShape
339  */
340 //=============================================================================
341 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
342 {
343   GEOM::GEOM_Object_var obj;
344   if(!_impl->IsMainShape()) {
345     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last)  one)
346     if(aFunction.IsNull()) return obj._retn();
347     GEOM_ISubShape ISS(aFunction);
348
349     aFunction = ISS.GetMainShape();
350     if(aFunction.IsNull()) return obj._retn();
351     TDF_Label aLabel  = aFunction->GetOwnerEntry();
352     if(aLabel.IsNull()) return obj._retn();
353     TCollection_AsciiString anEntry;
354     TDF_Tool::Entry(aLabel, anEntry);
355     return GEOM::GEOM_Object::_narrow
356       ( _engine->GetObject(_impl->GetDocID(), anEntry.ToCString()) );
357   }
358
359   return obj._retn();
360 }
361
362 bool GEOM_Object_i::IsShape()
363 {
364   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
365 }
366
367 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
368 {
369   TopoDS_Shape thisShape  = _impl->GetValue();
370   TopoDS_Shape otherShape;
371   if ( !CORBA::is_nil( other ) ) {
372     Handle(GEOM_Object) otherObject = Handle(GEOM_Object)::DownCast
373       ( GEOM_Engine::GetEngine()->GetObject( other->GetStudyID(), other->GetEntry(), false ));
374     if ( !otherObject.IsNull() )
375       otherShape = otherObject->GetValue();
376   }
377   return thisShape.IsSame( otherShape );
378 }