# Error handling
Simplygon API reports errors through the error handling class ErrorHandler. A user can derive a class from ErrorHandler, which then will receive processing errors, and handle them appropriately. In C# and Python, if no error handler is set, the errors are reported through exception handling.
This example shows how to use the Simplygon error handler.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include <string>
#include <stdlib.h>
#include <filesystem>
#include <future>
#include "SimplygonLoader.h"
class CustomErrorHandler : public Simplygon::ErrorHandler
{
public:
void HandleError(Simplygon::spObject object, const char* interfaceName, const char* methodName, Simplygon::rid errorType , const char* errorText) override
{
printf("Error (%s:%s): %s\n", interfaceName, methodName, errorText);
}
} customErrorHandler;
void RunReduction(Simplygon::ISimplygon* sg)
{
// Set the custom error handler to the Simplygon interface.
sg->SetErrorHandler(&customErrorHandler);
// Create the reduction pipeline.
Simplygon::spReductionPipeline sgReductionPipeline = sg->CreateReductionPipeline();
// To be able to use the T-Junction remover, the welder has to be enabled so this will trigger an
// error.
Simplygon::spRepairSettings sgRepairSettings = sgReductionPipeline->GetRepairSettings();
sgRepairSettings->SetUseWelding( false );
sgRepairSettings->SetUseTJunctionRemover( true );
// Start the reduction pipeline and the faulty settings will cause an error.
sgReductionPipeline->RunSceneFromFile("../Assets/SimplygonMan/SimplygonMan.obj", "output.fbx", Simplygon::EPipelineRunMode::RunInNewProcess);
}
void main()
{
Simplygon::ISimplygon* sg = NULL;
Simplygon::EErrorCodes initval = Simplygon::Initialize( &sg );
if( initval != Simplygon::EErrorCodes::NoError )
{
return;
}
RunReduction(sg);
Simplygon::Deinitialize(sg);
}
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Threading.Tasks;
public class CustomErrorHandler : Simplygon.ErrorHandler
{
public override void HandleError(Simplygon.spObject obj, string interfaceName, string methodName, int errorType , string errorText)
{
Console.WriteLine($"Error ({interfaceName}:{methodName}): {errorText}");
}
}
public class Program
{
static private CustomErrorHandler customErrorHandler;
static void RunReduction(Simplygon.ISimplygon sg)
{
// Set the custom error handler to the Simplygon interface.
sg.SetErrorHandler(customErrorHandler);
// Create the reduction pipeline.
using (Simplygon.spReductionPipeline sgReductionPipeline = sg.CreateReductionPipeline())
{
// To be able to use the T-Junction remover, the welder has to be enabled so this will trigger an
// error.
using (Simplygon.spRepairSettings sgRepairSettings = sgReductionPipeline.GetRepairSettings())
{
sgRepairSettings.SetUseWelding( false );
sgRepairSettings.SetUseTJunctionRemover( true );
}
// Start the reduction pipeline and the faulty settings will cause an error.
sgReductionPipeline.RunSceneFromFile("../Assets/SimplygonMan/SimplygonMan.obj", "output.fbx", Simplygon.EPipelineRunMode.RunInNewProcess);
}
}
static void Main(string[] args)
{
using (var sg = Simplygon.Loader.InitSimplygon(out var errorCode, out var errorMessage))
{
if (errorCode != Simplygon.EErrorCodes.NoError)
return;
customErrorHandler = new CustomErrorHandler();
RunReduction(sg);
}
}
}
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import math
import os
import sys
import glob
import gc
import threading
from pathlib import Path
from simplygon import simplygon_loader
from simplygon import Simplygon
class CustomErrorHandler(Simplygon.ErrorHandler):
def HandleError(self, object: Simplygon.spObject, interfaceName: str, methodName: str, errorType: int, errorText: str):
print('Error (%s:%s): %s' %(interfaceName, methodName, errorText))
customErrorHandler = CustomErrorHandler()
def RunReduction(sg: Simplygon.ISimplygon):
# Set the custom error handler to the Simplygon interface.
sg.SetErrorHandler(customErrorHandler)
# Create the reduction pipeline.
sgReductionPipeline = sg.CreateReductionPipeline()
# To be able to use the T-Junction remover, the welder has to be enabled so this will trigger an
# error.
sgRepairSettings = sgReductionPipeline.GetRepairSettings()
sgRepairSettings.SetUseWelding( False )
sgRepairSettings.SetUseTJunctionRemover( True )
# Start the reduction pipeline and the faulty settings will cause an error.
sgReductionPipeline.RunSceneFromFile("../Assets/SimplygonMan/SimplygonMan.obj", "output.fbx", Simplygon.EPipelineRunMode_RunInNewProcess)
if __name__ == '__main__':
sg = simplygon_loader.init_simplygon()
if sg is not None:
RunReduction(sg)
sg = None
gc.collect()