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

create drawing from template dwt

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Barabasishe
1262 Views, 5 Replies

create drawing from template dwt

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)?

5 REPLIES 5
Message 2 of 6
Hallex
in reply to: Barabasishe

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
Message 3 of 6
Barabasishe
in reply to: Hallex

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 {}

        }

Message 4 of 6
Hallex
in reply to: Barabasishe

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
Message 5 of 6
Barabasishe
in reply to: Hallex

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? 

Message 6 of 6
Barabasishe
in reply to: Barabasishe

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

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost