Salome HOME
428c6c737d07e36e8b7e2f63458ec45cd6e92664
[modules/geom.git] / src / XAOPlugin / XAOPlugin_IOperations_i.cc
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D, 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
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 to shape to this external file
59  *  \return boolean indicating if export was succeful.
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   bool isGood = false;
70   // Set a not done flag
71   GetOperations()->SetNotDone();
72
73   // Get the reference shape
74   Handle(GEOM_Object) reference = GetObjectImpl( shape );
75
76   // Get the reference groups
77   CORBA::ULong ind = 0;
78   std::list<Handle(GEOM_Object)> groupsObj;
79   for (; ind < groups.length(); ind++)
80   {
81     Handle(GEOM_Object) gobj = GetObjectImpl( groups[ind] );
82     if (gobj.IsNull()) return false;
83     groupsObj.push_back(gobj);
84   }
85
86   // Get the reference fields
87   ind = 0;
88   std::list<Handle(GEOM_Field)> fieldsObj;
89   for( ; ind < fields.length(); ind++ )
90   {
91     Handle(GEOM_Field) fobj = Handle(GEOM_Field)::DownCast( GetBaseObjectImpl( fields[ind] ) );
92     if( fobj.IsNull() ) return false;
93     fieldsObj.push_back(fobj);
94   }
95
96   if( !reference.IsNull() )
97   {
98     // Export XAO
99     isGood = GetOperations()->ExportXAO( reference, groupsObj, fieldsObj, author, fileName, shapeFileName );
100   }
101
102   return isGood;
103 }
104
105 //=============================================================================
106 /*!
107  *  Import a shape from XAO format
108  *  \param fileName The name of the file to import
109  *  \param shape The imported shape
110  *  \param subShapes The list of imported subShapes
111  *  \param groups The list of imported groups
112  *  \param fields The list of imported fields
113  *  \return boolean indicating if import was succeful.
114  */
115 //=============================================================================
116 CORBA::Boolean XAOPlugin_IOperations_i::ImportXAO( const char* fileName,
117                                                    GEOM::GEOM_Object_out shape,
118                                                    GEOM::ListOfGO_out subShapes,
119                                                    GEOM::ListOfGO_out groups,
120                                                    GEOM::ListOfFields_out fields)
121 {
122   GEOM::GEOM_Object_var vshape;
123   shape = vshape._retn();
124
125   subShapes = new GEOM::ListOfGO;
126   groups = new GEOM::ListOfGO;
127   fields = new GEOM::ListOfFields;
128
129   // Set a not done flag
130   GetOperations()->SetNotDone();
131
132   Handle(TColStd_HSequenceOfTransient) importedSubShapes = new TColStd_HSequenceOfTransient();
133   Handle(TColStd_HSequenceOfTransient) importedGroups = new TColStd_HSequenceOfTransient();
134   Handle(TColStd_HSequenceOfTransient) importedFields = new TColStd_HSequenceOfTransient();
135   Handle(GEOM_Object) hshape;
136   bool res = GetOperations()->ImportXAO( fileName, hshape, importedSubShapes, importedGroups, importedFields );
137
138   if( !GetOperations()->IsDone() || !res )
139     return false;
140
141   // parse fields
142   int n = importedSubShapes->Length();
143   subShapes->length(n);
144   for( int i = 1; i <= n; i++ )
145   {
146     (*subShapes)[i - 1] = GetObject( Handle(GEOM_Object)::DownCast( importedSubShapes->Value(i) ) );
147   }
148
149   // parse groups
150   n = importedGroups->Length();
151   groups->length(n);
152   for( int i = 1; i <= n; i++ )
153   {
154     (*groups)[i - 1] = GetObject( Handle(GEOM_Object)::DownCast( importedGroups->Value(i) ) );
155   }
156
157   // parse fields
158   n = importedFields->Length();
159   fields->length(n);
160   for( int i = 1; i <= n; i++ )
161   {
162     (*fields)[i - 1] = GEOM::GEOM_Field::_narrow(
163       GetBaseObject( Handle(GEOM_Field)::DownCast( importedFields->Value(i) ) ) );
164   }
165
166   shape = GetObject( hshape );
167
168   return res;
169 }
170
171 XAOPlugin_IOperations* XAOPlugin_IOperations_i::GetOperations()
172 {
173   return (XAOPlugin_IOperations*)GetImpl();
174 }