Salome HOME
RNC: Modified documentation for MakeVertexOnCurveByLength
[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 getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
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)getMinMaxShapeType( sub_shape, ismin );
157       if ( stype == GEOM::SHAPE ) continue;
158       if ( ismin && stype > ret )
159         ret = stype;
160       else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
161         ret = stype;
162     }
163   }
164
165   return ret;
166 }
167
168 //=============================================================================
169 /*!
170  *  GetMinShapeType
171  */
172 //=============================================================================
173 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
174 {
175   return getMinMaxShapeType( _impl->GetValue(), true );
176 }
177
178 //=============================================================================
179 /*!
180  *  GetMaxShapeType
181  */
182 //=============================================================================
183 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
184 {
185   return getMinMaxShapeType( _impl->GetValue(), false );
186 }
187
188 //=============================================================================
189 /*!
190  *  SetName
191  */
192 //=============================================================================
193 void GEOM_Object_i::SetName(const char* theName)
194 {
195   _impl->SetName(theName);
196 }
197
198
199 //=============================================================================
200 /*!
201  *  GetName
202  */
203 //=============================================================================
204 char* GEOM_Object_i::GetName()
205 {
206   char* aName = _impl->GetName();
207   if (aName)
208     return aName; // this is already copy of pointer (see implementation of _impl)
209   return strdup("");
210 }
211
212 //=============================================================================
213 /*!
214  *  SetColor
215  */
216 //=============================================================================
217 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
218 {
219   _impl->SetColor(theColor);
220 }
221
222
223 //=============================================================================
224 /*!
225  *  GetColor
226  */
227 //=============================================================================
228 SALOMEDS::Color GEOM_Object_i::GetColor()
229 {
230   return _impl->GetColor();
231 }
232
233
234 //=============================================================================
235 /*!
236  *  SetAutoColor
237  */
238 //=============================================================================
239 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
240 {
241   _impl->SetAutoColor(theAutoColor);
242 }
243
244
245 //=============================================================================
246 /*!
247  *  GetAutoColor
248  */
249 //=============================================================================
250 CORBA::Boolean GEOM_Object_i::GetAutoColor()
251 {
252   return _impl->GetAutoColor();
253 }
254
255
256 //=============================================================================
257 /*!
258  *  SetMarkerStd
259  */
260 //=============================================================================
261 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
262 {
263   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
264     _impl->UnsetMarker();
265   }
266   else {
267     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
268     double aSize = ((int)theSize+1)*0.5;
269     _impl->SetMarkerStd( aType, aSize );
270   }
271 }
272
273
274 //=============================================================================
275 /*!
276  *  SetMarkerTexture
277  */
278 //=============================================================================
279 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
280 {
281   _impl->SetMarkerTexture( theTextureId );
282 }
283
284
285 //=============================================================================
286 /*!
287  *  GetMarkerType
288  */
289 //=============================================================================
290 GEOM::marker_type GEOM_Object_i::GetMarkerType()
291 {
292   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
293 }
294
295
296 //=============================================================================
297 /*!
298  *  GetMarkerSize
299  */
300 //=============================================================================
301 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
302 {
303   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
304   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
305 }
306
307
308 //=============================================================================
309 /*!
310  *  GetMarkerTexture
311  */
312 //=============================================================================
313 CORBA::Long GEOM_Object_i::GetMarkerTexture()
314 {
315   return _impl->GetMarkerTexture();
316 }
317
318
319 //=============================================================================
320 /*!
321  *  SetStudyEntry
322  */
323 //=============================================================================
324 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
325 {
326   _impl->SetAuxData(theEntry);
327 }
328
329
330 //=============================================================================
331 /*!
332  *  GetStudyEntry
333  */
334 //=============================================================================
335 char* GEOM_Object_i::GetStudyEntry()
336 {
337   TCollection_AsciiString anEntry = _impl->GetAuxData();
338   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
339   return CORBA::string_dup("");
340 }
341
342
343 //=============================================================================
344 /*!
345  *  GetDependency
346  */
347 //=============================================================================
348 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
349 {
350   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
351   aList->length(0);
352
353   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
354   if (aSeq.IsNull()) return aList._retn();
355   int aLength = aSeq->Length();
356   if (aLength == 0) return aList._retn();
357
358   aList->length(aLength);
359
360   TCollection_AsciiString anEntry;
361
362   for (int i = 1; i<=aLength; i++) {
363     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
364     if (anObj.IsNull()) continue;
365     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
366     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
367     aList[i-1] = obj;
368   }
369
370   return aList._retn();
371 }
372
373 //=============================================================================
374 /*!
375  * GetLastDependency
376  */
377 //=============================================================================
378 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
379 {
380   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
381   aList->length(0);
382
383   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
384   if (aSeq.IsNull()) return aList._retn();
385   int aLength = aSeq->Length();
386   if (aLength == 0) return aList._retn();
387
388   aList->length(aLength);
389
390   TCollection_AsciiString anEntry;
391
392   for (int i = 1; i<=aLength; i++) {
393      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
394      if (anObj.IsNull()) continue;
395      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
396      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
397      aList[i-1] = obj;
398   }
399
400   return aList._retn();
401 }
402
403 //=================================================================================
404 // function : GetShapeStream
405 // Transfer resulting shape to client as sequence of bytes
406 //client can extract shape from stream using BrepTools::Read function
407 //=================================================================================
408 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
409 {
410   TopoDS_Shape aShape = _impl->GetValue();
411
412   if(aShape.IsNull()) return NULL;
413
414   std::ostringstream streamShape;
415   //Write TopoDS_Shape in ASCII format to the stream
416   BRepTools::Write(aShape, streamShape);
417   //Returns the number of bytes that have been stored in the stream's buffer.
418   int size = streamShape.str().size();
419   //Allocate octect buffer of required size
420   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
421   //Copy ostrstream content to the octect buffer
422   memcpy(OctetBuf, streamShape.str().c_str(), size);
423   //Create and return TMPFile
424   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
425   return SeqFile._retn();
426 }
427
428
429 //=======================================================================
430 //function : getShape
431 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
432 //=======================================================================
433 CORBA::LongLong GEOM_Object_i::getShape() {
434   _geom = _impl->GetValue();
435   return ((CORBA::LongLong)(&_geom));
436 }
437
438 //=============================================================================
439 /*!
440  *  GetSubShapeIndices
441  */
442 //=============================================================================
443 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
444 {
445   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
446
447   if(!_impl->IsMainShape()) {
448     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
449     if(aFunction.IsNull()) return anIndices._retn();
450     GEOM_ISubShape ISS(aFunction);
451     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
452     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
453     anIndices->length(anArray->Length());
454     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
455   }
456   else {
457     anIndices->length(0);
458   }
459
460   return anIndices._retn();
461 }
462
463
464 //=============================================================================
465 /*!
466  *  GetMainShape
467  */
468 //=============================================================================
469 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
470 {
471   GEOM::GEOM_Object_var obj;
472   if(!_impl->IsMainShape()) {
473     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
474     if(aFunction.IsNull()) return obj._retn();
475     GEOM_ISubShape ISS(aFunction);
476
477     aFunction = ISS.GetMainShape();
478     if(aFunction.IsNull()) return obj._retn();
479     TDF_Label aLabel  = aFunction->GetOwnerEntry();
480     if(aLabel.IsNull()) return obj._retn();
481     TCollection_AsciiString anEntry;
482     TDF_Tool::Entry(aLabel, anEntry);
483     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
484   }
485
486   return obj._retn();
487 }
488
489 bool GEOM_Object_i::IsShape()
490 {
491   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
492 }
493
494 void GEOM_Object_i::SetParameters(const char* theParameters)
495 {
496   _impl->SetParameters((char*)theParameters);
497 }
498
499 char* GEOM_Object_i::GetParameters()
500 {
501   return CORBA::string_dup(_impl->GetParameters().ToCString());
502 }
503