コード例 #1
0
        private MassElement[] GetSelectedMassElement()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionOptions Opts = new PromptSelectionOptions();
            PromptSelectionResult  psr  = ed.GetSelection(Opts);

            if (psr.Status == PromptStatus.OK)
            {
                Autodesk.AutoCAD.EditorInput.SelectionSet    ss   = psr.Value;
                Autodesk.AutoCAD.DatabaseServices.ObjectId[] oids = ss.GetObjectIds();
                int           count  = oids.Length;
                MassElement[] result = new MassElement[count];
                Database      db     = Application.DocumentManager.MdiActiveDocument.Database;
                Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
                Transaction trans = tm.StartTransaction();

                MassElement me = new MassElement();
                for (int i = 0; i < count; i++)
                {
                    Autodesk.AutoCAD.DatabaseServices.Entity ety = tm.GetObject(oids[i], OpenMode.ForWrite, true) as Autodesk.AutoCAD.DatabaseServices.Entity;
                    result[i] = ety as MassElement;
                }

                trans.Commit();
                trans.Dispose();
                return(result);
            }
            return(null);
        }
コード例 #2
0
        public void List()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                PromptSelectionOptions Opts = new PromptSelectionOptions();

                //Build	a filter list so that only block references	are	selected
                TypedValue[] filList = { new TypedValue((int)DxfCode.Start, "INSERT") };

                SelectionFilter       filter = new SelectionFilter(filList);
                PromptSelectionResult res    = ed.GetSelection(Opts, filter);

                //Do nothing if	selection is unsuccessful
                if (res.Status != PromptStatus.OK)
                {
                    return;
                }

                Autodesk.AutoCAD.EditorInput.SelectionSet SS = res.Value;
                ObjectId[] idArray = SS.GetObjectIds();

                //List the selection set
                EmployeeCount(idArray);
            }
            catch
            {
            }
        }
