Salome HOME
Make SetNodeOnEdge() tell a valid range if U is invalid
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_QuadrangleParams_i.cxx
1 // Copyright (C) 2007-2019  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                                            ::SMESH_Gen*            theGenImpl )
49      : SALOME::GenericObj_i( thePOA ), 
50        SMESH_Hypothesis_i( thePOA )
51 {
52   myBaseImpl = new ::StdMeshers_QuadrangleParams(theGenImpl->GetANewId(),
53                                                  theGenImpl);
54 }
55
56 //=============================================================================
57 /*!
58  *  StdMeshers_QuadrangleParams_i::~StdMeshers_QuadrangleParams_i
59  *
60  *  Destructor
61  */
62 //=============================================================================
63
64 StdMeshers_QuadrangleParams_i::~StdMeshers_QuadrangleParams_i()
65 {
66 }
67
68 //=============================================================================
69 /*!
70  *  StdMeshers_QuadrangleParams_i::SetTriaVertex
71  *
72  *  Set base vertex for triangles
73  */
74 //=============================================================================
75
76 void StdMeshers_QuadrangleParams_i::SetTriaVertex(CORBA::Long vertID)
77 {
78   ASSERT( myBaseImpl );
79   try {
80     this->GetImpl()->SetTriaVertex( vertID );
81   }
82   catch ( SALOME_Exception& S_ex ) {
83     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
84   }
85
86   // Update Python script
87   SMESH::TPythonDump() << _this() << ".SetTriaVertex( " << vertID << " )";
88 }
89
90 //=============================================================================
91 /*!
92  *  StdMeshers_QuadrangleParams_i::GetTriaVertex
93  *
94  *  Get base vertex for triangles
95  */
96 //=============================================================================
97
98 CORBA::Long StdMeshers_QuadrangleParams_i::GetTriaVertex()
99 {
100   ASSERT( myBaseImpl );
101   return this->GetImpl()->GetTriaVertex();
102 }
103
104 //=============================================================================
105 /*!
106  *  StdMeshers_QuadrangleParams_i::SetObjectEntry
107  *
108  *  Set the Entry for the Main Object
109  */
110 //=============================================================================
111
112 void StdMeshers_QuadrangleParams_i::SetObjectEntry( const char* entry )
113 {
114   ASSERT( myBaseImpl );
115
116   try {
117     this->GetImpl()->SetObjectEntry( entry );
118     // Update Python script
119     //    SMESH::TPythonDump() << _this() << ".SetObjectEntry( '" << entry << "' )";
120   }
121   catch ( SALOME_Exception& S_ex ) {
122     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(),
123                                   SALOME::BAD_PARAM );
124   }
125 }
126
127 //=============================================================================
128 /*!
129  *  StdMeshers_QuadrangleParams_i::GetObjectEntry
130  *
131  *  Set the Entry for the Main Object
132  */
133 //=============================================================================
134
135 char* StdMeshers_QuadrangleParams_i::GetObjectEntry()
136 {
137   ASSERT( myBaseImpl );
138   const char* entry;
139   try {
140     entry = this->GetImpl()->GetObjectEntry();
141   }
142   catch ( SALOME_Exception& S_ex ) {
143     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
144   }
145   return CORBA::string_dup( entry );
146 }
147
148 //=============================================================================
149 /*!
150  *  StdMeshers_QuadrangleParams_i::SetQuadType
151  *
152  *  Set the type of quadrangulation
153  */
154 //=============================================================================
155 void StdMeshers_QuadrangleParams_i::SetQuadType(StdMeshers::QuadType type)
156 {
157   ASSERT(myBaseImpl);
158
159   if (int(type) >= int(StdMeshers::QUAD_NB_TYPES)) {
160     THROW_SALOME_CORBA_EXCEPTION("Bad type of quadrangulation", SALOME::BAD_PARAM);
161   }
162
163   try {
164     this->GetImpl()->SetQuadType(StdMeshers_QuadType(int(type)));
165   }
166   catch (SALOME_Exception& S_ex) {
167     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
168   }
169
170   // Update Python script
171   const char* quadType;
172   switch (type) {
173   case StdMeshers::QUAD_STANDARD:
174     quadType = "StdMeshers.QUAD_STANDARD"; break;
175   case StdMeshers::QUAD_TRIANGLE_PREF:
176     quadType = "StdMeshers.QUAD_TRIANGLE_PREF"; break;
177   case StdMeshers::QUAD_QUADRANGLE_PREF:
178     quadType = "StdMeshers.QUAD_QUADRANGLE_PREF"; break;
179   case StdMeshers::QUAD_QUADRANGLE_PREF_REVERSED:
180     quadType = "StdMeshers.QUAD_QUADRANGLE_PREF_REVERSED"; break;
181   case StdMeshers::QUAD_REDUCED:
182     quadType = "StdMeshers.QUAD_REDUCED"; break;
183   default:
184     quadType = "UNKNOWN";
185   }
186   SMESH::TPythonDump() << _this() << ".SetQuadType( " << quadType << " )";
187 }
188
189 //=============================================================================
190 /*!
191  *  StdMeshers_QuadrangleParams_i::GetQuadType
192  *
193  *  Get the type of quadrangulation
194  */
195 //=============================================================================
196 StdMeshers::QuadType StdMeshers_QuadrangleParams_i::GetQuadType()
197 {
198   ASSERT(myBaseImpl);
199   return StdMeshers::QuadType(int(this->GetImpl()->GetQuadType()));
200 }
201
202 //================================================================================
203 /*!
204  * \brief Set positions of enforced nodes
205  */
206 //================================================================================
207
208 void StdMeshers_QuadrangleParams_i::SetEnforcedNodes(const GEOM::ListOfGO&     theVertices,
209                                                      const SMESH::nodes_array& thePoints)
210   throw ( SALOME::SALOME_Exception )
211 {
212   try {
213     std::vector< TopoDS_Shape > shapes;
214     std::vector< gp_Pnt       > points;
215     shapes.reserve( theVertices.length() );
216     points.reserve( thePoints.length() );
217
218     myShapeEntries.clear();
219
220     for ( size_t i = 0; i < theVertices.length(); ++i )
221     {
222       if ( CORBA::is_nil( theVertices[i] ))
223         continue;
224       CORBA::String_var entry = theVertices[i]->GetStudyEntry();
225       if ( !entry.in() || !entry.in()[0] )
226         THROW_SALOME_CORBA_EXCEPTION( "Not published enforced vertex shape", SALOME::BAD_PARAM );
227
228       shapes.push_back( StdMeshers_ObjRefUlils::GeomObjectToShape( theVertices[i].in() ));
229       myShapeEntries.push_back( entry.in() );
230     }
231     for ( size_t i = 0; i < thePoints.length(); ++i )
232     {
233       points.push_back( gp_Pnt( thePoints[i].x, thePoints[i].y, thePoints[i].z ));
234     }
235     this->GetImpl()->SetEnforcedNodes( shapes, points );
236   }
237   catch ( SALOME_Exception& S_ex ) {
238     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
239   }
240   // Update Python script
241   SMESH::TPythonDump() << _this() << ".SetEnforcedNodes( "
242                        << theVertices << ", " << thePoints << " )";
243 }
244   
245 //================================================================================
246 /*!
247  * \brief Returns positions of enforced nodes
248  */
249 //================================================================================
250
251 void StdMeshers_QuadrangleParams_i::GetEnforcedNodes(GEOM::ListOfGO_out     theVertices,
252                                                      SMESH::nodes_array_out thePoints)
253 {
254   SMESH_TRY;
255
256   std::vector< TopoDS_Shape > shapes;
257   std::vector< gp_Pnt       > points;
258   this->GetImpl()->GetEnforcedNodes( shapes, points );
259
260   theVertices = new GEOM::ListOfGO;
261   thePoints   = new SMESH::nodes_array;
262
263   size_t i = 0;
264   theVertices->length( myShapeEntries.size() );
265   for ( i = 0; i < myShapeEntries.size(); ++i )
266     theVertices[i] =
267       StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject( myShapeEntries[i], shapes[i] );
268
269   thePoints->length( points.size() );
270   for ( i = 0; i < points.size(); ++i )
271   {
272     thePoints[i].x = points[i].X();
273     thePoints[i].y = points[i].Y();
274     thePoints[i].z = points[i].Z();
275   }
276   SMESH_CATCH( SMESH::doNothing );
277 }
278
279 //================================================================================
280 /*!
281  * \brief Returns study entries of shapes defining enforced nodes
282  */
283 //================================================================================
284
285 SMESH::string_array* StdMeshers_QuadrangleParams_i::GetEnfVertices()
286 {
287   SMESH::string_array_var arr = new SMESH::string_array;
288   arr->length( myShapeEntries.size() );
289
290   for ( size_t i = 0; i < myShapeEntries.size(); ++i )
291     arr[ i ] = myShapeEntries[ i ].c_str();
292
293   return arr._retn();
294 }
295
296 //=============================================================================
297 /*!
298  * Set corner vertices
299  */
300 //=============================================================================
301
302 void StdMeshers_QuadrangleParams_i::SetCorners(const SMESH::long_array& theVertexIDs )
303 {
304   std::vector< int > ids;
305   for ( CORBA::ULong i = 0; i < theVertexIDs.length(); ++i )
306     ids.push_back( theVertexIDs[i] );
307
308   if ( ids != GetImpl()->GetCorners() )
309   {
310     GetImpl()->SetCorners( ids );
311     SMESH::TPythonDump() << _this() << ".SetCorners( " << theVertexIDs << " )";
312   }
313 }
314
315 //=============================================================================
316 /*!
317  * Return IDs of corner vertices
318  */
319 //=============================================================================
320
321 SMESH::long_array* StdMeshers_QuadrangleParams_i::GetCorners()
322 {
323   const std::vector< int >& ids = GetImpl()->GetCorners();
324
325   SMESH::long_array_var  result = new SMESH::long_array;
326   result->length( ids.size() );
327   for ( size_t i = 0; i < ids.size(); ++i )
328     result[ i ] = ids[ i ];
329
330   return result._retn();
331 }
332
333 //=============================================================================
334 /*!
335  *  StdMeshers_QuadrangleParams_i::GetImpl
336  *
337  *  Get implementation
338  */
339 //=============================================================================
340
341 ::StdMeshers_QuadrangleParams* StdMeshers_QuadrangleParams_i::GetImpl()
342 {
343   return ( ::StdMeshers_QuadrangleParams* )myBaseImpl;
344 }
345
346 //================================================================================
347 /*!
348  * \brief Verify whether hypothesis supports given entity type 
349   * \param type - dimension (see SMESH::Dimension enumeration)
350   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
351  * 
352  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
353  */
354 //================================================================================  
355 CORBA::Boolean StdMeshers_QuadrangleParams_i::IsDimSupported( SMESH::Dimension type )
356 {
357   return type == SMESH::DIM_2D;
358 }
359
360 //================================================================================
361 /*!
362  * \brief Write parameters in a string
363   * \retval char* - resulting string
364  */
365 //================================================================================
366
367 char* StdMeshers_QuadrangleParams_i::SaveTo()
368 {
369   ASSERT( myBaseImpl );
370   std::ostringstream os;
371
372   os << "ENTRIES: " << myShapeEntries.size();
373   for ( size_t i = 0; i < myShapeEntries.size(); ++i )
374     StdMeshers_ObjRefUlils::SaveToStream( myShapeEntries[ i ], os );
375   os << " ";
376
377   myBaseImpl->SaveTo( os );
378
379   return CORBA::string_dup( os.str().c_str() );
380 }
381
382 //================================================================================
383 /*!
384  * \brief Retrieve parameters from the string
385   * \param theStream - the input string
386  */
387 //================================================================================
388
389 void StdMeshers_QuadrangleParams_i::LoadFrom( const char* theStream )
390 {
391   ASSERT( myBaseImpl );
392
393   bool hasEntries = ( strncmp( "ENTRIES: ", theStream, 9 ) == 0 );
394   std::istringstream is( theStream + ( hasEntries ? 9 : 0 ));
395
396   if ( hasEntries )
397   {
398     int nb = 0;
399     if ( is >> nb && nb > 0 )
400     {
401       std::vector< TopoDS_Shape > shapes;
402       std::vector< gp_Pnt >       points;
403       myShapeEntries.resize( nb );
404
405       for ( int i = 0; i < nb; ++i )
406         shapes.push_back( StdMeshers_ObjRefUlils::LoadFromStream( is, & myShapeEntries[i] ));
407
408       try {
409         GetImpl()->SetEnforcedNodes( shapes, points );
410       }
411       catch (...) {
412       }
413     }
414   }
415
416   myBaseImpl->LoadFrom( is );
417 }
418
419 //================================================================================
420 /*!
421  * \brief Return geometry this hypothesis depends on. Return false if there is no geometry parameter
422  */
423 //================================================================================
424
425 bool
426 StdMeshers_QuadrangleParams_i::getObjectsDependOn( std::vector< std::string > & entryArray,
427                                                    std::vector< int >         & subIDArray ) const
428 {
429   const ::StdMeshers_QuadrangleParams* impl =
430     static_cast<const ::StdMeshers_QuadrangleParams*>( myBaseImpl );
431
432   subIDArray.push_back( impl->GetTriaVertex() );
433
434   entryArray.push_back( impl->GetObjectEntry() );
435   entryArray.insert( entryArray.end(), myShapeEntries.begin(), myShapeEntries.end() );
436
437   return true;
438 }
439
440 //================================================================================
441 /*!
442  * \brief Set new geometry instead of that returned by getObjectsDependOn()
443  */
444 //================================================================================
445
446 bool
447 StdMeshers_QuadrangleParams_i::setObjectsDependOn( std::vector< std::string > & entryArray,
448                                                    std::vector< int >         & subIDArray )
449 {
450   if ( !subIDArray.empty() )
451     GetImpl()->SetTriaVertex( subIDArray[0] );
452
453   GetImpl()->SetObjectEntry( entryArray[0].c_str() );
454
455   myShapeEntries.assign( ++entryArray.begin(), entryArray.end() );
456
457   std::vector< TopoDS_Shape > shapes;
458   std::vector< gp_Pnt       > points;
459   this->GetImpl()->GetEnforcedNodes( shapes, points );
460
461   shapes.clear();
462   for ( size_t i = 0; i < myShapeEntries.size(); ++i )
463     shapes.push_back( StdMeshers_ObjRefUlils::EntryToShape( myShapeEntries[i] ));
464
465   this->GetImpl()->SetEnforcedNodes( shapes, points );
466
467   return true;
468 }