open and close autocad api

83

open and close autocad api -

        [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());
            }
        }

Comments

Submit
0 Comments