@a.ai wrote:
I want to create a data tree (GH_Structure) from two collections of curves (superblocks and parcels), checking which parcels are inside which block and assigning them to the respective block (parent node) as children. Each branch should contain one GH_curve object.
I get a Null Exception (Object reference not set to an instance to an object).
What am I doing wrong? I suspect I dont generate a valid data tree.private void RunScript(List<object> x, List<object> y, ref object A, ref object B) { List<GH_Curve> BlockCurves = new List<GH_Curve>(); List<GH_Curve> ParcelCurves = new List<GH_Curve>(); GH_Structure<IGH_Goo> dataTree = new GH_Structure<IGH_Goo>(); List<GH_Curve> myParcelCurves = new List<GH_Curve>(); foreach (object crvItem in x) { GH_Curve gh_curve = new Grasshopper.Kernel.Types.GH_Curve(); GH_Convert.ToGHCurve(crvItem, GH_Conversion.Both, ref gh_curve); BlockCurves.Add(gh_curve); } Print(BlockCurves.Count.ToString()); foreach (object crvItem in y) { GH_Curve gh_curve = new Grasshopper.Kernel.Types.GH_Curve(); GH_Convert.ToGHCurve(crvItem, GH_Conversion.Both, ref gh_curve); ParcelCurves.Add(gh_curve); } Print(ParcelCurves.Count.ToString()); // check if information on buildable areas has been passed by user, use respective curve collection myParcelCurves = ParcelCurves; for (int i = 0; i < BlockCurves.Count; i++) //for each block create path and fill in GH_Curve { Polyline block = new Polyline(); bool cast = BlockCurves[i].Value.TryGetPolyline(out block); if (cast && block.IsValid && block.SegmentCount > 2) { GH_Path pth = new GH_Path(i); dataTree.Append(BlockCurves[i], pth); Print(pth.ToString()); //Print(i.ToString()); } else { Exception e = new Exception("Block at index " + i + " is either invalid or not a polyline."); } int parcelPathIdx = 0; for (int j = 0; j < myParcelCurves.Count; j++) //for each parcel check if in block, if yes, append to (new) path { Polyline parcel = new Polyline(); bool cast1 = myParcelCurves[j].Value.TryGetPolyline(out parcel); if (cast1 && parcel.IsValid && parcel.SegmentCount > 2) { if (IsInside(parcel.CenterPoint(), block)) { GH_Path path = new GH_Path(i, parcelPathIdx); dataTree.Append(myParcelCurves[j], path); //Print(parcelPathIdx.ToString()); Print(path.ToString()); parcelPathIdx++; } } else { Exception e = new Exception("Parcel at index " + j + " is either invalid or not a polyline."); }//check if parcelcurve is inside block } } Print(dataTree.DataCount.ToString()); //Print(dataTree.AllData().ToString()); A = dataTree; Print(dataTree.PathCount.ToString()); //B = myPaths; } // <Custom additional code> public static bool IsInside(Point3d _pt, Polyline _crv) { //checks if point is within a curve }
Posts: 5
Participants: 2