public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
        {
            if (_drawing)
            {
                _drawing = false;
                sender.CanvasPostPaintObjects -= CanvasPostPaintObjects;

                GH_Document doc = sender.Document;
                if (doc != null)
                {
                    IGH_Attributes att = doc.FindAttribute(e.CanvasLocation, true);
                    if (att != null)
                    {
                        if (att.DocObject is IModifiable)
                        {
                            Owner.RecordUndoEvent("Add Modifier");
                            Owner.AddTarget(att.DocObject.InstanceGuid);
                            IGH_ActiveObject obj = att.DocObject as IGH_ActiveObject;
                            if (obj != null)
                            {
                                obj.ExpireSolution(true);
                            }
                        }
                    }
                }

                sender.Invalidate();
                return(GH_ObjectResponse.Release);
            }

            return(base.RespondToMouseUp(sender, e));
        }
        internal PythonInstantiatorProxy(IGH_DocumentObject obj, object pythonType, dynamic operations, string location)
        {
            _operations = operations;
            _pythonType = pythonType;

            _location    = string.Empty;
            _description = new GH_InstanceDescription(obj);
            Guid         = obj.ComponentGuid;
            _icon        = obj.Icon_24x24;
            _exposure    = obj.Exposure;
            _obsolete    = obj.Obsolete;
            _compliant   = true;
            if (obj is IGH_ActiveObject)
            {
                IGH_ActiveObject actobj = (IGH_ActiveObject)obj;
                if (!actobj.SDKCompliancy(RhinoApp.ExeVersion, RhinoApp.ExeServiceRelease))
                {
                    this._compliant = false;
                }
            }
            Type             = obj.GetType();
            this.LibraryGuid = GH_Convert.StringToGuid(location);
            this._location   = location;
            if (this._location.Length > 0)
            {
                this._location = this._location.Replace("file:///", string.Empty);
                this._location = this._location.Replace("/", Convert.ToString(Path.DirectorySeparatorChar));
            }
        }
        public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
        {
            if (_drawing)
            {
                _drawing = false;
                sender.CanvasPostPaintObjects -= CanvasPostPaintObjects;

                GH_Document doc = sender.Document;
                if (doc != null)
                {
                    IGH_Attributes att = doc.FindAttribute(e.CanvasLocation, true);
                    if (att != null)
                    {
                        if (att is IRoomStructure <IGH_DocumentObject> target)
                        {
                            Owner.RecordUndoEvent("Add Modifier");
                            if (att.DocObject is RoomInstance)
                            {
                                if ((att.DocObject as RoomInstance).RoomId != (DocObject as RoomInstance).RoomId)
                                {
                                    if (targetObjectList.Find(item => (item as RoomInstance).RoomId == (att.DocObject as RoomInstance).RoomId) == null)
                                    {
                                        AddAdjacence(att.DocObject);
                                        target.AddAdjacence(this.DocObject as IGH_DocumentObject);
                                    }
                                    else
                                    {
                                        RemoveAdjacence(att.DocObject);
                                        target.RemoveAdjacence(this.DocObject as IGH_DocumentObject);
                                    }
                                }
                            }
                            else if (att.DocObject is HouseInstance houseInstance)
                            {
                                if ((att as HouseInstanceAttributes).roomInstancesList.Find(item => item.RoomId == (this.DocObject as RoomInstance).RoomId) == null)
                                {
                                    target.AddAdjacence(this.DocObject as IGH_DocumentObject);
                                }
                                else
                                {
                                    target.RemoveAdjacence(this.DocObject as IGH_DocumentObject);
                                }
                            }


                            //   Owner.AddTarget(att.DocObject.InstanceGuid);
                            IGH_ActiveObject obj = att.DocObject as IGH_ActiveObject;
                            //  if (obj != null)
                            //     obj.ExpireSolution(true);
                            // this.DocObject.ExpireSolution(true);
                        }
                    }
                }

                sender.Invalidate();
                return(GH_ObjectResponse.Release);
            }

            return(base.RespondToMouseUp(sender, e));
        }
Example #4
0
        public IGH_ActiveObject GetCapsure(string Name, string NickName)
        {
            GH_Document             ghdoc = Grasshopper.Instances.ActiveCanvas.Document;
            List <IGH_ActiveObject> aos   = ghdoc.ActiveObjects();

            for (int i = 0; i < aos.Count; i++)
            {
                IGH_ActiveObject ao = aos[i];
                Print("Type==> " + ao.Name);
                if (ao.Name == Name)
                {
                    Print("Name==> " + ao.NickName);
                    if (ao.NickName == NickName)
                    {
                        return(ao);
                    }
                }
            }
            return(null);
        }
Example #5
0
        public static string GetNamedPath(IGH_ActiveObject obj, string name, string suffix = ".txt", bool create = false)
        {
            string result = GetNamedLocationWithObject(obj, name, suffix, create);

            if (result != null)
            {
                return(result);
            }
            var strs = FindObjectInAssemblyFolder(name);

            if (strs.Count() > 0)
            {
                return(strs.First());
            }
            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, LanguagableComponent.GetTransLation(new string[]
            {
                $"Finding {name} Failed!", $"{name} 查找失败!"
            }));
            return(null);
        }
