From 7d1a127bbb12d9e3fd816f164feb07390bc6a725 Mon Sep 17 00:00:00 2001 From: Yoann Audouin Date: Thu, 6 Oct 2022 08:43:27 +0200 Subject: [PATCH] Corrections to handle adapation to shape --- idl/SMESH_Gen.idl | 4 +- src/SMESHGUI/SMESHGUI_CreateDualMeshOp.cxx | 5 +- src/SMESH_I/SMESH_Gen_i.cxx | 30 +++++-- src/SMESH_I/SMESH_Gen_i.hxx | 3 +- src/SMESH_SWIG/smeshBuilder.py | 6 +- src/SMESH_SWIG/smesh_tools.py | 45 ++++++----- test/SMESH_create_dual_mesh.py | 2 +- test/SMESH_create_dual_mesh_adapt.py | 92 ++++++++++++++++++++++ test/tests.set | 1 + 9 files changed, 153 insertions(+), 35 deletions(-) create mode 100644 test/SMESH_create_dual_mesh_adapt.py diff --git a/idl/SMESH_Gen.idl b/idl/SMESH_Gen.idl index 010525eaf..a5b48aaa6 100644 --- a/idl/SMESH_Gen.idl +++ b/idl/SMESH_Gen.idl @@ -288,9 +288,11 @@ module SMESH * Create a dual mesh of a Tetrahedron mesh * \param mesh - TetraHedron mesh to create dual from * \param meshName - a name of the new mesh + * \param adaptToShape - if True project boundary point on shape */ SMESH_Mesh CreateDualMesh(in SMESH_IDSource mesh, - in string meshName) + in string meshName, + in boolean adaptToShape) raises ( SALOME::SALOME_Exception ); /*! diff --git a/src/SMESHGUI/SMESHGUI_CreateDualMeshOp.cxx b/src/SMESHGUI/SMESHGUI_CreateDualMeshOp.cxx index 449f0a582..4ee81382a 100644 --- a/src/SMESHGUI/SMESHGUI_CreateDualMeshOp.cxx +++ b/src/SMESHGUI/SMESHGUI_CreateDualMeshOp.cxx @@ -45,6 +45,7 @@ // Qt includes #include +#include // IDL includes #include @@ -226,10 +227,10 @@ bool SMESHGUI_CreateDualMeshOp::onApply() SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen(); SMESH::SMESH_Mesh_var newMesh; QByteArray newMeshName=myDlg->myMeshName->text().toUtf8(); + bool adapt_to_shape=myDlg->myProjShape->isChecked(); try { - // TODO: change name as previous name + "_dual" - newMesh = gen->CreateDualMesh(mesh, newMeshName.constData()); + newMesh = gen->CreateDualMesh(mesh, newMeshName.constData(), adapt_to_shape); if ( !newMesh->_is_nil() ) if ( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) ) diff --git a/src/SMESH_I/SMESH_Gen_i.cxx b/src/SMESH_I/SMESH_Gen_i.cxx index 7157f413c..0fd53cbc7 100644 --- a/src/SMESH_I/SMESH_Gen_i.cxx +++ b/src/SMESH_I/SMESH_Gen_i.cxx @@ -2820,7 +2820,8 @@ SMESH_Gen_i::ConcatenateCommon(const SMESH::ListOfIDSources& theMeshesArray, //================================================================================ 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); @@ -2841,10 +2842,12 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh CORBA::String_var mesh_var=GetORB()->object_to_string(mesh); std::string mesh_ior = mesh_var.in(); - fs::path tmp_folder = fs::unique_path(fs::path("dual_mesh-%%%%-%%%%")); + //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 = "MESH"; + std::string mesh_name = meshName; + MESSAGE("Working in folder" + tmp_folder.string()); // Running Python script assert(Py_IsInitialized()); @@ -2853,11 +2856,20 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh std::string cmd="import salome.smesh.smesh_tools as smt"; PyRun_SimpleString(cmd.c_str()); + std::string ats; + if(adapt_to_shape) + ats = "True"; + else + ats = "False"; - cmd = "smt.create_dual_mesh(\"" + mesh_ior + "\", \"" + dual_mesh_file.string() + "\", \"" + mesh_name + "\")"; + cmd = "smt.smesh_create_dual_mesh(\"" + mesh_ior + "\", \"" + + dual_mesh_file.string() + "\", mesh_name=\"" + mesh_name + "\", adapt_to_shape=" + ats + ")"; + MESSAGE(cmd); PyRun_SimpleString(cmd.c_str()); PyGILState_Release(gstate); + MESSAGE("Executed python script"); + MESSAGE("Mesh created in " + dual_mesh_file.string()); // Import created MED SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil()); @@ -2867,21 +2879,25 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh SALOMEDS::SObject_wrap meshSO = ObjectToSObject( newMesh ); if ( !meshSO->_is_nil() ) { - SetName( meshSO, meshName, "Mesh" ); - SetPixMap( meshSO, "ICON_SMESH_TREE_MESH"); + SetName( meshSO, meshName, meshName ); + SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED"); } SMESH_Mesh& newMesh2 = newMesh_i->GetImpl(); newMesh2.MEDToMesh(dual_mesh_file.c_str(), meshName); + MESSAGE("Imported created MED") + SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS(); newMeshDS->Modified(); *pyDump << newMesh << " = " << this << ".CreateDualMesh(" - << "'" << meshName << "') "; + << mesh << ", " + << "'" << meshName << "', " + << ats << ") "; return newMesh._retn(); } diff --git a/src/SMESH_I/SMESH_Gen_i.hxx b/src/SMESH_I/SMESH_Gen_i.hxx index bd8872038..822e71f2e 100644 --- a/src/SMESH_I/SMESH_Gen_i.hxx +++ b/src/SMESH_I/SMESH_Gen_i.hxx @@ -255,7 +255,8 @@ public: // Create dual mesh of a tetrahedron mesh SMESH::SMESH_Mesh_ptr CreateDualMesh(SMESH::SMESH_IDSource_ptr meshPart, - const char* meshName); + const char* meshName, + CORBA::Boolean adapt_to_shape); // Copy a part of mesh SMESH::SMESH_Mesh_ptr CopyMesh(SMESH::SMESH_IDSource_ptr meshPart, diff --git a/src/SMESH_SWIG/smeshBuilder.py b/src/SMESH_SWIG/smeshBuilder.py index 42bad03cf..62db88951 100644 --- a/src/SMESH_SWIG/smeshBuilder.py +++ b/src/SMESH_SWIG/smeshBuilder.py @@ -773,7 +773,7 @@ class smeshBuilder( SMESH._objref_SMESH_Gen, object ): aMesh = Mesh( self, self.geompyD, aSmeshMesh, name=name ) return aMesh - def CreateDualMesh( self, mesh, meshName): + def CreateDualMesh( self, mesh, meshName, adaptToShape): """ Create a dual of a mesh. @@ -782,13 +782,15 @@ class smeshBuilder( SMESH._objref_SMESH_Gen, object ): :class:`mesh, `. meshName: a name of the new mesh + adpatToShape: if true project boundary points on shape Returns: an instance of class :class:`Mesh` """ if isinstance( mesh, Mesh ): mesh = mesh.GetMesh() - dualMesh = SMESH._objref_SMESH_Gen.CreateDualMesh(self, mesh, meshName) + print("calling createdualmesh from Python") + dualMesh = SMESH._objref_SMESH_Gen.CreateDualMesh(self, mesh, meshName, adaptToShape) return Mesh(self, self.geompyD, dualMesh) diff --git a/src/SMESH_SWIG/smesh_tools.py b/src/SMESH_SWIG/smesh_tools.py index 38cd978e0..1a6995eed 100644 --- a/src/SMESH_SWIG/smesh_tools.py +++ b/src/SMESH_SWIG/smesh_tools.py @@ -17,7 +17,7 @@ from salome.smesh import smeshBuilder smesh = smeshBuilder.New() -def create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name="MESH"): +def smesh_create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name="MESH"): """ Create a dual of the mesh in input_file into output_file Args: @@ -25,7 +25,6 @@ def create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name="MESH output_file (string): dual mesh file """ # Import mesh from file - # mesh = salome.orb.string_to_object(salome.salome_study.myStudy.FindObjectID(mesh_id).GetIOR()) mesh = salome.orb.string_to_object(mesh_ior) if not mesh: raise Exception("Could not find mesh using id: ", mesh_ior) @@ -35,11 +34,14 @@ def create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name="MESH # We got a meshProxy so we need to convert pointer to MEDCoupling int_ptr = mesh.ExportMEDCoupling(True, True) dab = mc.FromPyIntPtrToDataArrayByte(int_ptr) - tetras = mc.MEDFileMesh.New(dab)[0] + tetras = mc.MEDFileMesh.New(dab)[0] # End of SMESH -> MEDCoupling part for dualmesh tetras = mc.MEDCoupling1SGTUMesh(tetras) polyh = tetras.computeDualMesh() + dual_volume_raw = polyh.getMeasureField(True).accumulate()[0] + + # Getting list of new points added on the skin skin = tetras.buildUnstructured().computeSkin() skin_polyh = polyh.buildUnstructured().computeSkin() allNodesOnSkinPolyh = skin_polyh.computeFetchedNodeIds() @@ -51,24 +53,25 @@ def create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name="MESH ptsAddedCoo = ptsAddedMesh.getCoords() ptsAddedCooModified = ptsAddedCoo[:] - # We need the geometry for that - # TODO : Loop on faces identify points associated to which face + # Matching faces with their ids faces = geompy.ExtractShapes(shape, geompy.ShapeType["FACE"], True) - #assert( len(faces) == 1 ) - ## projection des points ajoutés par le dual sur la surface - #for i,tup in enumerate(ptsAddedCooModified): - # vertex = geompy.MakeVertex(*tuple(tup)) - # prj = geompy.MakeProjection(vertex, faces) - # newCoor = geompy.PointCoordinates( prj ) - # ptsAddedCooModified[i] = newCoor - ## assign coordinates with projected ones - #polyh.getCoords()[ptsAdded] = ptsAddedCooModified - - print("Writing dual mesh in ", output_file) + id2face = {} + for face in faces: + id2face[face.GetSubShapeIndices()[0]] = face + print(id2face) + + ## Projecting each points added by the dual mesh on the surface it is + # associated with + for i, tup in enumerate(ptsAddedCooModified): + vertex = geompy.MakeVertex(*tuple(tup)) + shapes = geompy.GetShapesNearPoint(shape, vertex, + geompy.ShapeType["FACE"]) + prj = geompy.MakeProjection(vertex, + id2face[shapes.GetSubShapeIndices()[0]]) + new_coor = geompy.PointCoordinates(prj) + ptsAddedCooModified[i] = new_coor + + polyh.getCoords()[ptsAdded] = ptsAddedCooModified + polyh.setName(mesh_name) polyh.write(output_file) - - - - - diff --git a/test/SMESH_create_dual_mesh.py b/test/SMESH_create_dual_mesh.py index 007dbb8a7..08621f30e 100644 --- a/test/SMESH_create_dual_mesh.py +++ b/test/SMESH_create_dual_mesh.py @@ -50,7 +50,7 @@ Mesh_1 = smesh.Mesh(Sphere_1,'Mesh_1') status = Mesh_1.AddHypothesis( Sphere_1, NETGEN_3D_Parameters_1 ) NETGEN_1D_2D_3D = Mesh_1.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D) isDone = Mesh_1.Compute() -dual_Mesh_1 = smesh.CreateDualMesh( Mesh_1, 'dual_Mesh_1') +dual_Mesh_1 = smesh.CreateDualMesh( Mesh_1, 'dual_Mesh_1', True) assert(dual_Mesh_1.NbPolyhedrons() > 0) diff --git a/test/SMESH_create_dual_mesh_adapt.py b/test/SMESH_create_dual_mesh_adapt.py new file mode 100644 index 000000000..c95e6a62e --- /dev/null +++ b/test/SMESH_create_dual_mesh_adapt.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python + +### +### This file is generated automatically by SALOME v9.9.0 with dump python functionality +### + +import sys +import salome + +salome.salome_init() +import salome_notebook +notebook = salome_notebook.NoteBook() +sys.path.insert(0, r'/home/B61570/work_in_progress/dual_mesh') + +### +### GEOM component +### + +import GEOM +from salome.geom import geomBuilder +import math +import SALOMEDS + + +geompy = geomBuilder.New() + +O = geompy.MakeVertex(0, 0, 0) +OX = geompy.MakeVectorDXDYDZ(1, 0, 0) +OY = geompy.MakeVectorDXDYDZ(0, 1, 0) +OZ = geompy.MakeVectorDXDYDZ(0, 0, 1) +Cylinder_1 = geompy.MakeCylinderRH(100, 400) +Sphere_1 = geompy.MakeSpherePntR(O, 100) +Fuse_1 = geompy.MakeFuseList([Cylinder_1, Sphere_1], True, True) + +[geomObj_1,geomObj_2,geomObj_3] = geompy.ExtractShapes(Fuse_1, geompy.ShapeType["FACE"], True) + +top = geompy.CreateGroup(Fuse_1, geompy.ShapeType["FACE"]) +geompy.UnionIDs(top, geomObj_1.GetSubShapeIndices()) + +middle = geompy.CreateGroup(Fuse_1, geompy.ShapeType["FACE"]) +geompy.UnionIDs(middle, geomObj_2.GetSubShapeIndices()) + +bottom = geompy.CreateGroup(Fuse_1, geompy.ShapeType["FACE"]) +geompy.UnionIDs(bottom, geomObj_3.GetSubShapeIndices()) + +#[top, middle, bottom] = geompy.GetExistingSubObjects(Fuse_1, False) + +geompy.addToStudy( O, 'O' ) +geompy.addToStudy( OX, 'OX' ) +geompy.addToStudy( OY, 'OY' ) +geompy.addToStudy( OZ, 'OZ' ) +geompy.addToStudy( Cylinder_1, 'Cylinder_1' ) +geompy.addToStudy( Sphere_1, 'Sphere_1' ) +geompy.addToStudy( Fuse_1, 'Fuse_1' ) +geompy.addToStudyInFather( Fuse_1, top, 'top' ) +geompy.addToStudyInFather( Fuse_1, middle, 'middle' ) +geompy.addToStudyInFather( Fuse_1, bottom, 'bottom' ) + +### +### SMESH component +### + +import SMESH, SALOMEDS +from salome.smesh import smeshBuilder + +smesh = smeshBuilder.New() + +NETGEN_3D_Parameters_1 = smesh.CreateHypothesisByAverageLength( 'NETGEN_Parameters', 'NETGENEngine', 50, 0 ) +Mesh_1 = smesh.Mesh(Fuse_1,'Mesh_1') +status = Mesh_1.AddHypothesis( Fuse_1, NETGEN_3D_Parameters_1 ) +NETGEN_1D_2D_3D = Mesh_1.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D) +top_1 = Mesh_1.GroupOnGeom(top,'top',SMESH.FACE) +middle_1 = Mesh_1.GroupOnGeom(middle,'middle',SMESH.FACE) +bottom_1 = Mesh_1.GroupOnGeom(bottom,'bottom',SMESH.FACE) +isDone = Mesh_1.Compute() + +[ top_1, middle_1, bottom_1 ] = Mesh_1.GetGroups() + + +dual_Mesh_1 = smesh.CreateDualMesh(Mesh_1, 'dual_Mesh_1', True) +dual_Mesh_raw_1 = smesh.CreateDualMesh(Mesh_1, 'dual_Mesh_1', False) + +#Comparing volumes +dual_volume = dual_Mesh_1.GetVolume() +dual_raw_volume = dual_Mesh_raw_1.GetVolume() +print("dual_volume: ", dual_volume) +print("dual_raw_volume: ", dual_raw_volume) + +assert (dual_volume >= dual_raw_volume) + +if salome.sg.hasDesktop(): + salome.sg.updateObjBrowser() diff --git a/test/tests.set b/test/tests.set index f00bde4c6..977f4a667 100644 --- a/test/tests.set +++ b/test/tests.set @@ -63,6 +63,7 @@ SET(BAD_TESTS SMESH_test2.py SMESH_test4.py SMESH_create_dual_mesh.py + SMESH_create_dual_mesh_adapt.py ) IF(NOT WIN32) LIST(APPEND BAD_TESTS -- 2.39.2