multiple layout export to separate CAD files using c#

multiple layout export to separate CAD files using c#

Ahmed_204
Enthusiast Enthusiast
3,641 Views
8 Replies
Message 1 of 9

multiple layout export to separate CAD files using c#

Ahmed_204
Enthusiast
Enthusiast

Hi All,

I am trying to run the below code which I got from theswamp.org website but its not working, kindly can anyone  highlight the issue and try to solve it.

EXPORTLAYOUTS command example (theswamp.org)

[CommandMethod("EXPORTLAYOUTS")]
public static void ExportLayoutsCommand()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var editor = doc.Editor;
try
{

// check if drawing saved or not
if ((short)Application.GetSystemVariable("DWGTITLED") == 0)
{
editor.WriteMessage("\nCommand cannot be used on an unnamed drawing");
return;
}
string format =Path.Combine(Path.GetDirectoryName(doc.Name),Path.GetFileNameWithoutExtension(doc.Name))+ "_{0}.dwg";
editor.WriteMessage(format);
string[] names = null;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
// Get the localized name of the model tab:
BlockTableRecord btr = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForRead) as BlockTableRecord;
Layout layout = btr.LayoutId.GetObject(OpenMode.ForRead) as Layout;
string model = layout.LayoutName;
// Open the Layout dictionary:
IDictionary layouts = db.LayoutDictionaryId.GetObject(OpenMode.ForRead) as IDictionary;
// Get the names and ids of all paper space layouts into a list:
names = layouts.Keys.Cast<string>().Where(name => name != model).ToArray();
tr.Commit();
}
int cmdecho = 0;
#if DEBUG
cmdecho = 1;
#endif
using (new ManagedSystemVariable("CMDECHO", cmdecho))
using (new ManagedSystemVariable("CMDDIA", 0))
using (new ManagedSystemVariable("FILEDIA", 0))
using (new ManagedSystemVariable("CTAB"))
{
foreach (string name in names)
{
string filename = string.Format(format, name);
editor.WriteMessage("\nExporting {0}\n", filename);
Application.SetSystemVariable("CTAB", name);
editor.Command("._EXPORTLAYOUT", filename);

}
}
}

public static class EditorInputExtensionMethods
{
public static PromptStatus Command(this Editor editor, params object[] args)
{
if (editor == null)throw new ArgumentNullException("editor");
return runCommand(editor, args);
}
static Func<Editor, object[], PromptStatus> runCommand = GenerateRunCommand();
static Func<Editor, object[], PromptStatus> GenerateRunCommand()
{
MethodInfo method = typeof(Editor).GetMethod("RunCommand",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var instance = Expression.Parameter(typeof(Editor), "instance");
var args = Expression.Parameter(typeof(object[]), "args");
return Expression.Lambda<Func<Editor, object[], PromptStatus>>(Expression.Call(instance, method, args), instance, args).Compile();
}
}
/// <summary>
/// Automates saving/changing/restoring system variables
/// </summary>
public class ManagedSystemVariable : IDisposable
{
string name /*= null*/;
object oldval /*= null*/;
public ManagedSystemVariable(string name, object value): this(name)
{
Application.SetSystemVariable(name, value);
}
public ManagedSystemVariable(string name)
{
if (string.IsNullOrWhiteSpace(name))throw new ArgumentException("name");
this.name = name;
this.oldval = Application.GetSystemVariable(name);
}
public void Dispose()
{
if (oldval != null)
{
object temp = oldval;
oldval = null;
Application.SetSystemVariable(name, temp);
}
}
}
catch (System.Exception ex)
{
#if DEBUG
editor.WriteMessage(ex.ToString());
#else
throw ex;
#endif
}
}

 

 

 

 

0 Likes
Accepted solutions (1)
3,642 Views
8 Replies
Replies (8)
Message 2 of 9

essam-salah
Advisor
Advisor
Accepted solution

Hi @Ahmed_204 

i tested EXPORTLAYOUTS2 and it works fine, what errors did you get?

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

// requires a reference to AcExportLayout.dll:
using AcExportLayout = Autodesk.AutoCAD.ExportLayout;

namespace ClassLibrary1
{
    public static class ExportLayoutsCommands
    {
        /// <summary>
        /// Automates the EXPORTLAYOUT command to export 
        /// all paper space layouts to .DWG files.
        /// 
        /// In this example, we export each layout to
        /// a drawing file in the same location as the
        /// current drawing, wherein each file has the
        /// name "<dwgname>_<layoutname>.dwg".
        /// 
        /// This is not a functionally-complete example:
        /// 
        /// No checking is done to see if any of the 
        /// files already exist, and existing files 
        /// are overwritten without warning or error.
        /// 
        /// No checking is done to detect if an existing
        /// file exists and is in-use by another user, or 
        /// cannot be overwritten for any other reason.
        /// 
        /// No checking is done to ensure that the user
        /// has sufficient rights to write files in the
        /// target location.
        /// 
        /// You can and should deal with any or all of
        /// the above as per your own requirements.
        /// 
        /// </summary>

        

