Personal tools
You are here: Home Forum Use Create mesh in batch mode / Macros

Create mesh in batch mode / Macros

Up to Use
  • Create mesh in batch mode / Macros

    Posted by Fabian Braennstroem at January 11. 2006
    Hi, I just do my first steps with salome and wonder if there is a possibility to create meshes from the console in batch mode. I would be really nice :) Greetings! Fabian
    • Re: Create mesh in batch mode / Macros

      Posted by Fabian Braennstroem at January 11. 2006
      Fabian Braennstroem wrote:
      Hi,

      I just do my first steps with salome and wonder if there is a possibility to create meshes from the console in batch mode.

      I would be really nice :)

      Greetings!
      Fabian

      Sorry, I just missed the last topic. Is there any special documentation or tutorial for it? Fabian
      • Re: Create mesh in batch mode / Macros

        Posted by Erwan ADAM at January 13. 2006
        Hi Fabian,

        It's easy to create geometries and meshes in the python console which
        appear in salome desktop ... But il also possible (with some work :) ) in
        pure batch mode (I mean in a terminal via a command line)

        To test theses functionalities, save the following script in a file named
        "as_you_want.py". You can test it in salome python console ( do an
        execfile "as_you_want.py";) and you can test it in "pure batch" (with
        "python as_you_want.py";). You should have created two files
        ("box_hexa.med" and "box_tetra.med";)

        Caution : There is a regression in salome version >= 2.2.6 the
        ExportMED line crash the application. This regression has been
        sent to SMESH team and should be corrected soon !

        Cheers,

                            E.A.

        #########################################

        # ----------
        # Tip to detect if we are in salome embedded python console
        # Indeed, in this context the __import__ function has been
        # modified ... It is just a tip ... and it uses a non
        # documented feature !
        # ----------

        try:
            __import_____module__ = __import__.__module__
        except:
            __import_____module__ = '__builtin__'
            pass

        if __import_____module__ == "import_hook":
            in_tui = 1
            print "Embedded python"
        else:
            in_tui = 0
            print "External python ('batch mode')"
            pass

        # -------------
        # Class SalomeSession to lanuch the services of salome
        # in 'batch mode' then to close this services at the
        # end of the script ... Of course, this class should be
        # defined elsewhere but for the script to be self-consistent
        # I've put it here
        # -------------

        class SalomeSession(object):
           
            def getKRD(self):
                import os
                KERNEL_ROOT_DIR = os.getenv("KERNEL_ROOT_DIR";)
                if KERNEL_ROOT_DIR is None:
                    msg  = '\n\n'
                    msg += 'KERNEL_ROOT_DIR not defined ...\n'
                    msg += 'Be sure you are in salome environement.\n'
                    raise Exception(msg)
                return KERNEL_ROOT_DIR
           
            def __init__(self, modules):
                #
                # Save (copy) the real user arguments
                #
                import sys
                argv_ini = sys.argv[:]
                #
                # Do like a runSalome --modules=... --logger -- terminal
                #
                import sys
                sys.argv  = ['bin/salome/runSalome.py']
                sys.argv += ['--modules=%s'%(",".join(modules))]
                sys.argv += ['--logger']
                sys.argv += ['--terminal']
                sys.argv += ['--standalone=pyContainer']
                #
                # Find a free port for naming service
                #
                port = self.searchFreePort()
                # --
                # E.A. : sys.path.insert(0, KERNEL_ROOT_DIR+"/bin/salome";)
                # because there is multiple runSalome.py in V2_2_4 distribution
                # This will be removed after ...
                #
                KERNEL_ROOT_DIR = self.getKRD()
                sys.path.insert(0, KERNEL_ROOT_DIR+"/bin/salome";)
                #
                import runSalome
                self.runSalome = runSalome
                clt, args = runSalome.main()
                runSalome.clt, runSalome.args = clt, args
                self.port = runSalome.args['port']
                self.naming_service = clt
                self.orb = clt.orb
                from LifeCycleCORBA import LifeCycleCORBA
                lcc = LifeCycleCORBA(clt.orb)
                self.lcc = lcc
                #
                # revert to the user args
                #
                import sys
                sys.argv = argv_ini
                #
                return
           
            def searchFreePort(self):
                print "Searching a free port for naming service:",
                NSPORT=2810
                limit=NSPORT
                limit=limit+100
                while 1:
                    print "%s "%(NSPORT),
                    import os
                    status = os.system("netstat -ltn | grep -E :%s"%(NSPORT))
                    if status:
                        home = os.environ['HOME']
                        hostname = os.environ['HOSTNAME']
                        os.environ['OMNIORB_CONFIG'] = '%s/.omniORB_%s.cfg'%(home, NSPORT)
                        f = open(os.environ['OMNIORB_CONFIG'], "w";)
                        f.write("ORBInitRef NameService=corbaname::%s:%s\n"%(hostname, NSPORT))
                        f.close()
                        print "- Ok"
                        break
                    if NSPORT == limit:
                        msg  = ""
                        msg += "I Can't find a free port to launch omniNames\n"
                        msg += "I suggest you to kill the running servers and try again.\n"
                        raise msg
                    NSPORT=NSPORT+1
                    pass
                return NSPORT
           
            def __del__(self):
                try:
                    port = self.port
                except:
                    return
                from os import system
                system("killSalomeWithPort.py %s"%(port))
                return
           
            pass

        #
        # Import modules
        #

        if in_tui:
            import salome
            def updateStudy():
                salome.sg.updateObjBrowser(1)
                return
            from geompy import *
        else:
            salome = SalomeSession(modules=['GEOM','SMESH'])
            def updateStudy():
                return
            from batchmode_geompy import *
            pass

        #
        # Construction of the geometrical box
        #

        box = MakeBox(0.0, 0.0, 0.0, 100.0, 100.0, 100.0)
        addToStudy(box, "box";)
        updateStudy()

        #
        # Construction of the mesh hexa
        #

        import StdMeshers
        import SMESH
        smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH";)
        smesh.SetCurrentStudy(myStudy)

        mesh = smesh.CreateMesh(box)
        ior  = salome.orb.object_to_string(mesh)
        sobj = myStudy.FindObjectIOR(ior)
        attr = sobj.FindAttribute("AttributeName";)[1]
        attr.SetValue("BOX_HEXA";)

        hyp1d = smesh.CreateHypothesis("Regular_1D", "libStdMeshersEngine.so";)
        hyp2d = smesh.CreateHypothesis("Quadrangle_2D", "libStdMeshersEngine.so";)
        hyp3d = smesh.CreateHypothesis("Hexa_3D", "libStdMeshersEngine.so";)

        algo1d = smesh.CreateHypothesis("NumberOfSegments", "libStdMeshersEngine.so";)
        algo1d.SetNumberOfSegments(10)

        for tool in [hyp1d, hyp2d, hyp3d, algo1d]:
            mesh.AddHypothesis(box, tool)
            pass

        b = smesh.Compute(mesh, box)
        if in_tui:
            smeshgui = salome.ImportComponentGUI("SMESH";)
            smeshgui.Init(salome.myStudyId)
            smeshgui.SetMeshIcon(salome.ObjectToID(mesh), b )
            pass

        updateStudy()

        file_name = "box_hexa.med"
        from os import system
        system("rm -f %s > /dev/null 2>&1"%(file_name))
        mesh.ExportMED(file_name, 0)


        #
        # Construction of the mesh tetra
        #

        import StdMeshers
        import SMESH
        smesh = salome.lcc.FindOrLoadComponent("FactoryServer", "SMESH";)
        smesh.SetCurrentStudy(myStudy)

        mesh = smesh.CreateMesh(box)
        ior  = salome.orb.object_to_string(mesh)
        sobj = myStudy.FindObjectIOR(ior)
        attr = sobj.FindAttribute("AttributeName";)[1]
        attr.SetValue("BOX_TETRA";)

        hyp1d = smesh.CreateHypothesis("Regular_1D", "libStdMeshersEngine.so";)
        hyp2d = smesh.CreateHypothesis("MEFISTO_2D", "libStdMeshersEngine.so";)
        hyp3d = smesh.CreateHypothesis("NETGEN_3D", "libNETGENEngine.so";)

        algo1d = smesh.CreateHypothesis("NumberOfSegments", "libStdMeshersEngine.so";)
        algo1d.SetNumberOfSegments(10)
        algo2d = smesh.CreateHypothesis("MaxElementArea", "libStdMeshersEngine.so";)
        algo2d.SetMaxElementArea(1000.0)
        algo3d = smesh.CreateHypothesis("MaxElementVolume", "libStdMeshersEngine.so";)
        algo3d.SetMaxElementVolume(100000.0)

        for tool in [hyp1d, hyp2d, hyp3d, algo1d, algo2d, algo3d]:
            mesh.AddHypothesis(box, tool)
            pass

        b = smesh.Compute(mesh, box)
        if in_tui:
            smeshgui = salome.ImportComponentGUI("SMESH";)
            smeshgui.Init(salome.myStudyId)
            smeshgui.SetMeshIcon(salome.ObjectToID(mesh), b )
            pass

        updateStudy()

        file_name = "box_tetra.med"
        from os import system
        system("rm -f %s > /dev/null 2>&1"%(file_name))
        mesh.ExportMED(file_name, 0)

        #########################################

        • Re: Create mesh in batch mode / Macros

          Posted by Erwan ADAM at January 13. 2006
          If you want to save the script easily, I've put a copy on my personal home page :

          http://erwan.adam.free.fr/Salome/mesh_box_batch_or_tui.py

          E.A.

          • Re: Create mesh in batch mode / Macros

            Posted by Fabian Braennstroem at January 15. 2006
            Hi Adam,
            ADAM Erwan wrote:
            If you want to save the script easily, I've put a copy on my personal home page :

            http://erwan.adam.free.fr/Salome/mesh_box_batch_or_tui.py

            E.A.


            thanks for the python script! Unfortunately, it does not work running: python mesh_box_batch_or_tui.py I get: `--> python mesh_box_batch_or_tui.py External python ('batch mode') tcp 0 0 0.0.0.0:2810 0.0.0.0:* LISTEN Searching a free port for naming service: 2810 2811 Traceback (most recent call last): File "mesh_box_batch_or_tui.py", line 137, in ? salome = SalomeSession(modules=['GEOM','SMESH']) File "mesh_box_batch_or_tui.py", line 61, in __init__ port = self.searchFreePort() File "mesh_box_batch_or_tui.py", line 99, in searchFreePort hostname = os.environ['HOSTNAME'] File "/usr/lib/python2.4/UserDict.py", line 17, in __getitem__ def __getitem__(self, key): return self.data[key] KeyError: 'HOSTNAME' Could that be related to a wrong installation? And in salome I get: Python 2.2.1 (#1, Aug 30 2002, 12:15:30) [GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on linux2 type help to get general information on environment >>> execfile "mesh_box_batch_or_tui.py" File "", line 1 execfile "mesh_box_batch_or_tui.py" ^ SyntaxError: invalid syntax >>> Do you have any idea!? Greetings! Fabian
            • Re: Create mesh in batch mode / Macros

              Posted by Fabian Braennstroem at January 15. 2006
              Hi Adam, sorry the post above was not readable ... hopefully this one is.
              ADAM Erwan wrote:
              If you want to save the script easily, I've put a copy on my personal home page :

              http://erwan.adam.free.fr/Salome/mesh_box_batch_or_tui.py

              E.A.


              thanks for the python script! Unfortunately, it does not work running: python mesh_box_batch_or_tui.py I get: `--> python mesh_box_batch_or_tui.py External python ('batch mode') tcp 0 0 0.0.0.0:2810 0.0.0.0:* LISTEN Searching a free port for naming service: 2810 2811 Traceback (most recent call last): File "mesh_box_batch_or_tui.py", line 137, in ? salome = SalomeSession(modules=['GEOM','SMESH']) File "mesh_box_batch_or_tui.py", line 61, in __init__ port = self.searchFreePort() File "mesh_box_batch_or_tui.py", line 99, in searchFreePort hostname = os.environ['HOSTNAME'] File "/usr/lib/python2.4/UserDict.py", line 17, in __getitem__ def __getitem__(self, key): return self.data[key] KeyError: 'HOSTNAME' Could that be related to a wrong installation? And in salome I get: Python 2.2.1 (#1, Aug 30 2002, 12:15:30) [GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on linux2 type help to get general information on environment >>> execfile "mesh_box_batch_or_tui.py" File "", line 1 execfile "mesh_box_batch_or_tui.py" ^ SyntaxError: invalid syntax >>> Do you have any idea!? Greetings! Fabian
              • Re: Create mesh in batch mode / Macros

                Posted by Fabian Braennstroem at January 15. 2006
                Strange... Does anybody know, how I can get those in readable format?
                • Re: Create mesh in batch mode / Macros

                  Posted by Erwan ADAM at January 16. 2006
                  Hi,

                  Could you try with the new version of the file I've put on my website
                  (I don't need an HOSTNAME variable anymore) ?

                  In salome, it's execfile("as_you_want.py";)

                  Cheers,

                           E.A.
                  • Re: Create mesh in batch mode / Macros

                    Posted by Fabian Braennstroem at January 18. 2006
                    Hi, thanks for the help. Unfortunately I get in salome: >>> execfile ("mesh_box_batch_or_tui.py") External python ('batch mode') Searching a free port for naming service: 2810 2811 - Ok source= ['--modules', 'GEOM', 'SMESH', '--logger', '--terminal', '--standalone', 'pyContainer'] opts= {'s': ['pyContainer'], 'm': ['GEOM', 'SMESH'], 'l': [], 't': []} args= {u'portkill': 1, 'file': 0, u'killall': 0, 'embedded': [u'registry', u'study', u'moduleCatalog', u'cppContainer'], 'standalone': ['pyContainer'], 'appname': 'salome', 'modules': ['GEOM', 'SMESH'], u'gui': 0, u'xterm': 0, 'SMESH_plugins': [u'NETGENPlugin', u'GHS3DPlugin'], 'key': [], u'logger': 1, 'interp': 0, 'port': 2811, 'containers': []} le fichier /tmp/fab_2811_SALOME_pidict des process SALOME n'est pas accessible startSalome {u'portkill': 1, 'file': 0, u'killall': 0, 'embedded': [u'registry', u'study', u'moduleCatalog', u'cppContainer'], 'standalone': ['pyContainer'], 'appname': 'salome', 'modules': ['GEOM', 'SMESH'], u'gui': 0, u'xterm': 0, 'SMESH_plugins': [u'NETGENPlugin', u'GHS3DPlugin'], 'key': [], u'logger': 1, 'interp': 0, 'port': 2811, 'containers': []} Searching Logger in Naming Service ++++++++++++++++++++++++++++++++++++++++ --- erreur au lancement Salome --- Saving of the dictionary of Salome processes in /tmp/fab_2811_SALOME_pidict To kill SALOME processes from a console (kill all sessions from all ports): python killSalome.py To kill SALOME from the present interpreter, if it is not closed : killLocalPort() --> kill this session (use CORBA port from args of runSalome) givenPortKill(port) --> kill a specific session with given CORBA port killAllPorts() --> kill all sessions runSalome, with --killall option, starts with killing the processes resulting from the previous execution. Traceback (most recent call last): File "/opt/salome/KERNEL_2.2.2/bin/salome/runSalome.py", line 667, in useSalome clt = startSalome(args, modules_list, modules_root_dir) File "/opt/salome/KERNEL_2.2.2/bin/salome/runSalome.py", line 499, in startSalome clt.waitLogger("Logger") File "/opt/salome/KERNEL_2.2.2/bin/salome/orbmodule.py", line 171, in waitLogger if count > maxcount : raise "Impossible de trouver %s" % name Impossible de trouver Logger Traceback (most recent call last): File "", line 1, in ? File "mesh_box_batch_or_tui.py", line 145, in ? salome = SalomeSession(modules=['GEOM','SMESH']) File "mesh_box_batch_or_tui.py", line 76, in __init__ self.orb = clt.orb AttributeError: 'NoneType' object has no attribute 'orb' And on the console: `--> python mesh_box_batch_or_tui.py External python ('batch mode') tcp 0 0 0.0.0.0:2810 0.0.0.0:* LISTEN Searching a free port for naming service: 2810 2811 - Ok Traceback (most recent call last): File "mesh_box_batch_or_tui.py", line 145, in ? salome = SalomeSession(modules=['GEOM','SMESH']) File "mesh_box_batch_or_tui.py", line 70, in __init__ import runSalome File "/opt/salome/KERNEL_2.2.2/bin/salome/runSalome.py", line 4, in ? import orbmodule File "/opt/salome/KERNEL_2.2.2/bin/salome/orbmodule.py", line 3, in ? from omniORB import CORBA ImportError: No module named omniORB Before, I had to define the KERNEL_ROOT_DIR (/opt/salome/KERNEL_2.2.2/). Do you have any more idea? Greetings! Fabian
                    • Re: Create mesh in batch mode / Macros

                      Posted by Erwan ADAM at January 19. 2006
                      For use in salome, could you give the output of :

                      print __import__

                      then

                      print __import__.__module__

                      in the python interpreter in salome


                      For the external use, are you sure that you've sourced
                      the whole salome environement before launching the
                      test ?

                      E.A.

                      • Re: Create mesh in batch mode / Macros

                        Posted by Fabian Braennstroem at January 19. 2006
                        Hi, in external 'mode' I forgot to source the environment; now it works! Thanks!
                        Fabian
                        • Re: Create mesh in batch mode / Macros

                          Posted by Fabian Braennstroem at January 19. 2006
                          ... but not in Salome: Output in Salome: >>> print __import__ then print __import__.__module__ File "", line 1 t __import__.__module__ ^ SyntaxError: invalid syntax >>> Fabian
                          • Re: Create mesh in batch mode / Macros

                            Posted by Fabian Braennstroem at January 19. 2006
                            ... and I have one more question: Where can I find more about python programming for salome? In particular I am looking for functions to save, load and export the geometry, to explode the geometry and apply different hypothesis/algorithms to certain faces!? To apply the hypothesis to a certain face, you probably do the same as for the box, just with a different name!? for tool in [hyp1d, hyp2d, hyp3d, algo1d]: mesh.AddHypothesis(face1, tool) pass Greetings! Fabian
                          • Re: Create mesh in batch mode / Macros

                            Posted by Erwan ADAM at January 20. 2006
                            Fabian Braennstroem wrote:
                            ... but not in Salome: Output in Salome: >>> print __import__ then print __import__.__module__ File "", line 1 t __import__.__module__ ^ SyntaxError: invalid syntax >>> Fabian

                            I think you have misunderstood ...
                            Just type "print __import__"  then try "print __import__.__module__"  .

                            Cheers,

                                                E.A.
                            • Re: Create mesh in batch mode / Macros

                              Posted by Fabian Braennstroem at January 23. 2006
                              Hehe, as you can see, I am pretty new to python... Now the output: >>> print __import__ >>> print __import__.__module__ Traceback (most recent call last): File "", line 1, in ? AttributeError: 'function' object has no attribute '__module__' Hope it is a bit more readable. Greetings! Fabian
                              • Re: Create mesh in batch mode / Macros

                                Posted by Edmondo Minisci at January 31. 2006
                                -- @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } -->

                                Dear All,
                                I'm new into the Salome world. I was just successful to install Salome on a SUSE 9.1 platform.
                                I downloaded the macro file “mesh_box_batch_or_tui.py” and now I'm trying to launch this application.

                                I got the following error message:
                                *** Abort *** an exception was raised, but no catch was found.
                                ... The exception is:0x41e762f5 : Standard_NumericError: OSD_Path::TrekValue : where is invalidTraceback (most recent call last):

                                File "mesh_box_batch_or_tui.py", line 223, in ?
                                mesh.ExportMED(file_name, 0)
                                File "/home/edmondo/Documents/salome_inst/SMESH_2.2.8/lib/python2.2/site-packages/salome/SMESH_Mesh_idl.py", line 477, in ExportMED
                                return _omnipy.invoke(self, "ExportMED", _0_SMESH.SMESH_Mesh._d_ExportMED, args)
                                omniORB.CORBA.COMM_FAILURE: Minor: 111, Completed: COMPLETED_NO.

                                Could anybody tell me what goes wrong?

                                Thank you in advance

                                Edmondo


                                • Re: Create mesh in batch mode / Macros

                                  Posted by Erwan ADAM at February 01. 2006
                                  Dear Edmondo,

                                  As it was mentionned in a previous message in the thread,  there is a regression for
                                  the ExortMED function in salome 2.2.6 to 2.2.8 (it is corrected now). You should avoid
                                  this crash giving the full path of the file to be saved :

                                  mesh.ExportMED(the_path/file_name, 0)

                                  sorry for the inconvenience,

                                  E.A.
                                  • Re: Create mesh in batch mode / Macros

                                    Posted by Edmondo Minisci at February 01. 2006
                                    Dear Erwan,

                                    actually, it works.
                                    Now, I will spend some time to understand what the script really does.

                                    Thank you very much.

                                    Edmondo
                                    • Re: Create mesh in batch mode / Macros

                                      Posted by Edmondo Minisci at February 02. 2006
                                      Dear All/Dear Erwan,

                                      the try option, to understand if we are in embedded python or not, seams not working on my installation. When I run the procedure from the salome console, "batch mode" is recognized and after a while the procedure stops with obvious error messages.

                                      All goes right if I modify the file and impose "in_tui=1".

                                      Do you have any idea why this happens?

                                      My bests

                                      Edmondo
                                      • Re: Create mesh in batch mode / Macros

                                        Posted by Erwan ADAM at February 03. 2006
                                        Dear Fabian, Edmondo and others ...,

                                        I've put a new copy of the script file on my web site :

                                        http://erwan.adam.free.fr/Salome/mesh_box_batch_or_tui.py

                                        It should correct the bugs encounter with the "batch" or "tui" detection.

                                        Cheers,

                                                  E.A.
                                        • Re: Create mesh in batch mode / Macros

                                          Posted by Fabian Braennstroem at February 21. 2006
                                          Hi Erwan, thanks! I still get error messages in the gui, but it works! Fabian
                                        • Re: Create mesh in batch mode / Macros

                                          Posted by Fabian Braennstroem at February 21. 2006
                                          Hi Erwan, thanks! I still get error messages in the gui, but it works! Fabian
Powered by Ploneboard
Document Actions