Salome HOME
Updated copyright comment
[modules/geom.git] / src / XAOPlugin / XAOPlugin_IOperations_i.cc
1 // Copyright (C) 2014-2024  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "XAOPlugin_IOperations_i.hh"
22 #include "XAOPlugin_IOperations.hxx"
23
24 // KERNEL includes
25 #include <utilities.h>
26
27 //=============================================================================
28 /*!
29  *   constructor:
30  */
31 //=============================================================================
32 XAOPlugin_IOperations_i::XAOPlugin_IOperations_i( PortableServer::POA_ptr thePOA,
33                                                   GEOM::GEOM_Gen_ptr theEngine,
34                                                   XAOPlugin_IOperations* theImpl )
35 :GEOM_IOperations_i( thePOA, theEngine, theImpl )
36 {
37   MESSAGE( "XAOPlugin_IOperations_i::XAOPlugin_IOperations_i" );
38 }
39
40 //=============================================================================
41 /*!
42  *  destructor
43  */
44 //=============================================================================
45 XAOPlugin_IOperations_i::~XAOPlugin_IOperations_i()
46 {
47   MESSAGE( "XAOPlugin_IOperations_i::~XAOPlugin_IOperations_i" );
48 }
49
50 //=============================================================================
51 /*!
52  *  Export a shape to XAO format file
53  *  \param shape The shape to export
54  *  \param groups The list of groups to export
55  *  \param fields The list of fields to export
56  *  \param author The author of the export
57  *  \param fileName The name of the exported file
58  *  \param shapeFileName If not empty, save the BREP shape to this external file
59  *  \return boolean indicating if export was successful.
60  */
61 //=============================================================================
62 CORBA::Boolean XAOPlugin_IOperations_i::ExportXAO( GEOM::GEOM_Object_ptr shape,
63                                                    const GEOM::ListOfGO& groups,
64                                                    const GEOM::ListOfFields& fields,
65                                                    const char* author,
66                                                    const char* fileName,
67                                                    const char* shapeFileName)
68 {
69   exportXAO( shape, groups, fields, author, true, fileName, shapeFileName );
70   return IsDone();
71 }
72
73 //=============================================================================
74 /*!
75  *  Export a shape to XAO format buffer
76  *  \param shape The shape to export
77  *  \param groups The list of groups to export
78  *  \param fields The list of fields to export
79  *  \param author The author of the export
80  *  \return The output buffer
81  */
82 //=============================================================================
83 SALOMEDS::TMPFile* XAOPlugin_IOperations_i::ExportXAOMem( GEOM::GEOM_Object_ptr shape,
84                                                           const GEOM::ListOfGO& groups,
85                                                           const GEOM::ListOfFields& fields,
86                                                           const char* author )
87 {
88   std::string anXMLBuf = exportXAO( shape, groups, fields, author, false, "", "" );
89
90   int size = anXMLBuf.size();
91   CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
92   memcpy(OctetBuf, anXMLBuf.c_str(), size);
93   SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile (size,size,OctetBuf,1);
94   return SeqFile._retn();
95 }
96
97 //=============================================================================
98 /*!
99  *  Export a shape to XAO format
100  *  \param shape The shape to export
101  *  \param groups The list of groups to export
102  *  \param fields The list of fields to export
103  *  \param author The author of the export
104  *  \param toFile Export to file if true, otherwise export to memory buffer (the returned string)
105  *  \param fileName The name of the exported file
106  *  \param shapeFileName If not empty, save the BREP shape to this external file
107  *  \return string The exported buffer if toFile=false, otherwise an empty string
108  */
109 //=============================================================================
110 std::string XAOPlugin_IOperations_i::exportXAO( GEOM::GEOM_Object_ptr shape,
111                                                 const GEOM::ListOfGO& groups,
112                                                 const GEOM::ListOfFields& fields,
113                                                 const char* author,
114                                                 const bool  toFile,
115                                                 const char* fileName,
116                                                 const char* shapeFileName)
117 {
118   std::string anXMLBuff;
119   // Set a not done flag
120   GetOperations()->SetNotDone();
121
122   // Get the reference shape
123   Handle(GEOM_Object) reference = GetObjectImpl( shape );
124   if( reference.IsNull() )
125     return anXMLBuff;
126
127   // Get the reference groups
128   CORBA::ULong ind = 0;
129   std::list<Handle(GEOM_Object)> groupsObj;
130   for (; ind < groups.length(); ind++)
131   {
132     Handle(GEOM_Object) gobj = GetObjectImpl( groups[ind] );
133     if (gobj.IsNull()) return anXMLBuff;
134     groupsObj.push_back(gobj);
135   }
136
137   // Get the reference fields
138   ind = 0;
139   std::list<Handle(GEOM_Field)> fieldsObj;
140   for( ; ind < fields.length(); ind++ )
141   {
142     Handle(GEOM_Field) fobj = Handle(GEOM_Field)::DownCast( GetBaseObjectImpl( fields[ind] ) );
143     if( fobj.IsNull() ) return anXMLBuff;
144     fieldsObj.push_back(fobj);
145   }
146
147   if ( toFile )
148   {
149     // Export XAO
150     GetOperations()->ExportXAO( reference, groupsObj, fieldsObj, author, fileName, shapeFileName );
151   }
152   else
153   {
154     anXMLBuff = GetOperations()->ExportXAOMem( reference, groupsObj, fieldsObj, author );
155   }
156
157   return anXMLBuff;
158 }
159
160 //=============================================================================
161 /*!
162  *  Import a shape from XAO format
163  *  \param fileName The name of the file to import
164  *  \param shape The imported shape
165  *  \param subShapes The list of imported subShapes
166  *  \param groups The list of imported groups
167  *  \param fields The list of imported fields
168  *  \return boolean indicating if import was successful.
169  */
170 //=============================================================================
171 CORBA::Boolean XAOPlugin_IOperations_i::ImportXAO( const char* fileName,
172                                                    GEOM::GEOM_Object_out shape,
173                                                    GEOM::ListOfGO_out subShapes,
174                                                    GEOM::ListOfGO_out groups,
175                                                    GEOM::ListOfFields_out fields)
176 {
177   SALOMEDS::TMPFile_var aBuff;
178   importXAO( true, fileName, aBuff, shape, subShapes, groups, fields);
179   return IsDone();
180 }
181
182 //=============================================================================
183 /*!
184  *  Import a shape from XAO format memory buffer
185  *  \param fileName The name of the file to import
186  *  \param shape The imported shape
187  *  \param subShapes The list of imported subShapes
188  *  \param groups The list of imported groups
189  *  \param fields The list of imported fields
190  *  \return boolean indicating if import was successful.
191  */
192 //=============================================================================
193 CORBA::Boolean XAOPlugin_IOperations_i::ImportXAOMem( const SALOMEDS::TMPFile& theBuff,
194                                                       GEOM::GEOM_Object_out shape,
195                                                       GEOM::ListOfGO_out subShapes,
196                                                       GEOM::ListOfGO_out groups,
197                                                       GEOM::ListOfFields_out fields)
198 {
199   importXAO( false, NULL, theBuff, shape, subShapes, groups, fields);
200   return IsDone();
201 }
202
203 //=============================================================================
204 /*!
205  *  Import a shape from XAO format
206  *  \param isFile Boolean flag to switch between import from file or from buffer
207  *  \param fileName The name of the file to import from if isFile=true
208  *  \param theBuff The buffer to import from if isFile=false
209  *  \param shape The imported shape
210  *  \param subShapes The list of imported subShapes
211  *  \param groups The list of imported groups
212  *  \param fields The list of imported fields
213  *  \return boolean indicating if import was successful.
214  */
215 //=============================================================================
216 CORBA::Boolean XAOPlugin_IOperations_i::importXAO( const bool isFile,
217                                                    const char* fileName,
218                                                    const SALOMEDS::TMPFile& theBuff,
219                                                    GEOM::GEOM_Object_out shape,
220                                                    GEOM::ListOfGO_out subShapes,
221                                                    GEOM::ListOfGO_out groups,
222                                                    GEOM::ListOfFields_out fields)
223 {
224   GEOM::GEOM_Object_var vshape;
225   shape = vshape._retn();
226
227   subShapes = new GEOM::ListOfGO;
228   groups = new GEOM::ListOfGO;
229   fields = new GEOM::ListOfFields;
230
231   // Set a not done flag
232   GetOperations()->SetNotDone();
233
234   Handle(TColStd_HSequenceOfTransient) importedSubShapes = new TColStd_HSequenceOfTransient();
235   Handle(TColStd_HSequenceOfTransient) importedGroups = new TColStd_HSequenceOfTransient();
236   Handle(TColStd_HSequenceOfTransient) importedFields = new TColStd_HSequenceOfTransient();
237   Handle(GEOM_Object) hshape;
238
239   if (isFile) {
240     GetOperations()->ImportXAO( fileName, hshape, importedSubShapes, importedGroups, importedFields );
241   }
242   else {
243     if (theBuff.length() < 1)
244       return false;
245
246     char* buf = (char*)theBuff.NP_data();
247     //std::string anXMLBuff (buf); // works wrongly
248     std::string anXMLBuff (buf, theBuff.length());
249     GetOperations()->ImportXAOMem( anXMLBuff, hshape, importedSubShapes, importedGroups, importedFields );
250   }
251
252   if( !GetOperations()->IsDone() )
253     return false;
254
255   // parse fields
256   int n = importedSubShapes->Length();
257   subShapes->length(n);
258   for( int i = 1; i <= n; i++ )
259   {
260     (*subShapes)[i - 1] = GetObject( Handle(GEOM_Object)::DownCast( importedSubShapes->Value(i) ) );
261   }
262
263   // parse groups
264   n = importedGroups->Length();
265   groups->length(n);
266   for( int i = 1; i <= n; i++ )
267   {
268     (*groups)[i - 1] = GetObject( Handle(GEOM_Object)::DownCast( importedGroups->Value(i) ) );
269   }
270
271   // parse fields
272   n = importedFields->Length();
273   fields->length(n);
274   for( int i = 1; i <= n; i++ )
275   {
276     (*fields)[i - 1] = GEOM::GEOM_Field::_narrow(
277       GetBaseObject( Handle(GEOM_Field)::DownCast( importedFields->Value(i) ) ) );
278   }
279
280   shape = GetObject( hshape );
281
282   return IsDone();
283 }
284
285 XAOPlugin_IOperations* XAOPlugin_IOperations_i::GetOperations()
286 {
287   return (XAOPlugin_IOperations*)GetImpl();
288 }