Switch Layout using COM API

Switch Layout using COM API

nishad.dhapre
Contributor Contributor
977 Views
7 Replies
Message 1 of 8

Switch Layout using COM API

nishad.dhapre
Contributor
Contributor
Hello All,

 

I want to switch layout from model space layout to specific paper space layout(Layout1 or Layout2 and so on.....) using COM API (ActiveX). How can we achieve?

 

I am using c# language.

 

Thank you in advance.

0 Likes
Accepted solutions (2)
978 Views
7 Replies
Replies (7)
Message 2 of 8

marcin.sachs
Advocate
Advocate

Below code work for me:

        [CommandMethod("SwitchToLayout")]
        public void SwitchToLayout()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            var ed = doc.Editor;
            PromptStringOptions pStrOpts = new PromptStringOptions("\nType Layout Name: ");
            pStrOpts.AllowSpaces = true;
            PromptResult pStrRes = ed.GetString(pStrOpts);
            string layoutName = pStrRes.StringResult;
            if (pStrRes.Status == PromptStatus.OK)
            {
                LayoutManager lm = LayoutManager.Current;
                if (lm.LayoutExists(layoutName))
                {
                    LayoutManager.Current.CurrentLayout = layoutName;
                }
                else
                {
                    ed.WriteMessage("\nLayout cannot be found");
                }
            }
        }

 

0 Likes
Message 3 of 8

nishad.dhapre
Contributor
Contributor

Hello @marcin.sachs ,

 

Thank you but this code will work for creating command.

I am creating standalone application so I need to use COM API.
This code will not work for the same.

 

Regards,

Nishad Dhapre

 

 

0 Likes
Message 4 of 8

marcin.sachs
Advocate
Advocate

If I understand well you want to switch autocad layout from external application?
Firstly you need to get AutoCAD Instance: (below code for AutoCAD 2022)

public static AcadApplication GetAcadApplication(bool CreateNewIfNotLaunched)
        {
            AcadApplication acApp = null;
            const string strProgId = "AutoCAD.Application.24.1";
            try
            {
                acApp = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch
            {
                if (CreateNewIfNotLaunched == true)
                {
                    try
                    {
                        acApp = new AcadApplication();
                        
                    }
                    catch
                    {
                        // If an instance of AutoCAD is not created then message and exit
                        System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
                                                             " could not be created.");

                    }
                }

            }
            
            return acApp;
        }

then you can send command to Autocad:

public static void SendCommandToCAD(object sender, EventArgs e, string command)
        {
            AcadApplication CAD = GetAcadApplication(false);
            if (CAD != null)
            {
                // wait for AutoCAD is visible
                while (true)
                {
                    try { CAD.Visible = true; break; }
                    catch { }
                }

                var doc = CAD.ActiveDocument;
                doc.SendCommand(command + " ");
            }
        }

  

0 Likes
Message 5 of 8

nishad.dhapre
Contributor
Contributor

Yes, you are right.  I want to switch autocad layout from external application.
Yes I have done coding to get instance of AutoCAD. According to the solution provided I have to create separate COMMAND and then load DLL into AutoCAD.

 

Later, in stand alone application c# code, I need to send newly created command to AutoCAD but it will work only if DLL is loaded into AutoCAD.

Can we do without sending command and control by external application using COM API?

0 Likes
Message 6 of 8

marcin.sachs
Advocate
Advocate
Accepted solution

you can switch layout directly from external software also:

public static void SwitchLayout(object sender, EventArgs e)
        {
            AcadApplication CAD = GetAcadApplication(false);
            if (CAD != null)
            {
                // wait for AutoCAD is visible
                while (true)
                {
                    try { CAD.Visible = true; break; }
                    catch { }
                }

                AcadLayouts layouts = CAD.ActiveDocument.Layouts;

                foreach(AcadLayout layout in layouts)
                {
                    if(layout.Name == "Layout1")
                    {
                        CAD.ActiveDocument.ActiveLayout = layout;
                        break;
                    }
                }
            }
        }

 

0 Likes
Message 7 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can set the value of the CTAB system variable.

acadApp.ActiveDocument.SetVariable("CTAB", "Layout1");


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

nishad.dhapre
Contributor
Contributor

Hi @marcin.sachs  and @_gile ,

 

Thank you both of you, using both the method I can switch the layout. 

0 Likes