Checking if (C#) ScriptComponent inport connected fails

rhino5
unhandled

#1

I’m obviously missing something, but I just noticed that my port-checking doesn’t detect whether an inport is connected or not. See this code:

var never_false = pt != null && pt != Point3d.Unset && pt.IsValid

How can I detect, in a convenient way, whether an inport is connected or not? (I do know that reflection way of checking it, but…)

// Rolf


(David Rutten) #2

A Point3d is a struct, so it cannot be null, furthermore IsValid tests for Unset, so you don’t need to do that twice either. Just pt.IsValid gives you the same result as you already have. However an uninitialised point will probably be (0,0,0) rather than Unset.

The Script component treats all inputs as optional so unfortunately it’s hard to detect whether or not you’ve actually got data or whether it was just the default(Point3d) value. You’ll have to disable the type hint (i.e. revert back to object) and then test for null. Afterwards you can test whether the data actually is a point:

  private void RunScript(object x, System.Object pt, ref object A)
  {
    if (pt == null)
    {
      A = "no data";
      return;
    }
    else if (!(pt is Point3d))
    {
      A = "pt is not of type Point3d";
      return;
    }

    A = (Point3d) pt;
  }

#3

Ah ok, that solves the proplem in an easy way. Thanks!

// Rolf