Create Layout 2016 C#

Create Layout 2016 C#

Anonymous
Not applicable
3,333 Views
3 Replies
Message 1 of 4

Create Layout 2016 C#

Anonymous
Not applicable

Hi All,

can someone tell me what I am missing?

I am trying to create a new layout with the following code.

please assume I have entered a valid layout name and that that layout name is not already in use.

 

the issue is, it seems to work, the layout is created in the current AutoCAD doc however it does not seem to be valid, the layout cannot be activated and disappears if I activate any other layout.

clearly, i am doing something wrong, any ideas

 

 

[CommandMethod("JT_CREATELAYOUT")]
        public void JT_CREATELAYOUT()
        {
            Editor ed = ACDAS.Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    DBDictionary layouts = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite);

                    PromptStringOptions pso = new PromptStringOptions("\nEnter the layout name: ");

                    PromptResult PR = ed.GetString(pso);
                    string name = PR.StringResult;

                    ObjectId layoutId = LayoutManager.Current.CreateLayout(name);

                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: " + ex.Message);
            }
        }

 

0 Likes
Accepted solutions (2)
3,334 Views
3 Replies
Replies (3)
Message 2 of 4

kerry_w_brown
Advisor
Advisor
Accepted solution

 

@Anonymous,

 

You are creating in the current document so you may be better served using the LayoutManager Class rather than work through the Dictionary.

 

Refer :

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-5FA86EF3-DEFD-4256-BB1C-56DAC32BD868-htm.html

 

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You shouldn't open the layout dictionary before creating the new layout with the LayoutManager.

 

And you don't need to start a transaction, the layout manager takes care of that.

 

        [CommandMethod("JT_CREATELAYOUT")]
        public void JT_CREATELAYOUT()
        {
            Editor ed = ACDAS.Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            PromptStringOptions pso = new PromptStringOptions("\nEnter the layout name: ");
            PromptResult PR = ed.GetString(pso);
            string name = PR.StringResult;

            ObjectId layoutId = LayoutManager.Current.CreateLayout(name);
            LayoutManager.Current.CurrentLayout = name;
        }

 

Oops!.. Kerry was faster...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

Anonymous
Not applicable

Thank you very much for your help

0 Likes