Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_QuadrangleParams_i.cxx
1 // Copyright (C) 2007-2016  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 //  File   : StdMeshers_QuadrangleParams_i.cxx
20 //  Author : Sergey KUUL, OCC
21 //  Module : SMESH
22
23 #include "StdMeshers_QuadrangleParams_i.hxx"
24
25 #include "SMESH_Gen.hxx"
26 #include "SMESH_Gen_i.hxx"
27 #include "SMESH_PythonDump.hxx"
28 #include "StdMeshers_ObjRefUlils.hxx"
29
30 #include <Utils_CorbaException.hxx>
31 #include <utilities.h>
32
33 #include <Standard_ErrorHandler.hxx>
34 #include "SMESH_TryCatch.hxx"
35
36 using namespace std;
37
38 //=============================================================================
39 /*!
40  *  StdMeshers_QuadrangleParams_i::StdMeshers_QuadrangleParams_i
41  *
42  *  Constructor
43  */
44 //=============================================================================
45
46 StdMeshers_QuadrangleParams_i::StdMeshers_QuadrangleParams_i
47                                           (PortableServer::POA_ptr thePOA,
48                                            int                     theStudyId,
49                                            ::SMESH_Gen*            theGenImpl )
50      : SALOME::GenericObj_i( thePOA ), 
51        SMESH_Hypothesis_i( thePOA )
52 {
53   myBaseImpl = new ::StdMeshers_QuadrangleParams(theGenImpl->GetANewId(),
54                                                  theStudyId,
55                                                  theGenImpl);
56 }
57
58 //=============================================================================
59 /*!
60  *  StdMeshers_QuadrangleParams_i::~StdMeshers_QuadrangleParams_i
61  *
62  *  Destructor
63  */
64 //=============================================================================
65
66 StdMeshers_QuadrangleParams_i::~StdMeshers_QuadrangleParams_i()
67 {
68 }
69
70 //=============================================================================
71 /*!
72  *  StdMeshers_QuadrangleParams_i::SetTriaVertex
73  *
74  *  Set base vertex for triangles
75  */
76 //=============================================================================
77
78 void StdMeshers_QuadrangleParams_i::SetTriaVertex(CORBA::Long vertID)
79 {
80   ASSERT( myBaseImpl );
81   try {
82     this->GetImpl()->SetTriaVertex( vertID );
83   }
84   catch ( SALOME_Exception& S_ex ) {
85     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
86   }
87
88   // Update Python script
89   SMESH::TPythonDump() << _this() << ".SetTriaVertex( " << vertID << " )";
90 }
91
92 //=============================================================================
93 /*!
94  *  StdMeshers_QuadrangleParams_i::GetTriaVertex
95  *
96  *  Get base vertex for triangles
97  */
98 //=============================================================================
99
100 CORBA::Long StdMeshers_QuadrangleParams_i::GetTriaVertex()
101 {
102   ASSERT( myBaseImpl );
103   return this->GetImpl()->GetTriaVertex();
104 }
105
106 //=============================================================================
107 /*!
108  *  StdMeshers_QuadrangleParams_i::SetObjectEntry
109  *
110  *  Set the Entry for the Main Object
111  */
112 //=============================================================================
113
114 void StdMeshers_QuadrangleParams_i::SetObjectEntry( const char* entry )
115 {
116   ASSERT( myBaseImpl );
117
118   try {
119     this->GetImpl()->SetObjectEntry( entry );
120     // Update Python script
121     //    SMESH::TPythonDump() << _this() << ".SetObjectEntry( '" << entry << "' )";
122   }
123   catch ( SALOME_Exception& S_ex ) {
124     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(),
125                                   SALOME::BAD_PARAM );
126   }
127 }
128
129 //=============================================================================
130 /*!
131  *  StdMeshers_QuadrangleParams_i::GetObjectEntry
132  *
133  *  Set the Entry for the Main Object
134  */
135 //=============================================================================
136
137 char* StdMeshers_QuadrangleParams_i::GetObjectEntry()
138 {
139   ASSERT( myBaseImpl );
140   const char* entry;
141   try {
142     entry = this->GetImpl()->GetObjectEntry();
143   }
144   catch ( SALOME_Exception& S_ex ) {
145     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
146   }
147   return CORBA::string_dup( entry );
148 }
149
150 //=============================================================================
151 /*!
152  *  StdMeshers_QuadrangleParams_i::SetQuadType
153  *
154  *  Set the type of quadrangulation
155  */
156 //=============================================================================
157 void StdMeshers_QuadrangleParams_i::SetQuadType(StdMeshers::QuadType type)
158 {
159   ASSERT(myBaseImpl);
160
161   if (int(type) >= int(StdMeshers::QUAD_NB_TYPES)) {
162     THROW_SALOME_CORBA_EXCEPTION("Bad type of quadrangulation", SALOME::BAD_PARAM);
163   }
164
165   try {
166     this->GetImpl()->SetQuadType(StdMeshers_QuadType(int(type)));
167   }
168   catch (SALOME_Exception& S_ex) {
169     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
170   }
171
172   // Update Python script
173   const char* quadType;
174   switch (type) {
175   case StdMeshers::QUAD_STANDARD:
176     quadType = "StdMeshers.QUAD_STANDARD"; break;
177   case StdMeshers::QUAD_TRIANGLE_PREF:
178     quadType = "StdMeshers.QUAD_TRIANGLE_PREF"; break;
179   case StdMeshers::QUAD_QUADRANGLE_PREF:
180     quadType = "StdMeshers.QUAD_QUADRANGLE_PREF"; break;
181   case StdMeshers::QUAD_QUADRANGLE_PREF_REVERSED:
182     quadType = "StdMeshers.QUAD_QUADRANGLE_PREF_REVERSED"; break;
183   case StdMeshers::QUAD_REDUCED:
184     quadType = "StdMeshers.QUAD_REDUCED"; break;
185   default:
186     quadType = "UNKNOWN";
187   }
188   SMESH::TPythonDump() << _this() << ".SetQuadType( " << quadType << " )";
189 }
190
191 //=============================================================================
192 /*!
193  *  StdMeshers_QuadrangleParams_i::GetQuadType
194  *
195  *  Get the type of quadrangulation
196  */
197 //=============================================================================
198 StdMeshers::QuadType StdMeshers_QuadrangleParams_i::GetQuadType()
199 {
200   ASSERT(myBaseImpl);
201   return StdMeshers::QuadType(int(this->GetImpl()->GetQuadType()));
202 }
203
204 //================================================================================
205 /*!
206  * \brief Set positions of enforced nodes
207  */
208 //================================================================================
209
210 void StdMeshers_QuadrangleParams_i::SetEnforcedNodes(const GEOM::ListOfGO&     theVertices,
211                                                      const SMESH::nodes_array& thePoints)
212   throw ( SALOME::SALOME_Exception )
213 {
214   try {
215     std::vector< TopoDS_Shape > shapes;
216     std::vector< gp_Pnt       > points;
217     shapes.reserve( theVertices.length() );
218     points.reserve( thePoints.length() );
219
220     myShapeEntries.clear();
221
222     for ( size_t i = 0; i < theVertices.length(); ++i )
223     {
224       if ( CORBA::is_nil( theVertices[i] ))
225         continue;
226       CORBA::String_var entry = theVertices[i]->GetStudyEntry();
227       if ( !entry.in() || !entry.in()[0] )
228         THROW_SALOME_CORBA_EXCEPTION( "Not published enforced vertex shape", SALOME::BAD_PARAM );
229
230       shapes.push_back( StdMeshers_ObjRefUlils::GeomObjectToShape( theVertices[i].in() ));
231       myShapeEntries.push_back( entry.in() );
232     }
233     for ( size_t i = 0; i < thePoints.length(); ++i )
234     {
235       points.push_back( gp_Pnt( thePoints[i].x, thePoints[i].y, thePoints[i].z ));
236     }
237     this->GetImpl()->SetEnforcedNodes( shapes, points );
238   }
239   catch ( SALOME_Exception& S_ex ) {
240     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
241   }
242   // Update Python script
243   SMESH::TPythonDump() << _this() << ".SetEnforcedNodes( "
244                        << theVertices << ", " << thePoints << " )";
245 }
246   
247 //================================================================================
248 /*!
249  * \brief Returns positions of enforced nodes
250  */
251 //================================================================================
252
253 void StdMeshers_QuadrangleParams_i::GetEnforcedNodes(GEOM::ListOfGO_out     theVertices,
254                                                      SMESH::nodes_array_out thePoints)
255 {
256   SMESH_TRY;
257
258   std::vector< TopoDS_Shape > shapes;
259   std::vector< gp_Pnt       > points;
260   this->GetImpl()->GetEnforcedNodes( shapes, points );
261
262   theVertices = new GEOM::ListOfGO;
263   thePoints   = new SMESH::nodes_array;
264
265   size_t i = 0;
266   theVertices->length( myShapeEntries.size() );
267   for ( i = 0; i < myShapeEntries.size(); ++i )
268     theVertices[i] =
269       StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject( myShapeEntries[i], shapes[i] );
270
271   thePoints->length( points.size() );
272   for ( i = 0; i < points.size(); ++i )
273   {
274     thePoints[i].x = points[i].X();
275     thePoints[i].y = points[i].Y();
276     thePoints[i].z = points[i].Z();
277   }
278   SMESH_CATCH( SMESH::doNothing );
279 }
280
281 //================================================================================
282 /*!
283  * \brief Returns study entries of shapes defining enforced nodes
284  */
285 //================================================================================
286
287 SMESH::string_array* StdMeshers_QuadrangleParams_i::GetEnfVertices()
288 {
289   SMESH::string_array_var arr = new SMESH::string_array;
290   arr->length( myShapeEntries.size() );
291
292   for ( size_t i = 0; i < myShapeEntries.size(); ++i )
293     arr[ i ] = myShapeEntries[ i ].c_str();
294
295   return arr._retn();
296 }
297
298 //=============================================================================
299 /*!
300  *  StdMeshers_QuadrangleParams_i::GetImpl
301  *
302  *  Get implementation
303  */
304 //=============================================================================
305
306 ::StdMeshers_QuadrangleParams* StdMeshers_QuadrangleParams_i::GetImpl()
307 {
308   return ( ::StdMeshers_QuadrangleParams* )myBaseImpl;
309 }
310
311 //================================================================================
312 /*!
313  * \brief Verify whether hypothesis supports given entity type 
314   * \param type - dimension (see SMESH::Dimension enumeration)
315   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
316  * 
317  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
318  */
319 //================================================================================  
320 CORBA::Boolean StdMeshers_QuadrangleParams_i::IsDimSupported( SMESH::Dimension type )
321 {
322   return type == SMESH::DIM_2D;
323 }
324
325 //================================================================================
326 /*!
327  * \brief Write parameters in a string
328   * \retval char* - resulting string
329  */
330 //================================================================================
331
332 char* StdMeshers_QuadrangleParams_i::SaveTo()
333 {
334   ASSERT( myBaseImpl );
335   std::ostringstream os;
336
337   os << "ENTRIES: " << myShapeEntries.size();
338   for ( size_t i = 0; i < myShapeEntries.size(); ++i )
339     StdMeshers_ObjRefUlils::SaveToStream( myShapeEntries[ i ], os );
340   os << " ";
341
342   myBaseImpl->SaveTo( os );
343
344   return CORBA::string_dup( os.str().c_str() );
345 }
346
347 //================================================================================
348 /*!
349  * \brief Retrieve parameters from the string
350   * \param theStream - the input string
351  */
352 //================================================================================
353
354 void StdMeshers_QuadrangleParams_i::LoadFrom( const char* theStream )
355 {
356   ASSERT( myBaseImpl );
357
358   bool hasEntries = ( strncmp( "ENTRIES: ", theStream, 9 ) == 0 );
359   std::istringstream is( theStream + ( hasEntries ? 9 : 0 ));
360
361   if ( hasEntries )
362   {
363     int nb = 0;
364     if ( is >> nb && nb > 0 )
365     {
366       std::vector< TopoDS_Shape > shapes;
367       std::vector< gp_Pnt >       points;
368       myShapeEntries.resize( nb );
369
370       for ( int i = 0; i < nb; ++i )
371         shapes.push_back( StdMeshers_ObjRefUlils::LoadFromStream( is, & myShapeEntries[i] ));
372
373       try {
374         GetImpl()->SetEnforcedNodes( shapes, points );
375       }
376       catch (...) {
377       }
378     }
379   }
380
381   myBaseImpl->LoadFrom( is );
382 }