示例#1
0
        public void ExportBref(FileExportOption exportmode)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            TypedValue[]          filter = { new TypedValue(0, "INSERT") };
            PromptSelectionResult psr    = ed.GetSelection(new SelectionFilter(filter));

            if (psr.Status != PromptStatus.OK)
            {
                return;
            }
            using (DocumentLock acdocLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        switch (exportmode)
                        {
                        case FileExportOption.Xls:
                            PromptPointResult ppr = ed.GetPoint("\nInsertion point: ");
                            if (ppr.Status != PromptStatus.OK)
                            {
                                return;
                            }
                            System.Data.DataTable dataTable = psr.Value.GetObjectIds()
                                                              .Select(id => new BlockAttribute(id.GetObject <BlockReference>()))
                                                              .ToDataTable("Extraction");
                            Table tbl = dataTable.ToAcadTable(9.0, 40.0);
                            tbl.Position = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                            BlockTableRecord btr = db.CurrentSpaceId.GetObject <BlockTableRecord>(OpenMode.ForWrite);
                            btr.AppendEntity(tbl);
                            tr.AddNewlyCreatedDBObject(tbl, true);
                            try
                            {
                                string filename = (string)AcAp.GetSystemVariable("dwgprefix") + "Extraction.xls";
                                string sheet    = "DataUnit";
                                dataTable.WriteXls(filename, sheet, true);
                                //blkdt = dataTable;
                            }
                            catch
                            {
                                AcAp.ShowAlertDialog("Failed to open Excel");
                            }
                            break;

                        case FileExportOption.CSV:
                            break;

                        case FileExportOption.Data:
                            blockdt = psr.Value.GetObjectIds()
                                      .Select(id => new BlockAttribute(id.GetObject <BlockReference>()))
                                      .ToDataTable("Data Unit");
                            break;
                        }
                    }
                    catch (System.Exception exp)
                    {
                        AcAp.ShowAlertDialog(exp.Message);
                    }
                    tr.Commit();
                }
            }
        }