.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

26 REPLIES 26
SOLVED
Reply
Message 1 of 27
kite15
21276 Views, 26 Replies

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

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

 

26 REPLIES 26
Message 21 of 27
SanjoyNath
in reply to: d.j.gray

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)
Message 22 of 27
k005
in reply to: _gile

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);
Message 23 of 27
k005
in reply to: kite15

 

@_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);
Message 24 of 27
k005
in reply to: _gile

@_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);

?
Message 25 of 27
khugt60
in reply to: _gile

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

 

Message 26 of 27
Alexander.Rivilis
in reply to: khugt60


@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

Message 27 of 27
Alexander.Rivilis
in reply to: khugt60

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report