Salome HOME
[bos #40035][EDF] Handle Salome Exception in interface to retun meaninfull error...
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_ViscousLayerBuilder_i.cxx
1 // Copyright (C) 2007-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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
21 //  File   : StdMeshers_ViscousLayers_i.cxx
22 //  Module : SMESH
23 //
24 #include "StdMeshers_ViscousLayerBuilder_i.hxx"
25
26 #include "SMESH_Gen.hxx"
27 #include "SMESH_Gen_i.hxx"
28 #include "SMESH_Group.hxx"
29 #include "SMESH_Group_i.hxx"
30 #include "SMESH_PythonDump.hxx"
31
32 #include "BRepTools.hxx"
33 #include "Utils_CorbaException.hxx"
34 #include "utilities.h"
35
36 #include <TCollection_AsciiString.hxx>
37
38 #include CORBA_SERVER_HEADER(SMESH_Group)
39
40 using namespace std;
41
42 //=============================================================================
43 /*!
44  *  StdMeshers_ViscousLayerBuilder_i::StdMeshers_ViscousLayerBuilder_i
45  *
46  *  Constructor
47  */
48 //=============================================================================
49
50 StdMeshers_ViscousLayerBuilder_i::StdMeshers_ViscousLayerBuilder_i( PortableServer::POA_ptr thePOA,
51                                                                       ::SMESH_Gen*            theGenImpl )
52   : SALOME::GenericObj_i( thePOA ), 
53       SMESH_Hypothesis_i( thePOA ),
54       SMESH_Algo_i( thePOA ),
55       SMESH_2D_Algo_i( thePOA )
56 {
57   myBaseImpl = new ::StdMeshers_ViscousLayerBuilder( theGenImpl->GetANewId(),
58                                                       theGenImpl );
59 }
60
61 ::StdMeshers_ViscousLayerBuilder* StdMeshers_ViscousLayerBuilder_i::GetImpl()
62 {
63   return ( ::StdMeshers_ViscousLayerBuilder* )myBaseImpl;
64 }
65
66 //================================================================================
67 /*!
68  * \brief Verify whether hypothesis supports given entity type 
69   * \param type - dimension (see SMESH::Dimension enumeration)
70   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
71  * 
72  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
73  */
74 //================================================================================  
75 CORBA::Boolean StdMeshers_ViscousLayerBuilder_i::IsDimSupported( SMESH::Dimension type )
76 {
77   return type == SMESH::DIM_3D || type == SMESH::DIM_2D;
78 }
79
80 void StdMeshers_ViscousLayerBuilder_i::SetFaces(const ::SMESH::long_array& faceIDs,
81                                                   CORBA::Boolean             toIgnore)
82 {
83   vector<int> ids( faceIDs.length() );
84   for ( unsigned i = 0; i < ids.size(); ++i )
85     if (( ids[i] = faceIDs[i] ) < 1 )
86       THROW_SALOME_CORBA_EXCEPTION( "Invalid face id", SALOME::BAD_PARAM );
87
88   GetImpl()->SetBndShapes( ids, toIgnore );
89
90   SMESH::TPythonDump() << _this() << ".SetFaces( " << faceIDs << ", " << toIgnore << " )";
91 }
92
93
94 void StdMeshers_ViscousLayerBuilder_i::SetIgnoreFaces(const ::SMESH::long_array& faceIDs)
95 {
96   vector<int> ids( faceIDs.length() );
97   for ( unsigned i = 0; i < ids.size(); ++i )
98     if (( ids[i] = faceIDs[i] ) < 1 )
99       THROW_SALOME_CORBA_EXCEPTION( "Invalid face id", SALOME::BAD_PARAM );
100   GetImpl()->SetBndShapes( ids, /*toIgnore=*/true );
101   SMESH::TPythonDump() << _this() << ".SetIgnoreFaces( " << faceIDs << " )";
102 }
103
104 void StdMeshers_ViscousLayerBuilder_i::SetTotalThickness(::CORBA::Double thickness)
105 {
106   if ( thickness < 1e-100 )
107     THROW_SALOME_CORBA_EXCEPTION( "Invalid thickness", SALOME::BAD_PARAM );
108   GetImpl()->SetTotalThickness(thickness);
109   SMESH::TPythonDump() << _this() << ".SetTotalThickness( " << SMESH::TVar(thickness) << " )";
110 }
111
112 void StdMeshers_ViscousLayerBuilder_i::SetNumberLayers(::CORBA::Short nb)
113 {
114   if ( nb < 1 )
115     THROW_SALOME_CORBA_EXCEPTION( "Invalid number of layers", SALOME::BAD_PARAM );
116   GetImpl()->SetNumberLayers( nb );
117   SMESH::TPythonDump() << _this() << ".SetNumberLayers( " << SMESH::TVar(nb) << " )";
118 }
119
120 void StdMeshers_ViscousLayerBuilder_i::SetStretchFactor(::CORBA::Double factor)
121 {
122   if ( factor < 1 )
123     THROW_SALOME_CORBA_EXCEPTION( "Invalid stretch factor, it must be >= 1.0", SALOME::BAD_PARAM );
124   GetImpl()->SetStretchFactor(factor);
125   SMESH::TPythonDump() << _this() << ".SetStretchFactor( " << SMESH::TVar(factor) << " )";
126 }
127
128 void StdMeshers_ViscousLayerBuilder_i::SetMethod( ::StdMeshers::VLExtrusionMethod how )
129 {
130   GetImpl()->SetMethod( ::StdMeshers_ViscousLayers::ExtrusionMethod( how ));
131   const char* methNames[3] = { "SURF_OFFSET_SMOOTH",
132                                "FACE_OFFSET",
133                                "NODE_OFFSET" };
134   if ( how >= 0 && how < 3 )
135     SMESH::TPythonDump() << _this() << ".SetMethod( StdMeshers." << methNames[ how ]<< " )";
136 }
137
138 void StdMeshers_ViscousLayerBuilder_i::SetGroupName(const char* name)
139 {
140   GetImpl()->SetGroupName( name );
141   SMESH::TPythonDump() << _this() << ".SetGroupName( '" << name << "' )";
142 }
143
144 GEOM::GEOM_Object_ptr StdMeshers_ViscousLayerBuilder_i::GetShrinkGeometry( SMESH::SMESH_Mesh_ptr finalMesh, GEOM::GEOM_Object_ptr theShapeObject )
145 {
146   
147   GEOM::GEOM_Object_var aShapeObj;
148   TopoDS_Shape theShape        = StdMeshers_ObjRefUlils::GeomObjectToShape( theShapeObject );
149   SMESH_Mesh_i* theFinalMesh_i = SMESH::DownCast< SMESH_Mesh_i* >( finalMesh );
150   TopoDS_Shape shrinkGeometry;
151   
152   try 
153   {
154     shrinkGeometry = GetImpl()->GetShrinkGeometry( theFinalMesh_i->GetImpl(), theShape );
155   }
156   catch ( std::exception& exc )
157   {
158     std::cout << exc.what() << "\n";
159     THROW_SALOME_CORBA_EXCEPTION( exc.what(), SALOME::INTERNAL_ERROR  );
160     return aShapeObj; // Maybe better to return a init and empty object(?)
161   }
162
163   if ( !shrinkGeometry.IsNull() )
164   {     
165     std::ostringstream streamShape;
166     //Write TopoDS_Shape in ASCII format to the stream
167     BRepTools::Write(shrinkGeometry, streamShape);
168     //Returns the number of bytes that have been stored in the stream's buffer.
169     int size = streamShape.str().size();
170     //Allocate octect buffer of required size
171     CORBA::Octet* OctetBuf = SALOMEDS::TMPFile::allocbuf(size);
172     //Copy ostrstream content to the octect buffer
173     memcpy(OctetBuf, streamShape.str().c_str(), size);
174     //Create and return TMPFile
175     SALOMEDS::TMPFile_var SeqFile = new SALOMEDS::TMPFile(size,size,OctetBuf,1);
176     // Get the geom engine
177     GEOM::GEOM_Gen_var geomEngine = theShapeObject->GetGen();
178     auto iOp                      = geomEngine->GetIInsertOperations();
179     aShapeObj                     = iOp->RestoreShape( SeqFile );
180     geomEngine->AddInStudy( aShapeObj, "Shrink", GEOM::GEOM_Object::_nil());        
181   }
182
183   return aShapeObj;
184 }
185
186 CORBA::Boolean StdMeshers_ViscousLayerBuilder_i::AddLayers( SMESH::SMESH_Mesh_ptr shrinkMesh, SMESH::SMESH_Mesh_ptr finalMesh, GEOM::GEOM_Object_ptr theShapeObject )
187 {
188   TopoDS_Shape theShape         = StdMeshers_ObjRefUlils::GeomObjectToShape( theShapeObject );
189   SMESH_Mesh_i* shrinkMesh_i    = SMESH::DownCast< SMESH_Mesh_i* >( shrinkMesh );
190   SMESH_Mesh_i* theFinalMesh_i  = SMESH::DownCast< SMESH_Mesh_i* >( finalMesh );
191   bool success = false;
192
193   try 
194   {
195     success = GetImpl()->AddLayers( shrinkMesh_i->GetImpl(), theFinalMesh_i->GetImpl(), theShape );  
196   }
197   catch ( std::exception& exc )
198   {
199     THROW_SALOME_CORBA_EXCEPTION( exc.what(), SALOME::INTERNAL_ERROR  );
200   }
201   
202   return success;
203 }
204 //=============================================================================
205 /*!
206  *  StdMeshers_ViscousLayerBuilder_i::~StdMeshers_ViscousLayerBuilder_i
207  *
208  *  Destructor
209  */
210 //=============================================================================
211 StdMeshers_ViscousLayerBuilder_i::~StdMeshers_ViscousLayerBuilder_i()
212 {
213 }