3. Writing the macro command code#

This is the main function of the order declared in the catalog. with the op argument, cf. Macro-order catalog:

op= OPS ("code_aster.macrocommands.conv_mail_med_ops.conv_mail_med_ops")

So the associated file is code_aster/macrocommands/conv_mail_med_ops.py Of which here is the skeleton provided:

from.Cata.Syntax import _F
from.. codeCommands import CREA_TABLE, IMPR_RESU, LIRE_MAILLAGE
from.. Messages import UTMESS


def conv_mail_med_ops (self, UNITE_IN,, FORMAT_IN,, UNITE_OUT, **args):
    "" "Convert a mesh from ASTER, GMSH, or GIBI format to MED format and
    Return some mesh properties.

    Arguments:
        **args (dict): User's keywords.

    Returns:
        Table: Object containing some characteristics of the mesh.
    """
    args = _F (args)
    info = args.get (" INFO ", 1)
    INFO is mandatory so:
    # info = args [" INFO "]
    # or adding INFO in arguments also works

    # print the information message
    UTMESS (...)

    # read the input mesh
    mesh =...
    # write the mesh into a MED file
    ...

    # extract some mesh characteristics
    nbnodes = mesh...
    nbcells = mesh...
    # fill the output table
    tab = CREA_TABLE (
        LISTE =(
            ...
        )
    )
    Return Tab

3.1. Detailed description#

See Programming rules.

We start by importing the functions used, in particular, the commands called. Note that we are importing the functions of the orders, not the catalog.

_F () ``is used in the command file. Keywords can be provided to commands as a Python dictionary. The ``_F object extends the dictionary to facilitate systematic processing.

Mandatory keywords should appear in function arguments since they must always be present (positional arguments). Optional keywords are passed into the keywords arguments. So we could have put INFO in the arguments of the function. As an example, we show that we could also have used args [" INFO "] ``. ``.get () ``is used for optional keywords, returns ``None (or the value provided) if the keyword is not present.

It is proposed to display a message to inform the user of the work in progress. The print () ``function is forbidden. You must use the UTMESS utility, see ``code_aster/messages/utmess.py.

The mesh is read by calling the LIRE_MAILLAGE command.

The conversion is done simply with IMPR_RESU in the right format.

To extract information from the mesh, the methods of the Mesh object are used. You can search for the available methods:

  • in examples/test cases (astest/ directory),

  • in the embedded documentation: ``xdg-open.. /install/mpi/share/doc/html/index.html  » (outside the container),

  • in the object*docstrings*:

    $.. /install/mpi/bin/run_aster
    > >> help (CA.mesh)
    

It is necessary to look for methods that make it possible to recover the number of knots and meshes.

We end the function by creating the table with CREA_TABLE. The test case provided shows the names of the parameters to be created in the table. Like a classic function, the table produced, returned to the user, is the value back from the function.

Complete the skeleton and test your code: Build and test.