📜  打开和关闭 autocad api - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:50.108000             🧑  作者: Mango

代码示例1
[CommandMethod("OPSV")]
        public static void OpenSaveDwgFiles()
        {
            try
            {
                var path = @"C:\TestBack\";
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] Files = d.GetFiles("*.dwg");
                foreach (FileInfo file in Files)
                {
                    var fileName = Path.GetFileName(file.FullName);
                    string dwgFlpath = path + fileName;
                    using (Database db = new Database(false, true))
                    {
                        db.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                            using (Circle crcl = new Circle())
                            {
                                crcl.Center = new Point3d(1, 1, 0);
                                crcl.Radius = 2;
                                btr.AppendEntity(crcl);
                                tr.AddNewlyCreatedDBObject(crcl, true);
                            }
                            tr.Commit();
                        }
                        db.SaveAs(dwgFlpath, DwgVersion.Current);
                    }
                }
                Application.ShowAlertDialog("All files processed");
            }
            catch (System.Exception ex)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
        }