2016-12-06 5 views
2

dgnを一括インポートする必要があります。私が使用してAPIの最新のコマンドオプションを使用してこの操作を行うことができます。セッション内でAutoCADコマンドを実行するContext

Application.DocumentManager.MdiActiveDocument.Editor.Command("-dgnimport", currentdgnname, "", "Master", "Standard"); 

しかし、問題は私も描画(および保存されたファイル名)の保存を制御する必要があるということです。私はこのようなセッションのコンテキストで私のコマンドを使用せずにこれを行う方法が表示されない:

const string DGNIMPORTBATCHname = "DGNIMPORTBATCH"; 
[CommandMethod(DGNIMPORTBATCHname, CommandFlags.Session)] 

Editor.Command方法をただしのみ、ドキュメントのコンテキストです。

ドキュメントロックの追加は機能しません。セッションコマンド中にドキュメントコンテキストで実行中のコードに切り替える方法はありますか?

**編集:Activationcontextとサンプルコード:

Application.DocumentManager.ExecuteInApplicationContext(); 
Application.DocumentManager.ExecuteInCommandContextAsync(); 

EDIT

const string DGNIMPORTBATCHname = "DGNIMPORTBATCH"; 
[CommandMethod(DGNIMPORTBATCHname)] 
public static void RunDGNIMPORTBATCH() 
{ 
    Document doc = Application.DocumentManager.MdiActiveDocument; 
    Editor ed = doc.Editor; 

    OpenFileDialog ofd = new OpenFileDialog("Convert DGNs", "", "dgn", "", OpenFileDialog.OpenFileDialogFlags.AllowMultiple); 
    System.Windows.Forms.DialogResult dr = ofd.ShowDialog(); 
    if (dr != System.Windows.Forms.DialogResult.OK) 
    return; 
    foreach (string f in ofd.GetFilenames()) 
    { 
    string dwgfilename = Path.Combine(Path.GetDirectoryName(f), Path.GetFileNameWithoutExtension(f) + ".dwg"); 
    if (File.Exists(dwgfilename)) 
     File.Delete(dwgfilename); 
    currentdgnname = f; 
    currentdwgname = dwgfilename; 
    List<string> names = new List<string>() { currentdgnname, currentdwgname }; 
    //creates our document and sets it current     Application.DocumentManager.ExecuteInApplicationContext(CreateDGNDocHelper, names); 
    currentdoc.Editor.Command("-dgnimport", currentdgnname, "", "Master", "Standard"); 
    currentdoc.Editor.Command("._ZOOM", "Extents"); 
    } 
} 
static Document currentdoc; 
static void CreateDGNDocHelper(object data) 
{ 
    currentdoc = Application.DocumentManager.Add("acad.dwt"); 
    Application.DocumentManager.MdiActiveDocument = currentdoc; 
} 
static string currentdgnname; 
static string currentdwgname; 

答えて

2
あなたはあなたのコマンドからの両方のアプリケーション(セッション)または文書のコンテキストで実行することができ

、これらのオプションを参照してください

元のサンプルコードに基づいて、ここにコードの提案があります:

const string DGNIMPORTBATCHname = "DGNIMPORTBATCH"; 
[CommandMethod(DGNIMPORTBATCHname)] 
public async static void RunDGNIMPORTBATCH() 
{ 
    OpenFileDialog ofd = new OpenFileDialog("Convert DGNs", "", "dgn", "", OpenFileDialog.OpenFileDialogFlags.AllowMultiple); 
    System.Windows.Forms.DialogResult dr = ofd.ShowDialog(); 
    if (dr != System.Windows.Forms.DialogResult.OK) return; 

    foreach (string f in ofd.GetFilenames()) 
    { 
    string dwgfilename = Path.Combine(Path.GetDirectoryName(f), Path.GetFileNameWithoutExtension(f) + ".dwg"); 
    if (File.Exists(dwgfilename)) File.Delete(dwgfilename); 

    currentdgnname = f; 
    currentdwgname = dwgfilename; 

    await Application.DocumentManager.ExecuteInCommandContextAsync(
    async (obj) => 
    { 
     Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 
     await ed.CommandAsync(new object[] { "-dgnimport", currentdgnname, "", "Master", "Standard" }); 
     await ed.CommandAsync(new object[] { "._ZOOM", "Extents" }); 
    }, 
    null); 
    } 
} 

this blog postに基づいて完全にはテストされていません。

+0

私はこれに従います。しかし、唯一の初期文書ではコマンドが得られます。 –

+0

@DavidWolfe、あなたは精巧にできますか? –

+0

サンプルコード –

関連する問題