algorithmic modeling for Rhino
I want to make components with C # Express. What is the problem with my coding? An error occurs. Only "null" is output. I want to outputpolyline. Please correct my code
--------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Grasshopper.Kernel;
using Rhino.Geometry;
namespace ClassLibrary1
{
public class Fachwerk: GH_Component
{
public Fachwerk():
base("Fachwerk","Fachwerk","Fachwerkstaebe","JinsooKim","Stab"){}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.Register_PointParam("Point","P","Input Point");
pManager.Register_IntegerParam("DivU","U","U-Divide");
pManager.Register_IntegerParam("DivV","V","V-Divide");
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.Register_CurveParam("Curve","A","Cirve list");
pManager.Register_CurveParam("Curve","B","Cirve list");
pManager.Register_CurveParam("Curve","C","Cirve list");
pManager.Register_CurveParam("Curve","D","Cirve list");
}
protected override void SolveInstance(IGH_DataAccess DA)
{
Point3d pts = new Point3d();
int divU = new int();
int divV = new int();
if (DA.GetData(0, ref pts)) {
if (DA.GetData(1, ref divU)) { return; }
if (DA.GetData(2, ref divV)) { return; }
List<Point3d> pts_list1 = new List<Point3d>();
pts_list1.Add(pts);
Point3d[,] ptsList = new Point3d[divU+1, divV+1];
int n = 0;
for (int i = 0; i <= divU; i++)
{
for (int j = 0; j <= divV; j++)
{
ptsList[i, j] = pts_list1[n];
n = n + 1;
}
}
List<Polyline> splLinesA = new List<Polyline>();
for (int i = 0; i <= divU - 4; i += 4)
{
for (int j = 0; j <= divV - 2; j += 2)
{
List<Point3d> ptArr = new List<Point3d>();
ptArr.Add(ptsList[i, j]);
ptArr.Add(ptsList[i + 2, j + 1]);
ptArr.Add(ptsList[i, j + 2]);
ptArr.Add(ptsList[i, j]);
Polyline pl = new Polyline(ptArr);
splLinesA.Add(pl);
}
}
DA.SetDataList(0, splLinesA);
List<Polyline> splLinesB = new List<Polyline>();
Polyline pl_B = new Polyline();
for (int i = 0; i <= divU - 4; i += 4)
{
for (int j = 0; j <= divV - 3; j += 2)
{
List<Point3d> ptArr = new List<Point3d>();
ptArr.Add(ptsList[i + 2, j + 1]);
ptArr.Add(ptsList[i + 4, j + 2]);
ptArr.Add(ptsList[i + 2, j + 3]);
ptArr.Add(ptsList[i + 2, j + 1]);
Polyline pl = new Polyline(ptArr);
splLinesB.Add(pl);
}
}
DA.SetDataList(1, splLinesB);
List<Polyline> splLinesC = new List<Polyline>();
for (int i = 0; i <= divU - 4; i += 4)
{
for (int j = 0; j <= divV - 3; j += 2)
{
List<Point3d> ptArr = new List<Point3d>();
ptArr.Add(ptsList[i, j + 2]);
ptArr.Add(ptsList[i + 2, j + 1]);
ptArr.Add(ptsList[i + 2, j + 3]);
ptArr.Add(ptsList[i, j + 2]);
Polyline pl = new Polyline(ptArr);
splLinesC.Add(pl);
}
}
DA.SetDataList(2, splLinesC);
List<Polyline> splLinesD = new List<Polyline>();
for (int i = 0; i <= divU - 4; i += 4)
{
for (int j = 0; j <= divV - 2; j += 2)
{
List<Point3d> ptArr = new List<Point3d>();
ptArr.Add(ptsList[i + 2, j + 1]);
ptArr.Add(ptsList[i + 4, j]);
ptArr.Add(ptsList[i + 4, j + 2]);
ptArr.Add(ptsList[i + 2, j + 1]);
Polyline pl = new Polyline(ptArr);
splLinesD.Add(pl);
}
}
DA.SetDataList(3, splLinesD);
}
}
public override Guid ComponentGuid
{
get {
return new Guid("{F85370B0-6C41-4579-AEEB-666A1E5BE175}");
}
}
}
}
Tags:
if (DA.GetData(1, ref divU)) { return; }
if (DA.GetData(2, ref divV)) { return; }
This is the problem I think. These functions return true when successful, so you abort on success. This code should be:
if (!DA.GetData(1, ref divU)) { return; }
if (!DA.GetData(2, ref divV)) { return; }
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Thank you very much for your kind response. But still, an error occurs.There is a problem with the index range. I will send the original file. Could you find a problem there?
This is error message.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
List<Point3d> pts_list1 = new List<Point3d>();
pts_list1.Add(pts);
...
ptsList[i, j] = pts_list1[n];
n = n + 1;
This is another bug. You only add a single point to pts_list1, so the onyl valid index you can use is pts_list1[0], however you keep incrementing it every iteration.
I think you need to make sure you can debug into your code so you can see exactly on what line exceptions are thrown and you can also see values assigned to all variables at any moment. Can you hit breakpoints in your code?
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by