Corrections to handle adapation to shape

This commit is contained in:
Yoann Audouin 2022-10-06 08:43:27 +02:00
parent 08485b06fa
commit 7d1a127bbb
9 changed files with 152 additions and 34 deletions

View File

@ -288,9 +288,11 @@ module SMESH
* Create a dual mesh of a Tetrahedron mesh * Create a dual mesh of a Tetrahedron mesh
* \param mesh - TetraHedron mesh to create dual from * \param mesh - TetraHedron mesh to create dual from
* \param meshName - a name of the new mesh * \param meshName - a name of the new mesh
* \param adaptToShape - if True project boundary point on shape
*/ */
SMESH_Mesh CreateDualMesh(in SMESH_IDSource mesh, SMESH_Mesh CreateDualMesh(in SMESH_IDSource mesh,
in string meshName) in string meshName,
in boolean adaptToShape)
raises ( SALOME::SALOME_Exception ); raises ( SALOME::SALOME_Exception );
/*! /*!

View File

@ -45,6 +45,7 @@
// Qt includes // Qt includes
#include <QLineEdit> #include <QLineEdit>
#include <QCheckBox>
// IDL includes // IDL includes
#include <SALOMEconfig.h> #include <SALOMEconfig.h>
@ -226,10 +227,10 @@ bool SMESHGUI_CreateDualMeshOp::onApply()
SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen(); SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
SMESH::SMESH_Mesh_var newMesh; SMESH::SMESH_Mesh_var newMesh;
QByteArray newMeshName=myDlg->myMeshName->text().toUtf8(); QByteArray newMeshName=myDlg->myMeshName->text().toUtf8();
bool adapt_to_shape=myDlg->myProjShape->isChecked();
try try
{ {
// TODO: change name as previous name + "_dual" newMesh = gen->CreateDualMesh(mesh, newMeshName.constData(), adapt_to_shape);
newMesh = gen->CreateDualMesh(mesh, newMeshName.constData());
if ( !newMesh->_is_nil() ) if ( !newMesh->_is_nil() )
if ( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) ) if ( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )

View File

@ -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, 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); 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); CORBA::String_var mesh_var=GetORB()->object_to_string(mesh);
std::string mesh_ior = mesh_var.in(); 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::create_directories(tmp_folder);
fs::path dual_mesh_file = tmp_folder / fs::path("tmp_dual_mesh.med"); 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 // Running Python script
assert(Py_IsInitialized()); 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"; std::string cmd="import salome.smesh.smesh_tools as smt";
PyRun_SimpleString(cmd.c_str()); 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()); PyRun_SimpleString(cmd.c_str());
PyGILState_Release(gstate); PyGILState_Release(gstate);
MESSAGE("Executed python script");
MESSAGE("Mesh created in " + dual_mesh_file.string());
// Import created MED // Import created MED
SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil()); 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 ); SALOMEDS::SObject_wrap meshSO = ObjectToSObject( newMesh );
if ( !meshSO->_is_nil() ) if ( !meshSO->_is_nil() )
{ {
SetName( meshSO, meshName, "Mesh" ); SetName( meshSO, meshName, meshName );
SetPixMap( meshSO, "ICON_SMESH_TREE_MESH"); SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
} }
SMESH_Mesh& newMesh2 = newMesh_i->GetImpl(); SMESH_Mesh& newMesh2 = newMesh_i->GetImpl();
newMesh2.MEDToMesh(dual_mesh_file.c_str(), meshName); newMesh2.MEDToMesh(dual_mesh_file.c_str(), meshName);
MESSAGE("Imported created MED")
SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS(); SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
newMeshDS->Modified(); newMeshDS->Modified();
*pyDump << newMesh << " = " << this *pyDump << newMesh << " = " << this
<< ".CreateDualMesh(" << ".CreateDualMesh("
<< "'" << meshName << "') "; << mesh << ", "
<< "'" << meshName << "', "
<< ats << ") ";
return newMesh._retn(); return newMesh._retn();
} }

View File

@ -255,7 +255,8 @@ public:
// Create dual mesh of a tetrahedron mesh // Create dual mesh of a tetrahedron mesh
SMESH::SMESH_Mesh_ptr CreateDualMesh(SMESH::SMESH_IDSource_ptr meshPart, 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 // Copy a part of mesh
SMESH::SMESH_Mesh_ptr CopyMesh(SMESH::SMESH_IDSource_ptr meshPart, SMESH::SMESH_Mesh_ptr CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,

View File

@ -773,7 +773,7 @@ class smeshBuilder( SMESH._objref_SMESH_Gen, object ):
aMesh = Mesh( self, self.geompyD, aSmeshMesh, name=name ) aMesh = Mesh( self, self.geompyD, aSmeshMesh, name=name )
return aMesh return aMesh
def CreateDualMesh( self, mesh, meshName): def CreateDualMesh( self, mesh, meshName, adaptToShape):
""" """
Create a dual of a mesh. Create a dual of a mesh.
@ -782,13 +782,15 @@ class smeshBuilder( SMESH._objref_SMESH_Gen, object ):
:class:`mesh, <SMESH.SMESH_IDSource>`. :class:`mesh, <SMESH.SMESH_IDSource>`.
meshName: a name of the new mesh meshName: a name of the new mesh
adpatToShape: if true project boundary points on shape
Returns: Returns:
an instance of class :class:`Mesh` an instance of class :class:`Mesh`
""" """
if isinstance( mesh, Mesh ): if isinstance( mesh, Mesh ):
mesh = mesh.GetMesh() 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) return Mesh(self, self.geompyD, dualMesh)

View File

@ -17,7 +17,7 @@ from salome.smesh import smeshBuilder
smesh = smeshBuilder.New() 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 """ Create a dual of the mesh in input_file into output_file
Args: 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 output_file (string): dual mesh file
""" """
# Import mesh from 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) mesh = salome.orb.string_to_object(mesh_ior)
if not mesh: if not mesh:
raise Exception("Could not find mesh using id: ", mesh_ior) 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 # We got a meshProxy so we need to convert pointer to MEDCoupling
int_ptr = mesh.ExportMEDCoupling(True, True) int_ptr = mesh.ExportMEDCoupling(True, True)
dab = mc.FromPyIntPtrToDataArrayByte(int_ptr) dab = mc.FromPyIntPtrToDataArrayByte(int_ptr)
tetras = mc.MEDFileMesh.New(dab)[0] tetras = mc.MEDFileMesh.New(dab)[0]
# End of SMESH -> MEDCoupling part for dualmesh # End of SMESH -> MEDCoupling part for dualmesh
tetras = mc.MEDCoupling1SGTUMesh(tetras) tetras = mc.MEDCoupling1SGTUMesh(tetras)
polyh = tetras.computeDualMesh() 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 = tetras.buildUnstructured().computeSkin()
skin_polyh = polyh.buildUnstructured().computeSkin() skin_polyh = polyh.buildUnstructured().computeSkin()
allNodesOnSkinPolyh = skin_polyh.computeFetchedNodeIds() 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() ptsAddedCoo = ptsAddedMesh.getCoords()
ptsAddedCooModified = ptsAddedCoo[:] ptsAddedCooModified = ptsAddedCoo[:]
# We need the geometry for that # Matching faces with their ids
# TODO : Loop on faces identify points associated to which face
faces = geompy.ExtractShapes(shape, geompy.ShapeType["FACE"], True) faces = geompy.ExtractShapes(shape, geompy.ShapeType["FACE"], True)
#assert( len(faces) == 1 ) id2face = {}
## projection des points ajoutés par le dual sur la surface for face in faces:
#for i,tup in enumerate(ptsAddedCooModified): id2face[face.GetSubShapeIndices()[0]] = face
# vertex = geompy.MakeVertex(*tuple(tup)) print(id2face)
# prj = geompy.MakeProjection(vertex, faces)
# newCoor = geompy.PointCoordinates( prj ) ## Projecting each points added by the dual mesh on the surface it is
# ptsAddedCooModified[i] = newCoor # associated with
## assign coordinates with projected ones for i, tup in enumerate(ptsAddedCooModified):
#polyh.getCoords()[ptsAdded] = 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
print("Writing dual mesh in ", output_file)
polyh.setName(mesh_name) polyh.setName(mesh_name)
polyh.write(output_file) polyh.write(output_file)

View File

@ -50,7 +50,7 @@ Mesh_1 = smesh.Mesh(Sphere_1,'Mesh_1')
status = Mesh_1.AddHypothesis( Sphere_1, NETGEN_3D_Parameters_1 ) status = Mesh_1.AddHypothesis( Sphere_1, NETGEN_3D_Parameters_1 )
NETGEN_1D_2D_3D = Mesh_1.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D) NETGEN_1D_2D_3D = Mesh_1.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D)
isDone = Mesh_1.Compute() 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) assert(dual_Mesh_1.NbPolyhedrons() > 0)

View File

@ -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()

View File

@ -63,6 +63,7 @@ SET(BAD_TESTS
SMESH_test2.py SMESH_test2.py
SMESH_test4.py SMESH_test4.py
SMESH_create_dual_mesh.py SMESH_create_dual_mesh.py
SMESH_create_dual_mesh_adapt.py
) )
IF(NOT WIN32) IF(NOT WIN32)
LIST(APPEND BAD_TESTS LIST(APPEND BAD_TESTS