Salome HOME
*** empty log message ***
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <Standard_OStream.hxx>
21
22 #include <GEOM_Object_i.hh>
23 //#include <GEOM_Gen_i.hh>
24 #include <GEOM_ISubShape.hxx>
25 #include <GEOMImpl_Types.hxx>
26
27 #include "utilities.h"
28 #include <fstream>
29 #include <strstream>
30
31 #include <OpUtil.hxx>
32 #include <Utils_ExceptHandlers.hxx>
33 #include <TDF_Tool.hxx>
34 #include <TDF_Label.hxx>
35 #include <TCollection_AsciiString.hxx>
36
37 #include <BRepTools_ShapeSet.hxx>
38 #include <BRepTools.hxx>
39 #include <TopAbs.hxx>
40
41 #ifdef WNT
42 #pragma warning( disable:4786 )
43 #endif
44
45 //=============================================================================
46 /*!
47  *   constructor:
48  */
49 //=============================================================================
50
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)
54 {
55 }
56
57 //=============================================================================
58 /*!
59  *  destructor
60  */
61 //=============================================================================
62
63 GEOM_Object_i::~GEOM_Object_i()
64 {}
65
66
67 //=============================================================================
68 /*!
69  *  GetEntry
70  */
71 //=============================================================================
72 char* GEOM_Object_i::GetEntry()
73 {
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);
79 }
80
81 //=============================================================================
82 /*!
83  *  GetStudyID
84  */
85 //=============================================================================
86 CORBA::Long GEOM_Object_i::GetStudyID()
87 {
88    return _impl->GetDocID();
89 }
90
91
92 //=============================================================================
93 /*!
94  *  GetType
95  */
96 //=============================================================================
97 CORBA::Long GEOM_Object_i::GetType()
98 {
99   return _impl->GetType();
100 }
101
102 //=============================================================================
103 /*!
104  *  GetShapeType
105  */
106 //=============================================================================
107 GEOM::shape_type GEOM_Object_i::GetShapeType()
108 {
109   TopoDS_Shape _geom = _impl->GetValue();
110   if(_geom.IsNull()) return GEOM::SHAPE;
111   return (GEOM::shape_type)_geom.ShapeType();
112 }
113
114 //=============================================================================
115 /*!
116  *  SetName
117  */
118 //=============================================================================
119 void GEOM_Object_i::SetName(const char* theName)
120 {
121   _impl->SetName(theName);
122 }
123
124
125 //=============================================================================
126 /*!
127  *  GetName
128  */
129 //=============================================================================
130 char* GEOM_Object_i::GetName()
131 {
132   char* aName = _impl->GetName();
133   if (aName)
134     return aName; // this is already copy of pointer (see implementation of _impl)
135   return strdup("");
136 }
137
138 //=============================================================================
139 /*!
140  *  SetStudyEntry
141  */
142 //=============================================================================
143 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
144 {
145   _impl->SetAuxData(theEntry);
146 }
147
148
149 //=============================================================================
150 /*!
151  *  GetStudyEntry
152  */
153 //=============================================================================
154 char* GEOM_Object_i::GetStudyEntry()
155 {
156   TCollection_AsciiString anEntry = _impl->GetAuxData();
157   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
158   return CORBA::string_dup("");
159 }
160
161
162 //=============================================================================
163 /*!
164  *  GetDependency
165  */
166 //=============================================================================
167 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
168 {
169   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
170   aList->length(0);
171
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();
176
177   aList->length(aLength);
178
179   TCollection_AsciiString anEntry;
180
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()));
186     aList[i-1] = obj;
187   }
188
189   return aList._retn();
190 }
191
192 //=============================================================================
193 /*!
194  * GetLastDependency
195  */
196 //=============================================================================
197 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
198 {
199   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
200   aList->length(0);
201
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();
206
207   aList->length(aLength);
208
209   TCollection_AsciiString anEntry;
210
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()));
216      aList[i-1] = obj;
217   }
218
219   return aList._retn();
220 }
221
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()
228 {
229   TopoDS_Shape aShape = _impl->GetValue();
230
231   if(aShape.IsNull()) return NULL;
232
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);
245
246   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
247   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
248   return SeqFile._retn();
249
250 }
251
252
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));
260 }
261
262 //=============================================================================
263 /*!
264  *  GetSubShapeIndices
265  */
266 //=============================================================================
267 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
268 {
269   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
270
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);
279   }
280   else {
281     anIndices->length(0);
282   }
283
284   return anIndices._retn();
285 }
286
287
288 //=============================================================================
289 /*!
290  *  GetMainShape
291  */
292 //=============================================================================
293 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
294 {
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);
300
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()));
308   }
309
310   return obj._retn();
311 }
312
313 bool GEOM_Object_i::IsShape()
314 {
315   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
316 }