I have a custom FilePath that I pass into the above method and I am trying to check to see if it is valid so I can alert the user if OpticStudio was found on the local machine or not. However, when I pass anything to the method the bool is true and only the HandleError() method in BeginStandAloneApplication() catches the incorrect FilePath input. Can someone point out my error or offer any advise as to what is going on? Thanks in advance.
The method StartZos is the only method I have changed so far.
TypePath is hardcoded to always be as 0 or 1.
The solution compiles and works as designed expect for the bool issue.
There is an XAML GUI calling the Program class.
namespace CSharpStandaloneApplication
{
public class Program
{
public static void Main() { }
public static string StartZos(int TypePath, string FilePath)
{
bool isInitialized = false;
if (TypePath == 0)
{
//Find the installed version of opticstudio automatically
isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize();
if (isInitialized)
{
BeginStandaloneApplication();
return "Searching for OpticStudio automatically at path: " + ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory();
}
else
{
return "Something went wrong in finding OpticStudio. Try a custom file path.";
}
}
else if (TypePath == 1)
{
//Find the installed version of opticstudio using a custom initialization path
isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(FilePath);
if (isInitialized)
{
BeginStandaloneApplication();
return "Searching for OpticStudio at custom path: " + FilePath;
}
else
{
return "Check the custom file path. OpticStudio was not found.";
}
}
else
{
return "Something went wrong in finding the path to OpticStudio";
}
}
public static void BeginStandaloneApplication()
{
// Create the initial connection class
ZOSAPI_Connection TheConnection = new ZOSAPI_Connection();
// Attempt to create a Standalone connection
IZOSAPI_Application TheApplication = TheConnection.CreateNewApplication();
if (TheApplication == null)
{
HandleError("An unknown connection error occurred!");
return;
}
// Check the connection status
if (!TheApplication.IsValidLicenseForAPI)
{
HandleError("Failed to connect to OpticStudio: " + TheApplication.LicenseStatus);
return;
}
if (TheApplication.Mode != ZOSAPI_Mode.Server)
{
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
return;
}
IOpticalSystem TheSystem = TheApplication.PrimarySystem;
// Add your custom code here...
//
//
//
//
//
//
// Clean up
FinishStandaloneApplication(TheApplication);
}
public static void FinishStandaloneApplication(IZOSAPI_Application TheApplication)
{
// Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases
if (TheApplication != null)
{
TheApplication.CloseApplication();
}
}
public static void LogInfo(string message)
{
// TODO - add custom logging
Console.WriteLine(message);
}
public static void HandleError(string errorMessage)
{
// TODO - add custom error handling
throw new Exception(errorMessage);
}
}
}