]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Object_i.cc
Salome HOME
WIN32 compilation.
[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 WNT
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 ), _engine(theEngine), _impl(theImpl)
65 {
66 }
67
68 //=============================================================================
69 /*!
70  *  destructor
71  */
72 //=============================================================================
73
74 GEOM_Object_i::~GEOM_Object_i()
75 {
76   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
77   GEOM_Engine::GetEngine()->RemoveObject(_impl);
78 }
79
80
81 //=============================================================================
82 /*!
83  *  GetEntry
84  */
85 //=============================================================================
86 char* GEOM_Object_i::GetEntry()
87 {
88   const TDF_Label& aLabel = _impl->GetEntry();
89   TCollection_AsciiString anEntry;
90   TDF_Tool::Entry(aLabel, anEntry);
91   const char* anEntstr = anEntry.ToCString();
92   return CORBA::string_dup(anEntstr);
93 }
94
95 //=============================================================================
96 /*!
97  *  GetStudyID
98  */
99 //=============================================================================
100 CORBA::Long GEOM_Object_i::GetStudyID()
101 {
102    return _impl->GetDocID();
103 }
104
105
106 //=============================================================================
107 /*!
108  *  GetType
109  */
110 //=============================================================================
111 CORBA::Long GEOM_Object_i::GetType()
112 {
113   return _impl->GetType();
114 }
115
116 //=============================================================================
117 /*!
118  *  GetShapeType
119  */
120 //=============================================================================
121 GEOM::shape_type GEOM_Object_i::GetShapeType()
122 {
123   TopoDS_Shape _geom = _impl->GetValue();
124   if(_geom.IsNull()) return GEOM::SHAPE;
125   return (GEOM::shape_type)_geom.ShapeType();
126 }
127
128 //=============================================================================
129 /*!
130  *  GetTopologyType
131  */
132 //=============================================================================
133 GEOM::shape_type GEOM_Object_i::GetTopologyType()
134 {
135   TopoDS_Shape shape = _impl->GetValue();
136   if(shape.IsNull()) return GEOM::SHAPE;
137
138   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
139     TopoDS_Shape shape_i;
140     TopoDS_Iterator It (shape, Standard_True, Standard_False);
141     for (; It.More(); It.Next()) {
142       if ( !shape_i.IsNull() ) return (GEOM::shape_type)shape.ShapeType();
143       shape_i = It.Value();
144     }
145     if ( !shape_i.IsNull() )
146       return (GEOM::shape_type) shape_i.ShapeType();
147   }
148
149   return (GEOM::shape_type)shape.ShapeType();
150 }
151
152 static GEOM::shape_type getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
153 {
154   if ( shape.IsNull() )
155     return GEOM::SHAPE;
156
157   GEOM::shape_type ret = (GEOM::shape_type)shape.ShapeType();
158
159   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
160     TopoDS_Iterator it(shape, Standard_True, Standard_False);
161     for (; it.More(); it.Next()) {
162       TopoDS_Shape sub_shape = it.Value();
163       if ( sub_shape.IsNull() ) continue;
164       GEOM::shape_type stype = (GEOM::shape_type)getMinMaxShapeType( sub_shape, ismin );
165       if ( stype == GEOM::SHAPE ) continue;
166       if ( ismin && stype > ret )
167         ret = stype;
168       else if ( !ismin && ( ret < GEOM::SOLID || stype < ret ) )
169         ret = stype;
170     }
171   }
172
173   return ret;
174 }
175
176 //=============================================================================
177 /*!
178  *  GetMinShapeType
179  */
180 //=============================================================================
181 GEOM::shape_type GEOM_Object_i::GetMinShapeType()
182 {
183   return getMinMaxShapeType( _impl->GetValue(), true );
184 }
185
186 //=============================================================================
187 /*!
188  *  GetMaxShapeType
189  */
190 //=============================================================================
191 GEOM::shape_type GEOM_Object_i::GetMaxShapeType()
192 {
193   return getMinMaxShapeType( _impl->GetValue(), false );
194 }
195
196 //=============================================================================
197 /*!
198  *  SetName
199  */
200 //=============================================================================
201 void GEOM_Object_i::SetName(const char* theName)
202 {
203   _impl->SetName(theName);
204 }
205
206
207 //=============================================================================
208 /*!
209  *  GetName
210  */
211 //=============================================================================
212 char* GEOM_Object_i::GetName()
213 {
214   TCollection_AsciiString aName = _impl->GetName();
215   return CORBA::string_dup( aName.ToCString() );
216 }
217
218 //=============================================================================
219 /*!
220  *  SetColor
221  */
222 //=============================================================================
223 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
224 {
225   ::GEOM_Object::Color aColor;
226   aColor.R = theColor.R;
227   aColor.G = theColor.G;
228   aColor.B = theColor.B;
229   _impl->SetColor(aColor);
230 }
231
232
233 //=============================================================================
234 /*!
235  *  GetColor
236  */
237 //=============================================================================
238 SALOMEDS::Color GEOM_Object_i::GetColor()
239 {
240   SALOMEDS::Color aColor;
241   aColor.R = _impl->GetColor().R;
242   aColor.G = _impl->GetColor().G;
243   aColor.B = _impl->GetColor().B;
244   return aColor;
245 }
246
247
248 //=============================================================================
249 /*!
250  *  SetAutoColor
251  */
252 //=============================================================================
253 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
254 {
255   _impl->SetAutoColor(theAutoColor);
256 }
257
258
259 //=============================================================================
260 /*!
261  *  GetAutoColor
262  */
263 //=============================================================================
264 CORBA::Boolean GEOM_Object_i::GetAutoColor()
265 {
266   return _impl->GetAutoColor();
267 }
268
269
270 //=============================================================================
271 /*!
272  *  SetMarkerStd
273  */
274 //=============================================================================
275 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
276 {
277   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
278     _impl->UnsetMarker();
279   }
280   else {
281     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
282     double aSize = ((int)theSize+1)*0.5;
283     _impl->SetMarkerStd( aType, aSize );
284   }
285 }
286
287
288 //=============================================================================
289 /*!
290  *  SetMarkerTexture
291  */
292 //=============================================================================
293 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
294 {
295   _impl->SetMarkerTexture( theTextureId );
296 }
297
298
299 //=============================================================================
300 /*!
301  *  GetMarkerType
302  */
303 //=============================================================================
304 GEOM::marker_type GEOM_Object_i::GetMarkerType()
305 {
306   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
307 }
308
309
310 //=============================================================================
311 /*!
312  *  GetMarkerSize
313  */
314 //=============================================================================
315 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
316 {
317   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
318   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
319 }
320
321
322 //=============================================================================
323 /*!
324  *  GetMarkerTexture
325  */
326 //=============================================================================
327 CORBA::Long GEOM_Object_i::GetMarkerTexture()
328 {
329   return _impl->GetMarkerTexture();
330 }
331
332
333 //=============================================================================
334 /*!
335  *  SetStudyEntry
336  */
337 //=============================================================================
338 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
339 {
340   _impl->SetAuxData(theEntry);
341 }
342
343
344 //=============================================================================
345 /*!
346  *  GetStudyEntry
347  */
348 //=============================================================================
349 char* GEOM_Object_i::GetStudyEntry()
350 {
351   TCollection_AsciiString anEntry = _impl->GetAuxData();
352   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
353   return CORBA::string_dup("");
354 }
355
356
357 //=============================================================================
358 /*!
359  *  GetDependency
360  */
361 //=============================================================================
362 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
363 {
364   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
365   aList->length(0);
366
367   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
368   if (aSeq.IsNull()) return aList._retn();
369   int aLength = aSeq->Length();
370   if (aLength == 0) return aList._retn();
371
372   aList->length(aLength);
373
374   TCollection_AsciiString anEntry;
375
376   for (int i = 1; i<=aLength; i++) {
377     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
378     if (anObj.IsNull()) continue;
379     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
380     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), (char*) anEntry.ToCString());
381     aList[i-1] = obj;
382   }
383
384   return aList._retn();
385 }
386
387 //=============================================================================
388 /*!
389  * GetLastDependency
390  */
391 //=============================================================================
392 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
393 {
394   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
395   aList->length(0);
396
397   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
398   if (aSeq.IsNull()) return aList._retn();
399   int aLength = aSeq->Length();
400   if (aLength == 0) return aList._retn();
401
402   aList->length(aLength);
403
404   TCollection_AsciiString anEntry;
405
406   for (int i = 1; i<=aLength; i++) {
407      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
408      if (anObj.IsNull()) continue;
409      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
410      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
411      aList[i-1] = obj;
412   }
413
414   return aList._retn();
415 }
416
417 //=================================================================================
418 // function : GetShapeStream
419 // Transfer resulting shape to client as sequence of bytes
420 //client can extract shape from stream using BrepTools::Read function
421 //=================================================================================
422 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
423 {
424   TopoDS_Shape aShape = _impl->GetValue();
425
426   if(aShape.IsNull()) return NULL;
427
428   std::ostringstream streamShape;
429   //Write TopoDS_Shape in ASCII format to the stream
430   BRepTools::Write(aShape, streamShape);
431   //Returns the number of bytes that have been stored in the stream's buffer.
432   int size = streamShape.str().size();
433   //Allocate octect buffer of required size
434   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
435   //Copy ostrstream content to the octect buffer
436   memcpy(OctetBuf, streamShape.str().c_str(), size);
437   //Create and return TMPFile
438   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
439   return SeqFile._retn();
440 }
441
442
443 //=======================================================================
444 //function : getShape
445 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
446 //=======================================================================
447 CORBA::LongLong GEOM_Object_i::getShape() {
448   _geom = _impl->GetValue();
449   return ((CORBA::LongLong)(&_geom));
450 }
451
452 //=============================================================================
453 /*!
454  *  GetSubShapeIndices
455  */
456 //=============================================================================
457 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
458 {
459   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
460
461   if(!_impl->IsMainShape()) {
462     Handle(GEOM_Function) aFunction = _impl->GetLastFunction(); //Get Sub-shape function (always the first (and last)  one)
463     if(aFunction.IsNull()) return anIndices._retn();
464     GEOM_ISubShape ISS(aFunction);
465     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
466     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
467     anIndices->length(anArray->Length());
468     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
469   }
470   else {
471     anIndices->length(0);
472   }
473
474   return anIndices._retn();
475 }
476
477
478 //=============================================================================
479 /*!
480  *  GetMainShape
481  */
482 //=============================================================================
483 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
484 {
485   GEOM::GEOM_Object_var obj;
486   if(!_impl->IsMainShape()) {
487     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get Sub-shape function (always the first (and last)  one)
488     if(aFunction.IsNull()) return obj._retn();
489     GEOM_ISubShape ISS(aFunction);
490
491     aFunction = ISS.GetMainShape();
492     if(aFunction.IsNull()) return obj._retn();
493     TDF_Label aLabel  = aFunction->GetOwnerEntry();
494     if(aLabel.IsNull()) return obj._retn();
495     TCollection_AsciiString anEntry;
496     TDF_Tool::Entry(aLabel, anEntry);
497     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
498   }
499
500   return obj._retn();
501 }
502
503 bool GEOM_Object_i::IsShape()
504 {
505   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
506 }
507
508 bool GEOM_Object_i::IsSame(GEOM::GEOM_Object_ptr other)
509 {
510   TopoDS_Shape thisShape  = _impl->GetValue();
511   TopoDS_Shape otherShape;
512   if ( !CORBA::is_nil( other ) ) {
513     Handle(GEOM_Object) otherObject = GEOM_Engine::GetEngine()->GetObject( other->GetStudyID(), other->GetEntry(), false );
514     if ( !otherObject.IsNull() )
515       otherShape = otherObject->GetValue();
516   }
517   return thisShape.IsSame( otherShape );
518 }
519
520 void GEOM_Object_i::SetParameters(const char* theParameters)
521 {
522   _impl->SetParameters((char*)theParameters);
523 }
524
525 char* GEOM_Object_i::GetParameters()
526 {
527   return CORBA::string_dup(_impl->GetParameters().ToCString());
528 }
529
530 GEOM::CreationInformation* GEOM_Object_i::GetCreationInformation()
531 {
532   GEOM::CreationInformation_var info = new GEOM::CreationInformation;
533
534   Handle(GEOM_BaseDriver) driver =
535     Handle(GEOM_BaseDriver)::DownCast( _impl->GetCreationDriver() );
536   if ( !driver.IsNull() )
537   {
538     std::vector<GEOM_Param> params;
539     std::string             operationName;
540     try
541     {
542       OCC_CATCH_SIGNALS;
543       if ( driver->GetCreationInformation( operationName, params ))
544       {
545         info->operationName = operationName.c_str();
546         info->params.length( params.size() );
547         for ( size_t i = 0; i < params.size(); ++i )
548         {
549           info->params[i].name  = params[i].name.c_str();
550           info->params[i].value = params[i].value.c_str();
551         }
552       }
553 #ifdef _DEBUG_
554       if ( operationName.empty() )
555       {
556         cout << endl << endl << endl << "Warning: " << endl << "Dear developer!!!" << endl
557              << "  Consider implementing "
558              <<    typeid(*(driver.operator->())).name() << "::GetCreationInformation() " << endl
559              << "  for the case of operation which has created '" << GetName() << "' object" << endl
560              << "PLEEEEEEEASE" << endl
561              << "\tPLEEEEEEEASE" << endl
562              << "\t\tPLEEEEEEEASE" << endl
563              << "\t\t\tPLEEEEEEEASE" << endl
564              << "\t\t\t\tPLEEEEEEEASE" << endl;
565       }
566 #endif
567     }
568     catch(...)
569     {
570 #ifdef _DEBUG_
571       cout << "Ecxeption in GEOM_Object_i::GetCreationInformation()" << endl;
572 #endif
573     }
574   }
575   return info._retn();
576 }