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)
55 thePOA->activate_object(this);
58 //=============================================================================
62 //=============================================================================
64 GEOM_Object_i::~GEOM_Object_i()
68 //=============================================================================
72 //=============================================================================
73 char* GEOM_Object_i::GetEntry()
75 const TDF_Label& aLabel = _impl->GetEntry();
76 TCollection_AsciiString anEntry;
77 TDF_Tool::Entry(aLabel, anEntry);
78 return CORBA::string_dup(anEntry.ToCString());
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();
133 if(aName) return strdup(aName);
137 //=============================================================================
141 //=============================================================================
142 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
144 _impl->SetAuxData(theEntry);
148 //=============================================================================
152 //=============================================================================
153 char* GEOM_Object_i::GetStudyEntry()
155 TCollection_AsciiString anEntry = _impl->GetAuxData();
156 if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
157 return CORBA::string_dup("");
161 //=============================================================================
165 //=============================================================================
166 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
168 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
171 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
172 if (aSeq.IsNull()) return aList._retn();
173 int aLength = aSeq->Length();
174 if (aLength == 0) return aList._retn();
176 aList->length(aLength);
178 TCollection_AsciiString anEntry;
180 for (int i = 1; i<=aLength; i++) {
181 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
182 if (anObj.IsNull()) continue;
183 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
184 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
188 return aList._retn();
191 //=============================================================================
195 //=============================================================================
196 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
198 GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
201 Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
202 if (aSeq.IsNull()) return aList._retn();
203 int aLength = aSeq->Length();
204 if (aLength == 0) return aList._retn();
206 aList->length(aLength);
208 TCollection_AsciiString anEntry;
210 for (int i = 1; i<=aLength; i++) {
211 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
212 if (anObj.IsNull()) continue;
213 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
214 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
218 return aList._retn();
221 //=================================================================================
222 // function : GetShapeStream
223 // Transfer resulting shape to client as sequence of bytes
224 //client can extract shape from stream using BrepTools::Read function
225 //=================================================================================
226 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
228 TopoDS_Shape aShape = _impl->GetValue();
230 if(aShape.IsNull()) return NULL;
232 ostrstream streamShape;
233 //Write TopoDS_Shape in ASCII format to the stream
234 BRepTools::Write(aShape, streamShape);
235 //Returns the number of bytes that have been stored in the stream's buffer.
236 int size = streamShape.pcount();
237 char* buf = new char [size];
238 //Get pointer on internal character array in ostrstream
239 char* valueOfStream = streamShape.str();
240 //Create copy of ostrstream content
241 memcpy(buf, valueOfStream, size);
242 //Allow automatic deletion of ostrstream content
243 streamShape.rdbuf()->freeze(0);
245 CORBA::Octet* OctetBuf = (CORBA::Octet*)buf;
246 SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
247 return SeqFile._retn();
252 //=======================================================================
253 //function : getShape
254 //purpose : return the TopoDS_Shape when client and servant are colocated, be careful
255 //=======================================================================
256 CORBA::LongLong GEOM_Object_i::getShape() {
257 _geom = _impl->GetValue();
258 return ((CORBA::LongLong)(&_geom));
261 //=============================================================================
265 //=============================================================================
266 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
268 GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
270 if(!_impl->IsMainShape()) {
271 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
272 if(aFunction.IsNull()) return anIndices._retn();
273 GEOM_ISubShape ISS(aFunction);
274 Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
275 if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
276 anIndices->length(anArray->Length());
277 for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
280 anIndices->length(0);
283 return anIndices._retn();
287 //=============================================================================
291 //=============================================================================
292 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
294 GEOM::GEOM_Object_var obj;
295 if(!_impl->IsMainShape()) {
296 Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last) one)
297 if(aFunction.IsNull()) return obj._retn();
298 GEOM_ISubShape ISS(aFunction);
300 aFunction = ISS.GetMainShape();
301 if(aFunction.IsNull()) return obj._retn();
302 TDF_Label aLabel = aFunction->GetOwnerEntry();
303 if(aLabel.IsNull()) return obj._retn();
304 TCollection_AsciiString anEntry;
305 TDF_Tool::Entry(aLabel, anEntry);
306 return GEOM::GEOM_Object::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
312 bool GEOM_Object_i::IsShape()
314 return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;