how to open an AutoCAD file update and close with save using C#

how to open an AutoCAD file update and close with save using C#

kite15
Advocate Advocate
25,530 Views
26 Replies
Message 1 of 27

how to open an AutoCAD file update and close with save using C#

kite15
Advocate
Advocate

Hi,

I am trying to open some AutoCAD file from a Location. After that in the newly opened file added a circle as required. Finally, Close the drawing file with save the newly drawing files.

Actually an error message shown that "Drawing is busy (COMException was caught)".

I have search so many link where discussed about the same. Whereas I am confuse which one the concrete solution for this type of coding.

Here is my Code please help!!

        [CommandMethod("OPSV",CommandFlags.Session)]
        public static void OpenSaveDwgFiles()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                var path = @"C:\TestBack\";
                string dwgFlpath = string.Empty;
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] Files = d.GetFiles("*.dwg");
                //string str = "";
                foreach (FileInfo file in Files)
                {
                    DocumentCollection docMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
                    var fileName = Path.GetFileName(file.FullName);
                    dwgFlpath = path+fileName;
                    DocumentCollectionExtension.Open(docMgr, dwgFlpath,false);
                    using (DocumentLock LckDoc = doc.LockDocument())
                    {
                        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();
                        } 
                    }
                    //doc.CloseAndDiscard();
                    doc.CloseAndSave(dwgFlpath.Substring(0,dwgFlpath.Length-4));
                }

            }
            catch (System.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
        }

Thanks

 

0 Likes
Accepted solutions (1)
25,531 Views
26 Replies
Replies (26)
Message 21 of 27

SanjoyNath
Advocate
Advocate

Dear Experts,

 

We dont know why even after so many years they are not giving record c sharp macro , record other(vbs or scr ) macros as .cs files or .vbs files 

 

 

Note

the Activity recorder is dumb concept now. People are habituated to create scripts on their mobiles and on social networking. They expect these (record csharp macros) as most fundamental requirement as command line. Word, Excel , powerpoint has made users too much greedy for these features        (Please understand)

Sanjoy Nath
BIM Manager And Digital Lead (Structures Online)
BOOST, AR , VR ,EPM,IFC API,PDF API , CAD API ,Revit API , Advance Steel API
Founder of Geometrifying Trigonometry(C)
0 Likes
Message 22 of 27

k005
Advisor
Advisor
Hello there

Can preview be included in this code?

How can we add these codes?

Thanks. 


Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
db.ThumbnailBitmap = doc.CapturePreviewImage(320, 240);
0 Likes
Message 23 of 27

k005
Advisor
Advisor

 

@_gile 

Message 3 of 22


//Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
//Database db = Application.DocumentManager.MdiActiveDocument.Database;
//db.ThumbnailBitmap = doc.CapturePreviewImage(320, 240);
0 Likes
Message 24 of 27

k005
Advisor
Advisor
@_gile

Message 3 of 22


//Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
//Database db = Application.DocumentManager.MdiActiveDocument.Database;
//db.ThumbnailBitmap = doc.CapturePreviewImage(320, 240);

?
0 Likes
Message 25 of 27

khugt60
Explorer
Explorer

Hi Sir

I want to change the input (Link) that's the input from the user.

What's the class or Method for this?

Thank you!

khugt60_0-1630611739686.png

 

0 Likes
Message 26 of 27

Alexander.Rivilis
Mentor
Mentor

@khugt60 wrote:

Hi Sir

I want to change the input (Link) that's the input from the user.

What's the class or Method for this?

Thank you!

 

 


For example, Editor.GetString() 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 27 of 27

Alexander.Rivilis
Mentor
Mentor

Also read this article: https://www.keanw.com/2009/08/allowing-a-user-to-select-from-multiple-file-formats-inside-autocad-us...

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes