MeshLib Documentation
Loading...
Searching...
No Matches
Convert text to mesh

Following code presents example of converting text to mesh

  • C++
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRMeshSave.h>
    #include <MRSymbolMesh/MRSymbolMesh.h>
    #include <iostream>
    int main( int argc, char* argv[] )
    {
    if ( argc < 3 )
    {
    std::cerr << "Usage: MeshFromText fontpath text" << std::endl;
    return EXIT_FAILURE;
    }
    const auto* fontPath = argv[1];
    const auto* text = argv[2];
    // set text-to-mesh parameters
    MR::SymbolMeshParams params {
    // input text
    .text = text,
    // font file name
    .pathToFontFile = fontPath,
    };
    auto convRes = MR::createSymbolsMesh( params );
    if ( !convRes )
    {
    std::cerr << "Failed to convert text to mesh: " << convRes.error() << std::endl;
    return EXIT_FAILURE;
    }
    auto saveRes = MR::MeshSave::toAnySupportedFormat( *convRes, "mesh.ply" );
    if ( !saveRes )
    {
    std::cerr << "Failed to save result: " << saveRes.error() << std::endl;
    return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    See also
    MR::createSymbolsMesh
  • Python
    import sys
    import meshlib.mrmeshpy as mrmeshpy
    if len(sys.argv) < 3:
    print("Usage: ./MeshFromText fontpath text", file=sys.stderr)
    sys.exit(1)
    font_path = sys.argv[1]
    text = sys.argv[2]
    params = mrmeshpy.SymbolMeshParams()
    params.text = text
    params.pathToFontFile = font_path
    try:
    conv_res = mrmeshpy.createSymbolsMesh(params)
    except ValueError as e:
    print(f"Failed to convert text to mesh: {e}", file=sys.stderr)
    sys.exit(1)
    mrmeshpy.saveMesh(conv_res, "mesh.ply")
  • C
    #include <MRCSymbolMesh/MRSymbolMesh.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( int argc, char** argv )
    {
    int rc = EXIT_FAILURE;
    if ( argc < 3 )
    {
    fprintf( stderr, "Usage: MeshFromText fontpath text" );
    goto bad_usage;
    }
    MR_SymbolMeshParams* params = MR_SymbolMeshParams_DefaultConstruct();
    MR_SymbolMeshParams_Set_text( params, argv[2], NULL );
    MR_SymbolMeshParams_Set_pathToFontFile( params, argv[1], NULL );
    MR_expected_MR_Mesh_std_string* convRes = MR_createSymbolsMesh( params );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( convRes );
    if ( !mesh )
    {
    fprintf( stderr, "Failed to convert text to mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( convRes ) ) );
    goto fail_conv;
    }
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat_3( mesh, "mesh.ply", NULL, NULL );
    if ( MR_expected_void_std_string_error( saveEx ) )
    {
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveEx ) ) );
    goto fail_save; // error while saving file
    }
    rc = EXIT_SUCCESS;
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_conv:
    MR_expected_MR_Mesh_std_string_Destroy( convRes );
    MR_SymbolMeshParams_Destroy( params );
    bad_usage:
    return rc;
    }