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 <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
    // 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
    Expected< Mesh > createSymbolsMesh(const SymbolMeshParams &params)
    Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    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 RuntimeError 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\n" );
    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( 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;
    }
    const std::shared_ptr< const Mesh > & mesh() const
  • C#
    public static class MeshFromTextExample
    {
    public static void Main(string[] args)
    {
    if (args.Length < 2)
    {
    Console.Error.WriteLine("Usage: MeshFromText fontpath text");
    Environment.Exit(1);
    }
    string fontPath = args[0];
    string text = args[1];
    // set text-to-mesh parameters
    MR.SymbolMeshParams parameters = new();
    // input text
    parameters.text.assign(text);
    // font file name
    parameters.pathToFontFile.assign(fontPath);
    var mesh = MR.createSymbolsMesh(parameters);
    MR.MeshSave.toAnySupportedFormat(mesh, "mesh.ply");
    }
    }
  • Rust
    use meshlib::SymbolMeshParams;
    fn main() -> Result<(), String>
    {
    let args: Vec<String> = std::env::args().collect();
    if args.len() < 3
    {
    return Err("Usage: MeshFromText fontpath text".to_string());
    }
    let font_path = &args[1];
    let text = &args[2];
    // set text-to-mesh parameters
    let mut params = SymbolMeshParams::new();
    // input text
    params.set_text(text);
    // font file name
    params.set_path_to_font_file(font_path);
    let mesh = meshlib::create_symbols_mesh(&params).into_result().map_err(|error| format!("Failed to convert text to mesh: {error}"))?;
    meshlib::mesh_save::to_any_supported_format(&mesh, "mesh.ply", None).into_result().map_err(|error| format!("Failed to save result: {error}"))?;
    Ok(())
    }
  • Java
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.SymbolMeshParams;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshFromText
    {
    public static void main(String[] args)
    {
    if (args.length < 2)
    {
    System.err.println("Usage: MeshFromText fontpath text");
    System.exit(1);
    }
    String fontPath = args[0];
    String text = args[1];
    // set text-to-mesh parameters
    SymbolMeshParams params = new SymbolMeshParams();
    // input text
    params.setText(text);
    // font file name
    params.setPathToFontFile(fontPath);
    // An error is thrown as `UnexpectedResultException`.
    Mesh mesh = Functions.createSymbolsMesh(params);
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "mesh.ply", null);
    }
    }