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::SetStudyEntry(const char* theEntry)
145 _impl->SetAuxData(theEntry);
149 //=============================================================================
153 //=============================================================================
154 char* GEOM_Object_i::GetStudyEntry()
156 TCollection_AsciiString anEntry = _impl->GetAuxData();
157 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
158 return CORBA::string_dup("");
162 //=============================================================================
166 //=============================================================================
167 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
169 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
172 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
173 if (aSeq.IsNull()) return aList._retn();
174 int aLength = aSeq->Length();
175 if (aLength == 0) return aList._retn();
177 aList->length(aLength);
179 TCollection_AsciiString anEntry;
181 for (int i = 1; i<=aLength; i++) {
182 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
183 if (anObj.IsNull()) continue;
184 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
185 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
189 return aList._retn();
192 //=============================================================================
196 //=============================================================================
197 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
199 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
202 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
203 if (aSeq.IsNull()) return aList._retn();
204 int aLength = aSeq->Length();
205 if (aLength == 0) return aList._retn();
207 aList->length(aLength);
209 TCollection_AsciiString anEntry;
211 for (int i = 1; i<=aLength; i++) {
212 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
213 if (anObj.IsNull()) continue;
214 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
215 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
219 return aList._retn();
222 //=================================================================================
223 // function : GetShapeStream
224 // Transfer resulting shape to client as sequence of bytes
225 //client can extract shape from stream using BrepTools::Read function
226 //=================================================================================
227 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
229 TopoDS_Shape aShape = _impl->GetValue();
231 if(aShape.IsNull()) return NULL;
233 ostrstream streamShape;
234 //Write TopoDS_Shape in ASCII format to the stream
235 BRepTools::Write(aShape, streamShape);
236 //Returns the number of bytes that have been stored in the stream's buffer.
237 int size = streamShape.pcount();
238 char* buf = new char [size];
239 //Get pointer on internal character array in ostrstream
240 char* valueOfStream = streamShape.str();
241 //Create copy of ostrstream content
242 memcpy(buf, valueOfStream, size);
243 //Allow automatic deletion of ostrstream content
244 streamShape.rdbuf()->freeze(0);
246 CORBA::Octet* OctetBuf = (CORBA::Octet*)buf;
247 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
248 return SeqFile._retn();
253 //=======================================================================
254 //function : getShape
255 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
256 //=======================================================================
257 CORBA::LongLong GEOM_Object_i::getShape() {
258 _geom = _impl->GetValue();
259 return ((CORBA::LongLong)(&_geom));
262 //=============================================================================
266 //=============================================================================
267 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
269 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
271 if(!_impl->IsMainShape()) {
272 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
273 if(aFunction.IsNull()) return anIndices._retn();
274 GEOM_ISubShape ISS(aFunction);
275 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
276 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
277 anIndices->length(anArray->Length());
278 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
281 anIndices->length(0);
284 return anIndices._retn();
288 //=============================================================================
292 //=============================================================================
293 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
295 GEOM::GEOM_Object_var obj;
296 if(!_impl->IsMainShape()) {
297 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
298 if(aFunction.IsNull()) return obj._retn();
299 GEOM_ISubShape ISS(aFunction);
301 aFunction = ISS.GetMainShape();
302 if(aFunction.IsNull()) return obj._retn();
303 TDF_Label aLabel = aFunction->GetOwnerEntry();
304 if(aLabel.IsNull()) return obj._retn();
305 TCollection_AsciiString anEntry;
306 TDF_Tool::Entry(aLabel, anEntry);
307 return GEOM::GEOM_Object::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
313 bool GEOM_Object_i::IsShape()
315 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;