Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Opening new drawings and performing a function on each drawing

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
gotMorris
1058 Views, 11 Replies

Opening new drawings and performing a function on each drawing

I have a program that creates a drawing of various entities. I would like to be able to start a new drawing then call my BuildDrawing method for each part in my jobs list. The code below is what I use to open new drawings. The drawings open OK but all jobs are built in the original document leaving the newly opened drawings blank. Any ideas?

 

 

 

          string strTemplatePath = "acad.dwt";
DocumentCollection acDocMgr = Application.DocumentManager; foreach (var partDrawing in jobs) { Document acDoc = acDocMgr.Add(strTemplatePath); acDocMgr.MdiActiveDocument = acDoc; var myBuilder = new MyDrawingBuilder(partDrawing); myBuilder.BuildDrawing(); }

 

11 REPLIES 11
Message 2 of 12

Are you using CommandFlags.Session for your command?

Message 3 of 12

@BrentBurgess1980 thanks for the info . That fixed that issue. However I have another problem. When I add code to programmatically save the drawing it looks OK until you reopen the drawing. At that point I get a "The drawing file require recovery" dialog. If I "Save As" manually from the AutoCad Gui on those drawings I don't get the error anymore. If you are not sure about this I will mark your answer as my solution and re-post this new issue. Thanks.

 

Added code

acDoc.Database.SaveAs(fileName, true, DwgVersion.Current, acDoc.Database.SecurityParameters)
Message 4 of 12

How are you closing your drawings after you save?

 

Perhaps try this method

AcDoc.CloseAndSave(fileName);
Message 5 of 12

I tried to CloseAndSave but get the following error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".

Message 6 of 12

Are you able to post your code? There could be something in there that is possibly contributing to it.
Message 7 of 12

 

 The program I am working on has a lot of stuff going on so I added a test method that gives me similar results.Thanks

 

 

        [CommandMethod("TestAddDwgs", CommandFlags.Session)]
        public void TestAddDwgs()
        {
            try
            {

                    BuildDrawingTest(2); 

            }
            catch (Exception ex)
            {

                Application.ShowAlertDialog(ex.Message);
            }
            finally
            {
                
            }
        }

        private void BuildDrawingTest(int numberOfDrawings)
        {
            var testDir = @"C:\Users\my.name\Desktop\myDir";
            for (int i = 0; i < numberOfDrawings; i++)
            {
                Document acDoc = acDocMgr.Add(defaultAcadTemplate);
                acDocMgr.MdiActiveDocument = acDoc;
                AddCircle(acDoc);
                var path = Path.Combine(testDir, i + ".dwg");
                 //Has Errors
                acDoc.Database.SaveAs(path, true, DwgVersion.Current, acDoc.Database.SecurityParameters);
                 //Has Errors
                //acDoc.CloseAndSave(path);
            }

        }
        private static void AddCircle(Document document)
        {

                // Get the current document and database
            document.LockDocument();
            Database acCurDb = document.Database;

                // Start a transaction
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // Open the Block table for read
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                    OpenMode.ForRead) as BlockTable;

                    // Open the Block table record Model space for write
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;

                    // Create a circle that is at 2,3 with a radius of 4.25
                    using (Circle acCirc = new Circle())
                    {
                        acCirc.Center = new Point3d(0, 0, 0);
                        acCirc.Radius = 4.25;

                        // Add the new object to the block table record and the transaction
                        acBlkTblRec.AppendEntity(acCirc);
                        acTrans.AddNewlyCreatedDBObject(acCirc, true);
                    }

                    // Save the new object to the database
                    acTrans.Commit();
                 
            }
        }

 

Message 8 of 12

I tested the code a few times, but I wasn't able to replicate your issue.

Ran the code (with the Database.Saveas and the doc.CloseAndSave), and was able to open the files, no recovery required.

 

You get the same results with this code?

Message 9 of 12

Yes. I added a screen cast below.

 

Screencast will be displayed here after you click Post.

36c4bc7e-7b25-41a6-8614-6bddd1332e6e

 

Message 10 of 12
gotMorris
in reply to: gotMorris

 

 

 

Message 11 of 12

Thats strange that I can't replicate that. Before you close the drawing, have you tried an audit to see what is being affected?

Message 12 of 12

When I run the audit command it finds no errors. However it still has the error when I reopen the dwg saying "One error was found in the drawing file during open. Would you..."

 

 

 

 

error.png

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report