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