Salome HOME
ff78f6f06b5e3ec7f5f1ae87dd57f76466ca311d
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_ImportSource1D_i.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 //  File   : StdMeshers_ImportSource1D_i.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_ImportSource1D_i.hxx"
28
29 #include "SMESH_Gen.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_Group_i.hxx"
32 #include "SMESH_PythonDump.hxx"
33 #include "StdMeshers_ObjRefUlils.hxx"
34
35 #include "Utils_CorbaException.hxx"
36 #include "utilities.h"
37
38 #include <TCollection_AsciiString.hxx>
39
40 #include CORBA_SERVER_HEADER(SMESH_Group)
41
42 using namespace std;
43
44 //=============================================================================
45 /*!
46  *  StdMeshers_ImportSource1D_i::StdMeshers_ImportSource1D_i
47  *
48  *  Constructor
49  */
50 //=============================================================================
51
52 StdMeshers_ImportSource1D_i::StdMeshers_ImportSource1D_i( PortableServer::POA_ptr thePOA,
53                                                           int                     theStudyId,
54                                                           ::SMESH_Gen*            theGenImpl )
55   : SALOME::GenericObj_i( thePOA ), 
56     SMESH_Hypothesis_i( thePOA )
57 {
58   MESSAGE( "StdMeshers_ImportSource1D_i::StdMeshers_ImportSource1D_i" );
59   myBaseImpl = new ::StdMeshers_ImportSource1D( theGenImpl->GetANewId(),
60                                                 theStudyId,
61                                                 theGenImpl );
62   _groupEntries = new SMESH::string_array();
63 }
64
65 //=============================================================================
66 /*!
67  *  StdMeshers_ImportSource1D_i::~StdMeshers_ImportSource1D_i
68  *
69  *  Destructor
70  */
71 //=============================================================================
72
73 StdMeshers_ImportSource1D_i::~StdMeshers_ImportSource1D_i()
74 {
75   MESSAGE( "StdMeshers_ImportSource1D_i::~StdMeshers_ImportSource1D_i" );
76 }
77
78 //=============================================================================
79 /*!
80  *  SetSourceEdges
81  */
82 //=============================================================================
83
84 void StdMeshers_ImportSource1D_i::SetSourceEdges(const SMESH::ListOfGroups& groups)
85 {
86   MESSAGE( "StdMeshers_ImportSource1D_i::SetSourceEdges" );
87   ASSERT( myBaseImpl );
88   try
89   {
90     std::vector<SMESH_Group*> smesh_groups;
91     std::vector<string> entries;
92     SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
93     for ( int i = 0; i < groups.length(); ++i )
94       if ( SMESH_GroupBase_i* gp_i = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i] ))
95       {
96         if ( gp_i->GetType() != SMESH::EDGE )
97           THROW_SALOME_CORBA_EXCEPTION("Wrong group type", SALOME::BAD_PARAM);
98         smesh_groups.push_back( gp_i->GetSmeshGroup() );
99
100         SALOMEDS::SObject_var so = SMESH_Gen_i::GetSMESHGen()->ObjectToSObject(study, groups[i]);
101         if ( !so->_is_nil())
102         {
103           CORBA::String_var entry = so->GetID();
104           entries.push_back( entry.in() );
105         }
106       }
107     this->GetImpl()->SetGroups( smesh_groups );
108
109     _groupEntries = new SMESH::string_array;
110     _groupEntries->length( entries.size ());
111     for ( int i = 0; i < entries.size(); ++i )
112       _groupEntries[i] = entries[i].c_str();
113   }
114   catch ( SALOME_Exception& S_ex )
115   {
116     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
117   }
118
119   // Update Python script
120   SMESH::TPythonDump() << _this() << ".SetSourceEdges( " << groups << " )";
121 }
122
123 //=============================================================================
124 /*!
125  * Return entries of groups
126  */
127 //=============================================================================
128
129 SMESH::string_array*  StdMeshers_ImportSource1D_i::GetSourceEdges()
130 {
131   MESSAGE( "StdMeshers_ImportSource1D_i::GetImportSource" );
132   SMESH::string_array_var res = new SMESH::string_array( _groupEntries );
133   return res._retn();
134 }
135
136 //================================================================================
137 /*!
138  * \brief Set to copy mesh and groups
139  */
140 //================================================================================
141
142 void StdMeshers_ImportSource1D_i::SetCopySourceMesh(CORBA::Boolean toCopyMesh,
143                                                     CORBA::Boolean toCopyGroups)
144 {
145   GetImpl()->SetCopySourceMesh(toCopyMesh,toCopyGroups);
146   SMESH::TPythonDump() << _this() << ".SetCopySourceMesh( "
147                        << toCopyMesh << ", " << toCopyGroups << " )";
148 }
149
150 //================================================================================
151 /*!
152  * \brief Return "to copy mesh and groups"
153  */
154 //================================================================================
155
156 void StdMeshers_ImportSource1D_i::GetCopySourceMesh(CORBA::Boolean& toCopyMesh,
157                                                     CORBA::Boolean& toCopyGroups)
158 {
159   GetImpl()->GetCopySourceMesh(toCopyMesh,toCopyGroups);
160 }
161
162 //================================================================================
163 /*!
164  * \brief Write parameters in a string
165   * \retval char* - resulting string
166  */
167 //================================================================================
168
169 char* StdMeshers_ImportSource1D_i::SaveTo()
170 {
171   std::ostringstream os;
172   os << " " << _groupEntries->length();
173
174   SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
175   for ( int i = 0; i < _groupEntries->length(); ++i )
176   {
177     // entry
178     os << " " << _groupEntries[i];
179
180     // id
181     SALOMEDS::SObject_var groupSO = study->FindObjectID( _groupEntries[i] );
182     CORBA::Object_var groupObj;
183     if ( !groupSO->_is_nil() )
184       groupObj = groupSO->GetObject();
185     StdMeshers_ObjRefUlils::SaveToStream( groupObj, os );
186   }
187
188   myBaseImpl->SaveTo( os );
189   cout << "SaveTo() " << os.str() << endl;
190
191   return CORBA::string_dup( os.str().c_str() );
192 }
193
194 //================================================================================
195 /*!
196  * \brief Retrieve parameters from the string
197   * \param theStream - the input string
198  */
199 //================================================================================
200
201 void StdMeshers_ImportSource1D_i::LoadFrom( const char* theStream )
202 {
203   std::istringstream is( theStream );
204
205   int nbGroups;
206   is >> nbGroups;
207
208   _groupEntries = new SMESH::string_array;
209   _groupEntries->length( nbGroups );
210   std::string id, entry;
211   for ( int i = 0; i < _groupEntries->length(); ++i )
212   {
213     if ( is >> entry )
214       _groupEntries[i] = entry.c_str();
215     else
216     {
217       _groupEntries->length( i );
218       is.clear(ios::badbit | is.rdstate());
219       break;
220     }
221     if ( is >> id )
222       _groupIDs.push_back( id );
223     else
224     {
225       is.clear(ios::badbit | is.rdstate());
226       break;
227     }
228   }
229
230   myBaseImpl->LoadFrom( is );
231 }
232
233 //================================================================================
234 /*!
235  * \brief Retrieve groups by their ids loaded by LoadFrom()
236  * This is possible only when all meshes are fully loaded
237  */
238 //================================================================================
239
240 void StdMeshers_ImportSource1D_i::UpdateAsMeshesRestored()
241 {
242   std::vector<SMESH_Group*> smesh_groups;
243   for ( unsigned i = 0; i < _groupIDs.size(); ++i )
244   {
245     std::istringstream is( _groupIDs[i].c_str() );
246     SMESH::SMESH_GroupBase_var group =
247       StdMeshers_ObjRefUlils::LoadObjectFromStream<SMESH::SMESH_GroupBase>( is );
248     if ( SMESH_GroupBase_i* gp_i = SMESH::DownCast<SMESH_GroupBase_i*>( group ))
249       smesh_groups.push_back( gp_i->GetSmeshGroup() );
250   }
251   cout << "UpdateAsMeshesRestored(): " << smesh_groups.size() << endl;
252   GetImpl()->RestoreGroups(smesh_groups);
253 }
254
255 //=============================================================================
256 /*!
257  *  StdMeshers_ImportSource1D_i::GetImpl
258  *
259  *  Get implementation
260  */
261 //=============================================================================
262
263 ::StdMeshers_ImportSource1D* StdMeshers_ImportSource1D_i::GetImpl()
264 {
265   MESSAGE( "StdMeshers_ImportSource1D_i::GetImpl" );
266   return ( ::StdMeshers_ImportSource1D* )myBaseImpl;
267 }
268
269 //================================================================================
270 /*!
271  * \brief Verify whether hypothesis supports given entity type 
272   * \param type - dimension (see SMESH::Dimension enumeration)
273   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
274  * 
275  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
276  */
277 //================================================================================  
278 CORBA::Boolean StdMeshers_ImportSource1D_i::IsDimSupported( SMESH::Dimension type )
279 {
280   return type == SMESH::DIM_1D;
281 }
282