public static void CheckForSplits(ILayoutAccess lo, LayoutWiringPoints wireData, IEnumerable <Component> toCheck) { Dictionary <WireSegment, List <Location> > breaks = new Dictionary <WireSegment, List <Location> >(); foreach (Component comp in toCheck) { foreach (ConnectionPoint p in comp.Connections) { Location pLoc = comp.GetLocation(lo).Translate(p.Dx, p.Dy); foreach (WireSegment w in wireData.GetWiresContaining(pLoc)) { List <Location> wLocs; bool found = breaks.TryGetValue(w, out wLocs); if (!found) { wLocs = new List <Location>(); breaks[w] = wLocs; } wLocs.Add(pLoc); } } } foreach (WireSegment w in breaks.Keys) { lo.RemoveWire(w); List <Location> result = breaks[w]; result.Add(w.End0); result.Add(w.End1); result.Sort(Location.XComparer); Location p0 = result[0]; foreach (Location p1 in result) { if (p0 != p1) { lo.AddWire(p0, p1); p0 = p1; } } } }
private static void AddWireSegment(ILayoutAccess lo, LayoutWiringPoints wiringData, WireSegment proposedSeg) { Location loc0 = proposedSeg.End0; Location loc1 = proposedSeg.End1; Dictionary <WireSegment, Location?> intersects = new Dictionary <WireSegment, Location?>(); List <Location> breaks = new List <Location>(); foreach (WireSegment w in wiringData.GetWiresContaining(loc0)) { intersects[w] = intersects.ContainsKey(w) ? (Location?)null : loc0; } foreach (WireSegment w in wiringData.GetWiresContaining(loc1)) { intersects[w] = intersects.ContainsKey(w) ? (Location?)null : loc1; } foreach (Location loc in proposedSeg.GetLocationsOnWire(Constants.GRID_SIZE, false)) { foreach (WireSegment w in wiringData.GetWiresEndingAt(loc)) { intersects[w] = intersects.ContainsKey(w) ? (Location?)null : loc; } if (wiringData.IsPortAt(loc)) { breaks.Add(loc); } } List <WireSegment> toRemove = new List <WireSegment>(); List <WireSegment> toAdd = new List <WireSegment>(); List <Location> endpoints = new List <Location>(); endpoints.Add(loc0); endpoints.Add(loc1); foreach (WireSegment w in intersects.Keys) { Location?intersect = intersects[w]; if (!intersect.HasValue) // segment crosses both endpoints - delete subsegment { toRemove.Add(w); } else if (w.IsCollinear(proposedSeg, Constants.GRID_SIZE)) { toRemove.Add(w); endpoints.Add(w.End0); endpoints.Add(w.End1); } else // segment crosses over this one - maybe we should split it { Location at = intersect.Value; breaks.Add(at); if (at != w.End0 && at != w.End1) { toRemove.Add(w); toAdd.Add(new WireSegment(w.End0, at)); toAdd.Add(new WireSegment(at, w.End1)); } } } endpoints.Sort(Location.XComparer); breaks.Add(endpoints[0]); breaks.Add(endpoints[endpoints.Count - 1]); breaks.Sort(Location.XComparer); Location cur = breaks[0]; foreach (Location loc in breaks) { if (cur != loc) { toAdd.Add(new WireSegment(cur, loc)); cur = loc; } } foreach (WireSegment seg in toRemove) { lo.RemoveWire(seg); } foreach (WireSegment seg in toAdd) { lo.AddWire(seg.End0, seg.End1); } }