From 8391d51792787a79e8a1823cb7e61a222c092d2f Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9dric=20Aguerre?= Date: Fri, 31 Oct 2014 15:00:27 +0100 Subject: [PATCH] [Demo] command_line [Demo] command_line --- CMakeLists.txt | 1 + examples/CMakeLists.txt | 20 +++++ examples/command_line/CMakeLists.txt | 27 +++++++ examples/command_line/dataset.py | 78 +++++++++++++++++++ examples/command_line/medio.py | 38 +++++++++ examples/command_line/medviewer.py | 111 +++++++++++++++++++++++++++ examples/command_line/run_demo.py | 34 ++++++++ 7 files changed, 309 insertions(+) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/command_line/CMakeLists.txt create mode 100644 examples/command_line/dataset.py create mode 100644 examples/command_line/medio.py create mode 100644 examples/command_line/medviewer.py create mode 100644 examples/command_line/run_demo.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 2744f38d..7d250118 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,6 +237,7 @@ IF(SALOME_BUILD_TESTS AND NOT SALOME_LIGHT_ONLY) ADD_SUBDIRECTORY(test) ENDIF() ADD_SUBDIRECTORY(bin) +ADD_SUBDIRECTORY(examples) # Header configuration # ==================== diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..84faaa4e --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (C) 2010-2014 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +ADD_SUBDIRECTORY(command_line) diff --git a/examples/command_line/CMakeLists.txt b/examples/command_line/CMakeLists.txt new file mode 100644 index 00000000..2928427f --- /dev/null +++ b/examples/command_line/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright (C) 2010-2014 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +SET(_examples_files + dataset.py + medio.py + medviewer.py + run_demo.py + ) + +SALOME_INSTALL_SCRIPTS("${_examples_files}" ${SALOME_INSTALL_SCRIPT_PYTHON}/command_line) diff --git a/examples/command_line/dataset.py b/examples/command_line/dataset.py new file mode 100644 index 00000000..cff4c01d --- /dev/null +++ b/examples/command_line/dataset.py @@ -0,0 +1,78 @@ +def create_geometry(out_filename=None): + import GEOM + import salome + from salome.geom import geomBuilder + geompy = geomBuilder.New(salome.myStudy) + + # create a cloud of points + points = [ + geompy.MakeVertex(0,0,0), + geompy.MakeVertex(9,0,0), + geompy.MakeVertex(0,9,0), + geompy.MakeVertex(9,9,0), + geompy.MakeVertex(3,3,1), + geompy.MakeVertex(6,6,2)] + + # create SmoothingSurface object + smoothingsurface = geompy.MakeSmoothingSurface(points) + if out_filename: + geompy.ExportBREP(smoothingsurface, out_filename) + + # add object in the study + id_smoothingsurface = geompy.addToStudy(smoothingsurface,"SmoothingSurface") + + return smoothingsurface +# + +def create_meshes(geometry, out_filename=None): + import SMESH + import salome + from salome.smesh import smeshBuilder + + smesh = smeshBuilder.New(salome.myStudy) + Mesh_tri = smesh.Mesh(geometry) + + Regular_1D = Mesh_tri.Segment() + Max_Size_1 = Regular_1D.MaxSize(1.28993) + + MEFISTO_2D = Mesh_tri.Triangle(algo=smeshBuilder.MEFISTO) + Nb_Segments_1 = smesh.CreateHypothesis('NumberOfSegments') + Nb_Segments_1.SetNumberOfSegments( 15 ) + Nb_Segments_1.SetDistrType( 0 ) + + Mesh_quad = smesh.Mesh(geometry) + status = Mesh_quad.AddHypothesis(Nb_Segments_1) + status = Mesh_quad.AddHypothesis(Regular_1D) + + Quadrangle_2D = Mesh_quad.Quadrangle(algo=smeshBuilder.QUADRANGLE) + isDone = Mesh_tri.Compute() + isDone = Mesh_quad.Compute() + smesh.SetName(Mesh_tri, 'Mesh_tri') + if out_filename: + Mesh_tri.ExportMED(out_filename, 0, SMESH.MED_V2_2, 1, None ,1) + smesh.SetName(Mesh_quad, 'Mesh_quad') + if out_filename: + Mesh_quad.ExportMED(out_filename, 0, SMESH.MED_V2_2, 0, None ,1) + + # Set names of Mesh objects + smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D') + smesh.SetName(Quadrangle_2D.GetAlgorithm(), 'Quadrangle_2D') + smesh.SetName(MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D') + smesh.SetName(Nb_Segments_1, 'Nb. Segments_1') + smesh.SetName(Max_Size_1, 'Max Size_1') + smesh.SetName(Mesh_tri.GetMesh(), 'Mesh_tri') + smesh.SetName(Mesh_quad.GetMesh(), 'Mesh_quad') + + return Mesh_tri, Mesh_quad +# + +from MEDLoader import * + +def create_field(mesh_filename, mesh_name, field_name, out_filename=None): + mesh = MEDLoader.ReadUMeshFromFile(mesh_filename, mesh_name) + src_field = mesh.fillFromAnalytic(ON_CELLS, 1, "7-sqrt((x-5.)*(x-5.)+(y-5.)*(y-5.)+(z-5.)*(z-5.))") + src_field.setName(field_name) + if out_filename: + MEDLoader.WriteField(out_filename, src_field, True) + return src_field +# diff --git a/examples/command_line/medio.py b/examples/command_line/medio.py new file mode 100644 index 00000000..5c0daf7e --- /dev/null +++ b/examples/command_line/medio.py @@ -0,0 +1,38 @@ +def __select_data(reader, dataname=None): + if dataname: + keys = reader.GetProperty("FieldsTreeInfo")[::2] + # list all the names of arrays that can be seen (including their spatial discretization) + arr_name_with_dis = [elt.split("/")[-1] for elt in keys] + # list all the names of arrays (Equal to those in the MED File) + separator = reader.GetProperty("Separator").GetData() + arr_name = [elt.split(separator)[0] for elt in arr_name_with_dis] + for idx in range(len(keys)): + if arr_name[idx] == dataname: + reader.AllArrays = keys[idx] + break + + return reader +# + +def get_element_type(reader): + # Return 'P0', 'P1'... + separator = reader.GetProperty("Separator").GetData() + return reader.AllArrays[0].split(separator)[1] +# + +def get_element_name(reader): + separator = reader.GetProperty("Separator").GetData() + return reader.AllArrays[0].split(separator)[0].split("/")[-1] +# + +def load_mesh(med_filename, mesh_name=None): + import pvsimple + reader = pvsimple.MEDReader(FileName=med_filename) + return __select_data(reader, mesh_name) +# + +def load_field(med_filename, field_name=None): + import pvsimple + reader = pvsimple.MEDReader(FileName=med_filename) + return __select_data(reader, field_name) +# diff --git a/examples/command_line/medviewer.py b/examples/command_line/medviewer.py new file mode 100644 index 00000000..d6c1ae9b --- /dev/null +++ b/examples/command_line/medviewer.py @@ -0,0 +1,111 @@ +class MEDViewer(): + + def __init__(self, createRenderView=True, interactive=False): + self.interactive = interactive + import pvsimple + + if createRenderView: + self.renderView = pvsimple.CreateRenderView() + else: + self.renderView = pvsimple.GetRenderView() + # + + def __displayInteractive(self, element): + from PyQt4 import QtCore, QtGui + from vtk.qt4 import QVTKRenderWindowInteractor + + class __InteractiveViewer(QtCore.QObject): + def __init__(self, viewer): + self.renderView = viewer.renderView + self.widget = QVTKRenderWindowInteractor.QVTKRenderWindowInteractor(\ + rw=self.renderView.GetRenderWindow(), + iren=self.renderView.GetInteractor()) + self.widget.Initialize() + self.widget.Start() + self.widget.show() + import pvsimple + pvsimple.ResetCamera() + pvsimple.Render(view=self.renderView) + # + def render(self): + pvsimple.Render(self.renderView) + # + # + app = QtGui.QApplication(['ParaView Python App']) + iv = __InteractiveViewer(self) + app.exec_() + # + + def __display(self, element, pause=False): + import pvsimple + pvsimple.SetActiveSource(element) + pvsimple.Show(view=self.renderView) + + if self.interactive: + self.__displayInteractive(element) + else: + pvsimple.Render(view=self.renderView) + pvsimple.ResetCamera() + pvsimple.SetActiveSource(element) + pvsimple.Render(view=self.renderView) + if pause: + raw_input("Press Enter key to continue") + # + + def display_mesh(self, element, pause=False): + self.__display(element, pause) + # + + def display_field(self, element, pause=False): + import pvsimple + pvsimple.SetActiveSource(element) + representation = pvsimple.Show(view=self.renderView) + + from medio import get_element_type, get_element_name + etype = get_element_type(element) + ename = get_element_name(element) + + if etype == 'P0': + representation.ColorArrayName = ("CELLS", ename) + data = element.CellData + elif etype == 'P1': + representation.ColorArrayName = ("POINTS", ename) + data = element.PointData + + # :TODO: Determine nb components + nb_cmp = 1 + mode = "Magnitude" # "Component" if nb_cmp > 1 + scalarbar = self.__build_scalar_bar(data, ename, nb_cmp, mode) + + pvsimple.SetActiveSource(element) + self.renderView.Representations.append(scalarbar) + self.__display(element, pause) + # + + def __build_scalar_bar(self, data, fieldname, nb_components, vector_mode): + import pvsimple + # Get data range (mini/maxi) + for n in range(data.GetNumberOfArrays()): + if data.GetArray(n).GetName() == fieldname: + mini,maxi = data.GetArray(n).GetRange() + + stepvalue = (maxi-mini)/100. # 100 steps + + # Build Lookup table + RGBPoints = [mini, 0.0, 0.0, 1.0, maxi, 1.0, 0.0, 0.0] + nb = int((maxi-mini)/stepvalue)-1 + Table = pvsimple.GetLookupTableForArray("", nb_components, VectorMode=vector_mode, ColorSpace='HSV') + Table.Discretize = 1 + Table.NumberOfTableValues = nb + Table.RGBPoints = RGBPoints + + representation = pvsimple.Show(view=self.renderView) + representation.Representation = 'Surface' + representation.LookupTable = Table + + # Build scalar bar + scalarbar = pvsimple.CreateScalarBar(LabelFormat = '%.1f',Title= "",LabelFontSize=12,Enabled=1,LookupTable=Table,TitleFontSize=12,LabelColor=[0.0, 0.0, 0.0],TitleColor=[0.0, 0.0, 0.0],) + return scalarbar + # + +# diff --git a/examples/command_line/run_demo.py b/examples/command_line/run_demo.py new file mode 100644 index 00000000..38ae4997 --- /dev/null +++ b/examples/command_line/run_demo.py @@ -0,0 +1,34 @@ +# To be run in SALOME environment (salome shell) +# A SALOME session MUST have been started (salome -t) + +import os + +from dataset import create_geometry, create_meshes, create_field + +out_basename = "smooth_surface" +dir_name = os.path.dirname(os.path.abspath(__file__)) +brep_filename = os.path.join(dir_name, out_basename+".brep") +med_filename = os.path.join(dir_name, out_basename+".med") +field_filename = os.path.join(dir_name, out_basename+"_and_field.med") + +def generate_data(): + geometry = create_geometry(out_filename=brep_filename) + mesh_tri, mesh_quad = create_meshes(geometry, out_filename=med_filename) + field = create_field(med_filename, "Mesh_tri", "field_on_tri_cells", out_filename=field_filename) +# + +import salome +salome.salome_init() + +generate_data() + +from medio import load_mesh, load_field +mesh = load_mesh(med_filename, mesh_name="Mesh_tri") +field = load_field(field_filename, field_name="field_on_tri_cells") + +from medviewer import MEDViewer +viewer = MEDViewer(interactive=False) +viewer.display_mesh(mesh, pause=True) + +viewer2 = MEDViewer(interactive=True) +viewer2.display_field(field, pause=False) -- 2.39.2