Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Macro / ShowSalomeObject.py
1 # Copyright (C) 2014-2022  CEA/DEN, EDF R&D
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 ###
21 # Macro to show SALOME object in ParaView viewer.
22
23 # The macro inspects current selection obtained from SALOME GUI
24 # and displays supported objects in the viewer:
25 # - GEOM object
26 # - SMESH mesh object
27 ###
28
29 import tempfile, os
30
31 import SALOME
32
33 import salome
34 salome.salome_init()
35
36 session = salome.naming_service.Resolve('/Kernel/Session')
37
38 from pvsimple import *
39
40 selection = session.getSelection()
41
42 for entry in selection:
43     sobj = salome.myStudy.FindObjectID(entry)
44     try:
45         import GEOM
46         from salome.geom import geomBuilder
47         geompy = geomBuilder.New()
48         go = sobj.GetObject()._narrow(GEOM.GEOM_Object)
49         if go:
50             tmpf = tempfile.NamedTemporaryFile(suffix='.vtk')
51             fname = tmpf.name
52             tmpf.close()
53             geompy.ExportVTK(go, fname)
54             ShowParaviewView()
55             p = LegacyVTKReader(FileNames=[fname])
56             renderView = GetActiveViewOrCreate('RenderView')
57             pd = Show(p, renderView)
58             renderView.ResetCamera()
59             os.remove(fname)
60             pass
61     except:
62         # not geom object
63         pass
64     try: 
65         import SMESH
66         from salome.smesh import smeshBuilder
67         mesh = smeshBuilder.New()
68         mo = sobj.GetObject()._narrow(SMESH.SMESH_Mesh)
69         if mo:
70             tmpf = tempfile.NamedTemporaryFile(suffix='.med')
71             fname = tmpf.name
72             tmpf.close()
73             mo.ExportMED(fname, True, True, True)
74             ShowParaviewView()
75             p = MEDReader(FileName=fname)
76             renderView = GetActiveViewOrCreate('RenderView')
77             pd = Show(p, renderView)
78             renderView.ResetCamera()
79             os.remove(fname)
80             pass
81     except:
82         # not mesh object
83         pass
84     pass