/// <summary> /// 清理无效的格线 /// 有时候表格里会有非格线的直线或多段线(比如钢筋图样),需要清理掉 /// 清理的原则是要求格线的头或尾,必须在某个正交方向的格线上 /// </summary> private void CleanLines() { //清理横线 AcTableLine preHLine = null; foreach (AcTableLine hLine in _tableHLines.ToArray()) { hLine.CrossPointCount = 0; foreach (AcTableLine vLine in _tableVLines) { Point2d pt = new Point2d(vLine.XorY, hLine.XorY); if (hLine.HasSegmentOn(pt, _tolerance) && vLine.HasSegmentOn(pt, _tolerance)) { hLine.CrossPointCount += 1; } } if (hLine.CrossPointCount <= 1) { _tableHLines.Remove(hLine); } else if (preHLine != null && preHLine.XorY - hLine.XorY <= _tolerance) //表格容错,挨的太近的格线,删除掉一根交叉点小的 { //string s = string.Format("{0:f3},{1:f3},{2:f3},{3:f3}", preHLine.XorY, hLine.XorY, preHLine.XorY - hLine.XorY, _tolerance); //System.Windows.Forms.MessageBox.Show(s); if (preHLine.CrossPointCount < hLine.CrossPointCount) { _tableHLines.Remove(preHLine); preHLine = hLine; } else { _tableHLines.Remove(hLine); } } else { preHLine = hLine; } } //清理竖线 AcTableLine preVLine = null; foreach (AcTableLine vLine in _tableVLines.ToArray()) { vLine.CrossPointCount = 0; foreach (AcTableLine hLine in _tableHLines) { Point2d pt = new Point2d(vLine.XorY, hLine.XorY); if (hLine.HasSegmentOn(pt, _tolerance) && vLine.HasSegmentOn(pt, _tolerance)) { vLine.CrossPointCount += 1; } } if (vLine.CrossPointCount <= 1) { _tableVLines.Remove(vLine); } else if (preVLine != null && vLine.XorY - preVLine.XorY < _tolerance) //表格容错,挨的太近的格线,删除掉一根交叉点小的 { if (preVLine.CrossPointCount < vLine.CrossPointCount) { _tableVLines.Remove(preVLine); preVLine = vLine; } else { _tableVLines.Remove(vLine); } } else { preVLine = vLine; } } //有时候表格会有双层外框,清理外层 if (_tableHLines.Count > 2 || _tableVLines.Count > 2) { if (_tableHLines[0].CrossPointCount <= 2 && _tableHLines.Last().CrossPointCount <= 2 && _tableVLines[0].CrossPointCount <= 2 && _tableVLines.Last().CrossPointCount <= 2) { _tableHLines.RemoveAt(0); _tableHLines.RemoveAt(_tableHLines.Count - 1); _tableVLines.RemoveAt(0); _tableVLines.RemoveAt(_tableVLines.Count - 1); } } //清理只有2个交点并且不等于表格宽和高的格线 double tableWidth = _tableHLines.Max(line => line.Length); foreach (AcTableLine hLine in _tableHLines.ToArray()) { if (hLine.CrossPointCount <= 2 && CommandUtils.Compare(hLine.Length, tableWidth) < 0) { //System.Windows.Forms.MessageBox.Show(hLine.Length.ToString()); _tableHLines.Remove(hLine); } } double tableHeight = _tableVLines.Max(line => line.Length); foreach (AcTableLine vLine in _tableVLines.ToArray()) { if (vLine.CrossPointCount <= 2 && CommandUtils.Compare(vLine.Length, tableHeight) < 0) { _tableVLines.Remove(vLine); } } }