1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_OStream.hxx>
24 #include <GEOM_Object_i.hh>
25 #include <GEOM_ISubShape.hxx>
26 #include <GEOM_Engine.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include "utilities.h"
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>
41 #include <BRepTools_ShapeSet.hxx>
42 #include <BRepTools.hxx>
46 #pragma warning( disable:4786 )
49 //=============================================================================
53 //=============================================================================
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)
61 //=============================================================================
65 //=============================================================================
67 GEOM_Object_i::~GEOM_Object_i()
69 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
70 GEOM_Engine::GetEngine()->RemoveObject(_impl);
74 //=============================================================================
78 //=============================================================================
79 char* GEOM_Object_i::GetEntry()
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);
88 //=============================================================================
92 //=============================================================================
93 CORBA::Long GEOM_Object_i::GetStudyID()
95 return _impl->GetDocID();
99 //=============================================================================
103 //=============================================================================
104 CORBA::Long GEOM_Object_i::GetType()
106 return _impl->GetType();
109 //=============================================================================
113 //=============================================================================
114 GEOM::shape_type GEOM_Object_i::GetShapeType()
116 TopoDS_Shape _geom = _impl->GetValue();
117 if(_geom.IsNull()) return GEOM::SHAPE;
118 return (GEOM::shape_type)_geom.ShapeType();
121 //=============================================================================
125 //=============================================================================
126 void GEOM_Object_i::SetName(const char* theName)
128 _impl->SetName(theName);
132 //=============================================================================
136 //=============================================================================
137 char* GEOM_Object_i::GetName()
139 char* aName = _impl->GetName();
141 return aName; // this is already copy of pointer (see implementation of _impl)
145 //=============================================================================
149 //=============================================================================
150 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
152 _impl->SetColor(theColor);
156 //=============================================================================
160 //=============================================================================
161 SALOMEDS::Color GEOM_Object_i::GetColor()
163 return _impl->GetColor();
167 //=============================================================================
171 //=============================================================================
172 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
174 _impl->SetAutoColor(theAutoColor);
178 //=============================================================================
182 //=============================================================================
183 CORBA::Boolean GEOM_Object_i::GetAutoColor()
185 return _impl->GetAutoColor();
189 //=============================================================================
193 //=============================================================================
194 void GEOM_Object_i::SetMarkerStd(GEOM::marker_type theType, GEOM::marker_size theSize)
196 if ( theType == GEOM::MT_NONE || theSize == GEOM::MS_NONE ) {
197 _impl->UnsetMarker();
200 Aspect_TypeOfMarker aType = (Aspect_TypeOfMarker)( (int)theType-1 );
201 double aSize = ((int)theSize+1)*0.5;
202 _impl->SetMarkerStd( aType, aSize );
207 //=============================================================================
211 //=============================================================================
212 void GEOM_Object_i::SetMarkerTexture(CORBA::Long theTextureId)
214 _impl->SetMarkerTexture( theTextureId );
218 //=============================================================================
222 //=============================================================================
223 GEOM::marker_type GEOM_Object_i::GetMarkerType()
225 return (GEOM::marker_type)( (int)_impl->GetMarkerType()+1 );
229 //=============================================================================
233 //=============================================================================
234 GEOM::marker_size GEOM_Object_i::GetMarkerSize()
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;
241 //=============================================================================
245 //=============================================================================
246 CORBA::Long GEOM_Object_i::GetMarkerTexture()
248 return _impl->GetMarkerTexture();
252 //=============================================================================
256 //=============================================================================
257 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
259 _impl->SetAuxData(theEntry);
263 //=============================================================================
267 //=============================================================================
268 char* GEOM_Object_i::GetStudyEntry()
270 TCollection_AsciiString anEntry = _impl->GetAuxData();
271 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
272 return CORBA::string_dup("");
276 //=============================================================================
280 //=============================================================================
281 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
283 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
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();
291 aList->length(aLength);
293 TCollection_AsciiString anEntry;
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());
303 return aList._retn();
306 //=============================================================================
310 //=============================================================================
311 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
313 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
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();
321 aList->length(aLength);
323 TCollection_AsciiString anEntry;
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()));
333 return aList._retn();
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()
343 TopoDS_Shape aShape = _impl->GetValue();
345 if(aShape.IsNull()) return NULL;
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);
360 CORBA::Octet* OctetBuf = (CORBA::Octet*)buf;
361 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
362 return SeqFile._retn();
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));
376 //=============================================================================
380 //=============================================================================
381 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
383 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
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);
395 anIndices->length(0);
398 return anIndices._retn();
402 //=============================================================================
406 //=============================================================================
407 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
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);
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());
427 bool GEOM_Object_i::IsShape()
429 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
432 void GEOM_Object_i::SetParameters( SALOME::Notebook_ptr theNotebook, const SALOME::StringArray& theParameters )
434 theNotebook->ClearDependencies( _this(), SALOME::Parameters );
437 int n = theParameters.length();
438 Handle( GEOM_Function ) aFunc = _impl->GetLastFunction();
439 for( int i = 0; i < n; i++ )
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 );
446 aFunc->SetParam( i+1, TCollection_AsciiString( aParam.c_str() ) );
447 _parameters.push_back( aParam );
451 SALOME::StringArray* GEOM_Object_i::GetParameters()
453 SALOME::StringArray_var aParams = new SALOME::StringArray();
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() );
460 return aParams._retn();
463 char* GEOM_Object_i::GetComponent()
465 return CORBA::string_dup( "GEOM" );
468 CORBA::Boolean GEOM_Object_i::IsValid()
473 void GEOM_Object_i::Update( SALOME::Notebook_ptr theNotebook )
475 //printf( "GEOM_Object_i::Update:\n" );
476 Handle( GEOM_Function ) aFunc = _impl->GetLastFunction();
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++ )
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 ) )
489 switch( aParam->GetType() )
491 case SALOME::TInteger:
492 aFunc->SetInteger( i, aParam->AsInteger() );
496 //printf( "real: %i-th = %lf\n", i, aParam->AsReal() );
497 aFunc->SetReal( i, aParam->AsReal() );
500 case SALOME::TString:
501 aFunc->SetString( i, aParam->AsString() );
509 //2. Recompute object
510 GEOM_Solver aSolver( GEOM_Engine::GetEngine() );
511 aSolver.ComputeFunction( aFunc );
514 void GEOM_Object_i::StoreDependencies( SALOME::Notebook_ptr theNotebook )
516 theNotebook->ClearDependencies( _this(), SALOME::Objects );
518 Handle(TColStd_HSequenceOfTransient) aDeps = _impl->GetAllDependency();
520 TCollection_AsciiString anEntry;
521 for( int i = 1, n = aDeps->Length(); i <= n; i++ )
523 Handle( GEOM_Object ) anObj = Handle( GEOM_Object )::DownCast( aDeps->Value( i ) );
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() );