private void buttonConfirm_Click(object sender, RoutedEventArgs e) { //1.将设置保存在xml文件中 using (var stream = File.Open("E2COptions.xml", FileMode.Create)) { var serializer = new XmlSerializer(typeof(E2COptions)); serializer.Serialize(stream, e2cOptions); } //2.绘制表格 Document doc = AcadApp.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; //获取当前Excel选择区域 Excel.Application excelApp = Excel.Application.GetActiveInstance(); //获取当前运行的Excel Excel.Range selRange = excelApp.Selection as Excel.Range; using (Transaction trans = db.TransactionManager.StartTransaction()) //开始事务处理 { //2.1 选择插入点 PromptPointOptions ptOpt = new PromptPointOptions("选择插入点"); PromptPointResult ptRes = ed.GetPoint(ptOpt); if (ptRes.Status == PromptStatus.OK) { Point3d sourcePt = Point3d.Origin; //全局移动起点 Point3d targetPt = ptRes.Value; //全局移动终点为所选点 double tableHeight = selRange.GetTableHeights().Sum(); //表格总高 double tableWidth = selRange.GetTableWidths().Sum(); //表格总宽 //根据插入点设置的不同,确定不同的全局移动起点 switch (Excel2CADSettings.e2cOptions.InsertPt) { case "左上": sourcePt = Point3d.Origin; break; case "中上": sourcePt = new Point3d(tableWidth / 2, 0, 0); break; case "右上": sourcePt = new Point3d(tableWidth, 0, 0); break; case "左中": sourcePt = new Point3d(0, -tableHeight / 2, 0); break; case "正中": sourcePt = new Point3d(tableWidth / 2, -tableHeight / 2, 0); break; case "右中": sourcePt = new Point3d(tableWidth, -tableHeight / 2, 0); break; case "左下": sourcePt = new Point3d(0, -tableHeight, 0); break; case "中下": sourcePt = new Point3d(tableWidth / 2, -tableHeight, 0); break; case "右下": sourcePt = new Point3d(tableWidth, -tableHeight, 0); break; } //将所有表格文字和线段并移动到位 ObjectIdCollection entIds = db.DrawTextsAndLines(selRange); foreach (ObjectId id in entIds) { id.Move(sourcePt, targetPt); } } trans.Commit();//执行事务处理 } this.Close(); }
private void checkBoxColumnAuto_Unchecked(object sender, RoutedEventArgs e) { Excel.Application excelApp = Excel.Application.GetActiveInstance(); //获取当前运行的Excel e2cOptions.ColOptList.Clear(); //清空原有列设置数据 if (excelApp != null) { Excel.Range selRange = excelApp.Selection as Excel.Range; if (selRange != null) { double[] tableWidths = selRange.GetTableWidths(true); for (int i = 1; i <= selRange.Columns.Count; i++) { ColumnOptions colOpts = new ColumnOptions( colName: selRange.Columns[i].Cells[1].Address, colWidth: tableWidths[i - 1] / e2cOptions.Scale); e2cOptions.ColOptList.Add(colOpts); } } } this.listViewColumnSetting.ItemsSource = e2cOptions.ColOptList; }
/// <summary> /// 绘制文字和边框线 /// 该函数为使用事务处理,外部调用时应放置在Transaction内 /// </summary> /// <param name="db">图形数据库</param> /// <param name="rng">用户选择的Excel表格区域</param> /// <returns>所有文字和直线的ObjectIdCollection</returns> public static ObjectIdCollection DrawTextsAndLines(this Database db, Excel.Range rng) { //初始化返回的ObjectIdCollection ObjectIdCollection entIds = new ObjectIdCollection(); //获取表格行高 double[] tableHeights = rng.GetTableHeights(); double tableH = tableHeights.Sum();//总高 //获取表格列宽 double[] tableWidths = rng.GetTableWidths(); double tableW = tableWidths.Sum();//总宽 //表格行列数 int nRow = rng.Rows.Count; int nCol = rng.Columns.Count; #region 1.绘制文字 for (int j = 0; j < nCol; j++) //列循环 { for (int i = 0; i < nRow; i++) //行循环 { DBText txt = new DBText(); txt.TextString = rng[i + 1, j + 1].Text as string; //内容 txt.Height = Excel2CADSettings.e2cOptions.TextHeight * Excel2CADSettings.e2cOptions.Scale; //文字高度 txt.TextStyleId = db.Textstyle; //样式为当前样式 txt.WidthFactor = Excel2CADSettings.e2cOptions.TextWidthFactor; //宽度系数 double txtX = 0; //初始化插入点横坐标 double txtY = 0; //初始化插入点纵坐标 //水平对齐位置 if (Excel2CADSettings.e2cOptions.ColumnAuto) //如果选择列自动,按中对齐 { txt.HorizontalMode = TextHorizontalMode.TextMid; //插入点X为单元格中心 if (rng[i + 1, j + 1].PositionInMergeRange() == PosInMergeRange.LeftTop) { txtX = tableWidths.GetSumAt(j - 1) + tableWidths.GetSumAt(j, rng[i + 1, j + 1].MergeArea.Columns.Count) / 2; } else { txtX = tableWidths.GetSumAt(j - 1) + tableWidths[j] / 2; } } else//如果不选择列自动 { switch (Excel2CADSettings.e2cOptions.ColOptList[j].HrAlignment) { case 0: txt.HorizontalMode = TextHorizontalMode.TextLeft; //插入点X为单元格左边线向右0.2单位 txtX = tableWidths.GetSumAt(j - 1) + 0.2 * Excel2CADSettings.e2cOptions.Scale; break; case 1: txt.HorizontalMode = TextHorizontalMode.TextMid; //插入点X为单元格中心 if (rng[i + 1, j + 1].PositionInMergeRange() == PosInMergeRange.LeftTop) { txtX = tableWidths.GetSumAt(j - 1) + tableWidths.GetSumAt(j, rng[i + 1, j + 1].MergeArea.Columns.Count) / 2; } else { txtX = tableWidths.GetSumAt(j - 1) + tableWidths[j] / 2; } break; case 2: txt.HorizontalMode = TextHorizontalMode.TextRight; //插入点X为单元格右边线向左0.2单位 txtX = tableWidths.GetSumAt(j) - 0.2 * Excel2CADSettings.e2cOptions.Scale; break; } } //竖直对齐位置 if (Excel2CADSettings.e2cOptions.ColumnAuto)//如果选择列自动,按中对齐 { txt.VerticalMode = TextVerticalMode.TextVerticalMid; //插入点Y为单元格中心 if (rng[i + 1, j + 1].PositionInMergeRange() == PosInMergeRange.LeftTop) { txtY = -(tableHeights.GetSumAt(i - 1) + tableHeights.GetSumAt(i, rng[i + 1, j + 1].MergeArea.Rows.Count) / 2); } else { txtY = -(tableHeights.GetSumAt(i - 1) + tableHeights[i] / 2); } } else { switch (Excel2CADSettings.e2cOptions.ColOptList[j].VtAlignment) { case 0: txt.VerticalMode = TextVerticalMode.TextTop; //插入点Y为单元格上边线向下0.2单位 txtY = -tableHeights.GetSumAt(i - 1) - 0.2 * Excel2CADSettings.e2cOptions.Scale; break; case 1: txt.VerticalMode = TextVerticalMode.TextVerticalMid; //插入点Y为单元格中心 if (rng[i + 1, j + 1].PositionInMergeRange() == PosInMergeRange.LeftTop) { txtY = -(tableHeights.GetSumAt(i - 1) + tableHeights.GetSumAt(i, rng[i + 1, j + 1].MergeArea.Rows.Count) / 2); } else { txtY = -(tableHeights.GetSumAt(i - 1) + tableHeights[i] / 2); } break; case 2: txt.VerticalMode = TextVerticalMode.TextBottom; //仅测试后,发现CAD该枚举内部应该有问题 //插入点Y为单元格下边线向上0.2单位 txtY = -tableHeights.GetSumAt(i) + 0.2 * Excel2CADSettings.e2cOptions.Scale; break; } } txt.AlignmentPoint = new Point3d(txtX, txtY, 0); db.AddToModelSpace(txt); entIds.Add(txt.ObjectId); } } #endregion #region 2.绘制边框线 //2.1 绘制外边框 //2.1.1 上边线 double stP = 0; //起始位置 double edP = tableW; //结束位置 bool flag = false; //是否有无边线格 for (int j = 0; j < nCol; j++) //遍历首行各单元格,注意COM编程时下标从1开始 { //如果存在无边框单元格 if (rng[1, j + 1].Borders[XlBordersIndex.xlEdgeTop].LineStyle.Equals(XlLineStyle.xlLineStyleNone)) { edP = tableWidths.GetSumAt(j - 1); //末端坐标移到无边框格起点 if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(stP, 0, 0), new Point3d(edP, 0, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } stP = tableWidths.GetSumAt(j); edP = tableW; flag = true; continue; } } if (flag == false)//如果没有无边框格 { Line line = new Line(new Point3d(0, 0, 0), new Point3d(tableW, 0, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } //2.1.2 下边线 stP = 0; //起始位置 edP = tableW; //结束位置 flag = false; //是否有无边线格 for (int j = 0; j < nCol; j++) //遍历末行各单元格,注意COM编程时下标从1开始 { //如果存在无边框单元格 if (rng[nRow, j + 1].Borders[XlBordersIndex.xlEdgeBottom].LineStyle.Equals(XlLineStyle.xlLineStyleNone)) { edP = tableWidths.GetSumAt(j - 1); //末端坐标移到无边框格起点 if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(stP, -tableH, 0), new Point3d(edP, -tableH, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } stP = tableWidths.GetSumAt(j); edP = tableW; flag = true; continue; } } if (flag == false)//如果没有无边框格 { Line line = new Line(new Point3d(0, -tableH, 0), new Point3d(tableW, -tableH, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } //2.1.3 左边线 stP = 0; //起始位置 edP = tableH; //结束位置 flag = false; //是否有无边线格 for (int i = 0; i < nRow; i++) //遍历首列各单元格,注意COM编程时下标从1开始 { //如果存在无边框单元格 if (rng[i + 1, 1].Borders[XlBordersIndex.xlEdgeLeft].LineStyle.Equals(XlLineStyle.xlLineStyleNone)) { edP = tableHeights.GetSumAt(i - 1); //末端坐标移到无边框格起点 if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(0, -stP, 0), new Point3d(0, -edP, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } stP = tableHeights.GetSumAt(i); edP = tableH; flag = true; continue; } } if (flag == false)//如果没有无边框格 { Line line = new Line(new Point3d(0, 0, 0), new Point3d(0, -tableH, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } //2.1.4 右边线 stP = 0; //起始位置 edP = tableH; //结束位置 flag = false; //是否有无边线格 for (int i = 0; i < nRow; i++) //遍历末列各单元格,注意COM编程时下标从1开始 { //如果存在无边框单元格 if (rng[i + 1, nCol].Borders[XlBordersIndex.xlEdgeRight].LineStyle.Equals(XlLineStyle.xlLineStyleNone)) { edP = tableHeights.GetSumAt(i - 1); //末端坐标移到无边框格起点 if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(tableW, -stP, 0), new Point3d(tableW, -edP, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } stP = tableHeights.GetSumAt(i); edP = tableH; flag = true; continue; } } if (flag == false)//如果没有无边框格 { Line line = new Line(new Point3d(tableW, 0, 0), new Point3d(tableW, -tableH, 0)); line.Color = Excel2CADSettings.e2cOptions.OuterLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } //2.2 绘制内框行线 //2.2.1 绘制行线 for (int i = 1; i < nRow; i++) //遍历中间各行 { stP = 0; //起始位置 edP = tableW; //结束位置 flag = false; //是否有无边线格 for (int j = 0; j < nCol; j++) //遍历每行各单元格,注意COM编程时下标从1开始 { //如果存在无边框单元格,或者本单元格为合并单元格的中间格 if (rng[i, j + 1].Borders[XlBordersIndex.xlEdgeBottom].LineStyle.Equals(XlLineStyle.xlLineStyleNone) && rng[i + 1, j + 1].Borders[XlBordersIndex.xlEdgeTop].LineStyle.Equals(XlLineStyle.xlLineStyleNone) || rng[i + 1, j + 1].MergeCells.Equals(true) && rng[i + 1, j + 1].Row != rng[i + 1, j + 1].MergeArea.Row) { edP = tableWidths.GetSumAt(j - 1); //末端坐标移到无边框格起点 if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(stP, -tableHeights.GetSumAt(i - 1), 0), new Point3d(edP, -tableHeights.GetSumAt(i - 1), 0)); line.Color = Excel2CADSettings.e2cOptions.InnerLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } stP = tableWidths.GetSumAt(j + rng[i + 1, j + 1].MergeArea.Columns.Count - 1); edP = tableW; flag = true; continue; } } if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(stP, -tableHeights.GetSumAt(i - 1), 0), new Point3d(edP, -tableHeights.GetSumAt(i - 1), 0)); line.Color = Excel2CADSettings.e2cOptions.InnerLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } } //2.2.2 绘制列线 for (int j = 1; j < nCol; j++) //遍历中间各行 { stP = 0; //起始位置 edP = tableH; //结束位置 flag = false; //是否有无边线格 for (int i = 0; i < nRow; i++) //遍历每列各单元格,注意COM编程时下标从1开始 { //如果存在无边框单元格 if (rng[i + 1, j].Borders[XlBordersIndex.xlEdgeRight].LineStyle.Equals(XlLineStyle.xlLineStyleNone) && rng[i + 1, j + 1].Borders[XlBordersIndex.xlEdgeLeft].LineStyle.Equals(XlLineStyle.xlLineStyleNone) || rng[i + 1, j + 1].MergeCells.Equals(true) && rng[i + 1, j + 1].Column != rng[i + 1, j + 1].MergeArea.Column) { edP = tableHeights.GetSumAt(i - 1); //末端坐标移到无边框格起点 if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(tableWidths.GetSumAt(j - 1), -stP, 0), new Point3d(tableWidths.GetSumAt(j - 1), -edP, 0)); line.Color = Excel2CADSettings.e2cOptions.InnerLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } stP = tableHeights.GetSumAt(i + rng[i + 1, j + 1].MergeArea.Rows.Count - 1); edP = tableH; flag = true; continue; } } if (edP != stP) //如果起终点不重合则绘制框线 { Line line = new Line(new Point3d(tableWidths.GetSumAt(j - 1), -stP, 0), new Point3d(tableWidths.GetSumAt(j - 1), -edP, 0)); line.Color = Excel2CADSettings.e2cOptions.InnerLineColor; db.AddToModelSpace(line); entIds.Add(line.ObjectId); } } #endregion #region 3.绘制标题 DBText title = new DBText(); title.TextString = Excel2CADSettings.e2cOptions.Title; //内容 title.Height = 1.5 * Excel2CADSettings.e2cOptions.TextHeight * Excel2CADSettings.e2cOptions.Scale; //标题高度为文字高度的1.5倍 title.TextStyleId = db.Textstyle; //样式为当前样式 title.WidthFactor = Excel2CADSettings.e2cOptions.TextWidthFactor; //宽度系数 title.HorizontalMode = TextHorizontalMode.TextMid; title.VerticalMode = TextVerticalMode.TextBottom; title.AlignmentPoint = new Point3d(tableW / 2, 5 * Excel2CADSettings.e2cOptions.Scale, 0);//标题位置为中部向上5个单位 db.AddToModelSpace(title); entIds.Add(title.ObjectId); #endregion return(entIds); }