ON_Light world_linear_light creation question

I created a linear light from the user interface, using this series of commands:
Command: LinearLight
Light origin: 1,2,3
Light length and direction: 4,7,9

This gave me a long cylindrical light in the first quadrant.

I tried to do the same thing using code, and got pretty close, except for the diameter of the light. What do I need to add or do differently?

void makeLinearLight(CRhinoDoc * rDoc)
{
  ON_Light* light = new ON_Light();
  light->SetStyle(ON::LightStyle(ON::world_linear_light));

  ON_3dPoint * lightPos = new ON_3dPoint(1, 2, 3);
  ON_3dPoint * endLoc = new ON_3dPoint(4, 7, 9);
  ON_3dVector dirVec(endLoc->x - lightPos->x, endLoc->y - lightPos->y, endLoc->z - lightPos->z);

  double length = lightPos->DistanceTo(*endLoc);

  light->SetLocation(*lightPos);
  light->SetDirection(dirVec);
  light->SetLength(dirVec);

  if (light->IsValid())
  {
    CRhinoLightTable& light_table = rDoc->m_light_table;
    light_table.AddLight(*light);
  }
  else
  {
    delete light;
  }
}

Hi @mgraham,

This should do it:

CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
  ON_3dPoint start(1.0, 2.0, 3.0);
  ON_3dPoint end(4.0, 7.0, 9.0);

  double width = 0.5; // 1 inch
  double scale = ON::UnitScale(ON::LengthUnitSystem::Inches, context.m_doc.ModelUnits());
  if (scale > 0.0 && ON_IsValid(scale))
    width *= scale;

  ON_Light light;
  light.SetStyle(ON::world_linear_light);
  light.SetLocation(start);

  ON_3dVector dir = (start - end);
  light.SetDirection(-dir);
  light.SetLength(light.Direction());

  ON_Plane plane(light.Location(), light.Direction());
  plane.xaxis.Unitize();
  light.SetWidth(plane.Xaxis() * min(width, dir.Length() / 20.0));

  if (light.IsValid())
  {
    context.m_doc.m_light_table.AddLight(light);
    context.m_doc.Redraw();
  }

  return CRhinoCommand::success;
}

– Dale