        /// <summary>
        /// 
        /// Doesn't use the command line, requires AutoCAD R12 
        /// or later and a reference to AcExportLayout.dll:
        /// 
        /// This version can be used from the application context, 
        /// which can make it easier to use in a batch process that
        /// exports layouts of many files.
        /// 
        /// The example also shows how to use the AcExportLayout
        /// component to export a layout to an in-memory Database
        /// without creating a drawing file.
        /// 
        /// </summary>

        [CommandMethod("EXPORTLAYOUTS2", CommandFlags.Session)]
        public static void ExportLayouts2()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var editor = doc.Editor;
            try
            {
                if ((short)Application.GetSystemVariable("DWGTITLED") == 0)
                {
                    editor.WriteMessage(
                       "\nCommand cannot be used on an unnamed drawing"
                    );
                    return;
                }
                string format =
                   Path.Combine(
                      Path.GetDirectoryName(doc.Name),
                      Path.GetFileNameWithoutExtension(doc.Name))
                   + "_{0}.dwg";

                Dictionary<string, ObjectId> layouts = null;

                using (doc.LockDocument())
                {
                    using (Transaction tr = doc.TransactionManager.StartTransaction())
                    {
                        // Get the localized name of the model tab:
                        BlockTableRecord btr = (BlockTableRecord)
                           SymbolUtilityServices.GetBlockModelSpaceId(db)
                              .GetObject(OpenMode.ForRead);
                        Layout layout = (Layout)
                           btr.LayoutId.GetObject(OpenMode.ForRead);
                        string model = layout.LayoutName;
                        // Open the Layout dictionary:
                        IDictionary layoutDictionary = (IDictionary)
                           db.LayoutDictionaryId.GetObject(OpenMode.ForRead);
                        // Get the names and ids of all paper space layouts 
                        // into a Dictionary<string,ObjectId>:
                        layouts = layoutDictionary.Cast<DictionaryEntry>()
                           .Where(e => ((string)e.Key) != model)
                           .ToDictionary(
                              e => (string)e.Key,
                              e => (ObjectId)e.Value);

                        tr.Commit();
                    }

                    /// Get the export layout 'engine':
                    Autodesk.AutoCAD.ExportLayout.Engine engine =
                       Autodesk.AutoCAD.ExportLayout.Engine.Instance();

                    using (new ManagedSystemVariable("CTAB"))
                    {
                        foreach (var entry in layouts)
                        {
                            string filename = string.Format(format, entry.Key);
                            editor.WriteMessage("\nExporting {0} => {1}\n", entry.Key, filename);
                            Application.SetSystemVariable("CTAB", entry.Key);
                            using (Database database = engine.ExportLayout(entry.Value))
                            {
                                if (engine.EngineStatus == AcExportLayout.ErrorStatus.Succeeded)
                                {
                                    database.SaveAs(filename, DwgVersion.Newest);
                                }
                                else
                                {
                                    editor.WriteMessage("\nExportLayout failed: ",
                                       engine.EngineStatus.ToString());
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
            editor.WriteMessage( ex.ToString() );
#else
                throw ex;
#endif
            }
        }
    }

    /// <summary>
    /// Automates saving/changing/restoring system variables
    /// </summary>
    public class ManagedSystemVariable : IDisposable
    {
        string name = null;
        object oldval = null;

        public ManagedSystemVariable(string name, object value)
           : this(name)
        {
            Application.SetSystemVariable(name, value);
        }

        public ManagedSystemVariable(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentException("name");
            this.name = name;
            this.oldval = Application.GetSystemVariable(name);
        }

        public void Dispose()
        {
            if (oldval != null)
            {
                object temp = oldval;
                oldval = null;
                Application.SetSystemVariable(name, temp);
            }
        }
    }

}

 

0 Likes
Message 3 of 9

essam-salah
Advisor
Advisor

@Ahmed_204 

you have to reference AcExportLayout.dll you found it in the same location of acdbmgd.dll.

0 Likes
Message 4 of 9

Ahmed_204
Enthusiast
Enthusiast

Hi @essam-salah ,

After I referenced AcExportLayout.dll , Exportlayout2 worked fine but my issue now while trying to use EXPORTLAYOUTS I am receiving the below error.

 

._EXPORTLAYOUT Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
at Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable(String name, Object value)
at Export_Layout.ManagedSystemVariable.Dispose() in H:\AutoCAD Building Add-ins with C#_LinkedIn Course\My Autocad Projects\Export Layouts\Export_Layout\myCommands.cs:line 356
at Export_Layout.MyCommands.ExportLayoutsCommand() in H:\AutoCAD Building Add-ins with C#_LinkedIn Course\My Autocad Projects\Export Layouts\Export_Layout\myCommands.cs:line 141

 

Thanks for your support.

0 Likes
Message 5 of 9

essam-salah
Advisor
Advisor

@Ahmed_204 

yes it gives the same error, and the problem is in line editor.Command("._EXPORTLAYOUT", filename);

and i think it's a common problem in autocad sending comman with parameters.

0 Likes
Message 6 of 9

Ahmed_204
Enthusiast
Enthusiast

Hi @_gile ,

 

Do you know any possible solution for the above error ?

 

Thanks for your support.

0 Likes
Message 7 of 9

Chuong.Ho
Advocate
Advocate

 

Any has ideal for this error ? Thanks you

ChuongHo_0-1630922157289.png

 

Chuong Ho

EESignature

0 Likes
Message 8 of 9

Anonymous
Not applicable

@Ahmed_204 wrote:

Hi All,

I am trying to run the below code which I got from theswamp.org website but its not working, kindly can anyone  highlight the issue and try to 밤알바 solve it.

EXPORTLAYOUTS command example (theswamp.org)

[CommandMethod("EXPORTLAYOUTS")]
public static void ExportLayoutsCommand()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var editor = doc.Editor;
try
{

// check if drawing saved or not
if ((short)Application.GetSystemVariable("DWGTITLED") == 0)
{
editor.WriteMessage("\nCommand cannot be used on an unnamed drawing");
return;
}
string format =Path.Combine(Path.GetDirectoryName(doc.Name),Path.GetFileNameWithoutExtension(doc.Name))+ "_{0}.dwg";
editor.WriteMessage(format);
string[] names = null;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
// Get the localized name of the model tab:
BlockTableRecord btr = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForRead) as BlockTableRecord;
Layout layout = btr.LayoutId.GetObject(OpenMode.ForRead) as Layout;
string model = layout.LayoutName;
// Open the Layout dictionary:
IDictionary layouts = db.LayoutDictionaryId.GetObject(OpenMode.ForRead) as IDictionary;
// Get the names and ids of all paper space layouts into a list:
names = layouts.Keys.Cast<string>().Where(name => name != model).ToArray();
tr.Commit();
}
int cmdecho = 0;
#if DEBUG
cmdecho = 1;
#endif
using (new ManagedSystemVariable("CMDECHO", cmdecho))
using (new ManagedSystemVariable("CMDDIA", 0))
using (new ManagedSystemVariable("FILEDIA", 0))
using (new ManagedSystemVariable("CTAB"))
{
foreach (string name in names)
{
string filename = string.Format(format, name);
editor.WriteMessage("\nExporting {0}\n", filename);
Application.SetSystemVariable("CTAB", name);
editor.Command("._EXPORTLAYOUT", filename);

}
}
}

public static class EditorInputExtensionMethods
{
public static PromptStatus Command(this Editor editor, params object[] args)
{
if (editor == null)throw new ArgumentNullException("editor");
return runCommand(editor, args);
}
static Func<Editor, object[], PromptStatus> runCommand = GenerateRunCommand();
static Func<Editor, object[], PromptStatus> GenerateRunCommand()
{
MethodInfo method = typeof(Editor).GetMethod("RunCommand",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var instance = Expression.Parameter(typeof(Editor), "instance");
var args = Expression.Parameter(typeof(object[]), "args");
return Expression.Lambda<Func<Editor, object[], PromptStatus>>(Expression.Call(instance, method, args), instance, args).Compile();
}
}
/// <summary>
/// Automates saving/changing/restoring system variables
/// </summary>
public class ManagedSystemVariable : IDisposable
{
string name /*= null*/;
object oldval /*= null*/;
public ManagedSystemVariable(string name, object value): this(name)
{
Application.SetSystemVariable(name, value);
}
public ManagedSystemVariable(string name)
{
if (string.IsNullOrWhiteSpace(name))throw new ArgumentException("name");
this.name = name;
this.oldval = Application.GetSystemVariable(name);
}
public void Dispose()
{
if (oldval != null)
{
object temp = oldval;
oldval = null;
Application.SetSystemVariable(name, temp);
}
}
}
catch (System.Exception ex)
{
#if DEBUG
editor.WriteMessage(ex.ToString());
#else
throw ex;
#endif
}
}

 

 

 

 


I was facing the same issue could you please let me know which solution you have found and what is the best one?actually i also facing the same issue.  

0 Likes
Message 9 of 9

Chuong.Ho
Advocate
Advocate

I have spent several weeks researching it and there is no solution to this problem

Chuong Ho

EESignature

0 Likes