Example #6
0
        public virtual void SetParamPointCapsure(string NickName, IEnumerable <Point3d> pts)
        {
            GH_Document             ghdoc = Grasshopper.Instances.ActiveCanvas.Document;
            List <IGH_ActiveObject> aos   = ghdoc.ActiveObjects();

            for (int i = 0; i < aos.Count; i++)
            {
                IGH_ActiveObject ao = aos[i];
                Print("Type==> " + ao.Name);
                if (ao.Name == "Point")
                {
                    Param_Point comp = (Param_Point)ao;
                    Print("Name==> " + comp.NickName);
                    if (ao.NickName == NickName)
                    {
                        GH_Path path = new GH_Path(1);
                        comp.AddVolatileDataList(path, pts);
                    }
                }
            }
        }
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        List <Point3d> P = null;

        if (inputs[0] != null)
        {
            P = GH_DirtyCaster.CastToList <Point3d>(inputs[0]);
        }
        Mesh M = default(Mesh);

        if (inputs[1] != null)
        {
            M = (Mesh)(inputs[1]);
        }

        double step = default(double);

        if (inputs[2] != null)
        {
            step = (double)(inputs[2]);
        }

        bool reset = default(bool);

        if (inputs[3] != null)
        {
            reset = (bool)(inputs[3]);
        }

        bool go = default(bool);

        if (inputs[4] != null)
        {
            go = (bool)(inputs[4]);
        }



        //3. Declare output parameters
        object Pos    = null;
        object status = null;
        object Trails = null;


        //4. Invoke RunScript
        RunScript(P, M, step, reset, go, ref Pos, ref status, ref Trails);

        try
        {
            //5. Assign output parameters to component...
            if (Pos != null)
            {
                if (GH_Format.TreatAsCollection(Pos))
                {
                    IEnumerable __enum_Pos = (IEnumerable)(Pos);
                    DA.SetDataList(1, __enum_Pos);
                }
                else
                {
                    if (Pos is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(Pos));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, Pos);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (status != null)
            {
                if (GH_Format.TreatAsCollection(status))
                {
                    IEnumerable __enum_status = (IEnumerable)(status);
                    DA.SetDataList(2, __enum_status);
                }
                else
                {
                    if (status is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(status));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, status);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
            if (Trails != null)
            {
                if (GH_Format.TreatAsCollection(Trails))
                {
                    IEnumerable __enum_Trails = (IEnumerable)(Trails);
                    DA.SetDataList(3, __enum_Trails);
                }
                else
                {
                    if (Trails is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(3, (Grasshopper.Kernel.Data.IGH_DataTree)(Trails));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(3, Trails);
                    }
                }
            }
            else
            {
                DA.SetData(3, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
Example #8
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        int nX = default(int);

        if (inputs[0] != null)
        {
            nX = (int)(inputs[0]);
        }

        double nY = default(double);

        if (inputs[1] != null)
        {
            nY = (double)(inputs[1]);
        }

        double step = default(double);

        if (inputs[2] != null)
        {
            step = (double)(inputs[2]);
        }

        Point3d att = default(Point3d);

        if (inputs[3] != null)
        {
            att = (Point3d)(inputs[3]);
        }

        double rA = default(double);

        if (inputs[4] != null)
        {
            rA = (double)(inputs[4]);
        }

        double rB = default(double);

        if (inputs[5] != null)
        {
            rB = (double)(inputs[5]);
        }

        double thres = default(double);

        if (inputs[6] != null)
        {
            thres = (double)(inputs[6]);
        }

        bool map = default(bool);

        if (inputs[7] != null)
        {
            map = (bool)(inputs[7]);
        }



        //3. Declare output parameters
        object C = null;


        //4. Invoke RunScript
        RunScript(nX, nY, step, att, rA, rB, thres, map, ref C);

        try
        {
            //5. Assign output parameters to component...
            if (C != null)
            {
                if (GH_Format.TreatAsCollection(C))
                {
                    IEnumerable __enum_C = (IEnumerable)(C);
                    DA.SetDataList(1, __enum_C);
                }
                else
                {
                    if (C is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(C));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, C);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
Example #9
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        int n = default(int);

        if (inputs[0] != null)
        {
            n = (int)(inputs[0]);
        }

        List <string> S = null;

        if (inputs[1] != null)
        {
            S = GH_DirtyCaster.CastToList <string>(inputs[1]);
        }


        //3. Declare output parameters
        object A = null;
        object B = null;
        object C = null;
        object D = null;
        object E = null;
        object F = null;


        //4. Invoke RunScript
        RunScript(n, S, ref A, ref B, ref C, ref D, ref E, ref F);

        try
        {
            //5. Assign output parameters to component...
            if (A != null)
            {
                if (GH_Format.TreatAsCollection(A))
                {
                    IEnumerable __enum_A = (IEnumerable)(A);
                    DA.SetDataList(1, __enum_A);
                }
                else
                {
                    if (A is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(A));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, A);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (B != null)
            {
                if (GH_Format.TreatAsCollection(B))
                {
                    IEnumerable __enum_B = (IEnumerable)(B);
                    DA.SetDataList(2, __enum_B);
                }
                else
                {
                    if (B is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(B));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, B);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
            if (C != null)
            {
                if (GH_Format.TreatAsCollection(C))
                {
                    IEnumerable __enum_C = (IEnumerable)(C);
                    DA.SetDataList(3, __enum_C);
                }
                else
                {
                    if (C is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(3, (Grasshopper.Kernel.Data.IGH_DataTree)(C));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(3, C);
                    }
                }
            }
            else
            {
                DA.SetData(3, null);
            }
            if (D != null)
            {
                if (GH_Format.TreatAsCollection(D))
                {
                    IEnumerable __enum_D = (IEnumerable)(D);
                    DA.SetDataList(4, __enum_D);
                }
                else
                {
                    if (D is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(4, (Grasshopper.Kernel.Data.IGH_DataTree)(D));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(4, D);
                    }
                }
            }
            else
            {
                DA.SetData(4, null);
            }
            if (E != null)
            {
                if (GH_Format.TreatAsCollection(E))
                {
                    IEnumerable __enum_E = (IEnumerable)(E);
                    DA.SetDataList(5, __enum_E);
                }
                else
                {
                    if (E is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(5, (Grasshopper.Kernel.Data.IGH_DataTree)(E));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(5, E);
                    }
                }
            }
            else
            {
                DA.SetData(5, null);
            }
            if (F != null)
            {
                if (GH_Format.TreatAsCollection(F))
                {
                    IEnumerable __enum_F = (IEnumerable)(F);
                    DA.SetDataList(6, __enum_F);
                }
                else
                {
                    if (F is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(6, (Grasshopper.Kernel.Data.IGH_DataTree)(F));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(6, F);
                    }
                }
            }
            else
            {
                DA.SetData(6, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
  private int runCount;                            //Legacy field.
  
  public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List<object> inputs, IGH_DataAccess DA)
  {
    //Prepare for a new run...
    //1. Reset lists
    this.__out.Clear();
    this.__err.Clear();

    this.Component = owner;
    this.Iteration = iteration;
    this.GrasshopperDocument = owner.OnPingDocument();
    this.RhinoDocument = rhinoDocument as Rhino.RhinoDoc;

    this.owner = this.Component;
    this.runCount = this.Iteration;
    this. doc = this.RhinoDocument;

    //2. Assign input parameters
        List<Point3d> P = null;
    if (inputs[0] != null)
    {
      P = GH_DirtyCaster.CastToList<Point3d>(inputs[0]);
    }
    int deg = default(int);
    if (inputs[1] != null)
    {
      deg = (int)(inputs[1]);
    }

    bool periodic = default(bool);
    if (inputs[2] != null)
    {
      periodic = (bool)(inputs[2]);
    }



    //3. Declare output parameters
      object Pl = null;
  object C = null;


    //4. Invoke RunScript
    RunScript(P, deg, periodic, ref Pl, ref C);
      
    try
    {
      //5. Assign output parameters to component...
            if (Pl != null)
      {
        if (GH_Format.TreatAsCollection(Pl))
        {
          IEnumerable __enum_Pl = (IEnumerable)(Pl);
          DA.SetDataList(1, __enum_Pl);
        }
        else
        {
          if (Pl is Grasshopper.Kernel.Data.IGH_DataTree)
          {
            //merge tree
            DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(Pl));
          }
          else
          {
            //assign direct
            DA.SetData(1, Pl);
          }
        }
      }
      else
      {
        DA.SetData(1, null);
      }
      if (C != null)
      {
        if (GH_Format.TreatAsCollection(C))
        {
          IEnumerable __enum_C = (IEnumerable)(C);
          DA.SetDataList(2, __enum_C);
        }
        else
        {
          if (C is Grasshopper.Kernel.Data.IGH_DataTree)
          {
            //merge tree
            DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(C));
          }
          else
          {
            //assign direct
            DA.SetData(2, C);
          }
        }
      }
      else
      {
        DA.SetData(2, null);
      }

    }
    catch (Exception ex)
    {
      this.__err.Add(string.Format("Script exception: {0}", ex.Message));
    }
    finally
    {
      //Add errors and messages... 
      if (owner.Params.Output.Count > 0)
      {
        if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
        {
          List<string> __errors_plus_messages = new List<string>();
          if (this.__err != null) { __errors_plus_messages.AddRange(this.__err); }
          if (this.__out != null) { __errors_plus_messages.AddRange(this.__out); }
          if (__errors_plus_messages.Count > 0) 
            DA.SetDataList(0, __errors_plus_messages);
        }
      }
    }
  }
Example #11
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        bool reset = default(bool);

        if (inputs[0] != null)
        {
            reset = (bool)(inputs[0]);
        }

        bool go = default(bool);

        if (inputs[1] != null)
        {
            go = (bool)(inputs[1]);
        }

        List <Point3d> P = null;

        if (inputs[2] != null)
        {
            P = GH_DirtyCaster.CastToList <Point3d>(inputs[2]);
        }
        List <Vector3d> V = null;

        if (inputs[3] != null)
        {
            V = GH_DirtyCaster.CastToList <Vector3d>(inputs[3]);
        }
        double nR = default(double);

        if (inputs[4] != null)
        {
            nR = (double)(inputs[4]);
        }

        double coS = default(double);

        if (inputs[5] != null)
        {
            coS = (double)(inputs[5]);
        }

        double alS = default(double);

        if (inputs[6] != null)
        {
            alS = (double)(inputs[6]);
        }

        double seS = default(double);

        if (inputs[7] != null)
        {
            seS = (double)(inputs[7]);
        }

        double seR = default(double);

        if (inputs[8] != null)
        {
            seR = (double)(inputs[8]);
        }



        //3. Declare output parameters
        object Ap = null;
        object Av = null;


        //4. Invoke RunScript
        RunScript(reset, go, P, V, nR, coS, alS, seS, seR, ref Ap, ref Av);

        try
        {
            //5. Assign output parameters to component...
            if (Ap != null)
            {
                if (GH_Format.TreatAsCollection(Ap))
                {
                    IEnumerable __enum_Ap = (IEnumerable)(Ap);
                    DA.SetDataList(1, __enum_Ap);
                }
                else
                {
                    if (Ap is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(Ap));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, Ap);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (Av != null)
            {
                if (GH_Format.TreatAsCollection(Av))
                {
                    IEnumerable __enum_Av = (IEnumerable)(Av);
                    DA.SetDataList(2, __enum_Av);
                }
                else
                {
                    if (Av is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(Av));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, Av);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
Example #12
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        List <Point3d> P = null;

        if (inputs[0] != null)
        {
            P = GH_DirtyCaster.CastToList <Point3d>(inputs[0]);
        }
        List <Vector3d> V = null;

        if (inputs[1] != null)
        {
            V = GH_DirtyCaster.CastToList <Vector3d>(inputs[1]);
        }
        List <Vector3d> vF = null;

        if (inputs[2] != null)
        {
            vF = GH_DirtyCaster.CastToList <Vector3d>(inputs[2]);
        }
        double pR = default(double);

        if (inputs[3] != null)
        {
            pR = (double)(inputs[3]);
        }

        double cI = default(double);

        if (inputs[4] != null)
        {
            cI = (double)(inputs[4]);
        }

        double aI = default(double);

        if (inputs[5] != null)
        {
            aI = (double)(inputs[5]);
        }

        double sI = default(double);

        if (inputs[6] != null)
        {
            sI = (double)(inputs[6]);
        }

        object fI = default(object);

        if (inputs[7] != null)
        {
            fI = (object)(inputs[7]);
        }

        double mF = default(double);

        if (inputs[8] != null)
        {
            mF = (double)(inputs[8]);
        }



        //3. Declare output parameters
        object Planes = null;


        //4. Invoke RunScript
        RunScript(P, V, vF, pR, cI, aI, sI, fI, mF, ref Planes);

        try
        {
            //5. Assign output parameters to component...
            if (Planes != null)
            {
                if (GH_Format.TreatAsCollection(Planes))
                {
                    IEnumerable __enum_Planes = (IEnumerable)(Planes);
                    DA.SetDataList(1, __enum_Planes);
                }
                else
                {
                    if (Planes is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(Planes));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, Planes);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            if (ghDocument == null)
            {
                ghDocument      = OnPingDocument();
                ghActiveObjects = ghDocument.ActiveObjects();
                originalPivots  = new List <PointF>();

                foreach (GH_ActiveObject ghActiveObject in ghActiveObjects)
                {
                    originalPivots.Add(ghActiveObject.Attributes.Pivot);
                }
            }

            if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.LeftAlt))
            {
                isRestarting = true;
            }

            if (isRestarting) // 元の位置に戻す
            {
                isRestarting = false;

                for (int i = 0; i < ghActiveObjects.Count; i++)
                {
                    IGH_ActiveObject ghActiveObject = ghActiveObjects[i];

                    if (ghActiveObject == null)
                    {
                        continue;
                    }

                    if (ghActiveObject == this)
                    {
                        continue;
                    }

                    PointF pivot = ghActiveObject.Attributes.Pivot;

                    // ピポットをもとの位置に戻す
                    PointF newPivot = new PointF(
                        (float)(1.0 - k) * pivot.X + (float)k * originalPivots[i].X,
                        (float)(1.0 - k) * pivot.Y + (float)k * originalPivots[i].Y);

                    // ピボットが元の位置に達したかどうかを確認
                    if (Math.Abs(newPivot.X - pivot.X) > 1.0 || Math.Abs(newPivot.Y - pivot.Y) > 1.0)
                    {
                        isRestarting = true;
                        ghActiveObject.Attributes.Pivot = new PointF((float)newPivot.X, (float)newPivot.Y);
                        ghActiveObject.Attributes.ExpireLayout();
                    }
                }
            }
            else if (flagNum == 0)// マウスカーソルによってコンポーネントを動かしていく
            {
                PointF  cursorF = Instances.ActiveCanvas.CursorCanvasPosition;
                Point3d cursor  = new Point3d(cursorF.X, cursorF.Y, 0.0);

                for (int i = 0; i < ghActiveObjects.Count; i++)
                {
                    IGH_ActiveObject ghActiveObject = ghActiveObjects[i];

                    if (ghActiveObject == null)
                    {
                        continue;
                    }

                    if (ghActiveObject == this) // ここの処理が重要。コンポーネント自体を移動させると削除できなくなる
                    {
                        continue;
                    }

                    RectangleF bounds  = ghActiveObject.Attributes.Bounds; // キャンバス上のコンポーネントの位置と寸法を記述する矩形
                    PointF     centerF = new PointF(
                        (bounds.Left + bounds.Right) * 0.5f,
                        (bounds.Top + bounds.Bottom) * 0.5f);
                    Point3d center = new Point3d(centerF.X, centerF.Y, 0.0);

                    double r = Math.Sqrt(bounds.Width * bounds.Width + bounds.Height * bounds.Height) * 0.5;

                    Vector3d v = center - cursor;

                    if (v.Length < minDistance + r) // マウスカーソルがコンポーネントの位置に十分に近い場合
                    {
                        // コンポーネントをマウスカーソルから離して、minDistanceを満たすまで移動します。
                        PointF  pivot     = ghActiveObject.Attributes.Pivot;
                        Point3d newCenter = center + v / v.Length * k * (minDistance + r - v.Length);

                        PointF newPivot = new PointF(
                            (float)(newCenter.X + pivot.X - center.X),
                            (float)(newCenter.Y + pivot.Y - center.Y));

                        ghActiveObject.Attributes.Pivot = newPivot;
                        ghActiveObject.Attributes.ExpireLayout();
                    }
                }
            }
            else if (flagNum == 1)
            {
                PointF  cursorF = Instances.ActiveCanvas.CursorCanvasPosition;
                Point3d cursor  = new Point3d(cursorF.X, cursorF.Y, 0.0);

                for (int i = 0; i < ghActiveObjects.Count; i++)
                {
                    IGH_ActiveObject ghActiveObject = ghActiveObjects[i];

                    if (ghActiveObject == null)
                    {
                        continue;
                    }

                    if (ghActiveObject == this)
                    {
                        continue;
                    }

                    RectangleF bounds  = ghActiveObject.Attributes.Bounds;
                    PointF     centerF = new PointF(
                        (bounds.Left + bounds.Right) * 0.5f,
                        (bounds.Top + bounds.Bottom) * 0.5f);
                    Point3d center = new Point3d(centerF.X, centerF.Y, 0.0);

                    double r = Math.Sqrt(bounds.Width * bounds.Width + bounds.Height * bounds.Height) * 0.5;

                    Vector3d v = center - cursor;

                    if (v.Length < minDistance + r)
                    {
                        PointF  pivot     = ghActiveObject.Attributes.Pivot;
                        Point3d newCenter = cursor;

                        PointF newPivot = new PointF(
                            (float)(newCenter.X),
                            (float)(newCenter.Y));

                        ghActiveObject.Attributes.Pivot = newPivot;
                        ghActiveObject.Attributes.ExpireLayout();
                    }
                }
            }

            ExpireSolution(true);
        }
Example #14
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        Point3d P0 = default(Point3d);

        if (inputs[0] != null)
        {
            P0 = (Point3d)(inputs[0]);
        }

        int n = default(int);

        if (inputs[1] != null)
        {
            n = (int)(inputs[1]);
        }

        double freq = default(double);

        if (inputs[2] != null)
        {
            freq = (double)(inputs[2]);
        }

        double amp = default(double);

        if (inputs[3] != null)
        {
            amp = (double)(inputs[3]);
        }

        double speed = default(double);

        if (inputs[4] != null)
        {
            speed = (double)(inputs[4]);
        }

        bool reset = default(bool);

        if (inputs[5] != null)
        {
            reset = (bool)(inputs[5]);
        }

        bool go = default(bool);

        if (inputs[6] != null)
        {
            go = (bool)(inputs[6]);
        }

        bool GHType = default(bool);

        if (inputs[7] != null)
        {
            GHType = (bool)(inputs[7]);
        }



        //3. Declare output parameters
        object P = null;


        //4. Invoke RunScript
        RunScript(P0, n, freq, amp, speed, reset, go, GHType, ref P);

        try
        {
            //5. Assign output parameters to component...
            if (P != null)
            {
                if (GH_Format.TreatAsCollection(P))
                {
                    IEnumerable __enum_P = (IEnumerable)(P);
                    DA.SetDataList(1, __enum_P);
                }
                else
                {
                    if (P is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(P));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, P);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        Mesh mesh = default(Mesh);

        if (inputs[0] != null)
        {
            mesh = (Mesh)(inputs[0]);
        }

        Vector3d direction = default(Vector3d);

        if (inputs[1] != null)
        {
            direction = (Vector3d)(inputs[1]);
        }

        Color color = default(Color);

        if (inputs[2] != null)
        {
            color = (Color)(inputs[2]);
        }

        double intensity = default(double);

        if (inputs[3] != null)
        {
            intensity = (double)(inputs[3]);
        }



        //3. Declare output parameters
        object result = null;


        //4. Invoke RunScript
        RunScript(mesh, direction, color, intensity, ref result);

        try
        {
            //5. Assign output parameters to component...
            if (result != null)
            {
                if (GH_Format.TreatAsCollection(result))
                {
                    IEnumerable __enum_result = (IEnumerable)(result);
                    DA.SetDataList(1, __enum_result);
                }
                else
                {
                    if (result is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(result));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, result);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        Vector3d V = default(Vector3d);

        if (inputs[0] != null)
        {
            V = (Vector3d)(inputs[0]);
        }



        //3. Declare output parameters
        object VSort = null;
        object vStar = null;


        //4. Invoke RunScript
        RunScript(V, ref VSort, ref vStar);

        try
        {
            //5. Assign output parameters to component...
            if (VSort != null)
            {
                if (GH_Format.TreatAsCollection(VSort))
                {
                    IEnumerable __enum_VSort = (IEnumerable)(VSort);
                    DA.SetDataList(1, __enum_VSort);
                }
                else
                {
                    if (VSort is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(VSort));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, VSort);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (vStar != null)
            {
                if (GH_Format.TreatAsCollection(vStar))
                {
                    IEnumerable __enum_vStar = (IEnumerable)(vStar);
                    DA.SetDataList(2, __enum_vStar);
                }
                else
                {
                    if (vStar is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(vStar));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, vStar);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
Example #17
0
        public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
        {
            if (_drawing)
            {
                _drawing = false;
                sender.CanvasPostPaintObjects -= CanvasPostPaintObjects;

                GH_Document doc = sender.Document;
                if (doc != null)
                {
                    IGH_Attributes att = doc.FindAttribute(e.CanvasLocation, true);
                    if (att != null)
                    {
                        if (att is IRoomStructure <IGH_DocumentObject> target)
                        {
                            Owner.RecordUndoEvent("Add Modifier");
                            if (att.DocObject is RoomInstance)
                            {
                                if ((att.DocObject as RoomInstance).InstanceGuid != (DocObject as RoomInstance).InstanceGuid)
                                {
                                    if (RoomInstance.allAdjacencesList.Find(i => i.a == att.DocObject.InstanceGuid.ToString() && i.b == DocObject.InstanceGuid.ToString()) == null &&
                                        RoomInstance.allAdjacencesList.Find(i => i.b == att.DocObject.InstanceGuid.ToString() && i.a == DocObject.InstanceGuid.ToString()) == null)
                                    {
                                        AddAdjacence(att.DocObject);
                                        target.AddAdjacence(this.DocObject as IGH_DocumentObject);
                                    }

                                    /*
                                     * if (targetObjectList.Find(item => (item as RoomInstance).RoomId == (att.DocObject as RoomInstance).RoomId) == null)
                                     * {
                                     *  AddAdjacence(att.DocObject);
                                     *  target.AddAdjacence(this.DocObject as IGH_DocumentObject);
                                     * }*/
                                    else
                                    {
                                        RemoveAdjacence(att.DocObject);
                                        target.RemoveAdjacence(this.DocObject as IGH_DocumentObject);
                                    }

                                    RoomInstance.allAdjacencesList = RoomInstance.allAdjacencesList.Distinct().ToList();
                                }
                            }
                            else if (att.DocObject is HouseInstance houseInstance)
                            {
                                if ((att as HouseInstanceAttributes).roomInstancesGuidList.Find(item => item == (this.DocObject as RoomInstance).InstanceGuid.ToString()) == null)
                                {
                                    target.AddAdjacence(this.DocObject as IGH_DocumentObject);
                                }
                                else
                                {
                                    target.RemoveAdjacence(this.DocObject as IGH_DocumentObject);
                                }
                            }

                            IGH_ActiveObject obj = att.DocObject as IGH_ActiveObject;
                        }
                    }
                }

                sender.Invalidate();
                return(GH_ObjectResponse.Release);
            }

            return(base.RespondToMouseUp(sender, e));
        }
Example #18
0
    public static void UpStreamObjects(List <IGH_DocumentObject> upComponents, IGH_DocumentObject obj, GH_Document docu)
    {
        //Find all the upstream components from a given starting node.


        IGH_Param param = obj as IGH_Param;

        if (param != null)
        {
            if (!upComponents.Contains(obj))
            {
                upComponents.Add(obj);
            }

            IList <IGH_Param> params2 = param.Sources;
            foreach (IGH_Param p in params2)
            {
                Guid id = p.Attributes.GetTopLevel.InstanceGuid;
                IGH_DocumentObject comp2 = docu.FindObject(id, true);
                IGH_ActiveObject   PA    = comp2 as IGH_ActiveObject;
                UpStreamObjects(upComponents, PA, docu);
            }
        }

        IGH_Component comp = obj as IGH_Component;

        if (comp != null)
        {
            List <IGH_DocumentObject> PreviousComponents = new List <IGH_DocumentObject>();


            List <IGH_Param> paramtrs = comp.Params.Input;

            foreach (IGH_Param pm in paramtrs)
            {
                IList <IGH_Param> sources = pm.Sources;
                foreach (IGH_Param pm2 in sources)
                {
                    Guid id = pm2.Attributes.GetTopLevel.InstanceGuid;
                    IGH_DocumentObject comp2 = docu.FindObject(id, true);
                    if (comp2 != null)
                    {
                        PreviousComponents.Add(comp2);
                    }
                }
            }

            if (PreviousComponents.Count == 0)
            {
                if (!upComponents.Contains(obj))
                {
                    upComponents.Add(obj);
                }
            }

            else
            {
                if (!upComponents.Contains(obj))
                {
                    upComponents.Add(obj);
                }


                foreach (IGH_DocumentObject obj3 in PreviousComponents)
                {
                    UpStreamObjects(upComponents, obj3, docu);
                }
            }
        }
    }
    private int runCount;                                   //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        Brep Bnd = default(Brep);

        if (inputs[0] != null)
        {
            Bnd = (Brep)(inputs[0]);
        }

        List <Curve> RdA = null;

        if (inputs[1] != null)
        {
            RdA = GH_DirtyCaster.CastToList <Curve>(inputs[1]);
        }
        int Crt1 = default(int);

        if (inputs[2] != null)
        {
            Crt1 = (int)(inputs[2]);
        }

        double AmpC1 = default(double);

        if (inputs[3] != null)
        {
            AmpC1 = (double)(inputs[3]);
        }

        int Dist1 = default(int);

        if (inputs[4] != null)
        {
            Dist1 = (int)(inputs[4]);
        }

        int Crt2 = default(int);

        if (inputs[5] != null)
        {
            Crt2 = (int)(inputs[5]);
        }

        double AmpC2 = default(double);

        if (inputs[6] != null)
        {
            AmpC2 = (double)(inputs[6]);
        }

        int Dist2 = default(int);

        if (inputs[7] != null)
        {
            Dist2 = (int)(inputs[7]);
        }

        double RndPara = default(double);

        if (inputs[8] != null)
        {
            RndPara = (double)(inputs[8]);
        }

        int CtnRng1 = default(int);

        if (inputs[9] != null)
        {
            CtnRng1 = (int)(inputs[9]);
        }

        int CtnRng2 = default(int);

        if (inputs[10] != null)
        {
            CtnRng2 = (int)(inputs[10]);
        }

        int LenRat = default(int);

        if (inputs[11] != null)
        {
            LenRat = (int)(inputs[11]);
        }

        int Sd = default(int);

        if (inputs[12] != null)
        {
            Sd = (int)(inputs[12]);
        }



        //3. Declare output parameters
        object BLOCKS = null;
        object RDA    = null;
        object RDB    = null;
        object RDC    = null;


        //4. Invoke RunScript
        RunScript(Bnd, RdA, Crt1, AmpC1, Dist1, Crt2, AmpC2, Dist2, RndPara, CtnRng1, CtnRng2, LenRat, Sd, ref BLOCKS, ref RDA, ref RDB, ref RDC);

        try
        {
            //5. Assign output parameters to component...
            if (BLOCKS != null)
            {
                if (GH_Format.TreatAsCollection(BLOCKS))
                {
                    IEnumerable __enum_BLOCKS = (IEnumerable)(BLOCKS);
                    DA.SetDataList(1, __enum_BLOCKS);
                }
                else
                {
                    if (BLOCKS is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(BLOCKS));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, BLOCKS);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (RDA != null)
            {
                if (GH_Format.TreatAsCollection(RDA))
                {
                    IEnumerable __enum_RDA = (IEnumerable)(RDA);
                    DA.SetDataList(2, __enum_RDA);
                }
                else
                {
                    if (RDA is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(RDA));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, RDA);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
            if (RDB != null)
            {
                if (GH_Format.TreatAsCollection(RDB))
                {
                    IEnumerable __enum_RDB = (IEnumerable)(RDB);
                    DA.SetDataList(3, __enum_RDB);
                }
                else
                {
                    if (RDB is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(3, (Grasshopper.Kernel.Data.IGH_DataTree)(RDB));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(3, RDB);
                    }
                }
            }
            else
            {
                DA.SetData(3, null);
            }
            if (RDC != null)
            {
                if (GH_Format.TreatAsCollection(RDC))
                {
                    IEnumerable __enum_RDC = (IEnumerable)(RDC);
                    DA.SetDataList(4, __enum_RDC);
                }
                else
                {
                    if (RDC is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(4, (Grasshopper.Kernel.Data.IGH_DataTree)(RDC));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(4, RDC);
                    }
                }
            }
            else
            {
                DA.SetData(4, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
Example #20
0
    private List<string> __out = new List<string>(); //Do not modify this list directly.

    #endregion Fields

    #region Methods

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List<object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component = owner;
        this.Iteration = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument = rhinoDocument as Rhino.RhinoDoc;

        this.owner = this.Component;
        this.runCount = this.Iteration;
        this. doc = this.RhinoDocument;

        //2. Assign input parameters
        Mesh Profile = default(Mesh);
        if (inputs[0] != null)
        {
          Profile = (Mesh)(inputs[0]);
        }

        List<Mesh> x = null;
        if (inputs[1] != null)
        {
          x = GH_DirtyCaster.CastToList<Mesh>(inputs[1]);
        }
        bool y = default(bool);
        if (inputs[2] != null)
        {
          y = (bool)(inputs[2]);
        }

        //3. Declare output parameters
          object A = null;

        //4. Invoke RunScript
        RunScript(Profile, x, y, ref A);

        try
        {
          //5. Assign output parameters to component...
            if (A != null)
          {
        if (GH_Format.TreatAsCollection(A))
        {
          IEnumerable __enum_A = (IEnumerable)(A);
          DA.SetDataList(1, __enum_A);
        }
        else
        {
          if (A is Grasshopper.Kernel.Data.IGH_DataTree)
          {
            //merge tree
            DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(A));
          }
          else
          {
            //assign direct
            DA.SetData(1, A);
          }
        }
          }
          else
          {
        DA.SetData(1, null);
          }

        }
        catch (Exception ex)
        {
          this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
          //Add errors and messages...
          if (owner.Params.Output.Count > 0)
          {
        if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
        {
          List<string> __errors_plus_messages = new List<string>();
          if (this.__err != null) { __errors_plus_messages.AddRange(this.__err); }
          if (this.__out != null) { __errors_plus_messages.AddRange(this.__out); }
          if (__errors_plus_messages.Count > 0)
            DA.SetDataList(0, __errors_plus_messages);
        }
          }
        }
    }
Example #21
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        ///


        protected override void SolveInstance(IGH_DataAccess DA)
        {   ///The logic for finding the different chains is far from perfect
            docu = this.OnPingDocument();

            //Get al components in the document
            List <IGH_DocumentObject> objects = new List <IGH_DocumentObject>();

            try
            {
                objects = docu.Objects.ToList <IGH_DocumentObject>();
            }
            catch { }

            //Filter those components to only get active components (remove groups, scribbles etc)
            List <IGH_ActiveObject> activeObjects = new List <IGH_ActiveObject>();

            foreach (IGH_DocumentObject obji in objects)
            {
                IGH_ActiveObject actiObj = obji as IGH_ActiveObject;
                if (actiObj != null & obji != this)
                {
                    activeObjects.Add(actiObj);
                }
            }


            //Sort element in chains
            List <List <IGH_ActiveObject> > chains = new List <List <IGH_ActiveObject> >();

            while (activeObjects.Count > 0)
            {
                //Start with the first document of the list

                IGH_ActiveObject        obj   = activeObjects[0];
                List <IGH_ActiveObject> chain = new List <IGH_ActiveObject>();
                //List<IGH_DocumentObject> longest_set = new List<IGH_DocumentObject>();

                //Retrieve all the components downstream and get last elements, once the last elements is found
                //all the upstream elements will be colected from here
                List <IGH_ActiveObject> downObjects = docu.FindAllDownstreamObjects(obj);
                downObjects.Insert(0, obj);



                //Retrieve all the component upstream from the last element, this process is repeated until
                // all the downstream elements have been added to the chain.
                while (downObjects.Count != 0)
                {
                    IGH_ActiveObject lastElement = downObjects[downObjects.Count - 1];

                    List <IGH_DocumentObject> upstream = new List <IGH_DocumentObject>();
                    Helpers.UpStreamObjects(upstream, lastElement, docu);
                    foreach (GH_DocumentObject ob in upstream)
                    {
                        IGH_ActiveObject ob_Active = ob as IGH_ActiveObject;
                        downObjects.Remove(ob_Active);
                        activeObjects.Remove(ob_Active);
                        if (!chain.Contains(ob_Active))
                        {
                            chain.Add(ob_Active);
                        }
                    }
                }

                //Add the chain to the collection of chains
                chains.Add(chain);
            }

            //Previous logic is quite shitty and you may still end up with two chains which are not totally independant,
            //To fix this chains are compared to see if any component if common to both, if that happend both chains are merged.
            //List<List<IGH_ActiveObject>> cleanChains = new List<List<IGH_ActiveObject>>();

            List <List <IGH_ActiveObject> > cleanChains = new List <List <IGH_ActiveObject> >();

            while (chains.Count > 0)
            {
                bool flag = true;
                List <IGH_ActiveObject> checkingChain = chains[0];
                for (int i = 1; i < chains.Count; i++)
                {
                    List <IGH_ActiveObject> otherChain = chains[i];
                    if (checkingChain.Intersect(otherChain).Any())
                    {
                        flag = false;
                        foreach (IGH_ActiveObject chainObj in otherChain)
                        {
                            if (!checkingChain.Contains(chainObj))
                            {
                                checkingChain.Add(chainObj);
                            }
                            chains.Remove(otherChain);
                        }
                        break;
                    }
                }

                if (flag)
                {
                    cleanChains.Add(checkingChain);
                    chains.Remove(checkingChain);
                }
            }

            chains = cleanChains;
            //Group each of the chains
            int    counter = 1;
            Random random  = new Random();

            foreach (List <IGH_ActiveObject> chain in chains)
            {
                Grasshopper.Kernel.Special.GH_Group group = new Grasshopper.Kernel.Special.GH_Group();

                group.Colour   = System.Drawing.Color.FromArgb(200, (int)(random.NextDouble() * 255), (int)(random.NextDouble() * 255), (int)(random.NextDouble() * 255));
                group.Name     = string.Format(("Chain {0}"), counter.ToString());
                group.NickName = string.Format(("Chain {0}"), counter.ToString());
                docu.AddObject(group, false, docu.ObjectCount);

                foreach (IGH_ActiveObject obj in chain)
                {
                    group.AddObject(obj.Attributes.InstanceGuid);
                }

                group.ExpireCaches();
                counter += 1;
            }
        }
Example #22
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        bool reset = default(bool);

        if (inputs[0] != null)
        {
            reset = (bool)(inputs[0]);
        }

        string path = default(string);

        if (inputs[1] != null)
        {
            path = (string)(inputs[1]);
        }

        string controlComponentName = default(string);

        if (inputs[2] != null)
        {
            controlComponentName = (string)(inputs[2]);
        }



        //3. Declare output parameters
        object A = null;


        //4. Invoke RunScript
        RunScript(reset, path, controlComponentName, ref A);

        try
        {
            //5. Assign output parameters to component...
            if (A != null)
            {
                if (GH_Format.TreatAsCollection(A))
                {
                    IEnumerable __enum_A = (IEnumerable)(A);
                    DA.SetDataList(1, __enum_A);
                }
                else
                {
                    if (A is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(A));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, A);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
Example #23
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        bool store = default(bool);

        if (inputs[0] != null)
        {
            store = (bool)(inputs[0]);
        }

        string path = default(string);

        if (inputs[1] != null)
        {
            path = (string)(inputs[1]);
        }

        List <int> NumSliders = null;

        if (inputs[2] != null)
        {
            NumSliders = GH_DirtyCaster.CastToList <int>(inputs[2]);
        }
        List <int> SliderVals = null;

        if (inputs[3] != null)
        {
            SliderVals = GH_DirtyCaster.CastToList <int>(inputs[3]);
        }
        int NumPoints = default(int);

        if (inputs[4] != null)
        {
            NumPoints = (int)(inputs[4]);
        }

        List <Point3d> Points = null;

        if (inputs[5] != null)
        {
            Points = GH_DirtyCaster.CastToList <Point3d>(inputs[5]);
        }


        //3. Declare output parameters


        //4. Invoke RunScript
        RunScript(store, path, NumSliders, SliderVals, NumPoints, Points);

        try
        {
            //5. Assign output parameters to component...
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
        internal static bool SetParameter(IGH_ActiveObject obj, DB.Parameter parameter, IGH_Goo goo)
        {
            if (goo is null)
            {
                return(false);
            }

            try
            {
                var element  = parameter.Element;
                var value    = goo.ScriptVariable();
                var document = default(DB.Document);
                if (value is DB.Parameter paramValue)
                {
                    switch (paramValue.StorageType)
                    {
                    case DB.StorageType.Integer: value = paramValue.AsInteger(); break;

                    case DB.StorageType.Double: value = paramValue.AsDoubleInRhinoUnits(); break;

                    case DB.StorageType.String: value = paramValue.AsString(); break;

                    case DB.StorageType.ElementId: value = paramValue.AsElementId(); document = paramValue.Element.Document; break;
                    }
                }

                switch (parameter.StorageType)
                {
                case DB.StorageType.Integer:
                {
                    switch (value)
                    {
                    case bool boolean: parameter.Set(boolean ? 1 : 0); break;

                    case int integer: parameter.Set(integer); break;

                    case double real: parameter.SetDoubleInRhinoUnits((int)Clamp(Round(real), int.MinValue, int.MaxValue)); break;

                    case System.Drawing.Color color: parameter.Set(((int)color.R) | ((int)color.G << 8) | ((int)color.B << 16)); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.Double:
                {
                    switch (value)
                    {
                    case int integer: parameter.Set((double)integer); break;

                    case double real: parameter.SetDoubleInRhinoUnits(real); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.String:
                {
                    switch (value)
                    {
                    case string str: parameter.Set(str); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.ElementId:
                {
                    switch (value)
                    {
                    case DB.Element ele:
                        if (element.Document.Equals(ele.Document))
                        {
                            parameter.Set(ele.Id);
                        }
                        else
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Referencing elements from other documents is not valid");
                        }
                        break;

                    case DB.Category cat:
                        if (element.Document.Equals(cat.Document()))
                        {
                            parameter.Set(cat.Id);
                        }
                        else
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Referencing categories from other documents is not valid");
                        }
                        break;

                    case DB.ElementId id:
                        if (document is object)
                        {
                            if (element.Document.Equals(document))
                            {
                                parameter.Set(id);
                            }
                            else
                            {
                                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Referencing elements from other documents is not valid");
                            }
                        }
                        else
                        {
                            element = null;
                        }
                        break;

                    default: element = null; break;
                    }
                    break;
                }

                default:
                {
                    element = null;
                    break;
                }
                }

                if (element is null && parameter is object)
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Failed to cast from {value.GetType().Name} to {parameter.StorageType.ToString()}.");
                    return(false);
                }
            }
            catch (Autodesk.Revit.Exceptions.InvalidOperationException e)
            {
                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Failed to set '{parameter.Definition.Name}' value : {e.Message}");
                return(false);
            }

            return(true);
        }
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        double x = default(double);

        if (inputs[0] != null)
        {
            x = (double)(inputs[0]);
        }

        double y = default(double);

        if (inputs[1] != null)
        {
            y = (double)(inputs[1]);
        }

        double x_origin = default(double);

        if (inputs[2] != null)
        {
            x_origin = (double)(inputs[2]);
        }

        double y_origin = default(double);

        if (inputs[3] != null)
        {
            y_origin = (double)(inputs[3]);
        }



        //3. Declare output parameters
        object aa = null;
        object bb = null;


        //4. Invoke RunScript
        RunScript(x, y, x_origin, y_origin, ref aa, ref bb);

        try
        {
            //5. Assign output parameters to component...
            if (aa != null)
            {
                if (GH_Format.TreatAsCollection(aa))
                {
                    IEnumerable __enum_aa = (IEnumerable)(aa);
                    DA.SetDataList(1, __enum_aa);
                }
                else
                {
                    if (aa is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(aa));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, aa);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (bb != null)
            {
                if (GH_Format.TreatAsCollection(bb))
                {
                    IEnumerable __enum_bb = (IEnumerable)(bb);
                    DA.SetDataList(2, __enum_bb);
                }
                else
                {
                    if (bb is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(bb));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, bb);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
 public override bool DependsOn(IGH_ActiveObject potentialSource)
 {
     return(false);
 }
        internal static DB.Parameter GetParameter(IGH_ActiveObject obj, DB.Element element, IGH_Goo key)
        {
            DB.Parameter parameter = null;
            switch (key as Types.ParameterKey ?? key.ScriptVariable())
            {
            case Types.ParameterKey parameterKey:
                if (parameterKey.Document.Equals(element.Document))
                {
                    if (Enum.IsDefined(typeof(DB.BuiltInParameter), parameterKey.Id.IntegerValue))
                    {
                        parameter = element.get_Parameter((DB.BuiltInParameter)parameterKey.Id.IntegerValue);
                        if (parameter is null)
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{DB.LabelUtils.GetLabelFor((DB.BuiltInParameter) parameterKey.Id.IntegerValue)}' not defined in 'Element'");
                        }
                    }
                    else if (element.Document.GetElement(parameterKey.Id) is DB.ParameterElement parameterElement)
                    {
                        parameter = element.get_Parameter(parameterElement.GetDefinition());
                        if (parameter is null)
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterElement.Name}' not defined in 'Element'");
                        }
                    }
                    else
                    {
                        obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element");
                    }
                }
                else
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"'ParameterKey' doesn't belong same document as 'Element'");
                }

                break;

            case DB.Parameter param:
                if (param.Element.Document.Equals(element.Document) && param.Element.Id == element.Id)
                {
                    parameter = param;
                }
                else
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Parameter '{param.Definition.Name}' doesn't belong to 'Element'");
                }

                break;

            case string parameterName:
                parameter = element.GetParameter(parameterName, DBX.ParameterClass.Any);
                if (parameter is null)
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterName}' not defined in 'Element'");
                }
                break;

            case int parameterId:
                if (Enum.IsDefined(typeof(DB.BuiltInParameter), parameterId))
                {
                    parameter = element.get_Parameter((DB.BuiltInParameter)parameterId);
                    if (parameter is null)
                    {
                        obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{DB.LabelUtils.GetLabelFor((DB.BuiltInParameter) parameterId)}' not defined in 'Element'");
                    }
                }
                else if (element.Document.GetElement(new DB.ElementId(parameterId)) is DB.ParameterElement parameterElement)
                {
                    parameter = element.get_Parameter(parameterElement.GetDefinition());
                    if (parameter is null)
                    {
                        obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterElement.Name}' not defined in 'Element'");
                    }
                }
                else
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element");
                }
                break;

            case DB.ElementId parameterElementId:
                if (Enum.IsDefined(typeof(DB.BuiltInParameter), parameterElementId.IntegerValue))
                {
                    parameter = element.get_Parameter((DB.BuiltInParameter)parameterElementId.IntegerValue);
                    if (parameter is null)
                    {
                        obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{DB.LabelUtils.GetLabelFor((DB.BuiltInParameter) parameterElementId.IntegerValue)}' not defined in 'Element'");
                    }
                }
                else if (element.Document.GetElement(parameterElementId) is DB.ParameterElement parameterElement)
                {
                    parameter = element.get_Parameter(parameterElement.GetDefinition());
                    if (parameter is null)
                    {
                        obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterElement.Name}' not defined in 'Element'");
                    }
                }
                else
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element");
                }
                break;

            case Guid guid:
                parameter = element.get_Parameter(guid);
                if (parameter is null)
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{guid}' not defined in 'Element'");
                }
                break;

            default:
                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element");
                break;
            }

            return(parameter);
        }
 public bool DependsOn(IGH_ActiveObject PotentialSource)
 {
     throw new NotImplementedException();
 }
Example #29
0
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        Mesh M = default(Mesh);

        if (inputs[0] != null)
        {
            M = (Mesh)(inputs[0]);
        }

        Mesh V = default(Mesh);

        if (inputs[1] != null)
        {
            V = (Mesh)(inputs[1]);
        }

        int n = default(int);

        if (inputs[2] != null)
        {
            n = (int)(inputs[2]);
        }

        int s = default(int);

        if (inputs[3] != null)
        {
            s = (int)(inputs[3]);
        }



        //3. Declare output parameters
        object Pts = null;


        //4. Invoke RunScript
        RunScript(M, V, n, s, ref Pts);

        try
        {
            //5. Assign output parameters to component...
            if (Pts != null)
            {
                if (GH_Format.TreatAsCollection(Pts))
                {
                    IEnumerable __enum_Pts = (IEnumerable)(Pts);
                    DA.SetDataList(0, __enum_Pts);
                }
                else
                {
                    if (Pts is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(0, (Grasshopper.Kernel.Data.IGH_DataTree)(Pts));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(0, Pts);
                    }
                }
            }
            else
            {
                DA.SetData(0, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
    private int runCount;                              //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        string FilePath = default(string);

        if (inputs[0] != null)
        {
            FilePath = (string)(inputs[0]);
        }

        Point3d origin = default(Point3d);

        if (inputs[1] != null)
        {
            origin = (Point3d)(inputs[1]);
        }

        double Scale = default(double);

        if (inputs[2] != null)
        {
            Scale = (double)(inputs[2]);
        }



        //3. Declare output parameters
        object Crv  = null;
        object Pt   = null;
        object Type = null;


        //4. Invoke RunScript
        RunScript(FilePath, origin, Scale, ref Crv, ref Pt, ref Type);

        try
        {
            //5. Assign output parameters to component...
            if (Crv != null)
            {
                if (GH_Format.TreatAsCollection(Crv))
                {
                    IEnumerable __enum_Crv = (IEnumerable)(Crv);
                    DA.SetDataList(1, __enum_Crv);
                }
                else
                {
                    if (Crv is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(Crv));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, Crv);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
            if (Pt != null)
            {
                if (GH_Format.TreatAsCollection(Pt))
                {
                    IEnumerable __enum_Pt = (IEnumerable)(Pt);
                    DA.SetDataList(2, __enum_Pt);
                }
                else
                {
                    if (Pt is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(2, (Grasshopper.Kernel.Data.IGH_DataTree)(Pt));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(2, Pt);
                    }
                }
            }
            else
            {
                DA.SetData(2, null);
            }
            if (Type != null)
            {
                if (GH_Format.TreatAsCollection(Type))
                {
                    IEnumerable __enum_Type = (IEnumerable)(Type);
                    DA.SetDataList(3, __enum_Type);
                }
                else
                {
                    if (Type is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(3, (Grasshopper.Kernel.Data.IGH_DataTree)(Type));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(3, Type);
                    }
                }
            }
            else
            {
                DA.SetData(3, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
    private int runCount;                                   //Legacy field.

    public override void InvokeRunScript(IGH_Component owner, object rhinoDocument, int iteration, List <object> inputs, IGH_DataAccess DA)
    {
        //Prepare for a new run...
        //1. Reset lists
        this.__out.Clear();
        this.__err.Clear();

        this.Component           = owner;
        this.Iteration           = iteration;
        this.GrasshopperDocument = owner.OnPingDocument();
        this.RhinoDocument       = rhinoDocument as Rhino.RhinoDoc;

        this.owner    = this.Component;
        this.runCount = this.Iteration;
        this.doc      = this.RhinoDocument;

        //2. Assign input parameters
        List <double> Density = null;

        if (inputs[0] != null)
        {
            Density = GH_DirtyCaster.CastToList <double>(inputs[0]);
        }
        double RemapIndex = default(double);

        if (inputs[1] != null)
        {
            RemapIndex = (double)(inputs[1]);
        }



        //3. Declare output parameters
        object FAR = null;


        //4. Invoke RunScript
        RunScript(Density, RemapIndex, ref FAR);

        try
        {
            //5. Assign output parameters to component...
            if (FAR != null)
            {
                if (GH_Format.TreatAsCollection(FAR))
                {
                    IEnumerable __enum_FAR = (IEnumerable)(FAR);
                    DA.SetDataList(1, __enum_FAR);
                }
                else
                {
                    if (FAR is Grasshopper.Kernel.Data.IGH_DataTree)
                    {
                        //merge tree
                        DA.SetDataTree(1, (Grasshopper.Kernel.Data.IGH_DataTree)(FAR));
                    }
                    else
                    {
                        //assign direct
                        DA.SetData(1, FAR);
                    }
                }
            }
            else
            {
                DA.SetData(1, null);
            }
        }
        catch (Exception ex)
        {
            this.__err.Add(string.Format("Script exception: {0}", ex.Message));
        }
        finally
        {
            //Add errors and messages...
            if (owner.Params.Output.Count > 0)
            {
                if (owner.Params.Output[0] is Grasshopper.Kernel.Parameters.Param_String)
                {
                    List <string> __errors_plus_messages = new List <string>();
                    if (this.__err != null)
                    {
                        __errors_plus_messages.AddRange(this.__err);
                    }
                    if (this.__out != null)
                    {
                        __errors_plus_messages.AddRange(this.__out);
                    }
                    if (__errors_plus_messages.Count > 0)
                    {
                        DA.SetDataList(0, __errors_plus_messages);
                    }
                }
            }
        }
    }
        public static bool SetParameter(IGH_ActiveObject obj, DB.Element element, DB.Parameter parameter, IGH_Goo goo)
        {
            try
            {
                var value = goo.ScriptVariable();
                switch (parameter?.StorageType)
                {
                case DB.StorageType.Integer:
                {
                    switch (value)
                    {
                    case bool boolean: parameter.Set(boolean ? 1 : 0); break;

                    case int integer: parameter.Set(integer); break;

                    case double real: parameter.Set(Math.Round(ToHost(real, parameter.Definition.ParameterType)).Clamp(int.MinValue, int.MaxValue)); break;

                    case System.Drawing.Color color: parameter.Set(((int)color.R) | ((int)color.G << 8) | ((int)color.B << 16)); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.Double:
                {
                    switch (value)
                    {
                    case int integer: parameter.Set((double)integer); break;

                    case double real: parameter.Set(ToHost(real, parameter.Definition.ParameterType)); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.String:
                {
                    switch (value)
                    {
                    case string str: parameter.Set(str); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.ElementId:
                {
                    switch (value)
                    {
                    case DB.Element ele: parameter.Set(ele.Id); break;

                    case DB.Category cat: parameter.Set(cat.Id); break;

                    case int integer: parameter.Set(new DB.ElementId(integer)); break;

                    default: element = null; break;
                    }
                    break;
                }

                default:
                {
                    element = null;
                    break;
                }
                }

                if (element is null && parameter is object)
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, string.Format("Unable to cast 'Value' from {0} to {1}.", value.GetType().Name, parameter.StorageType.ToString()));
                }

                return(true);
            }
            catch (Exception e)
            {
                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, string.Format("Unable to set 'Value' to parameter {0} : {1}", parameter.Definition.Name, e.Message));
            }

            return(false);
        }