コード例 #3
0
        static public void SpaceOnAttributeName()
        {
            // Получение текущего документа и базы данных
            App.Document acDoc   = App.Application.DocumentManager.MdiActiveDocument;
            Db.Database  acCurDb = acDoc.Database;
            Ed.Editor    acEd    = acDoc.Editor;

            // старт транзакции
            using (Db.Transaction acTrans = acCurDb.TransactionManager.StartOpenCloseTransaction())
            {
                Db.TypedValue[] acTypValAr = new Db.TypedValue[1];
                acTypValAr.SetValue(new Db.TypedValue((int)Db.DxfCode.Start, "INSERT"), 0);
                Ed.SelectionFilter acSelFtr = new Ed.SelectionFilter(acTypValAr);

                Ed.PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection(acSelFtr);
                if (acSSPrompt.Status == Ed.PromptStatus.OK)
                {
                    Ed.SelectionSet acSSet = acSSPrompt.Value;
                    foreach (Ed.SelectedObject acSSObj in acSSet)
                    {
                        if (acSSObj != null)
                        {
                            if (acSSObj.ObjectId.ObjectClass.IsDerivedFrom(Rtm.RXClass.GetClass(typeof(Db.BlockReference))))
                            {
                                Db.BlockReference acEnt = acTrans.GetObject(acSSObj.ObjectId,
                                                                            Db.OpenMode.ForRead) as Db.BlockReference;

                                Db.BlockTableRecord blr = acTrans.GetObject(acEnt.BlockTableRecord,
                                                                            Db.OpenMode.ForRead) as Db.BlockTableRecord;
                                if (acEnt.IsDynamicBlock)
                                {
                                    blr = acTrans.GetObject(acEnt.DynamicBlockTableRecord,
                                                            Db.OpenMode.ForRead) as Db.BlockTableRecord;
                                }

                                if (blr.HasAttributeDefinitions)
                                {
                                    foreach (Db.ObjectId id in blr)
                                    {
                                        if (id.ObjectClass.IsDerivedFrom(Rtm.RXClass.GetClass(typeof(Db.AttributeDefinition))))
                                        {
                                            Db.AttributeDefinition acAttrRef = acTrans.GetObject(id,
                                                                                                 Db.OpenMode.ForWrite) as Db.AttributeDefinition;

                                            if (acAttrRef != null)
                                            {
                                                acAttrRef.Tag = acAttrRef.Tag.Replace('_', ' ');
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                acTrans.Commit();
            }
        }
コード例 #4
0
ファイル: DBCustomEntity.cs プロジェクト: presscad/DrawTunnel
        protected void modifiyValue(Action <Entity> handler)
        {
            Document doc    = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db     = doc.Database;
            Entity   entity = null;
            Editor   ed     = doc.Editor;

            PromptSelectionResult res = ed.SelectAll();

            if (res.Status == PromptStatus.Error)
            {
                return;
            }
            Autodesk.AutoCAD.EditorInput.SelectionSet            SS      = res.Value;
            Autodesk.AutoCAD.DatabaseServices.ObjectId[]         idarray = SS.GetObjectIds();
            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm      = db.TransactionManager;

            using (DocumentLock docLock = doc.LockDocument())
            {
                Transaction myT = null;
                if (tm.NumberOfActiveTransactions == 0)
                {
                    myT = tm.StartTransaction();
                }
                else
                {
                    myT = tm.TopTransaction;
                }

                foreach (Autodesk.AutoCAD.DatabaseServices.ObjectId id in idarray)
                {
                    if (id.Handle.Value == HandleValue)
                    {
                        entity = (RoadwayWrapper)myT.GetObject(id, OpenMode.ForWrite, false);
                        handler(entity);
                        break;
                    }
                }
                myT.Commit();
            }
        }
コード例 #5
0
        public void List()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                PromptSelectionOptions Opts    = new PromptSelectionOptions();
                TypedValue[]           filList = new TypedValue[1];
                //Build a filter list so that only block references are selected
                filList[0] = new TypedValue((int)DxfCode.Start, "INSERT");
                SelectionFilter       filter = new SelectionFilter(filList);
                PromptSelectionResult res    = ed.GetSelection(Opts, filter);
                //Do nothing if selection is unsuccessful
                if (res.Status != PromptStatus.OK)
                {
                    return;
                }
                Autodesk.AutoCAD.EditorInput.SelectionSet SS = res.Value;
                ObjectId[] idArray;
                idArray = SS.GetObjectIds();
                string[] saEmployeeList = new string[4];
                //collect all employee details in saEmployeeList array
                foreach (ObjectId employeeId in idArray)
                {
                    ListEmployee(employeeId, ref saEmployeeList);
                    //Print employee details to the command line
                    foreach (string employeeDetail in saEmployeeList)
                    {
                        ed.WriteMessage(employeeDetail);
                    }
                    //separator
                    ed.WriteMessage("----------------------" + "\r\n");
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("Error Listing Employees: " + ex.Message);
            }
        }
コード例 #6
0
        public void ExportTrueTable()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            using (Transaction transaction = doc.Database.TransactionManager.StartTransaction())
            {
                Editor ed = doc.Editor;
                PromptSelectionOptions op = new PromptSelectionOptions();
                op.MessageForAdding = "\n可点选或框选多个表格";
                PromptSelectionResult ents = ed.GetSelection(op);
                if (ents.Status != PromptStatus.OK)
                {
                    return;
                }
                Autodesk.AutoCAD.EditorInput.SelectionSet selectResults = ents.Value;
                if (selectResults == null)
                {//用户没有选中任何
                    return;
                }
                IWorkbook wb         = new HSSFWorkbook();
                int       sheetIndex = 1;
                foreach (var item in selectResults.GetObjectIds())
                {
                    if (item == ObjectId.Null)
                    {
                        continue;
                    }
                    Entity ent = item.GetObject(OpenMode.ForRead) as Entity;
                    if ((ent as Table) != null)
                    {
                        List <TableCell>  tablecellList  = new List <TableCell>();
                        List <MergeRange> mergeRangeList = new List <MergeRange>();
                        Table             tbl            = ent as Table;
                        for (int i = 0; i < tbl.Rows.Count; i++)
                        {
                            for (int j = 0; j < tbl.Columns.Count; j++)
                            {
                                TableCell tableCell = new TableCell();
                                tableCell.RowIndex   = i;
                                tableCell.ColumIndex = j;
                                string value = "";
                                if (tbl.Cells[i, j].Value != null)
                                {
                                    value = tbl.Cells[i, j].GetTextString(FormatOption.IgnoreMtextFormat);
                                }
                                MergeRange mergeRange = new MergeRange();
                                CellRange  cellRange  = tbl.Cells[i, j].GetMergeRange();//合并的信息
                                if (IsMergeRange(cellRange))
                                {
                                    tableCell.IsMergeRange = true;
                                    if (mergeRangeList.Where(o => o.BottomRow == cellRange.BottomRow && o.TopRow == cellRange.TopRow && o.LeftColumn == cellRange.LeftColumn && o.RightColumn == cellRange.RightColumn).Count() == 0)
                                    {
                                        mergeRange.TopRow      = cellRange.TopRow;
                                        mergeRange.BottomRow   = cellRange.BottomRow;
                                        mergeRange.LeftColumn  = cellRange.LeftColumn;
                                        mergeRange.RightColumn = cellRange.RightColumn;
                                        mergeRangeList.Add(mergeRange);
                                    }
                                    else
                                    {
                                        mergeRange = null;
                                    }
                                }
                                else
                                {
                                    tableCell.IsMergeRange = false;
                                    mergeRange             = null;
                                }
                                tableCell.Value      = value;
                                tableCell.MergeRange = mergeRange;
                                tablecellList.Add(tableCell);
                            }
                        }
                        if (tablecellList.Count <= 0)
                        {
                            continue;
                        }
                        #region 生成文件
                        //创建表
                        ISheet sh         = wb.CreateSheet("sheet" + sheetIndex);
                        int    rowindex   = tablecellList.Last().RowIndex;
                        int    columindex = tablecellList.Last().ColumIndex;
                        for (int i = 0; i <= rowindex; i++)
                        {
                            IRow row = sh.CreateRow(i);
                            for (int j = 0; j <= columindex; j++)
                            {
                                row.CreateCell(j);
                            }
                        }
                        foreach (var itemCell in tablecellList)
                        {
                            if (!(itemCell.IsMergeRange && itemCell.MergeRange == null))
                            {
                                sh.GetRow(itemCell.RowIndex).GetCell(itemCell.ColumIndex).SetCellValue(itemCell.Value);
                            }
                            if (itemCell.MergeRange != null &&
                                itemCell.MergeRange.TopRow >= 0 &&
                                itemCell.MergeRange.BottomRow >= 0 &&
                                itemCell.MergeRange.LeftColumn >= 0 &&
                                itemCell.MergeRange.RightColumn >= 0)
                            {
                                sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(itemCell.MergeRange.TopRow, itemCell.MergeRange.BottomRow, itemCell.MergeRange.LeftColumn, itemCell.MergeRange.RightColumn));
                            }
                        }
                        #endregion
                        sheetIndex++;
                    }
                }
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter           = "所有文件(*.xls)|*.xls|所有文件(*.xlsx)|*.xlsx"; //设置要选择的文件的类型
                fileDialog.RestoreDirectory = true;
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = fileDialog.FileName;//返回文件的完整路径
                    using (FileStream stm = System.IO.File.OpenWrite(file))
                    {
                        wb.Write(stm);
                    }
                    MessageBox.Show("导出数据成功");
                }
            }
        }
コード例 #7
0
        public void ExportFakeTable()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            using (Transaction transaction = doc.Database.TransactionManager.StartTransaction())
            {
                Editor ed = doc.Editor;

                Polyline3d pl = GetRegion();
                if (null == pl)
                {
                    return;
                }
                Extents3d ext3d = pl.GeometricExtents; //选择区域
                if (ext3d.MinPoint == new Extents3d().MinPoint&& ext3d.MaxPoint == new Extents3d().MaxPoint)
                {
                    return;
                }
                //获取区域内所有对象
                PromptSelectionResult ents = ed.SelectCrossingWindow(ext3d.MinPoint, ext3d.MaxPoint);
                if (ents.Status != PromptStatus.OK)
                {
                    return;
                }
                ObjectId[]            entsIn  = new ObjectId[] { };
                PromptSelectionResult entsRIn = ed.SelectWindow(ext3d.MinPoint, ext3d.MaxPoint);
                if (entsRIn.Status == PromptStatus.OK && entsRIn.Value != null)
                {
                    entsIn = entsRIn.Value.GetObjectIds();
                }

                Autodesk.AutoCAD.EditorInput.SelectionSet selectResults = ents.Value;
                if (selectResults == null)
                {//用户没有选中任何
                    return;
                }
                //ed.WriteMessage("\n相交内:"+ selectResults.GetObjectIds().Count()+"包含内"+ entsIn.Count());
                IWorkbook wb         = new HSSFWorkbook();
                int       sheetIndex = 1;
                Dictionary <Point3d, List <DBText> > dbtextList = new Dictionary <Point3d, List <DBText> >();
                Dictionary <Point3d, List <MText> >  mtextList  = new Dictionary <Point3d, List <MText> >();
                List <Line> verticalLine   = new List <Line>();
                List <Line> horizontalLine = new List <Line>();
                foreach (var item in selectResults.GetObjectIds())
                {
                    if (item == ObjectId.Null)
                    {
                        continue;
                    }
                    Entity ent = item.GetObject(OpenMode.ForRead) as Entity;
                    DealEntity(ent, dbtextList, mtextList, verticalLine, horizontalLine, entsIn);
                }
                if (!(verticalLine.Count > 0 && horizontalLine.Count > 0))
                {
                    return;
                }
                #region 垂直 判断列数
                List <Line> verticalLineClone = new List <Line>();
                verticalLine.ForEach((item) => {
                    verticalLineClone.Add(item.Clone() as Line);
                });
                List <double> xList = new List <double>()
                {
                    verticalLineClone[0].StartPoint.X
                };                                                                            //记录X轴
                for (int i = 0; i < verticalLineClone.Count; i++)
                {
                    if (xList.Where(o => Math.Abs(o - verticalLineClone[i].StartPoint.X) > 0.1).Count() >= xList.Count)
                    {//垂直线 间距 大于0.1的间距,判定为一列
                        xList.Add(verticalLineClone[i].StartPoint.X);
                        verticalLineClone.RemoveAt(i);
                        i--;
                    }
                }

                #endregion
                #region 水平 判断行数
                List <Line> horizontalLineClone = new List <Line>();
                horizontalLine.ForEach((item) => {
                    horizontalLineClone.Add(item.Clone() as Line);
                });
                List <double> yList = new List <double>()
                {
                    horizontalLineClone[0].StartPoint.Y
                };                                                                              //记录Y轴
                for (int i = 0; i < horizontalLineClone.Count; i++)
                {
                    if (yList.Where(o => Math.Abs(o - horizontalLineClone[i].StartPoint.Y) > 0.1).Count() >= yList.Count)
                    {//水平线 间距 大于0.1的间距,判定为一行
                        yList.Add(horizontalLineClone[i].StartPoint.Y);
                        horizontalLineClone.RemoveAt(i);
                        i--;
                    }
                }
                yList = yList.Distinct().OrderByDescending(o => o).ToList();
                #endregion
                #region 去掉与选择区域边框相交的 线段
                for (int i = 0; i < verticalLine.Count; i++)
                {
                    Point3dCollection pc = new Point3dCollection();
                    verticalLine[i].IntersectWith(pl, Intersect.OnBothOperands, pc, 0, 0);
                    if (pc.Count > 0)
                    {
                        if (isPointInRegion(ext3d, verticalLine[i].StartPoint))
                        {
                            if (yList.Select(o => Math.Abs(o - verticalLine[i].StartPoint.Y)).Min() > 0.1)
                            {
                                xList.Remove(verticalLine[i].StartPoint.X);
                            }
                        }
                        else
                        {
                            if (yList.Select(o => Math.Abs(o - verticalLine[i].EndPoint.Y)).Min() > 0.1)
                            {
                                xList.Remove(verticalLine[i].StartPoint.X);
                            }
                        }
                    }
                }
                for (int i = 0; i < horizontalLine.Count; i++)
                {
                    Point3dCollection pc = new Point3dCollection();
                    horizontalLine[i].IntersectWith(pl, Intersect.OnBothOperands, pc, 0, 0);
                    if (pc.Count > 0)
                    {
                        if (isPointInRegion(ext3d, horizontalLine[i].StartPoint))
                        {
                            if (xList.Select(o => Math.Abs(o - horizontalLine[i].StartPoint.X)).Min() > 0.1)
                            {
                                yList.Remove(horizontalLine[i].StartPoint.Y);
                            }
                        }
                        else
                        {
                            if (xList.Select(o => Math.Abs(o - horizontalLine[i].EndPoint.X)).Min() > 0.1)
                            {
                                yList.Remove(horizontalLine[i].EndPoint.Y);
                            }
                        }
                    }
                }
                #endregion
                //xList 从左到右排序
                xList = xList.Distinct().OrderBy(o => o).ToList();
                //yList 从上到下排序
                yList = yList.Distinct().OrderByDescending(o => o).ToList();

                double xL = Math.Abs(xList.ElementAt(1) - xList.ElementAt(0));
                double xR = Math.Abs(xList.ElementAt(xList.Count - 1) - xList.ElementAt(xList.Count - 2));
                double yT = Math.Abs(yList.ElementAt(0) - yList.ElementAt(1));
                double yB = Math.Abs(yList.ElementAt(yList.Count - 2) - yList.ElementAt(yList.Count - 1));

                Line lineL = new Line(ext3d.MinPoint, new Point3d(ext3d.MinPoint.X, ext3d.MaxPoint.Y, 0));
                Line lineR = new Line(new Point3d(ext3d.MaxPoint.X, ext3d.MinPoint.Y, 0), ext3d.MaxPoint);
                Line lineT = new Line(new Point3d(ext3d.MinPoint.X, ext3d.MaxPoint.Y, 0), ext3d.MaxPoint);
                Line lineB = new Line(ext3d.MinPoint, new Point3d(ext3d.MaxPoint.X, ext3d.MinPoint.Y, 0));
                if (!(xList.Count > 1 && yList.Count > 1))
                {
                    MessageBox.Show("未识别到表格,请扩大选择范围");
                    return;
                }
                TypedValue[] acTypValAr = new TypedValue[5];
                acTypValAr.SetValue(new TypedValue((int)DxfCode.Operator, "<or"), 0);
                acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "Line"), 1);
                acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "LWPOLYLINE"), 2);
                acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "INSERT"), 3);
                acTypValAr.SetValue(new TypedValue((int)DxfCode.Operator, "or>"), 4);
                SelectionFilter   acSelFtr           = new SelectionFilter(acTypValAr);
                List <TableCell>  tablecellList      = new List <TableCell>();
                List <MergeRange> mergeRangeList     = new List <MergeRange>();
                Dictionary <Point3d, MergeRange> dic = new Dictionary <Point3d, MergeRange>();
                try
                {
                    for (int y = 0; y < yList.Count - 1; y++)
                    {
                        for (int x = 0; x < xList.Count - 1; x++)
                        {
                            double  xmid1   = (xList[x] + (xList[x + 1] - xList[x]) / 2); //当前单元格中间x轴位置
                            double  ymid1   = (yList[y] - (yList[y] - yList[y + 1]) / 2); //当前单元格中间y轴位置
                            Point3d point1  = new Point3d(xmid1, ymid1, 0);
                            Point3d pointx2 = Point3d.Origin;
                            Point3d pointy2 = Point3d.Origin;
                            bool    isX     = false;//X轴是否有交集
                            if (x + 2 < xList.Count)
                            {
                                #region xList
                                double xmid2 = (xList[x + 1] + (xList[x + 2] - xList[x + 1]) / 2);
                                pointx2 = new Point3d(xmid2, ymid1, 0);
                                PromptSelectionResult crossing = ed.SelectCrossingWindow(point1, pointx2, acSelFtr);
                                if (crossing.Status == PromptStatus.OK)
                                {
                                    SelectionSet ss = crossing.Value;
                                    ObjectId[]   objectidcollectionX = ss.GetObjectIds();
                                    List <Line>  lines = new List <Line>();
                                    foreach (var objectid in objectidcollectionX)
                                    {
                                        Entity ent = objectid.GetObject(OpenMode.ForRead) as Entity;
                                        if (ent.GeometricExtents.MinPoint == new Extents3d().MinPoint&& ent.GeometricExtents.MaxPoint == new Extents3d().MaxPoint)
                                        {
                                            continue;
                                        }
                                        DealEntity(ent, lines, pl, ext3d, lineL, lineR, xL, xR, entsIn);
                                    }
                                    foreach (var o in lines)
                                    {
                                        if (verticalLine.Where(l => l.StartPoint == o.StartPoint && l.EndPoint == o.EndPoint).Count() > 0)
                                        {
                                            isX = true;
                                            break;
                                        }
                                    }
                                }
                                #endregion
                            }
                            else
                            {//最边上的单元格
                                isX = true;
                            }
                            bool isY = false;//Y轴是否有交集
                            if (y + 2 < yList.Count)
                            {
                                #region yList
                                double ymid2 = (yList[y + 1] + (yList[y + 2] - yList[y + 1]) / 2);
                                pointy2 = new Point3d(xmid1, ymid2, 0);
                                PromptSelectionResult crossing = ed.SelectCrossingWindow(point1, pointy2, acSelFtr);
                                if (crossing.Status == PromptStatus.OK)
                                {
                                    SelectionSet ss = crossing.Value;
                                    ObjectId[]   objectidcollectionY = ss.GetObjectIds();

                                    List <Line> lines = new List <Line>();
                                    foreach (var objectid in objectidcollectionY)
                                    {
                                        Entity ent = objectid.GetObject(OpenMode.ForRead) as Entity;
                                        if (ent.GeometricExtents.MinPoint == new Extents3d().MinPoint&& ent.GeometricExtents.MaxPoint == new Extents3d().MaxPoint)
                                        {
                                            continue;
                                        }
                                        DealEntity(ent, lines, pl, ext3d, lineT, lineB, yT, yB, entsIn);
                                    }
                                    foreach (var o in lines)
                                    {
                                        if (horizontalLine.Where(l => l.StartPoint == o.StartPoint && l.EndPoint == o.EndPoint).Count() > 0)
                                        {
                                            isY = true;
                                            break;
                                        }
                                    }
                                }
                                #endregion
                            }
                            else
                            {//最边上的单元格
                                isY = true;
                            }
                            TableCell tableCell = new TableCell();
                            tableCell.RowIndex   = y;
                            tableCell.ColumIndex = x;

                            if (dic.Values.Any(o => y >= o.TopRow && y <= o.BottomRow && x >= o.LeftColumn && x <= o.RightColumn))
                            {//之前统计过的合并单元格
                                tableCell.IsMergeRange = true;
                                tableCell.MergeRange   = null;
                                tablecellList.Add(tableCell);
                                continue;
                            }
                            if ((isX && isY))
                            {//非合并的单元格
                                tableCell.IsMergeRange = false;
                                tableCell.MergeRange   = null;
                                var tempdbtextList = dbtextList.Where(o => o.Key.X >= xList[x] && o.Key.X <= xList[x + 1] && o.Key.Y >= yList[y + 1] && o.Key.Y <= yList[y]).ToList();
                                foreach (var itemDBText1 in tempdbtextList.GroupBy(o => o.Key.Y).OrderByDescending(o => o.Key))
                                {
                                    foreach (var itemDBText2 in itemDBText1.OrderBy(o => o.Key.X))
                                    {
                                        itemDBText2.Value.ForEach((dbtext) => {
                                            tableCell.Value += dbtext.TextString;
                                        });
                                    }
                                    tableCell.Value += "\n";
                                }
                                var tempmtextList = mtextList.Where(o => o.Key.X >= xList[x] && o.Key.X <= xList[x + 1] && o.Key.Y >= yList[y + 1] && o.Key.Y <= yList[y]).ToList();
                                foreach (var itemMText1 in tempmtextList.GroupBy(o => o.Key.Y).OrderByDescending(o => o.Key))
                                {
                                    foreach (var itemMText2 in itemMText1.OrderBy(o => o.Key.X))
                                    {
                                        itemMText2.Value.ForEach((dbtext) => {
                                            tableCell.Value += dbtext.Text;
                                        });
                                    }
                                    tableCell.Value += "\n";
                                }
                                if (!string.IsNullOrEmpty(tableCell.Value))
                                {
                                    tableCell.Value.TrimEnd('\n');
                                }
                                tablecellList.Add(tableCell);
                            }
                            else
                            {//发生合并
                                MergeRange mr = new MergeRange()
                                {
                                    TopRow = y, LeftColumn = x, BottomRow = y, RightColumn = x
                                };
                                if (!isX)
                                {
                                    mr.RightColumn++;
                                    dic.addIsCover(point1, mr, true);
                                    SetMergeRangeX(mr, xList, x + 1, ed, pointx2, acSelFtr, verticalLine, dic, point1);
                                }
                                if (!isY)
                                {
                                    mr.BottomRow++;
                                    dic.addIsCover(point1, mr, true);
                                    SetMergeRangeY(mr, yList, y + 1, ed, pointy2, acSelFtr, horizontalLine, dic, point1);
                                }
                                tableCell.IsMergeRange = true;
                                tableCell.MergeRange   = mr;
                                int cNum           = (mr.RightColumn - mr.LeftColumn); //列跨度
                                int rNum           = (mr.BottomRow - mr.TopRow);       //行跨度
                                var tempdbtextList = dbtextList.Where(o => o.Key.X >= xList[x] && o.Key.X <= xList[x + 1 + cNum] && o.Key.Y >= yList[y + 1 + rNum] && o.Key.Y <= yList[y]).ToList();
                                foreach (var itemDBText1 in tempdbtextList.GroupBy(o => o.Key.Y).OrderByDescending(o => o.Key))
                                {
                                    foreach (var itemDBText2 in itemDBText1.OrderBy(o => o.Key.X))
                                    {
                                        itemDBText2.Value.ForEach((dbtext) => {
                                            tableCell.Value += dbtext.TextString;
                                        });
                                    }
                                    tableCell.Value += "\n";
                                }
                                var tempmtextList = mtextList.Where(o => o.Key.X >= xList[x] && o.Key.X <= xList[x + 1 + cNum] && o.Key.Y >= yList[y + 1 + rNum] && o.Key.Y <= yList[y]).ToList();
                                foreach (var itemMText1 in tempmtextList.GroupBy(o => o.Key.Y).OrderByDescending(o => o.Key))
                                {
                                    foreach (var itemMText2 in itemMText1.OrderBy(o => o.Key.X))
                                    {
                                        itemMText2.Value.ForEach((dbtext) => {
                                            tableCell.Value += dbtext.Text;
                                        });
                                    }
                                    tableCell.Value += "\n";
                                }
                                if (!string.IsNullOrEmpty(tableCell.Value))
                                {
                                    tableCell.Value.TrimEnd('\n');
                                }
                                tablecellList.Add(tableCell);
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                }
                if (tablecellList.Count <= 0)
                {
                    MessageBox.Show("未识别到表格,请扩大选择范围");
                    return;
                }
                #region 生成文件
                //创建表
                ISheet sh         = wb.CreateSheet("sheet" + sheetIndex);
                int    rowindex   = tablecellList.Last().RowIndex;
                int    columindex = tablecellList.Last().ColumIndex;
                for (int i = 0; i <= rowindex; i++)
                {
                    IRow row = sh.CreateRow(i);
                    for (int j = 0; j <= columindex; j++)
                    {
                        row.CreateCell(j);
                    }
                }
                foreach (var itemCell in tablecellList)
                {
                    if (!(itemCell.IsMergeRange && itemCell.MergeRange == null))
                    {
                        sh.GetRow(itemCell.RowIndex).GetCell(itemCell.ColumIndex).SetCellValue(itemCell.Value);
                    }
                    if (itemCell.MergeRange != null &&
                        itemCell.MergeRange.TopRow >= 0 &&
                        itemCell.MergeRange.BottomRow >= 0 &&
                        itemCell.MergeRange.LeftColumn >= 0 &&
                        itemCell.MergeRange.RightColumn >= 0)
                    {
                        sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(itemCell.MergeRange.TopRow, itemCell.MergeRange.BottomRow, itemCell.MergeRange.LeftColumn, itemCell.MergeRange.RightColumn));
                    }
                }

                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter           = "所有文件(*.xls)|*.xls|所有文件(*.xlsx)|*.xlsx"; //设置要选择的文件的类型
                fileDialog.RestoreDirectory = true;
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = fileDialog.FileName;//返回文件的完整路径
                    using (FileStream stm = System.IO.File.OpenWrite(file))
                    {
                        wb.Write(stm);
                    }
                    MessageBox.Show("导出数据成功");
                }
                #endregion
            }
        }
