4

Im trying to modify a .dwg file and overwrite the .dwg file once done, all that without opening it in autocad. I've read about RealDWG but the goal here is not to be in a standalone mode. the autocad plugin runs from autocad and needs to modify and write to some .dwg files that are not opened in the current project. So far I create a Database object and Read the .dwg file into it but im having errors when I want to rewrite into it. Here is the code its in C#,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace ModifyClosedDWG
{
    public class DWGEditor
    {
        private static string filepath = @"E:\...\DrawingTest.dwg";

        [CommandMethod("modifyDWG")]
        public void addTextToDWG()
        {
            Database workingDB = HostApplicationServices.WorkingDatabase;
            Editor ed = doc.Editor;

            Database db = new Database(false, true);

            try
            {
                db.ReadDwgFile(filepath, System.IO.FileShare.ReadWrite, false, "");                    
                db.CloseInput(true);
                HostApplicationServices.WorkingDatabase = db;
            }
            catch (Autodesk.AutoCAD.Runtime.Exception  e)
            {
                ed.WriteMessage("\nUnable to read drawing file : " + e.StackTrace);
                return;
            }

            using (BlockTable bt = db.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
            {
                using (BlockTableRecord btr = bt[BlockTableRecord.ModelSpace].Open(OpenMode.ForWrite) as BlockTableRecord)
                {
                    MText objText = new MText();
                    objText.SetDatabaseDefaults();
                    objText.Location = new Autodesk.AutoCAD.Geometry.Point3d(2, 2, 0);
                    objText.Contents = "added text in a closed .dwg file!";
                    objText.TextStyleId = db.Textstyle;
                    btr.AppendEntity(objText);
                }                    
            }

            HostApplicationServices.WorkingDatabase = workingDB;
            db.SaveAs(filepath, DwgVersion.Current);
        }
    }
}

the error output :

Autodesk.AutoCAD.Runtime.Exception: eWasOpenForWrite
   at Autodesk.AutoCAD.DatabaseServices.Database.SaveAs(String fileName, DwgVersion version)
   at ModifyClosedDWG.DWGEditor.addTextToDWG() in E:\PROJETS\ELI017\07 In Works documents\VisualC#Projects\ModifyClosedDWG\ModifyClosedDWG\DWGEditor.cs:line 64
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

I found the solution so I'll put the new code here...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace ModifyClosedDWG
{
    public class DWGEditor
    {
        private static string filepath = @"E:\PROJETS\ELI017\07 In Works documents\dwg\DrawingTest.dwg";

        [CommandMethod("modifyDWG")]
        public void addTextToDWG()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database workingDB = HostApplicationServices.WorkingDatabase;           
            Database db = new Database(false, true);

            try
            {
                db.ReadDwgFile(filepath, System.IO.FileShare.ReadWrite, false, "");   
                db.CloseInput(true);
                HostApplicationServices.WorkingDatabase = db;
            }
            catch (Autodesk.AutoCAD.Runtime.Exception  e)
            {
                ed.WriteMessage("\nUnable to open .dwg file : " + e.StackTrace);
                return;
            }

            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;
                    MText objText = new MText();
                    objText.SetDatabaseDefaults();
                    objText.Location = new Autodesk.AutoCAD.Geometry.Point3d(2, 2, 0);
                    objText.Contents = "added text in a closed .dwg file!";
                    objText.TextStyleId = db.Textstyle;
                    btr.AppendEntity(objText);
                    tr.AddNewlyCreatedDBObject(objText, true);
                    tr.Commit();
            }
            HostApplicationServices.WorkingDatabase = workingDB;
            db.SaveAs(filepath, DwgVersion.Current);              
        }
    }
}
malcolm waldron
  • 667
  • 6
  • 6

0 Answers0