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