- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
}
}
Solved! Go to Solution.