コード例 #8
0
        private void DrawCircle(Point3d ZoomPoint)
        {
            DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
            Document     acDoc   = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database     acCurDb = acDoc.Database;
            Entity       entity  = null;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                LayerTable       acLyrTbl     = acTrans.GetObject(acCurDb.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as LayerTable;
                string           strLayerName = "Zoom";
                LayerTableRecord acLyrTblRec  = new LayerTableRecord();

                if (acLyrTbl.Has(strLayerName) == false)
                {
                    acLyrTblRec.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 255, 255);
                    acLyrTblRec.Name  = strLayerName;
                    acLyrTbl.UpgradeOpen();
                    acLyrTbl.Add(acLyrTblRec);
                    acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
                }
                else
                {
                    TypedValue[] glq = new TypedValue[]
                    {
                        new TypedValue((int)DxfCode.LayerName, strLayerName)
                    };
                    SelectionFilter       sf = new SelectionFilter(glq);
                    PromptSelectionResult SS = acDoc.Editor.SelectAll(sf);
                    Autodesk.AutoCAD.EditorInput.SelectionSet SSet = SS.Value;
                    if (SSet != null)
                    {
                        foreach (ObjectId id in SSet.GetObjectIds())
                        {
                            AcadObject Adobj = null;
                            entity = (Entity)acTrans.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, true);
                            DBObject obj = (DBObject)entity;
                            Adobj = (AcadObject)obj.AcadObject;
                            Adobj.Delete();
                        }
                    }
                }

                BlockTable       acBlkTbl;
                BlockTableRecord acBlkTblRec;
                acBlkTbl    = acTrans.GetObject(acCurDb.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as BlockTable;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) as BlockTableRecord;

                Circle acCirc = new Circle();
                acCirc.SetDatabaseDefaults();
                acCirc.Center = ZoomPoint;
                acCirc.Radius = 0.05;
                acCirc.Layer  = strLayerName;

                acBlkTblRec.AppendEntity(acCirc);
                acTrans.AddNewlyCreatedDBObject(acCirc, true);

                ObjectIdCollection collection = new ObjectIdCollection();
                collection.Add(acCirc.ObjectId);

                Hatch hatch = new Hatch();
                hatch.HatchStyle = HatchStyle.Normal;
                hatch.Layer      = strLayerName;
                hatch.Color      = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 255);
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
                hatch.AppendLoop(HatchLoopTypes.Default, collection);
                hatch.EvaluateHatch(true);
                acBlkTblRec.AppendEntity(hatch);
                acTrans.AddNewlyCreatedDBObject(hatch, true);

                docLock.Dispose();
                acTrans.Commit();
            }
        }
