How to handle input/output definition changes when a component gets updated in a new version

Here’s the source for the Interpolate Curve upgrader I wrote in 2012.

The UpgradeFrom and UpgradeTo IDs tell Grasshopper what types of object this upgrader can work on. It has to be the ComponentGUIDs of the obsolete and new component. The Version I think is used to decide between upgraders if two happen to collide on their UpgradeFrom ID.

Ultimately the Upgrade() method is called and you can do whatever you like in there. Typically you’d instantiate a new object of the correct type and try to replace the old one. There are some useful functions in GH_UpgradeUtil to help out with this.

  public class Component_InterpCurveUpgrader : IGH_UpgradeObject
  {
    public DateTime Version
    {
      get { return new DateTime(2012, 4, 2, 16, 00, 00); }
    }
    public Guid UpgradeFrom
    {
      get { return new Guid("{F5EA9D41-F062-487e-8DBF-7666CA53FBCD}"); }
    }
    public Guid UpgradeTo
    {
      get { return new Guid("{2B2A4145-3DFF-41d4-A8DE-1EA9D29EEF33}"); }
    }

    public IGH_DocumentObject Upgrade(IGH_DocumentObject target, GH_Document document)
    {
      IGH_Component comp = GH_UpgradeUtil.SwapComponents((IGH_Component)target, UpgradeTo);
      if (comp == null) { return null; }

      Grasshopper.Kernel.Parameters.Param_Integer param = new Grasshopper.Kernel.Parameters.Param_Integer();
      param.NickName = "K";
      param.Name = "KnotStyle";
      param.Description = "Knot spacing (0=uniform, 1=chord, 2=sqrtchord)";
      param.SetPersistentData(0);

      comp.Params.RegisterInputParam(param);
      return comp;
    }
  }
6 Likes