Salome HOME
Preparation of intermediate revision
[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 <GEOM_Engine.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 #include <TColStd_Array1OfAsciiString.hxx>
40
41 #include <BRepTools_ShapeSet.hxx>
42 #include <BRepTools.hxx>
43 #include <TopAbs.hxx>
44
45 #ifdef WNT
46 #pragma warning( disable:4786 )
47 #endif
48
49 //=============================================================================
50 /*!
51  *   constructor:
52  */
53 //=============================================================================
54
55 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
56                               Handle(GEOM_Object) theImpl)
57 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
58 {
59 }
60
61 //=============================================================================
62 /*!
63  *  destructor
64  */
65 //=============================================================================
66
67 GEOM_Object_i::~GEOM_Object_i()
68 {
69   MESSAGE("GEOM_Object_i::~GEOM_Object_i");
70   GEOM_Engine::GetEngine()->RemoveObject(_impl);
71 }
72
73
74 //=============================================================================
75 /*!
76  *  GetEntry
77  */
78 //=============================================================================
79 char* GEOM_Object_i::GetEntry()
80 {
81   const TDF_Label& aLabel = _impl->GetEntry();
82   TCollection_AsciiString anEntry;
83   TDF_Tool::Entry(aLabel, anEntry);
84   const char* anEntstr = anEntry.ToCString();
85   return CORBA::string_dup(anEntstr);
86 }
87
88 //=============================================================================
89 /*!
90  *  GetStudyID
91  */
92 //=============================================================================
93 CORBA::Long GEOM_Object_i::GetStudyID()
94 {
95    return _impl->GetDocID();
96 }
97
98
99 //=============================================================================
100 /*!
101  *  GetType
102  */
103 //=============================================================================
104 CORBA::Long GEOM_Object_i::GetType()
105 {
106   return _impl->GetType();
107 }
108
109 //=============================================================================
110 /*!
111  *  GetShapeType
112  */
113 //=============================================================================
114 GEOM::shape_type GEOM_Object_i::GetShapeType()
115 {
116   TopoDS_Shape _geom = _impl->GetValue();
117   if(_geom.IsNull()) return GEOM::SHAPE;
118   return (GEOM::shape_type)_geom.ShapeType();
119 }
120
121 //=============================================================================
122 /*!
123  *  SetName
124  */
125 //=============================================================================
126 void GEOM_Object_i::SetName(const char* theName)
127 {
128   _impl->SetName(theName);
129 }
130
131
132 //=============================================================================
133 /*!
134  *  GetName
135  */
136 //=============================================================================
137 char* GEOM_Object_i::GetName()
138 {
139   char* aName = _impl->GetName();
140   if (aName)
141     return aName; // this is already copy of pointer (see implementation of _impl)
142   return strdup("");
143 }
144
145 //=============================================================================
146 /*!
147  *  SetColor
148  */
149 //=============================================================================
150 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
151 {
152   _impl->SetColor(theColor);
153 }
154
155
156 //=============================================================================
157 /*!
158  *  GetColor
159  */
160 //=============================================================================
161 SALOMEDS::Color GEOM_Object_i::GetColor()
162 {
163   return _impl->GetColor();
164 }
165
166
167 //=============================================================================
168 /*!
169  *  SetAutoColor
170  */
171 //=============================================================================
172 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
173 {
174   _impl->SetAutoColor(theAutoColor);
175 }
176
177
178 //=============================================================================
179 /*!
180  *  GetAutoColor
181  */
182 //=============================================================================
183 CORBA::Boolean GEOM_Object_i::GetAutoColor()
184 {
185   return _impl->GetAutoColor();
186 }
187
188
189 //=============================================================================
190 /*!
191  *  SetMarkerStd
192  */
193 //=============================================================================
194 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
195 {
196   if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
197     _impl->UnsetMarker();
198   }
199   else {
200     Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
201     double aSize = ((int)theSize+1)*0.5;
202     _impl->SetMarkerStd( aType, aSize );
203   }
204 }
205
206
207 //=============================================================================
208 /*!
209  *  SetMarkerTexture
210  */
211 //=============================================================================
212 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
213 {
214   _impl->SetMarkerTexture( theTextureId );
215 }
216
217
218 //=============================================================================
219 /*!
220  *  GetMarkerType
221  */
222 //=============================================================================
223 GEOM::marker_type GEOM_Object_i::GetMarkerType()
224 {
225   return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
226 }
227
228
229 //=============================================================================
230 /*!
231  *  GetMarkerSize
232  */
233 //=============================================================================
234 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
235 {
236   int aSize = (int)( _impl->GetMarkerSize()/0.5 ) - 1;
237   return aSize < GEOM::MS_10 || aSize > GEOM::MS_70 ? GEOM::MS_NONE : (GEOM::marker_size)aSize;
238 }
239
240
241 //=============================================================================
242 /*!
243  *  GetMarkerTexture
244  */
245 //=============================================================================
246 CORBA::Long GEOM_Object_i::GetMarkerTexture()
247 {
248   return _impl->GetMarkerTexture();
249 }
250
251
252 //=============================================================================
253 /*!
254  *  SetStudyEntry
255  */
256 //=============================================================================
257 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
258 {
259   _impl->SetAuxData(theEntry);
260 }
261
262
263 //=============================================================================
264 /*!
265  *  GetStudyEntry
266  */
267 //=============================================================================
268 char* GEOM_Object_i::GetStudyEntry()
269 {
270   TCollection_AsciiString anEntry = _impl->GetAuxData();
271   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
272   return CORBA::string_dup("");
273 }
274
275
276 //=============================================================================
277 /*!
278  *  GetDependency
279  */
280 //=============================================================================
281 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
282 {
283   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
284   aList->length(0);
285
286   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
287   if (aSeq.IsNull()) return aList._retn();
288   int aLength = aSeq->Length();
289   if (aLength == 0) return aList._retn();
290
291   aList->length(aLength);
292
293   TCollection_AsciiString anEntry;
294
295   for (int i = 1; i<=aLength; i++) {
296     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
297     if (anObj.IsNull()) continue;
298     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
299     GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
300     aList[i-1] = obj;
301   }
302
303   return aList._retn();
304 }
305
306 //=============================================================================
307 /*!
308  * GetLastDependency
309  */
310 //=============================================================================
311 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
312 {
313   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
314   aList->length(0);
315
316   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
317   if (aSeq.IsNull()) return aList._retn();
318   int aLength = aSeq->Length();
319   if (aLength == 0) return aList._retn();
320
321   aList->length(aLength);
322
323   TCollection_AsciiString anEntry;
324
325   for (int i = 1; i<=aLength; i++) {
326      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
327      if (anObj.IsNull()) continue;
328      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
329      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
330      aList[i-1] = obj;
331   }
332
333   return aList._retn();
334 }
335
336 //=================================================================================
337 // function : GetShapeStream
338 // Transfer resulting shape to client as sequence of bytes
339 //client can extract shape from stream using BrepTools::Read function
340 //=================================================================================
341 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
342 {
343   TopoDS_Shape aShape = _impl->GetValue();
344
345   if(aShape.IsNull()) return NULL;
346
347   ostrstream streamShape;
348   //Write TopoDS_Shape in ASCII format to the stream
349   BRepTools::Write(aShape, streamShape);
350   //Returns the number of bytes that have been stored in the stream's buffer.
351   int size = streamShape.pcount();
352   char* buf = new char [size];
353   //Get pointer on internal character array in ostrstream
354   char* valueOfStream = streamShape.str();
355   //Create copy of ostrstream content
356   memcpy(buf, valueOfStream, size);
357   //Allow automatic deletion of ostrstream content
358   streamShape.rdbuf()->freeze(0);
359
360   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
361   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
362   return SeqFile._retn();
363
364 }
365
366
367 //=======================================================================
368 //function : getShape
369 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
370 //=======================================================================
371 CORBA::LongLong GEOM_Object_i::getShape() {
372   _geom = _impl->GetValue();
373   return ((CORBA::LongLong)(&_geom));
374 }
375
376 //=============================================================================
377 /*!
378  *  GetSubShapeIndices
379  */
380 //=============================================================================
381 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
382 {
383   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
384
385   if(!_impl->IsMainShape()) {
386     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
387     if(aFunction.IsNull()) return anIndices._retn();
388     GEOM_ISubShape ISS(aFunction);
389     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
390     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
391     anIndices->length(anArray->Length());
392     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
393   }
394   else {
395     anIndices->length(0);
396   }
397
398   return anIndices._retn();
399 }
400
401
402 //=============================================================================
403 /*!
404  *  GetMainShape
405  */
406 //=============================================================================
407 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
408 {
409   GEOM::GEOM_Object_var obj;
410   if(!_impl->IsMainShape()) {
411     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
412     if(aFunction.IsNull()) return obj._retn();
413     GEOM_ISubShape ISS(aFunction);
414
415     aFunction = ISS.GetMainShape();
416     if(aFunction.IsNull()) return obj._retn();
417     TDF_Label aLabel  = aFunction->GetOwnerEntry();
418     if(aLabel.IsNull()) return obj._retn();
419     TCollection_AsciiString anEntry;
420     TDF_Tool::Entry(aLabel, anEntry);
421     return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
422   }
423
424   return obj._retn();
425 }
426
427 bool GEOM_Object_i::IsShape()
428 {
429   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
430 }
431
432 void GEOM_Object_i::SetParameters( SALOME::Notebook_ptr theNotebook, const SALOME::StringArray& theParameters )
433 {
434   theNotebook->ClearDependencies( _this(), SALOME::Parameters );
435   _parameters.clear();
436
437   int n = theParameters.length();
438   Handle( GEOM_Function ) aFunc = _impl->GetLastFunction();
439   for( int i = 0; i < n; i++ )
440   {
441     std::string aParam = CORBA::string_dup( theParameters[i] );
442     SALOME::Parameter_ptr aParamPtr = theNotebook->GetParameter( aParam.c_str() );
443     if( !CORBA::is_nil( aParamPtr ) )
444       theNotebook->AddDependency( _this(), aParamPtr );
445
446     aFunc->SetParam( i+1, TCollection_AsciiString( aParam.c_str() ) );
447     _parameters.push_back( aParam );
448   }
449 }
450
451 SALOME::StringArray* GEOM_Object_i::GetParameters()
452 {
453   SALOME::StringArray_var aParams = new SALOME::StringArray();
454
455   aParams->length( _parameters.size() );
456   std::list<std::string>::const_iterator it = _parameters.begin(), last = _parameters.end();
457   for( int i=0; it!=last; it++, i++ )
458     aParams[i] = CORBA::string_dup( it->c_str() );
459
460   return aParams._retn();
461 }
462
463 char* GEOM_Object_i::GetComponent()
464 {
465   return CORBA::string_dup( "GEOM" );
466 }
467
468 CORBA::Boolean GEOM_Object_i::IsValid()
469 {
470   return true;
471 }
472
473 void GEOM_Object_i::Update( SALOME::Notebook_ptr theNotebook )
474 {
475   //printf( "GEOM_Object_i::Update:\n" );
476   Handle( GEOM_Function ) aFunc = _impl->GetLastFunction();
477
478   //1. Update parameter values
479   int n = aFunc->GetArgsCount();
480   //printf( "args count = %i\n", n );
481   for( int i = 1; i <= n; i++ )
482   {
483     TCollection_AsciiString aParamName = aFunc->GetParam( i );
484     //printf( "arg = %s\n", aParamName.ToCString() );
485     SALOME::Parameter_ptr aParam = theNotebook->GetParameter( aParamName.ToCString() );
486     if( CORBA::is_nil( aParam ) )
487       continue;
488
489     switch( aParam->GetType() )
490     {
491     case SALOME::TInteger:
492       aFunc->SetInteger( i, aParam->AsInteger() );
493       break;
494
495     case SALOME::TReal:
496       //printf( "real: %i-th = %lf\n", i, aParam->AsReal() );
497       aFunc->SetReal( i, aParam->AsReal() );
498       break;
499
500     case SALOME::TString:
501       aFunc->SetString( i, aParam->AsString() );
502       break;
503
504     default:
505       return;
506     }
507   }
508
509   //2. Recompute object
510   GEOM_Solver aSolver( GEOM_Engine::GetEngine() );
511   aSolver.ComputeFunction( aFunc );
512 }
513
514 void GEOM_Object_i::StoreDependencies( SALOME::Notebook_ptr theNotebook )
515 {
516   theNotebook->ClearDependencies( _this(), SALOME::Objects );
517
518   Handle(TColStd_HSequenceOfTransient) aDeps = _impl->GetAllDependency();
519
520   TCollection_AsciiString anEntry;
521   for( int i = 1, n = aDeps->Length(); i <= n; i++ )
522   {
523     Handle( GEOM_Object ) anObj = Handle( GEOM_Object )::DownCast( aDeps->Value( i ) );
524     if( anObj.IsNull() )
525       continue;
526
527     TDF_Tool::Entry( anObj->GetEntry(), anEntry );
528     GEOM::GEOM_Object_var obj = _engine->GetObject( anObj->GetDocID(), anEntry.ToCString() );
529     theNotebook->AddDependency( _this(), obj._retn() );
530   }
531 }