Hello everybody out there!

    After further investigations, it turns out that tools from SLIM are
producing files on MSH format version 3, while WaveWatch 3 is using MSH
format version 2. I will probably modify WaveWatch 3 to make it accept
version 3 format, but before I need to convert files from version 3 to
version 2. Therefore, my questions are:

 1. Where can I find the description of MSH format version 3?
 2. Is there any script converting from version 3 to version 2?

    Anyway, I have started some code to convert files (code attached to
this message), but without version 3 description, I am a bit stuck.

    Best regards.

-- 
Yoann LE BARS
http://le-bars.net/yoann
*Diaspora : yleb...@framasphere.org

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Convert a MSH version 3 file to a MSH version 2 file.

:module: convertmsh
:author: Le Bars, Yoann
:version: 1.0
:date: 2018/04/12

This program is distributed under CeCLL-B license, it can be copied and
modified freely as long as initial author is cited. Complete text
of CeCILL licence can be found on-line:

<http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html>
"""

# Program version.
__version__ = '1.0'

import sys
from os import path

def eprint (*args, **kwargs):
    """
    Write into errors dedicated output.

    :param args: Command line arguments.
    :param kargs: End of command line arguments.
    """

    print(*args, file = sys.stderr, **kwargs)

def convert (inputFileName, outputFileName):
    """
    Actually converts the file.

    :param inputFileName: Name of the MSH version 3 input file.
    :param outputFileName: Name of the MSH version 2 output file.
    """

    # Input file.
    inFile = open(inputFileName, 'r')
    # Output file.
    outFile = open(outputFileName, 'w')

    # Current line to be processed.
    for line in inFile:
        if '$MeshFormat' in line:
            outFile.write('$MeshFormat\n')
            line = inFile.readline()
            # Mesh type descriptors.
            versionNumber, fileType, dataSize = line.split()
            outFile.write('2.2 0 ' + dataSize + '\n')
            line = inFile.readline()
            outFile.write('$EndMeshFormat\n')
        elif '$Nodes' in line:
            outFile.write('$Nodes$\n')
            line = inFile.readline()
            outFile.write(line)
            # How many nodes in the mesh.
            nNodes = int(line)
            for i in range(0, nNodes):
                line = inFile.readline()
                # Node descriptors.
                n, x, y, dum1, dum2, dum3, z, dum4 = line.split()
                outFile.write(n + ' ' + x + ' ' + y + ' ' + z + '\n')
            line = inFile.readline()
            outFile.write('$EndNodes\n')
        elif '$Elements' in line:
            outFile.write('$Elements\n')
            line = inFile.readline()
            outFile.write(line)
            # How many elements in the mesh.
            nElements = int(line)
            for i in range(0, nElements):
                line = inFile.readline()
                outFile.write(line)
            line = inFile.readline()
            outFile.write('$EndElements\n')

    inFile.close()
    outFile.close()
            

def _main ():
    """
    Main function of the program.
    
    :return: 0 if everything went good and -1 if the input file is incorrect,
             -2 if the the path for output exists and is not a file.
    """

    import argparse

    # Command line parser.
    parser = argparse.ArgumentParser(description='Convertmsh convert a MSH '
                                                 'version 2 file into a MSH '
                                                 'version 3 file.')
    parser.add_argument('-v', '--version', action = 'version',
                        version = '%(prog)s ' + __version__,
                        help = 'Display program version and exit.')
    parser.add_argument('-f', '--force', action = 'store_true',
                        help = 'Force overwriting output file.')
    parser.add_argument('input', action = 'store', type = str,
                        help = 'Input file name.')
    parser.add_argument('output', action = 'store', type = str,
                        help = 'Output file name.')
    # Arguments in command line.
    args = parser.parse_args()

    if (not path.exists(args.input)) or (not path.isfile(args.input)):
        eprint('Error: incorrect input file.')
        return -1

    elif (path.exists(args.output)) and (not path.isfile(args.output)):
        eprint('Error: "' + args.output + '" exists and is not a file.')
        return -2

    else:
        if path.exists(args.output) and (not args.force):
            possibleChoices = ['y', 'Y', 'yes', 'Yes', 'YES', 'yEs', 'yeS',
                               'YEs', 'YeS', 'yES', 'n', 'N', 'no', 'No',
                               'NO', 'nO']
            # Users’ choice.
            choice = ''
            while not (choice in possibleChoices):
                choice = input('"' + args.output + '" exists, overwrite it '
                               '(Y or N)? ')
            if (choice[0].lower() == 'n'):
                return 0
        convert(args.input, args.output)
        return 0

if __name__ == '__main__':
    sys.exit(_main())
_______________________________________________
gmsh mailing list
gmsh@onelab.info
http://onelab.info/mailman/listinfo/gmsh

Reply via email to