.NET

Reply
Active Member
lukehuang
Posts: 9
Registered: ‎07-14-2011
Message 1 of 4 (964 Views)

Help! Create new DWG from a template file (DWT) and Save

964 Views, 3 Replies
07-14-2011 11:40 AM

Hello I am "new" programmer to AutoCAD ObjectARX.net (I am a .Net programmer).

Current development env:  AutoCAD 2012, VS 2010, ObjectARX.Net (2012)

 

My target:  in AutoCAD pops a dialog, select filename (related to our internal projects) string, then it will create a DWG file from a template DWT file. Then some operation like insert elements into the DWG file and then SAVE (with the filename assigned).

 

Here is the code I currently implemented:

***************************************************************************************

    class DWGWritter

    {

        const string TemplateFile = @"C:\temp\MyTemplate.dwt";

 

        public DWGWritter( string filename )

        {

 

            DocumentCollection docMgr = Application.DocumentManager;

            docMgr.DocumentActivationEnabled = true;

 

            Document newdoc = docMgr.Add(TemplateFile);  //use to open new dwg with custom template file

 

            docMgr.MdiActiveDocument = newdoc;  //set active document (?)

 

            Database db0 = Application.DocumentManager.MdiActiveDocument.Database;

 

            DocumentLock lc = Application.DocumentManager.MdiActiveDocument.LockDocument();

 

            using (lc)

            {

                #region CUSTOM OBJECTS

                      //some code to write contents into the dwg file

                #endregion

            }

           

            

            //db0.Save();             // #1

            //db0.SaveAs(filename, DwgVersion.Current);  // #2

            //Application.DocumentManager.MdiActiveDocument.CloseAndSave(filename);  // #3

            //Application.DocumentManager.CloseAll();  // #4

 

        }

}

 

*************************************************************************************

 

Well it doesn't work on either of #1-4

#1 give out "eFileInternalErr" exception, which I found the answer - you can't call save() if it's open from exist file

#2 works ... but it just save the contents we write to the database but won't save template file contents, ie. we have put in layers inside the template file but they doesn't save to the new file

#3 give out "drawing is busy" error, no clue why it's "busy" ...

#4 give out same error as #3

 

 

Anyone can tell me what I did wrong? or my solution isn't get the right way to do so? Thanks!



Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 2 of 4 (945 Views)

Re: Help! Create new DWG from a template file (DWT) and Save

07-14-2011 04:01 PM in reply to: lukehuang

There should be more examples here and at the link below just enter SaveAs in the search box.

 

Hard to tell with out all the code or a small example that reproduces the error.

 

#2 error explanation is a little confusing maybe a better explanation will help

 

But here is a simple example

 

The poo.dwt will fail and will end up using the default template just did that because was quicker.

 

        [CommandMethod("SaveNewDwg")]
        public void SaveNewDwg()
        {
            DocumentCollection docs = Application.DocumentManager;

            using (Document doc = docs.Add("poo.dwt"))
            {
                using (DocumentLock docLoc = doc.LockDocument())
                using (Transaction trx = doc.Database.TransactionManager.StartTransaction())
                {
                    BlockTableRecord modelBtr =
                        (BlockTableRecord)trx.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(doc.Database), OpenMode.ForWrite);

                    Circle circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
                    modelBtr.AppendEntity(circle);
                    trx.AddNewlyCreatedDBObject(circle, true);
                    trx.Commit();
                    doc.Database.SaveAs(@"C:\Test\Whatever.dwg", DwgVersion.Current);
                }

                doc.CloseAndDiscard();
            }
            
        }

 

 

You can also find your answers @ TheSwamp
Active Member
lukehuang
Posts: 9
Registered: ‎07-14-2011
Message 3 of 4 (938 Views)

Re: Help! Create new DWG from a template file (DWT) and Save

07-14-2011 05:20 PM in reply to: Jeffrey_H

Thank you Jeffery. I copy your code sample and it works within my code framework! I'll figure it out how you coding it.

 

One piece of information I didn't provide you is that I actually create a windows form to host the method call (button click). By searching other posts it seems a Modal dialog had some issues with application context thus bring some problem when close/save an open document.

Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 4 of 4 (932 Views)

Re: Help! Create new DWG from a template file (DWT) and Save

07-14-2011 06:56 PM in reply to: lukehuang

Notice how this sets the Active Document the newly created one.

 

I need to use the ComandFlags.Session.

 

So I start a command in one drawing then change the active document, is maybe a simple way of looking at it. I would need to read up to make sure if that is a good way to look ar it.

When you save a drawing with .net it does crate a thumbnail.

Added one way of doing it

 

        [CommandMethod("SaveNewDwg",CommandFlags.Session)]
        public void SaveNewDwg()
        {
            DocumentCollection docs = Application.DocumentManager;

            using (Document doc = docs.Add("poo.dwt"))
            {
                Application.DocumentManager.MdiActiveDocument = doc;
                using (DocumentLock docLoc = doc.LockDocument())
                using (Transaction trx = doc.Database.TransactionManager.StartTransaction())
                {

                    BlockTableRecord modelBtr =
                        (BlockTableRecord)trx.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(doc.Database), OpenMode.ForWrite);

                    Circle circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
                    modelBtr.AppendEntity(circle);
                    trx.AddNewlyCreatedDBObject(circle, true);                   
                    trx.Commit();
                    doc.Database.ThumbnailBitmap = doc.CapturePreviewImage(500, 500);
                    doc.Database.SaveAs(@"C:\Test\Whatever.dwg", DwgVersion.Current);
                }

                doc.CloseAndDiscard();
            }
            
        }

 

 

 

 

On the same topic

http://www.theswamp.org/index.php?topic=38587.msg436962#msg436962

You can also find your answers @ TheSwamp
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.