コード例 #9
0
        static public void transformToTunnel()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;
            PromptSelectionResult res = ed.SelectAll();

            if (res.Status == PromptStatus.Error)
            {
                return;
            }

            Autodesk.AutoCAD.EditorInput.SelectionSet SS = res.Value;

            var tmpidarray = SS.GetObjectIds();
            var idArray    = SS.GetObjectIds();

            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
            List <DBTunnel> inList = new List <DBTunnel>();

            Utils.TransactionControl(() =>
            {
                foreach (ObjectId id in tmpidarray)
                {
                    Entity entity = (Entity)tm.GetObject(id, OpenMode.ForWrite, true);

                    if (!(entity is Line) && !(entity is Autodesk.AutoCAD.DatabaseServices.Polyline))
                    {
                        continue;
                    }

                    if (entity is Line)
                    {
                        Line line           = entity as Line;
                        DBTunnel dbTunnel   = new DBTunnel();
                        dbTunnel.BasePoints = new List <DBVertice>
                        {
                            new DBVertice(line.StartPoint), new DBVertice(line.EndPoint)
                        };
                        inList.Add(dbTunnel);
                        line.Erase();
                    }
                    else if (entity is Autodesk.AutoCAD.DatabaseServices.Polyline)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Polyline polyline =
                            entity as Autodesk.AutoCAD.DatabaseServices.Polyline;

                        DBTunnel dbTunnel   = new DBTunnel();
                        dbTunnel.BasePoints = new List <DBVertice>();

                        for (int i = 0; i < polyline.NumberOfVertices; i++)
                        {
                            dbTunnel.BasePoints.Add(new DBVertice(polyline.GetPoint3dAt(i)));
                        }

                        if (polyline.Closed == true)
                        {
                            dbTunnel.IsClosed = true;
                        }

                        inList.Add(dbTunnel);
                        polyline.Erase();
                    }
                }
            });
            var resList = DrawTunnel.StaticDrawSquareTunnel(inList);
        }