#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
+#include <boost/filesystem.hpp>
+
+namespace fs = boost::filesystem;
using namespace std;
using SMESH::TPythonDump;
//================================================================================
SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh,
- const char* meshName)
+ const char* meshName,
+ CORBA::Boolean adapt_to_shape)
{
Unexpect aCatch(SALOME_SalomeException);
if ( CORBA::is_nil( mesh ))
THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
- std::cout << mesh << std::endl;
SMESH::SMESH_Mesh_var srcMesh = mesh->GetMesh();
SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
if ( !srcMesh_i )
THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
- SMESH_Mesh& srcMesh2 = srcMesh_i->GetImpl();
-
- // TODO: Get it
- CORBA::String_var mesh_ior=GetORB()->object_to_string(mesh);
- std::string mesh_id = mesh_ior.in();
- std::string dual_mesh_file="/tmp/test_dual.med";
- std::string mesh_name = "MESH";
-
- std::string python_code;
- python_code += "import sys\n";
- python_code += "import salome\n";
- python_code += "import medcoupling as mc\n";
- python_code += "from math import pi\n";
- python_code += "salome.salome_init()\n";
- python_code += "import GEOM\n";
- python_code += "from salome.geom import geomBuilder\n";
- python_code += "geompy = geomBuilder.New()\n";
- python_code += "import SMESH, SALOMEDS\n";
- python_code += "from salome.smesh import smeshBuilder\n";
- python_code += "smesh = smeshBuilder.New()\n";
- python_code += "def create_dual_mesh(mesh_ior, output_file):\n";
- python_code += " mesh = salome.orb.string_to_object(mesh_ior)\n";
- python_code += " shape = mesh.GetShapeToMesh()\n";
- python_code += " if not mesh:\n";
- python_code += " raise Exception(\"Could not find mesh using id: \", mesh_id)\n";
- python_code += " int_ptr = mesh.ExportMEDCoupling(True, True)\n";
- python_code += " dab = mc.FromPyIntPtrToDataArrayByte(int_ptr)\n";
- python_code += " tetras = mc.MEDFileMesh.New(dab)[0]\n";
- python_code += " tetras = mc.MEDCoupling1SGTUMesh(tetras)\n";
- python_code += " polyh = tetras.computeDualMesh()\n";
- python_code += " skin = tetras.buildUnstructured().computeSkin()\n";
- python_code += " skin_polyh = polyh.buildUnstructured().computeSkin()\n";
- python_code += " allNodesOnSkinPolyh = skin_polyh.computeFetchedNodeIds()\n";
- python_code += " allNodesOnSkin = skin.computeFetchedNodeIds()\n";
- python_code += " ptsAdded = allNodesOnSkinPolyh.buildSubstraction(allNodesOnSkin)\n";
- python_code += " ptsAddedMesh = mc.MEDCouplingUMesh.Build0DMeshFromCoords( skin_polyh.getCoords()[ptsAdded] )\n";
- python_code += " ptsAddedCoo = ptsAddedMesh.getCoords()\n";
- python_code += " ptsAddedCooModified = ptsAddedCoo[:]\n";
- python_code += " polyh.setName(\"MESH\")\n";
- python_code += " polyh.write(output_file)\n";
+ CORBA::String_var mesh_var=GetORB()->object_to_string(mesh);
+ std::string mesh_ior = mesh_var.in();
+
+ //temporary folder for the generation of the med file
+ fs::path tmp_folder = fs::temp_directory_path() / fs::unique_path(fs::path("dual_mesh-%%%%"));
+ fs::create_directories(tmp_folder);
+ fs::path dual_mesh_file = tmp_folder / fs::path("tmp_dual_mesh.med");
+ std::string mesh_name(meshName);
+ MESSAGE("Working in folder" + tmp_folder.string());
// Running Python script
assert(Py_IsInitialized());
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
- PyRun_SimpleString(python_code.c_str());
- std::string cmd="";
- cmd += "create_dual_mesh(\"" + mesh_id + "\", \"" + dual_mesh_file + "\")";
- PyRun_SimpleString(cmd.c_str());
+
+
+ std::string ats;
+ if(adapt_to_shape)
+ ats = "True";
+ else
+ ats = "False";
+
+ std::string cmd="import salome.smesh.smesh_tools as smt\n";
+ cmd +="smt.smesh_create_dual_mesh(\"" + mesh_ior + "\", \"" +
+ dual_mesh_file.string() + "\", mesh_name=\"" + mesh_name + "\", adapt_to_shape=" + ats + ")";
+ MESSAGE(cmd);
+
+ PyObject *py_main = PyImport_AddModule("__main__");
+ PyObject *py_dict = PyModule_GetDict(py_main);
+
+ PyRun_String(cmd.c_str(), Py_file_input, py_dict, py_dict);
+
+ if (PyErr_Occurred()) {
+ // Restrieving python error
+ MESSAGE("Catching error")
+ PyObject *errtype, *errvalue, *traceback;
+ PyErr_Fetch(&errtype, &errvalue, &traceback);
+ if(errvalue != NULL) {
+ MESSAGE("Error has a value")
+ PyObject *s = PyObject_Str(errvalue);
+ Py_ssize_t size;
+ std::string msg = PyUnicode_AsUTF8AndSize(s, &size);
+ msg = "Issue with the execution of create_dual_mesh:\n"+msg;
+ MESSAGE("throwing exception")
+ // We need to deactivate the GIL before throwing the exception
+ PyGILState_Release(gstate);
+ THROW_SALOME_CORBA_EXCEPTION(msg.c_str(), SALOME::INTERNAL_ERROR );
+ Py_DECREF(s);
+ }
+ Py_XDECREF(errvalue);
+ Py_XDECREF(errtype);
+ Py_XDECREF(traceback);
+ }
PyGILState_Release(gstate);
+ MESSAGE("Mesh created in " + dual_mesh_file.string());
+
// Import created MED
SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
SALOMEDS::SObject_wrap meshSO = ObjectToSObject( newMesh );
if ( !meshSO->_is_nil() )
{
- SetName( meshSO, meshName, "Mesh" );
+ SetName( meshSO, meshName, meshName );
SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
}
+ int ret = newMesh_i->ImportMEDFile(dual_mesh_file.string().c_str(), meshName);
+ if(ret)
+ THROW_SALOME_CORBA_EXCEPTION( "Issue when importing mesh", SALOME::INTERNAL_ERROR );
+ /*
SMESH_Mesh& newMesh2 = newMesh_i->GetImpl();
- newMesh2.MEDToMesh(dual_mesh_file.c_str(), "MESH");
- SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
+ MESSAGE("Loading file: " << dual_mesh_file.string() << " with mesh " << meshName);
+ int ret = newMesh2.MEDToMesh(dual_mesh_file.c_str(), meshName);
+ */
- newMeshDS->Modified();
+ newMesh_i->GetImpl().GetMeshDS()->Modified();
*pyDump << newMesh << " = " << this
- << ".CreateDualMesh( " << mesh << ", "
- << "'" << meshName << "') ";
+ << ".CreateDualMesh("
+ << mesh << ", "
+ << "'" << mesh_name << "', "
+ << ats << ") ";
+
+ pyDumpDeleter.reset(); // allow dump in GetGroups()
+
+ if ( srcMesh_i->GetImpl().GetGroupIds().size() > 0 ) // dump created groups
+ MESSAGE("Dump of groups");
+ SMESH::ListOfGroups_var groups = newMesh->GetGroups();
return newMesh._retn();
}