1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_OStream.hxx>
22 #include <GEOM_Object_i.hh>
23 //#include <GEOM_Gen_i.hh>
24 #include <GEOM_ISubShape.hxx>
25 #include <GEOMImpl_Types.hxx>
27 #include "utilities.h"
32 #include <Utils_ExceptHandlers.hxx>
33 #include <TDF_Tool.hxx>
34 #include <TDF_Label.hxx>
35 #include <TCollection_AsciiString.hxx>
37 #include <BRepTools_ShapeSet.hxx>
38 #include <BRepTools.hxx>
42 #pragma warning( disable:4786 )
45 //=============================================================================
49 //=============================================================================
51 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
52 Handle(GEOM_Object) theImpl)
53 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
57 //=============================================================================
61 //=============================================================================
63 GEOM_Object_i::~GEOM_Object_i()
67 //=============================================================================
71 //=============================================================================
72 char* GEOM_Object_i::GetEntry()
74 const TDF_Label& aLabel = _impl->GetEntry();
75 TCollection_AsciiString anEntry;
76 TDF_Tool::Entry(aLabel, anEntry);
77 const char* anEntstr = anEntry.ToCString();
78 return CORBA::string_dup(anEntstr);
81 //=============================================================================
85 //=============================================================================
86 CORBA::Long GEOM_Object_i::GetStudyID()
88 return _impl->GetDocID();
92 //=============================================================================
96 //=============================================================================
97 CORBA::Long GEOM_Object_i::GetType()
99 return _impl->GetType();
102 //=============================================================================
106 //=============================================================================
107 GEOM::shape_type GEOM_Object_i::GetShapeType()
109 TopoDS_Shape _geom = _impl->GetValue();
110 if(_geom.IsNull()) return GEOM::SHAPE;
111 return (GEOM::shape_type)_geom.ShapeType();
114 //=============================================================================
118 //=============================================================================
119 void GEOM_Object_i::SetName(const char* theName)
121 _impl->SetName(theName);
125 //=============================================================================
129 //=============================================================================
130 char* GEOM_Object_i::GetName()
132 char* aName = _impl->GetName();
134 return aName; // this is already copy of pointer (see implementation of _impl)
138 //=============================================================================
142 //=============================================================================
143 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
145 _impl->SetColor(theColor);
149 //=============================================================================
153 //=============================================================================
154 SALOMEDS::Color GEOM_Object_i::GetColor()
156 return _impl->GetColor();
160 //=============================================================================
164 //=============================================================================
165 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
167 _impl->SetAutoColor(theAutoColor);
171 //=============================================================================
175 //=============================================================================
176 CORBA::Boolean GEOM_Object_i::GetAutoColor()
178 return _impl->GetAutoColor();
182 //=============================================================================
186 //=============================================================================
187 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
189 _impl->SetAuxData(theEntry);
193 //=============================================================================
197 //=============================================================================
198 char* GEOM_Object_i::GetStudyEntry()
200 TCollection_AsciiString anEntry = _impl->GetAuxData();
201 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
202 return CORBA::string_dup("");
206 //=============================================================================
210 //=============================================================================
211 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
213 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
216 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
217 if (aSeq.IsNull()) return aList._retn();
218 int aLength = aSeq->Length();
219 if (aLength == 0) return aList._retn();
221 aList->length(aLength);
223 TCollection_AsciiString anEntry;
225 for (int i = 1; i<=aLength; i++) {
226 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
227 if (anObj.IsNull()) continue;
228 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
229 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
233 return aList._retn();
236 //=============================================================================
240 //=============================================================================
241 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
243 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
246 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
247 if (aSeq.IsNull()) return aList._retn();
248 int aLength = aSeq->Length();
249 if (aLength == 0) return aList._retn();
251 aList->length(aLength);
253 TCollection_AsciiString anEntry;
255 for (int i = 1; i<=aLength; i++) {
256 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
257 if (anObj.IsNull()) continue;
258 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
259 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
263 return aList._retn();
266 //=================================================================================
267 // function : GetShapeStream
268 // Transfer resulting shape to client as sequence of bytes
269 //client can extract shape from stream using BrepTools::Read function
270 //=================================================================================
271 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
273 TopoDS_Shape aShape = _impl->GetValue();
275 if(aShape.IsNull()) return NULL;
277 ostrstream streamShape;
278 //Write TopoDS_Shape in ASCII format to the stream
279 BRepTools::Write(aShape, streamShape);
280 //Returns the number of bytes that have been stored in the stream's buffer.
281 int size = streamShape.pcount();
282 char* buf = new char [size];
283 //Get pointer on internal character array in ostrstream
284 char* valueOfStream = streamShape.str();
285 //Create copy of ostrstream content
286 memcpy(buf, valueOfStream, size);
287 //Allow automatic deletion of ostrstream content
288 streamShape.rdbuf()->freeze(0);
290 CORBA::Octet* OctetBuf = (CORBA::Octet*)buf;
291 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
292 return SeqFile._retn();
297 //=======================================================================
298 //function : getShape
299 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
300 //=======================================================================
301 CORBA::LongLong GEOM_Object_i::getShape() {
302 _geom = _impl->GetValue();
303 return ((CORBA::LongLong)(&_geom));
306 //=============================================================================
310 //=============================================================================
311 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
313 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
315 if(!_impl->IsMainShape()) {
316 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
317 if(aFunction.IsNull()) return anIndices._retn();
318 GEOM_ISubShape ISS(aFunction);
319 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
320 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
321 anIndices->length(anArray->Length());
322 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
325 anIndices->length(0);
328 return anIndices._retn();
332 //=============================================================================
336 //=============================================================================
337 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
339 GEOM::GEOM_Object_var obj;
340 if(!_impl->IsMainShape()) {
341 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
342 if(aFunction.IsNull()) return obj._retn();
343 GEOM_ISubShape ISS(aFunction);
345 aFunction = ISS.GetMainShape();
346 if(aFunction.IsNull()) return obj._retn();
347 TDF_Label aLabel = aFunction->GetOwnerEntry();
348 if(aLabel.IsNull()) return obj._retn();
349 TCollection_AsciiString anEntry;
350 TDF_Tool::Entry(aLabel, anEntry);
351 return GEOM::GEOM_Object::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
357 bool GEOM_Object_i::IsShape()
359 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;