]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Object_i.cc
Salome HOME
fix for Bug IPAL9442
[modules/geom.git] / src / GEOM_I / GEOM_Object_i.cc
1 #include <Standard_OStream.hxx>
2
3 #include <GEOM_Object_i.hh>
4 //#include <GEOM_Gen_i.hh>
5 #include <GEOM_ISubShape.hxx>
6 #include <GEOMImpl_Types.hxx>
7
8 #include "utilities.h"
9 #include <fstream>
10 #include <strstream>
11
12 #include <OpUtil.hxx>
13 #include <Utils_ExceptHandlers.hxx>
14 #include <TDF_Tool.hxx>
15 #include <TDF_Label.hxx>
16 #include <TCollection_AsciiString.hxx>
17
18 #include <BRepTools_ShapeSet.hxx>
19 #include <BRepTools.hxx>
20 #include <TopAbs.hxx>
21
22 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 //=============================================================================
27 /*!
28  *   constructor:
29  */
30 //=============================================================================
31
32 GEOM_Object_i::GEOM_Object_i (PortableServer::POA_ptr thePOA, GEOM::GEOM_Gen_ptr theEngine,
33                               Handle(GEOM_Object) theImpl)
34 #ifndef WNT
35 : SALOME::GenericObj_i( thePOA ), _engine(theEngine), _impl(theImpl)
36 #endif
37 {
38   thePOA->activate_object(this);
39 }
40
41 //=============================================================================
42 /*!
43  *  destructor
44  */
45 //=============================================================================
46
47 GEOM_Object_i::~GEOM_Object_i()
48 {}
49
50
51 //=============================================================================
52 /*!
53  *  GetEntry
54  */
55 //=============================================================================
56 char* GEOM_Object_i::GetEntry()
57 {
58   const TDF_Label& aLabel = _impl->GetEntry();
59   TCollection_AsciiString anEntry;
60   TDF_Tool::Entry(aLabel, anEntry);
61   return CORBA::string_dup(anEntry.ToCString());
62 }
63
64 //=============================================================================
65 /*!
66  *  GetStudyID
67  */
68 //=============================================================================
69 CORBA::Long GEOM_Object_i::GetStudyID()
70 {
71    return _impl->GetDocID();
72 }
73
74
75 //=============================================================================
76 /*!
77  *  GetType
78  */
79 //=============================================================================
80 CORBA::Long GEOM_Object_i::GetType()
81 {
82   return _impl->GetType();
83 }
84
85 //=============================================================================
86 /*!
87  *  GetShapeType
88  */
89 //=============================================================================
90 GEOM::shape_type GEOM_Object_i::GetShapeType()
91 {
92   TopoDS_Shape _geom = _impl->GetValue();
93   if(_geom.IsNull()) return GEOM::SHAPE;
94   return (GEOM::shape_type)_geom.ShapeType();
95 }
96
97 //=============================================================================
98 /*!
99  *  SetName
100  */
101 //=============================================================================
102 void GEOM_Object_i::SetName(const char* theName)
103 {
104   _impl->SetName(theName);
105 }
106
107
108 //=============================================================================
109 /*!
110  *  GetName
111  */
112 //=============================================================================
113 char* GEOM_Object_i::GetName()
114 {
115   char* aName = _impl->GetName();
116   if(aName) return strdup(aName);
117   return strdup("");
118 }
119
120 //=============================================================================
121 /*!
122  *  SetStudyEntry
123  */
124 //=============================================================================
125 void GEOM_Object_i::SetStudyEntry(const char* theEntry)
126 {
127   _impl->SetAuxData(theEntry);
128 }
129
130
131 //=============================================================================
132 /*!
133  *  GetStudyEntry
134  */
135 //=============================================================================
136 char* GEOM_Object_i::GetStudyEntry()
137 {
138   TCollection_AsciiString anEntry = _impl->GetAuxData();
139   if(!anEntry.IsEmpty()) return strdup(anEntry.ToCString());
140   return strdup("");
141 }
142
143
144 //=============================================================================
145 /*!
146  *  GetDependency
147  */
148 //=============================================================================
149 GEOM::ListOfGO* GEOM_Object_i::GetDependency()
150 {
151   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
152   aList->length(0);
153
154   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetAllDependency();
155   if (aSeq.IsNull()) return aList._retn();
156   int aLength = aSeq->Length();
157   if (aLength == 0) return aList._retn();
158
159   aList->length(aLength);
160
161   TCollection_AsciiString anEntry;
162
163   for (int i = 1; i<=aLength; i++) {
164     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
165     if (anObj.IsNull()) continue;
166     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
167     GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
168     aList[i-1] = obj;
169   }
170
171   return aList._retn();
172 }
173
174 //=============================================================================
175 /*!
176  * GetLastDependency
177  */
178 //=============================================================================
179 GEOM::ListOfGO* GEOM_Object_i::GetLastDependency()
180 {
181   GEOM::ListOfGO_var aList = new GEOM::ListOfGO();
182   aList->length(0);
183
184   Handle(TColStd_HSequenceOfTransient) aSeq = _impl->GetLastDependency();
185   if (aSeq.IsNull()) return aList._retn();
186   int aLength = aSeq->Length();
187   if (aLength == 0) return aList._retn();
188
189   aList->length(aLength);
190
191   TCollection_AsciiString anEntry;
192
193   for (int i = 1; i<=aLength; i++) {
194      Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
195      if (anObj.IsNull()) continue;
196      TDF_Tool::Entry(anObj->GetEntry(), anEntry);
197      GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_duplicate(_engine->GetObject(anObj->GetDocID(), anEntry.ToCString()));
198      aList[i-1] = obj;
199   }
200
201   return aList._retn();
202 }
203
204 //=================================================================================
205 // function : GetShapeStream
206 // Transfer resulting shape to client as sequence of bytes
207 //client can extract shape from stream using BrepTools::Read function
208 //=================================================================================
209 SALOMEDS::TMPFile* GEOM_Object_i::GetShapeStream()
210 {
211   TopoDS_Shape aShape = _impl->GetValue();
212
213   if(aShape.IsNull()) return NULL;
214
215   ostrstream streamShape;
216   //Write TopoDS_Shape in ASCII format to the stream
217   BRepTools::Write(aShape, streamShape);
218   //Returns the number of bytes that have been stored in the stream's buffer.
219   int size = streamShape.pcount();
220   char* buf = new char [size];
221   //Get pointer on internal character array in ostrstream
222   char* valueOfStream = streamShape.str();
223   //Create copy of ostrstream content
224   memcpy(buf, valueOfStream, size);
225   //Allow automatic deletion of ostrstream content
226   streamShape.rdbuf()->freeze(0);
227
228   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
229   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
230   return SeqFile._retn();
231
232 }
233
234
235 //=======================================================================
236 //function : getShape
237 //purpose  : return the TopoDS_Shape when client and servant are colocated, be careful
238 //=======================================================================
239 long GEOM_Object_i::getShape() {
240   _geom = _impl->GetValue();
241   return((long)(&_geom));
242 }
243
244 //=============================================================================
245 /*!
246  *  GetSubShapeIndices
247  */
248 //=============================================================================
249 GEOM::ListOfLong* GEOM_Object_i::GetSubShapeIndices()
250 {
251   GEOM::ListOfLong_var anIndices = new GEOM::ListOfLong;
252
253   if(!_impl->IsMainShape()) {
254     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
255     if(aFunction.IsNull()) return anIndices._retn();
256     GEOM_ISubShape ISS(aFunction);
257     Handle(TColStd_HArray1OfInteger) anArray = ISS.GetIndices();
258     if(anArray.IsNull() || anArray->Length() < 1) return anIndices._retn();
259     anIndices->length(anArray->Length());
260     for(int i=1; i<=anArray->Length(); i++) anIndices[i-1] = anArray->Value(i);
261   }
262   else {
263     anIndices->length(0);
264   }
265
266   return anIndices._retn();
267 }
268
269
270 //=============================================================================
271 /*!
272  *  GetMainShape
273  */
274 //=============================================================================
275 GEOM::GEOM_Object_ptr GEOM_Object_i::GetMainShape()
276 {
277   GEOM::GEOM_Object_var obj;
278   if(!_impl->IsMainShape()) {
279     Handle(GEOM_Function) aFunction = _impl->GetFunction(1); //Get SubShape function (always the first (and last)  one)
280     if(aFunction.IsNull()) return obj._retn();
281     GEOM_ISubShape ISS(aFunction);
282
283     aFunction = ISS.GetMainShape();
284     if(aFunction.IsNull()) return obj._retn();
285     TDF_Label aLabel  = aFunction->GetOwnerEntry();
286     if(aLabel.IsNull()) return obj._retn();
287     TCollection_AsciiString anEntry;
288     TDF_Tool::Entry(aLabel, anEntry);
289     return GEOM::GEOM_Object::_duplicate(_engine->GetObject(_impl->GetDocID(), anEntry.ToCString()));
290   }
291
292   return obj._retn();
293 }
294
295 bool GEOM_Object_i::IsShape()
296 {
297   return !_impl->GetValue().IsNull() && _impl->GetType() != GEOM_MARKER;
298 }