VarianAPIs / Varian-Code-Samples

Code samples for ESAPI and other Varian APIs and web services.
MIT License
117 stars 108 forks source link

Issue of running stand alone program #69

Open anthonyma24 opened 4 years ago

anthonyma24 commented 4 years ago

I was testing the example stand along project of loading patient on a tool box, and it's been working fine. However, when I tried to start building from scratch with a vs console template, I ran into an issue whenever I launched the program. I even copied the same cs file and references from the example directly into the project and I still got the same error. Has anyone experience this before? Thanks!

"An unhandled exception of type 'System.BadImageFormatException' occurred in mscorlib.dll

Additional information: Could not load file or assembly 'VMS.TPS.Common.Model.API, Version=1.0.200.43, Culture=neutral, PublicKeyToken=305b81e210ec4b89' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Here I attached the example code:

using System; using System.Linq; using System.Text; using System.Collections.Generic; using VMS.TPS.Common.Model.API; using VMS.TPS.Common.Model.Types;

namespace Example_Patients { class Program { [STAThread] // Do not remove this attribute, otherwise the script will not work static void Main(string[] args) { try { Console.WriteLine("Logging in..."); using (Application app = Application.CreateApplication()) { Console.WriteLine("Running script..."); Execute(app); } } catch (Exception exception) { Console.WriteLine("Exception was thrown:" + exception.Message); }

        Console.WriteLine("Execution finished. Press any key to exit.");
        Console.ReadKey();
    }

    static void Execute(Application app)
    {
        // Iterate through all patients

        int counter = 0;

        foreach (var patientSummary in app.PatientSummaries)
        {
            // Stop after when a few records have been found
            if (counter > 10)
                break;

            // For the test purpose we are interesting in patient records created during the last 6 months
            DateTime startDate = DateTime.Now - TimeSpan.FromDays(30 * 6);

            if (patientSummary.CreationDateTime > startDate)
            {
                // Retrieve patient information
                Patient patient = app.OpenPatient(patientSummary);
                if (patient == null)
                    throw new ApplicationException("Cannot open patient " + patientSummary.Id);

                // Iterate through all patient courses...
                foreach (var course in patient.Courses)
                {
                    // ... and plans
                    foreach (var planSetup in course.PlanSetups)
                    {
                        try
                        {
                            // For the test purpose we will look into approved plans with calculated 3D dose only...
                            PlanSetupApprovalStatus status = planSetup.ApprovalStatus;
                            if ((status == PlanSetupApprovalStatus.PlanningApproved || status == PlanSetupApprovalStatus.TreatmentApproved) && planSetup.Dose != null)
                            {
                                // ... select dose values to be in absolute unit
                                planSetup.DoseValuePresentation = DoseValuePresentation.Absolute;

                                // ... and display information about max dose
                                string message = string.Format("Patient: {0}, Course: {1}, Plan: {2}, Max dose: {3}", patient.Id, course.Id, planSetup.Id, planSetup.Dose.DoseMax3D.ToString());
                                Console.WriteLine(message);
                                counter = counter + 1;
                            }
                        }
                        catch (ApplicationException exception)
                        {
                            // In case of any error we will display some useful information...
                            string errorMsg = string.Format("Exception was thrown. Patient Id: {0}, Course: {1}, Exception: {2}", patient.Id, course.Id, exception.Message);
                            Console.WriteLine(errorMsg);
                            // ... and then move to the next patient
                            continue;
                        }
                    }
                }
                // Close the current patient, otherwise we will not be able to open another patient
                app.ClosePatient();
            }
        }
    }
}

}

Kiragroh commented 4 years ago

Did you start a new project with VisualStudio or with Eclipse Script Wizard. Please try the latter. The Wizard will connect the right standard dependencies. For additional dependencies (using ...) you have to reference them manually. screenshot_374

joecastelo commented 4 years ago

Hello @anthonyma24, are you trying to run the script from another computer that is not the eclipse installed computer? Citrix in example. If yes, than the executable must be launched from the local disk of the computer that has eclipse installed. if this is your case this piece of code can help you.

namespace VMS.TPS // Do not Change
{
    public class Script    // Do not Change
    {
        public void Execute(ScriptContext context) // Do not Change
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"Client\Shared$\folder";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var filePath = openFileDialog1;
                try
                {
                    var tmpPath = Path.GetTempPath();
                    var randomfolder = Path.GetRandomFileName().Split('.')[0];
                    var extension = Path.GetExtension(filePath.FileName);
                    var dest = Path.Combine(tmpPath,randomfolder);
                    //MessageBox.Show(dest);
                    var fileonTmp = Path.Combine(dest, filePath.SafeFileName, filePath.DefaultExt) ;
                    //MessageBox.Show(fileonTmp);

                    try
                    {
                        Copy(Path.GetDirectoryName(filePath.FileName), dest);
                        System.Diagnostics.Process.Start(fileonTmp);
                    }
                    catch (Exception) 
                    {

                        throw new Exception("Did not work") ;
                    }

                }

                catch (SecurityException ex)
                {
                    MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                    $"Details:\n\n{ex.StackTrace}");
                }
        }

        }

        public static void Copy(string sourceDirectory, string targetDirectory)
        {
            var diSource = new DirectoryInfo(sourceDirectory);
            var diTarget = new DirectoryInfo(targetDirectory);

            CopyAll(diSource, diTarget);
        }

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            Directory.CreateDirectory(target.FullName);

            // Copy each file into the new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
            }

            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

    }
}
anthonyma24 commented 4 years ago

Did you start a new project with VisualStudio or with Eclipse Script Wizard. Please try the latter. The Wizard will connect the right standard dependencies. For additional dependencies (using ...) you have to reference them manually. screenshot_374

Thanks Kiragroh, the example I ran was a stand along program from wizard which was working. The program I tried to create myself is based on a VS template. For learning purpose, I copied the cs code from the example, and added all the references. I was hoping it should work too if the two programs have same source code and references. I attached the screen shot of the solution panel for both programs. The first one is the example, and the second one is my test program. image

image

anthonyma24 commented 4 years ago

Hello @anthonyma24, are you trying to run the script from another computer that is not the eclipse installed computer? Citrix in example. If yes, than the executable must be launched from the local disk of the computer that has eclipse installed.

Thanks joecastelo. I was running on a tool box. Actually I have the example and my own programs saved under the same folder. However, the example is working without any issue, but the program I created kept getting the error I mentioned. I tested both programs in VS.

image

Kiragroh commented 4 years ago

Please try the following:

anthonyma24 commented 4 years ago

Please try the following:

  • use x64 for compiling
  • check your vms.tps-dependencies (should be on your C-drive)
  • try to use your test project without vms.tps-dependencies and a simple "hello world" code
  • google what die badimage-error could mean

Thanks Kiragroh! I think I have found the issue. I should have selected x64 for compiling instead of any cpu. It seems like the vms.tps-dependencies require this setting.