create drawing from template dwt

create drawing from template dwt

Anonymous
Not applicable
2,179 Views
5 Replies
Message 1 of 6

create drawing from template dwt

Anonymous
Not applicable

Hi!

 

I want to open dwg from template:

 

   DocumentCollection acDocMgr = Application.DocumentManager;

   Document acDoc;

 

   try

       {

              acDoc = acDocMgr.Add(FileNameTemplate);

      acDocMgr.MdiActiveDocument = acDoc

        }

   catch

       {

 

       }

 

But on the line "acDocMgr.MdiActiveDocument = acDoc" AutoCAD is hanging and in around 10 sec AutoCAD closes.

How does the new document from template set active (do current)?

0 Likes
Accepted solutions (1)
2,180 Views
5 Replies
Replies (5)
Message 2 of 6

Hallex
Advisor
Advisor

You have to use CommandFlags.Seesion and also

use LockDocument

Here is a quick sample:

       [CommandMethod("opdwt", CommandFlags.Session)]
        public void testOpenFromTemplate()
        {
            DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            try
            {
                using (DocumentLock doclock = doc.LockDocument())
                {

                    string tmpPath = "acad.dwt";
                    //change file name to save:
                    string fn = @"C:\Test\FromTemplate.dwg";

                    Document newdoc = dm.Add(tmpPath);

                    Database newdb = newdoc.Database;

                    using (DocumentLock newdoclock = newdoc.LockDocument())
                    {
                        using (Transaction newtr = newdoc.TransactionManager.StartTransaction())
                        {
                            dm.MdiActiveDocument = newdoc;

                            Editor newed = newdoc.Editor;

                            Point3d pt = newed.GetPoint("\nPick point: ").Value;

                            Line ln = new Line(pt, db.Extmax);

                            ln.ColorIndex = 1;

                            BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(newdb.CurrentSpaceId, OpenMode.ForWrite);

                            newbtr.AppendEntity(ln);

                            newtr.AddNewlyCreatedDBObject(ln, true);

                            newtr.Commit();

                            MessageBox.Show("Saved as: " + fn);
                        }
                    }
                    newdoc.CloseAndSave(fn);

                    // just to work further if you wanted be:
                    dm.Open(fn, false);

                    MessageBox.Show("You can try to do your rest job here");
                }
                doc.CloseAndDiscard();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                // do nothing
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 6

Anonymous
Not applicable
Accepted solution

Yeah, it works good. Thanks. It is done this way:

 

        [CommandMethod("calc", CommandFlags.Session)]
        public void doIt()
        {

           openFromTemplate();

 

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            // my calculation

        }

 

        [CommandMethod("opdwt", CommandFlags.Session)]
        public void openFromTemplate()
        {
            DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            try
            {
                using (DocumentLock doclock = doc.LockDocument())
                {

                    string tmpPath = "my template file dwt";
                    Document newdoc = dm.Add(tmpPath);

                    Database newdb = newdoc.Database;

                    using (DocumentLock newdoclock = newdoc.LockDocument())
                    {
                        using (Transaction newtr = newdoc.TransactionManager.StartTransaction())
                        {
                            dm.MdiActiveDocument = newdoc;
                        }                 

                    }

                }

            }

            catch {}

        }

0 Likes
Message 4 of 6

Hallex
Advisor
Advisor

Glad you get it worked, just one note though:

Don't use empty code block for CATCH statement,

better yet to display an error message in case you

have got it,

Cheers Smiley Happy

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 6

Anonymous
Not applicable

But it's not work for one situation.

 

In main function i call two methods - 1. load dwg through method readdwgfile 2. create drawing from template

 

In order to the second func works i add this expression above the main func

[CommandMethod("testSituation", CommandFlags.Session)]

 

But now the first method (readDwgFile) give me error - eNotOpenForWrite. If i remove this expression, the first method can work and the second won't work.

 

Test code:

 

[CommandMethod("testSituation", CommandFlags.Session)]
        public void testSit()
        {
            Database database = loadDWG(@"C:\...\myfile.dwg");

            openDrawingFromTemplate(@"\\tn...\A2_P.dwt");
        }
public Database loadDWG(string PathDWG)
        {
            Database newDWG = new Database(false, true);

            if (System.IO.File.Exists(PathDWG))
            {
                try
                {
                    newDWG.ReadDwgFile(PathDWG, FileOpenMode.OpenTryForReadShare, true, "");
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, "Error");
                }
                return newDWG;
            }
            else
            {                
                System.Windows.Forms.MessageBox.Show(String.Format("File {0} is not exist", PathDWG), "Error");
                return null;
            }

        }

  

Is such way exist which can help to solve this situation? 

0 Likes
Message 6 of 6

Anonymous
Not applicable

May be there are such flags which can help to work both methods?

0 Likes