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_Gen_i.hh>
26 #include <GEOM_ISubShape.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include "utilities.h"
34 #include <Utils_ExceptHandlers.hxx>
35 #include <TDF_Tool.hxx>
36 #include <TDF_Label.hxx>
37 #include <TCollection_AsciiString.hxx>
39 #include <BRepTools_ShapeSet.hxx>
40 #include <BRepTools.hxx>
44 #pragma warning( disable:4786 )
47 //=============================================================================
51 //=============================================================================
53 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
54 Handle(GEOM_Object) theImpl)
55 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
59 //=============================================================================
63 //=============================================================================
65 GEOM_Object_i::~GEOM_Object_i()
67 GEOM_Engine::GetEngine()->RemoveObject(_impl);
71 //=============================================================================
75 //=============================================================================
76 char* GEOM_Object_i::GetEntry()
78 const TDF_Label& aLabel = _impl->GetEntry();
79 TCollection_AsciiString anEntry;
80 TDF_Tool::Entry(aLabel, anEntry);
81 const char* anEntstr = anEntry.ToCString();
82 return CORBA::string_dup(anEntstr);
85 //=============================================================================
89 //=============================================================================
90 CORBA::Long GEOM_Object_i::GetStudyID()
92 return _impl->GetDocID();
96 //=============================================================================
100 //=============================================================================
101 CORBA::Long GEOM_Object_i::GetType()
103 return _impl->GetType();
106 //=============================================================================
110 //=============================================================================
111 GEOM::shape_type GEOM_Object_i::GetShapeType()
113 TopoDS_Shape _geom = _impl->GetValue();
114 if(_geom.IsNull()) return GEOM::SHAPE;
115 return (GEOM::shape_type)_geom.ShapeType();
118 //=============================================================================
122 //=============================================================================
123 void GEOM_Object_i::SetName(const char* theName)
125 _impl->SetName(theName);
129 //=============================================================================
133 //=============================================================================
134 char* GEOM_Object_i::GetName()
136 char* aName = _impl->GetName();
138 return aName; // this is already copy of pointer (see implementation of _impl)
142 //=============================================================================
146 //=============================================================================
147 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
149 _impl->SetColor(theColor);
153 //=============================================================================
157 //=============================================================================
158 SALOMEDS::Color GEOM_Object_i::GetColor()
160 return _impl->GetColor();
164 //=============================================================================
168 //=============================================================================
169 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
171 _impl->SetAutoColor(theAutoColor);
175 //=============================================================================
179 //=============================================================================
180 CORBA::Boolean GEOM_Object_i::GetAutoColor()
182 return _impl->GetAutoColor();
186 //=============================================================================
190 //=============================================================================
191 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
193 _impl->SetAuxData(theEntry);
197 //=============================================================================
201 //=============================================================================
202 char* GEOM_Object_i::GetStudyEntry()
204 TCollection_AsciiString anEntry = _impl->GetAuxData();
205 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
206 return CORBA::string_dup("");
210 //=============================================================================
214 //=============================================================================
215 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
217 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
220 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
221 if (aSeq.IsNull()) return aList._retn();
222 int aLength = aSeq->Length();
223 if (aLength == 0) return aList._retn();
225 aList->length(aLength);
227 TCollection_AsciiString anEntry;
229 for (int i = 1; i<=aLength; i++) {
230 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
231 if (anObj.IsNull()) continue;
232 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
233 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
237 return aList._retn();
240 //=============================================================================
244 //=============================================================================
245 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
247 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
250 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
251 if (aSeq.IsNull()) return aList._retn();
252 int aLength = aSeq->Length();
253 if (aLength == 0) return aList._retn();
255 aList->length(aLength);
257 TCollection_AsciiString anEntry;
259 for (int i = 1; i<=aLength; i++) {
260 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
261 if (anObj.IsNull()) continue;
262 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
263 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
267 return aList._retn();
270 //=================================================================================
271 // function : GetShapeStream
272 // Transfer resulting shape to client as sequence of bytes
273 //client can extract shape from stream using BrepTools::Read function
274 //=================================================================================
275 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
277 TopoDS_Shape aShape = _impl->GetValue();
279 if(aShape.IsNull()) return NULL;
281 ostrstream streamShape;
282 //Write TopoDS_Shape in ASCII format to the stream
283 BRepTools::Write(aShape, streamShape);
284 //Returns the number of bytes that have been stored in the stream's buffer.
285 int size = streamShape.pcount();
286 char* buf = new char [size];
287 //Get pointer on internal character array in ostrstream
288 char* valueOfStream = streamShape.str();
289 //Create copy of ostrstream content
290 memcpy(buf, valueOfStream, size);
291 //Allow automatic deletion of ostrstream content
292 streamShape.rdbuf()->freeze(0);
294 CORBA::Octet* OctetBuf = (CORBA::Octet*)buf;
295 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
296 return SeqFile._retn();
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));
310 //=============================================================================
314 //=============================================================================
315 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
317 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
319 if(!_impl->IsMainShape()) {
320 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape 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);
329 anIndices->length(0);
332 return anIndices._retn();
336 //=============================================================================
340 //=============================================================================
341 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
343 GEOM::GEOM_Object_var obj;
344 if(!_impl->IsMainShape()) {
345 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
346 if(aFunction.IsNull()) return obj._retn();
347 GEOM_ISubShape ISS(aFunction);
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::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
361 bool GEOM_Object_i::IsShape()
363 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
366 void GEOM_Object_i::SetParameters(const char* theParameters)
368 _impl->SetParameters((char*)theParameters);
371 char* GEOM_Object_i::GetParameters()
373 return CORBA::string_dup(_impl->GetParameters().ToCString());