]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Object_i.cc
Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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  *  SetColor
141  */
142 //=============================================================================
143 void GEOM_Object_i::SetColor(const SALOMEDS::Color& theColor)
144 {
145   _impl->SetColor(theColor);
146 }
147
148
149 //=============================================================================
150 /*!
151  *  GetColor
152  */
153 //=============================================================================
154 SALOMEDS::Color GEOM_Object_i::GetColor()
155 {
156   return _impl->GetColor();
157 }
158
159
160 //=============================================================================
161 /*!
162  *  SetAutoColor
163  */
164 //=============================================================================
165 void GEOM_Object_i::SetAutoColor(CORBA::Boolean theAutoColor)
166 {
167   _impl->SetAutoColor(theAutoColor);
168 }
169
170
171 //=============================================================================
172 /*!
173  *  GetAutoColor
174  */
175 //=============================================================================
176 CORBA::Boolean GEOM_Object_i::GetAutoColor()
177 {
178   return _impl->GetAutoColor();
179 }
180
181
182 //=============================================================================
183 /*!
184  *  SetStudyEntry
185  */
186 //=============================================================================
187 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
188 {
189   _impl->SetAuxData(theEntry);
190 }
191
192
193 //=============================================================================
194 /*!
195  *  GetStudyEntry
196  */
197 //=============================================================================
198 char* GEOM_Object_i::GetStudyEntry()
199 {
200   TCollection_AsciiString anEntry = _impl->GetAuxData();
201   if(!anEntry.IsEmpty()) return CORBA::string_dup(anEntry.ToCString());
202   return CORBA::string_dup("");
203 }
204
205
206 //=============================================================================
207 /*!
208  *  GetDependency
209  */
210 //=============================================================================
211 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
212 {
213   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
214   aList->length(0);
215
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();
220
221   aList->length(aLength);
222
223   TCollection_AsciiString anEntry;
224
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()));
230     aList[i-1] = obj;
231   }
232
233   return aList._retn();
234 }
235
236 //=============================================================================
237 /*!
238  * GetLastDependency
239  */
240 //=============================================================================
241 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
242 {
243   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
244   aList->length(0);
245
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();
250
251   aList->length(aLength);
252
253   TCollection_AsciiString anEntry;
254
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()));
260      aList[i-1] = obj;
261   }
262
263   return aList._retn();
264 }
265
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()
272 {
273   TopoDS_Shape aShape = _impl->GetValue();
274
275   if(aShape.IsNull()) return NULL;
276
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);
289
290   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
291   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
292   return SeqFile._retn();
293
294 }
295
296
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));
304 }
305
306 //=============================================================================
307 /*!
308  *  GetSubShapeIndices
309  */
310 //=============================================================================
311 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
312 {
313   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
314
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);
323   }
324   else {
325     anIndices->length(0);
326   }
327
328   return anIndices._retn();
329 }
330
331
332 //=============================================================================
333 /*!
334  *  GetMainShape
335  */
336 //=============================================================================
337 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
338 {
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);
344
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()));
352   }
353
354   return obj._retn();
355 }
356
357 bool GEOM_Object_i::IsShape()
358 {
359   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
360 }