Salome HOME
sources v1.2
[modules/geom.git] / src / GEOM / GEOM_Shape_i.cc
1 //  GEOM GEOM : implementaion of GEOM_Gen.idl and GEOM_Shape.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GEOM_Shape_i.cc
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include <BRepTools_ShapeSet.hxx>
31 #include "GEOM_Shape_i.hh"
32 #include "SALOME_NamingService.hxx"
33 #include <fstream.h>
34 #include <BRepTools.hxx>
35
36
37
38 //=================================================================================
39 // function : GEOM_Shape_i() constructor (no arguments)
40 // purpose  : for what now ?
41 //=================================================================================
42 GEOM_Shape_i::GEOM_Shape_i() { }
43
44
45
46 //=================================================================================
47 // function : constructor
48 // purpose  : constructor for servant creation
49 //=================================================================================
50 GEOM_Shape_i::GEOM_Shape_i(TopoDS_Shape geom,
51                            CORBA::ORB_ptr orb,
52                            GEOM::GEOM_Gen_ptr engine,
53                            const GEOM::GEOM_Shape::ListOfSubShapeID& index,
54                            GEOM::shape_type sht,
55                            bool ismain) {
56   _geom = geom;
57   _orb = orb;
58   _engine = engine;
59   _shapetype = sht ;
60   _ismain = ismain;
61   _index = index ;
62
63   _shapeid = "";
64   _studyshapeid = "";
65
66   _name = "";
67   _mainname ="";
68   _nametype ="";
69
70 }
71
72
73
74 //=================================================================================
75 // function : destructor
76 // purpose  : deleting the internal geometry structure
77 //=================================================================================
78 GEOM_Shape_i::~GEOM_Shape_i() { delete &_geom; }
79
80
81 //=================================================================================
82 // function : Name (set method)
83 // purpose  : to set the attribute 'name' of this shape.
84 //          : WARNING : Register to naming service actually removed !
85 //=================================================================================
86 void GEOM_Shape_i::Name(const char* name) {
87   _name = strdup(name);
88   GEOM::GEOM_Shape_ptr g = GEOM::GEOM_Shape::_narrow(_this());
89
90   // Removed declaration of shapes to naming service
91   //SALOME_NamingService * ns = new SALOME_NamingService(_orb);
92   //ns->Register(g, _name);
93 }
94
95
96 //=================================================================================
97 // function : Name (get method)
98 // purpose  : to get the attribute 'name' of this shape
99 //=================================================================================
100 char* GEOM_Shape_i::Name() { return strdup(_name); }
101
102 //=================================================================================
103 // function : MainName (set method)
104 // purpose  : to set the attribute 'name' of this mainshape. 
105 //=================================================================================
106 void GEOM_Shape_i::MainName(const char* name) {
107   _mainname = strdup(name);
108 }
109
110
111 //=================================================================================
112 // function : MainName (get method)
113 // purpose  : to get the attribute 'name' of this shape
114 //=================================================================================
115 char* GEOM_Shape_i::MainName() { return strdup(_mainname); }
116
117 //=================================================================================
118 // function : IsMainShape (get method)
119 // purpose  : return 'true' if this is a main shape (not a sub shape)
120 //=================================================================================
121 bool GEOM_Shape_i::IsMainShape() { return _ismain ; }
122
123
124 //=================================================================================
125 // function : IsMainShape (set method)
126 // purpose  : to set the property 'ismain' true or false of this shape
127 //=================================================================================
128 void GEOM_Shape_i::IsMainShape(const bool abool) { _ismain = abool ; }
129
130
131 //=================================================================================
132 // function : ShapeId
133 // purpose  : to get the id of this shape from GEOM (OCAF entry)
134 //=================================================================================
135 char* GEOM_Shape_i::ShapeId() { return strdup(_shapeid) ; }
136
137
138 //=================================================================================
139 // function : ShapeId (set method) 
140 // purpose  : to set the id of this shape in GEOM/OCAF doc
141 //=================================================================================
142 void GEOM_Shape_i::ShapeId(const char * shapeid) { _shapeid = strdup(shapeid) ; }
143
144
145
146 //=================================================================================
147 // function : StudyShapeId (get method)
148 // purpose  : to get the id of this shape from the study document (OCAF entry)
149 //=================================================================================
150 char* GEOM_Shape_i::StudyShapeId() { return strdup(_studyshapeid) ; }
151
152
153 //=================================================================================
154 // function : StudyShapeId (set method) 
155 // purpose  : to set the id of this shape in the Study document (OCAF entry)
156 //=================================================================================
157 void GEOM_Shape_i::StudyShapeId(const char * studyshapeid)
158 { _studyshapeid = strdup(studyshapeid) ; }
159
160
161
162 //=================================================================================
163 // function : Index (get method)
164 // purpose  : to get the index of this sub shape in its main shape
165 //=================================================================================
166 GEOM::GEOM_Shape::ListOfSubShapeID* GEOM_Shape_i::Index() { 
167   unsigned int _length = _index.length();
168   GEOM::GEOM_Shape::ListOfSubShapeID_var _list = new GEOM::GEOM_Shape::ListOfSubShapeID;
169   _list->length(_length);
170
171   for (unsigned int ind = 0; ind < _length; ind++) {
172     _list[ind] = _index[ind];
173   }
174
175   return _list._retn() ; 
176 }
177
178
179 //=================================================================================
180 // function : Index (set method) 
181 // purpose  : to set the index of this sub shape (in a main shape)
182 //=================================================================================
183 void GEOM_Shape_i::Index(const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfSubShapeID) 
184 { _index = ListOfSubShapeID ; }
185
186
187 //=================================================================================
188 // function : ShapeType (get method)
189 // purpose  : to get the topological type of this shape as defined in enum of IDL file
190 //=================================================================================
191 GEOM::shape_type GEOM_Shape_i::ShapeType() { return _shapetype ; }
192
193
194 //=================================================================================
195 // function : SetType (set method)
196 // purpose  : to set the topological type of this shape (see GetType)
197 //=================================================================================
198 void GEOM_Shape_i::ShapeType(GEOM::shape_type sht) { _shapetype = sht ; }
199
200
201 //=================================================================================
202 // function : NameType (set method)
203 // purpose  : to set the attribute 'nametype' of this shape. 
204 //=================================================================================
205 void GEOM_Shape_i::NameType(const char* name) {
206 }
207
208 //=================================================================================
209 // function : NameType (get method)
210 // purpose  : to get the attribute 'nametype' of this shape
211 //=================================================================================
212 char* GEOM_Shape_i::NameType() { return strdup(_nametype); }
213
214 //=================================================================================
215 // function : GetShapeStream
216 // Transfer resulting shape to client as sequence of bytes
217 //client can extract shape from stream using BrepTools::Read function
218 //=================================================================================
219 GEOM::GEOM_Shape::TMPFile* GEOM_Shape_i::GetShapeStream()
220 {
221   ostrstream streamShape;
222   //Write TopoDS_Shape in ASCII format to the stream
223   BRepTools::Write(_geom, streamShape);
224   //Returns the number of bytes that have been stored in the stream's buffer.
225   int size = streamShape.pcount();
226   char* buf = new char [size];
227   //Get pointer on internal character array in ostrstream
228   char* valueOfStream = streamShape.str();
229   //Create copy of ostrstream content
230   memcpy(buf, valueOfStream, size);
231   //Allow automatic deletion of ostrstream content 
232   streamShape.rdbuf()->freeze(0);
233
234   CORBA::Octet* OctetBuf =  (CORBA::Octet*)buf;
235   GEOM::GEOM_Shape::TMPFile_var SeqFile = new GEOM::GEOM_Shape::TMPFile(size,size,OctetBuf,1);
236   return SeqFile._retn();
237
238 }
239
240 //=======================================================================
241 //function : Engine
242 //purpose  : return generator engine
243 //=======================================================================
244
245 GEOM::GEOM_Gen_ptr GEOM_Shape_i::Engine()
246 {
247   return _engine;
248 }