• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Member
    Posts: 7
    Registered: ‎10-17-2012
    Accepted Solution

    create drawing from template dwt

    221 Views, 5 Replies
    11-13-2012 07:25 PM

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

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,337
    Registered: ‎10-08-2008

    Re: create drawing from template dwt

    11-14-2012 02:24 AM 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
    Please use plain text.
    Active Member
    Posts: 7
    Registered: ‎10-17-2012

    Re: create drawing from template dwt

    11-14-2012 07:49 PM 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 {}

            }

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,337
    Registered: ‎10-08-2008

    Re: create drawing from template dwt

    11-15-2012 12:22 AM 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 :smileyhappy:

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Member
    Posts: 7
    Registered: ‎10-17-2012

    Re: create drawing from template dwt

    11-26-2012 12:38 AM 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? 

    Please use plain text.
    Active Member
    Posts: 7
    Registered: ‎10-17-2012

    Re: create drawing from template dwt

    11-26-2012 01:07 AM in reply to: Barabasishe

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

    Please use plain text.