1 // Copyright (C) 2014-2024 CEA, EDF, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "XAOPlugin_IOperations_i.hh"
22 #include "XAOPlugin_IOperations.hxx"
25 #include <utilities.h>
27 //=============================================================================
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 )
37 MESSAGE( "XAOPlugin_IOperations_i::XAOPlugin_IOperations_i" );
40 //=============================================================================
44 //=============================================================================
45 XAOPlugin_IOperations_i::~XAOPlugin_IOperations_i()
47 MESSAGE( "XAOPlugin_IOperations_i::~XAOPlugin_IOperations_i" );
50 //=============================================================================
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.
61 //=============================================================================
62 CORBA::Boolean XAOPlugin_IOperations_i::ExportXAO( GEOM::GEOM_Object_ptr shape,
63 const GEOM::ListOfGO& groups,
64 const GEOM::ListOfFields& fields,
67 const char* shapeFileName)
69 exportXAO( shape, groups, fields, author, true, fileName, shapeFileName );
73 //=============================================================================
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
82 //=============================================================================
83 SALOMEDS::TMPFile* XAOPlugin_IOperations_i::ExportXAOMem( GEOM::GEOM_Object_ptr shape,
84 const GEOM::ListOfGO& groups,
85 const GEOM::ListOfFields& fields,
88 std::string anXMLBuf = exportXAO( shape, groups, fields, author, false, "", "" );
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();
97 //=============================================================================
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
109 //=============================================================================
110 std::string XAOPlugin_IOperations_i::exportXAO( GEOM::GEOM_Object_ptr shape,
111 const GEOM::ListOfGO& groups,
112 const GEOM::ListOfFields& fields,
115 const char* fileName,
116 const char* shapeFileName)
118 std::string anXMLBuff;
119 // Set a not done flag
120 GetOperations()->SetNotDone();
122 // Get the reference shape
123 Handle(GEOM_Object) reference = GetObjectImpl( shape );
124 if( reference.IsNull() )
127 // Get the reference groups
128 CORBA::ULong ind = 0;
129 std::list<Handle(GEOM_Object)> groupsObj;
130 for (; ind < groups.length(); ind++)
132 Handle(GEOM_Object) gobj = GetObjectImpl( groups[ind] );
133 if (gobj.IsNull()) return anXMLBuff;
134 groupsObj.push_back(gobj);
137 // Get the reference fields
139 std::list<Handle(GEOM_Field)> fieldsObj;
140 for( ; ind < fields.length(); ind++ )
142 Handle(GEOM_Field) fobj = Handle(GEOM_Field)::DownCast( GetBaseObjectImpl( fields[ind] ) );
143 if( fobj.IsNull() ) return anXMLBuff;
144 fieldsObj.push_back(fobj);
150 GetOperations()->ExportXAO( reference, groupsObj, fieldsObj, author, fileName, shapeFileName );
154 anXMLBuff = GetOperations()->ExportXAOMem( reference, groupsObj, fieldsObj, author );
160 //=============================================================================
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.
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)
177 SALOMEDS::TMPFile_var aBuff;
178 importXAO( true, fileName, aBuff, shape, subShapes, groups, fields);
182 //=============================================================================
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.
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)
199 importXAO( false, NULL, theBuff, shape, subShapes, groups, fields);
203 //=============================================================================
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.
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)
224 GEOM::GEOM_Object_var vshape;
225 shape = vshape._retn();
227 subShapes = new GEOM::ListOfGO;
228 groups = new GEOM::ListOfGO;
229 fields = new GEOM::ListOfFields;
231 // Set a not done flag
232 GetOperations()->SetNotDone();
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;
240 GetOperations()->ImportXAO( fileName, hshape, importedSubShapes, importedGroups, importedFields );
243 if (theBuff.length() < 1)
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 );
252 if( !GetOperations()->IsDone() )
256 int n = importedSubShapes->Length();
257 subShapes->length(n);
258 for( int i = 1; i <= n; i++ )
260 (*subShapes)[i - 1] = GetObject( Handle(GEOM_Object)::DownCast( importedSubShapes->Value(i) ) );
264 n = importedGroups->Length();
266 for( int i = 1; i <= n; i++ )
268 (*groups)[i - 1] = GetObject( Handle(GEOM_Object)::DownCast( importedGroups->Value(i) ) );
272 n = importedFields->Length();
274 for( int i = 1; i <= n; i++ )
276 (*fields)[i - 1] = GEOM::GEOM_Field::_narrow(
277 GetBaseObject( Handle(GEOM_Field)::DownCast( importedFields->Value(i) ) ) );
280 shape = GetObject( hshape );
285 XAOPlugin_IOperations* XAOPlugin_IOperations_i::GetOperations()
287 return (XAOPlugin_IOperations*)GetImpl();