Skip to main content

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);
}

}
}

 

Hi Michael,

A few things:

  1. What is the errorMessage from BeginStandaloneApplication?
  2. When you are passing in TypePath=1, what is the GetZemaxDirectory() output when you give it an invalid filepath? 
  3. The ZOSAPI_NetHelper is designed to have a few fallbacks when searching for OpticStudio.  When running the Initialize() method, the NetHelper returns the first directory it finds which has the OpticStudio.exe, ZOSAPI.dll and ZOSAPI_Interfaces.dll installed.  So, if you pass in a "random” folder but it has these 3 files, then the Initialize() will return true even though OpticStudio will not properly start.  The following search order is used for the NetHelper:
    1. User defined install path (if passed in)
    2. A few hard-coded paths w.r.t. the current application
    3. A windows registry location written when OpticStudio is first installed 

So, I suspect that 3.3 is occurring since you’re probably not uninstalling OpticStudio when you’re testing and/or you’re not testing on a machine without OpticStudio. 

Also, since a string can be nullable, a cleaner code would be:

public static void StartZos(string FilePath = null)
{
bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(FilePath);
if (isInitialized)
{
BeginStandaloneApplication();
}
else
{
throw new Exception("could not initialize ZOS");
}
}

Hi Michael,

                   Thank you for the explanation and help.  Apologies for the late reply.

  1. For some reason this error is no longer coming up.  I am not sure what happened, but it seems to have fixed itself.  The script now finds OpticStudio regardless of what I type in and no error is generated.
  2. Same as 1.
  3. Thank you for letting me know about this!  I didn’t go back to the look at how the function worked, so I appreciate you pointing this out.

Thank you for the tip on nulling out the string.  Very much appreciated.


Reply