/// <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) { //字典立面的Key必须唯一,如何保证TempDict的Key值唯一? //调换Curve和Dict的顺序 //这里存在的一个bug Point3d Pt = default(Point3d); List <Curve> Crvs = new List <Curve>(); int Num = 1; Dictionary <Curve, double> _TempDict = new Dictionary <Curve, double>(); if (!DA.GetDataList <Curve>(0, Crvs)) { return; } if (!DA.GetData(1, ref Pt)) { return; } if (!DA.GetData(2, ref Num)) { return; } if (Num == 0) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Num为0"); } double Param = 0; foreach (var Crv in Crvs) { if (Crv.ClosestPoint(Pt, out Param)) { Point3d TempPt = Crv.PointAt(Param); double Dist = Pt.DistanceTo(TempPt); _TempDict.Add(Crv, Dist); } } var ResultDict = _TempDict.OrderByDescending(item => - item.Value).ToDictionary(item => item.Key, item => item.Value); List <Curve> TempCrvs = ResultDict.Keys.ToList(); List <Curve> ResultCrvs = new List <Curve>(); if (TempCrvs.Count < Math.Abs(Num)) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Num 数值过大"); return; } if (this.FindType == 0) { ResultCrvs = TempCrvs.GetRange(0, Math.Abs(Num)); } else { ResultCrvs = TempCrvs.GetRange(TempCrvs.Count - Num - 1, TempCrvs.Count); } DA.SetDataList(0, ResultCrvs); }
/// <summary> /// Needs to calculate Length of the curve. If you have it - create this class passing 'length' parameter. /// </summary> /// <param name="point"></param> /// <returns></returns> public Percent PercentAt(Point3d point) { double t; if (!Crv.ClosestPoint(point, out t)) { throw new Exception("CurveNormalized.P(Point3d point) failed to get T from 3d point!"); } return(PercentAt(t)); }