Error while accessing PlanSum

Jul 12, 2016 at 1:55 AM
Hi

I am novice with Eclipse scripting and also with C .
I have written a simple script to get DVH info such as Dose to structures and PTV (modified from sample scripts ) . It works fine with 3D CRT and IMRT plans but returns an error ("Object reference not set to an instance of an object") while a PlanSum, is selected. Could some one help me with this ?

regards
Paul
Coordinator
Jul 12, 2016 at 2:54 AM
Hi Paul,

Could you post a bit of the code that's causing the problem? The error "Object reference not set to an instance of an object" means that you're trying to access a property or method of a variable that is currently null. A variable being null has many causes, so it would help me to understand what you're doing.

Thanks.
Jul 12, 2016 at 5:24 PM
Hi

Thanks a lot. Here is the code I tried. It works for IMRT but not for PlanSum with the same patient and structure set.

Paul

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


namespace VMS.TPS
{
public class Script
{
public Script()
{
}
public ScriptContext newcontext;
public void Execute(ScriptContext context /*, System.Windows.Window window*/)
{
// Show the coordinates of isocenter of first beam
    var isoc = context.PlanSetup.Beams.First().IsocenterPosition;

    string msg = isoc.x.ToString() + ", " +
                 isoc.y.ToString() + ", " +
                 isoc.z.ToString();
   // MessageBox.Show("Isocenter (x,y,z in mm): " + msg);

  // TODO : Add here your code that is called when the script is launched from Eclipse
    string msg1 = string.Format(
            "Context:\n\tPatient=\t\t{0}\n\tImage=\t\t{1}\n\tCourse=\t\t{2}\n\tPlan =\t\t{3}\n\tStructure Set =\t{4}\n",
            context.Patient.Id,
    context.Image.Id,
            context.Course.Id,
            context.PlanSetup.Id,
          context.StructureSet.Id);




Structure COMB_LUNG_Minus_PTV = null;




    // declare local variables that reference the objects we need.
    PlanSetup plan = context.PlanSetup;
    StructureSet ss = context.StructureSet;
    var listStructures = context.StructureSet.Structures;

     // Get active structureset

    if (ss == null)
        throw new ApplicationException("Please load a structure set.");


foreach (Structure scan in listStructures)
    {




        if (scan.Id == "COMB_LUNG-PTV")
        {
            COMB_LUNG_Minus_PTV = scan;
        }


    }


DoseValue Voldose20 = new DoseValue(Convert.ToDouble(2000), DoseValue.DoseUnit.cGy);


//Get DVH statistics for COMBINED LUNG MINUS PTV

   double Volume20Lung = plan.GetVolumeAtDose(COMB_LUNG_Minus_PTV, Voldose20, VolumePresentation.Relative);
//DoseValue dose20Lung = context.PlanSetup.GetDoseAtVolume(COMB_LUNG_Minus_PTV, 20, VolumePresentation.Relative, DoseValuePresentation.Absolute);


MessageBox.Show( Volume20Lung.ToString("0.#") );


}
}
}
Coordinator
Jul 12, 2016 at 9:32 PM
Paul,

Whenever you say "context.PlanSetup" in your code it refers only to the active plan (not plan sum). If there is no active plan, then PlanSetup will be null, which is likely causing the error.

If you want to take care of plan sums, you need to look into "context.PlanSumsInScope" (which may be null if there are no open plan sums). Once you have the plan sum(s) you're interested in, you can access each plan it contains with the PlanSetups property of the PlanSum class.
Jul 13, 2016 at 2:07 AM
Hi

Thank you very much. Could you please let me know the changes required in the above script to work for plan sum?

Thanks

Paul
Jul 13, 2016 at 10:11 AM
Could you please give me a small sample script to access the PlanSum?
Paul
Coordinator
Jul 15, 2016 at 10:13 PM
Paul,

There may be multiple plan sums open, so you can go through each one as follows:
if (context.PlanSumsInScope != null)
{
    foreach (var planSum in context.PlanSumsInScope)
    {
        if (planSum.PlanSetups != null)
        {
            foreach (var plan in planSum.PlanSetups)
            {
                // Do something with the plan in the plan sum
            }
        }
    }
}
For each plan sum, the code goes through each plan it contains. You can do whatever you want with those plans, they are just PlanSetup objects.

Hope that helps!