Grasshopper

algorithmic modeling for Rhino

hello

im trying the below code to create multiple bars in robot from grasshopper but im receiving this error message :

"1. Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index (line: 0)"

the code is enclosed below

thank you 

private void RunScript(List<Line> Bars, bool activate, ref object A)
{
if (activate == true)
{
IRobotApplication robApp = new RobotApplication();

if (robApp.Visible == 0)
{
robApp.Interactive = 1;
robApp.Visible = 1;
}
robApp.Project.New(IRobotProjectType.I_PT_SHELL);
RobotProjectPreferences projPref;
projPref = robApp.Project.Preferences;
projPref.SetActiveCode(IRobotCodeType.I_CT_STEEL_STRUCTURES, "ANSI/AISC 360-10");

IRobotStructure str = robApp.Project.Structure;
int nd = str.Nodes.FreeNumber;
int bm = str.Nodes.FreeNumber;
List<Rhino.Geometry.Line> BARS = new List<Rhino.Geometry.Line>();
Rhino.Geometry.Point3d p3d = new Rhino.Geometry.Point3d();
Rhino.Geometry.Point3d p3d2 = new Rhino.Geometry.Point3d();
BARS = Bars;

for (int i = 0; i < BARS.Count; i++)
{
p3d = BARS[i].PointAt(0);
str.Nodes.Create(1, p3d.X, p3d.Y, p3d.Z);
p3d2 = BARS[i].PointAt(1);
str.Nodes.Create(2, p3d2.X, p3d2.Y, p3d2.Z);
str.Bars.Create(bm, 1, 2);
}

}


}

 

Views: 803

Replies to This Discussion

A collection as a list or array, have assigned spaces which contain the data, ordered with indexes. If you access at a space and this had not been assigned, it throws the exception of index out of range. Flattening, if you have a bag with 5 candies you can not take the sixth candy, does not exist. Therefore, the error must be here somewhere:

for (int i = 0; i < BARS.Count; i++)
{
p3d = BARS[i].PointAt(0);
str.Nodes.Create(1, p3d.X, p3d.Y, p3d.Z);
p3d2 = BARS[i].PointAt(1);
str.Nodes.Create(2, p3d2.X, p3d2.Y, p3d2.Z);
str.Bars.Create(bm, 1, 2);
}

Usually this occurs for two reasons.
1. The collection is empty, so before agreeing to it have to ask if it contains data. Something like, in vb: If Bars.count > 0 then...

2. The index is above the range. Then ask before you access. if i >= Bars.Count then exit for

But! try first putting: for (int i = 0; i < BARS.Count - 1; i++)

thank you daniel.

its working properly now , the option of BARS.Count -1 is what i tried .

cheers 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service