public void BuildLinks(IEnumerable <ILinkSpec> links, ILinkContainer container) { foreach (var link in links) { var context = contextFactory.CreateContext(link); try { container.AddLink(link.Id, new Link() { Href = options.HrefTransformation?.Transform(context), Rel = options.RelTransformation?.Transform(context), Method = link.HttpMethod.ToString() }); } catch (Exception ex) { throw new LinkTransformationException($"Unable to transform link {link.Id}", ex, context); } } }
/// <summary> /// add the link /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnBuild_Click(object sender, EventArgs e) { MapLinks links = new MapLinks("[[121.2133820,31.285886],[121.2139638, 31.2865935]]", "[[121.2141660, 31.2866371]," + "[121.2160953, 31.2854671],[121.2163246, 31.2852151],[121.2163601, 31.2851393],[121.2163758, 31.2850646],[121.2163814, 31.2848733]," + " [121.2163925, 31.2840516],[121.2163873, 31.2839563],[121.2163765, 31.2838958],[121.2163440, 31.2837675],[121.2161880, 31.2834861]," + "[121.2152711, 31.2823441],[121.2151738, 31.2822313],[121.2150853, 31.2821396],[121.2149775, 31.2820633],[121.2148896, 31.2820111]," + "[121.2144256, 31.2818966],[121.2142475, 31.2819505],[121.2142216, 31.2819903],[121.2142148, 31.2820574],[121.2142168, 31.2821373]," + "[121.2142148, 31.2846036],[121.2142126, 31.2847458],[121.2141895, 31.2849460],[121.2140973, 31.2851871],[121.2138221, 31.2854833], " + "[121.2133835, 31.2857569]]", "[[121.2133550, 31.2856843],[121.2137920, 31.2854163],[121.2139446, 31.2852710],[121.2140375, 31.2851205]," + "[121.2141033, 31.2849395],[121.2141205, 31.2848163],[121.2141286, 31.2820856],[121.2141338, 31.2820461],[121.2141946, 31.2819106]," + "[121.2142898, 31.2818595],[121.2144180, 31.2818543],[121.2148526, 31.2819563],[121.2149904, 31.2820246],[121.2150815, 31.2820901]," + "[121.2151610, 31.2821546],[121.2152441, 31.2822470],[121.2162668, 31.2835153],[121.2163321, 31.2836178],[121.2163766, 31.2837161]," + "[121.2164089, 31.2838223],[121.2164310, 31.2839981],[121.2164246, 31.2849813],[121.2164263, 31.2850688],[121.2163846, 31.2852246]," + "[121.2163203, 31.2853258],[121.2162000, 31.2854486],[121.2141725, 31.2866793]]", "[[121.2139551,31.2866538],[121.2133464, 31.2858938]]", "[[121.2122708,31.2863276],[121.2131735, 31.2857866]]", "[[121.2132083,31.2858663],[121.2123150, 31.2864003]]"); List <MapConnector> connectors = new List <MapConnector>(); connectors.Add(new MapConnector(1, 1, 2, 1, 1, "[[121.2139761, 31.2866085],[121.2140386, 31.2866530],[121.2141176, 31.2866628]]")); connectors.Add(new MapConnector(2, 1, 1, 1, 1, "[[121.2133590, 31.2857876],[121.2133515, 31.2858090],[121.2133551, 31.2858465]]")); connectors.Add(new MapConnector(2, 1, 6, 1, 1, "[[121.2133313, 31.2857863],[121.2133009, 31.2858019],[121.2132711, 31.2858210]]")); connectors.Add(new MapConnector(3, 1, 4, 1, 1, "[[121.2141185, 31.2867008],[121.2140918, 31.2867055],[121.2140635, 31.2867065]]")); connectors.Add(new MapConnector(4, 1, 6, 1, 1, "[[121.2133193, 31.2858745],[121.2132875, 31.2858613],[121.2132538, 31.2858563]]")); connectors.Add(new MapConnector(4, 1, 3, 1, 1, "[[121.2132968, 31.2858205],[121.2132896, 31.2857678],[121.2133031, 31.2857371]]")); connectors.Add(new MapConnector(5, 1, 1, 1, 1, "[[121.2132478, 31.2857801],[121.2132943, 31.2857968],[121.2133323, 31.2858291]]")); connectors.Add(new MapConnector(5, 1, 3, 1, 1, "[[121.2132310, 31.2857565],[121.2132573, 31.2857410],[121.2132801, 31.2857261]]")); try { ILinkContainer linkCtn = VissimSim.net.Links; foreach (ILink link in linkCtn) { linkCtn.RemoveLink(link); } for (int i = 0; i < links.Links.Count(); i++) { object[] array = { 3.5 }; string str = "LineString("; for (int j = 0; j < links.Links[i].nodes.Count(); j++) { List <double> loc1 = Moc(links.Links[i].nodes[j].gpsPot); str += Convert.ToString(loc1[0]) + ' ' + Convert.ToString(loc1[1]) + ','; } str = str.Remove(str.Length - 1, 1); str += ")"; linkCtn.AddLink(links.Links[i].LinkNo, str, array); Thread.Sleep(500); //sleep 500ms } for (int i = 0; i < connectors.Count(); i++) { ILane laneFrom = linkCtn.ItemByKey[connectors[i].FLinkId].Lanes.ItemByKey[connectors[i].FLaneId]; ILane laneEnd = linkCtn.ItemByKey[connectors[i].TLinkId].Lanes.ItemByKey[connectors[i].TLaneId]; double fromPos = linkCtn.ItemByKey[connectors[i].FLinkId].AttValue["Length2D"]; double toPos = 0; string str = "LineString("; for (int j = 0; j < connectors[i].link.nodes.Count(); j++) { List <double> loc1 = Moc(connectors[i].link.nodes[j].gpsPot); str += Convert.ToString(loc1[0]) + ' ' + Convert.ToString(loc1[1]) + ','; } str = str.Remove(str.Length - 1, 1); str += ")"; linkCtn.AddConnector(Convert.ToInt32(connectors[i].FLinkId.ToString() + "000" + connectors[i].TLinkId.ToString()), laneFrom, fromPos, laneEnd, toPos, connectors[i].LaneNum, str); Thread.Sleep(500); //sleep 500ms } foreach (ILink link in linkCtn) { if (Convert.ToBoolean(link.AttValue["IsConn"])) { link.RecalculateSpline(); } } } catch (Exception ex) { VissimSim.vissim.Exit(); MessageBox.Show(ex.ToString()); } //object[] array = { 3.5, 3.5 }; // Console.WriteLine(array[0].ToString()); // ILink link = linkCtn.AddLink(42, "LINESTRING(1 1,2 2)", array); // Console.WriteLine(Convert.ToString(link.AttValue["FromPos"])); }