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 <GEOMImpl_Types.hxx>
28 #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 MESSAGE("GEOM_Object_i::~GEOM_Object_i");
68 GEOM_Engine::GetEngine()->RemoveObject(_impl);
72 //=============================================================================
76 //=============================================================================
77 char* GEOM_Object_i::GetEntry()
79 const TDF_Label& aLabel = _impl->GetEntry();
80 TCollection_AsciiString anEntry;
81 TDF_Tool::Entry(aLabel, anEntry);
82 const char* anEntstr = anEntry.ToCString();
83 return CORBA::string_dup(anEntstr);
86 //=============================================================================
90 //=============================================================================
91 CORBA::Long GEOM_Object_i::GetStudyID()
93 return _impl->GetDocID();
97 //=============================================================================
101 //=============================================================================
102 CORBA::Long GEOM_Object_i::GetType()
104 return _impl->GetType();
107 //=============================================================================
111 //=============================================================================
112 GEOM::shape_type GEOM_Object_i::GetShapeType()
114 TopoDS_Shape _geom = _impl->GetValue();
115 if(_geom.IsNull()) return GEOM::SHAPE;
116 return (GEOM::shape_type)_geom.ShapeType();
119 //=============================================================================
123 //=============================================================================
124 void GEOM_Object_i::SetName(const char* theName)
126 _impl->SetName(theName);
130 //=============================================================================
134 //=============================================================================
135 char* GEOM_Object_i::GetName()
137 char* aName = _impl->GetName();
139 return aName; // this is already copy of pointer (see implementation of _impl)
143 //=============================================================================
147 //=============================================================================
148 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
150 _impl->SetColor(theColor);
154 //=============================================================================
158 //=============================================================================
159 SALOMEDS::Color GEOM_Object_i::GetColor()
161 return _impl->GetColor();
165 //=============================================================================
169 //=============================================================================
170 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
172 _impl->SetAutoColor(theAutoColor);
176 //=============================================================================
180 //=============================================================================
181 CORBA::Boolean GEOM_Object_i::GetAutoColor()
183 return _impl->GetAutoColor();
187 //=============================================================================
191 //=============================================================================
192 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
194 _impl->SetAuxData(theEntry);
198 //=============================================================================
202 //=============================================================================
203 char* GEOM_Object_i::GetStudyEntry()
205 TCollection_AsciiString anEntry = _impl->GetAuxData();
206 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
207 return CORBA::string_dup("");
211 //=============================================================================
215 //=============================================================================
216 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
218 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
221 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
222 if (aSeq.IsNull()) return aList._retn();
223 int aLength = aSeq->Length();
224 if (aLength == 0) return aList._retn();
226 aList->length(aLength);
228 TCollection_AsciiString anEntry;
230 for (int i = 1; i<=aLength; i++) {
231 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
232 if (anObj.IsNull()) continue;
233 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
234 GEOM::GEOM_Object_var obj = _engine->GetObject(anObj->GetDocID(), anEntry.ToCString());
238 return aList._retn();
241 //=============================================================================
245 //=============================================================================
246 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
248 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
251 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
252 if (aSeq.IsNull()) return aList._retn();
253 int aLength = aSeq->Length();
254 if (aLength == 0) return aList._retn();
256 aList->length(aLength);
258 TCollection_AsciiString anEntry;
260 for (int i = 1; i<=aLength; i++) {
261 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
262 if (anObj.IsNull()) continue;
263 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
264 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
268 return aList._retn();
271 //=================================================================================
272 // function : GetShapeStream
273 // Transfer resulting shape to client as sequence of bytes
274 //client can extract shape from stream using BrepTools::Read function
275 //=================================================================================
276 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
278 TopoDS_Shape aShape = _impl->GetValue();
280 if(aShape.IsNull()) return NULL;
282 ostrstream streamShape;
283 //Write TopoDS_Shape in ASCII format to the stream
284 BRepTools::Write(aShape, streamShape);
285 //Returns the number of bytes that have been stored in the stream's buffer.
286 int size = streamShape.pcount();
287 char* buf = new char [size];
288 //Get pointer on internal character array in ostrstream
289 char* valueOfStream = streamShape.str();
290 //Create copy of ostrstream content
291 memcpy(buf, valueOfStream, size);
292 //Allow automatic deletion of ostrstream content
293 streamShape.rdbuf()->freeze(0);
295 CORBA::Octet* OctetBuf = (CORBA::Octet*)buf;
296 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
297 return SeqFile._retn();
302 //=======================================================================
303 //function : getShape
304 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
305 //=======================================================================
306 CORBA::LongLong GEOM_Object_i::getShape() {
307 _geom = _impl->GetValue();
308 return ((CORBA::LongLong)(&_geom));
311 //=============================================================================
315 //=============================================================================
316 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
318 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
320 if(!_impl->IsMainShape()) {
321 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
322 if(aFunction.IsNull()) return anIndices._retn();
323 GEOM_ISubShape ISS(aFunction);
324 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
325 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
326 anIndices->length(anArray->Length());
327 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
330 anIndices->length(0);
333 return anIndices._retn();
337 //=============================================================================
341 //=============================================================================
342 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
344 GEOM::GEOM_Object_var obj;
345 if(!_impl->IsMainShape()) {
346 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
347 if(aFunction.IsNull()) return obj._retn();
348 GEOM_ISubShape ISS(aFunction);
350 aFunction = ISS.GetMainShape();
351 if(aFunction.IsNull()) return obj._retn();
352 TDF_Label aLabel = aFunction->GetOwnerEntry();
353 if(aLabel.IsNull()) return obj._retn();
354 TCollection_AsciiString anEntry;
355 TDF_Tool::Entry(aLabel, anEntry);
356 return _engine->GetObject(_impl->GetDocID(), anEntry.ToCString());
362 bool GEOM_Object_i::IsShape()
364 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
367 void GEOM_Object_i::SetParameters(const char* theParameters)
369 _impl->SetParameters((char*)theParameters);
372 char* GEOM_Object_i::GetParameters()
374 return CORBA::string_dup(_impl->GetParameters().ToCString());