2016-12-06 54 views
2

我需要批量導入dgns。我可以用用在API的最新命令選項做到這一點:在會話上下文中運行AutoCAD命令

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

然而,問題是,我還需要控制保存繪圖(和保存的文件名)。我不明白的方式來做到這一點沒有在會議環境中使用我的命令是這樣的:

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

的Editor.Command方法雖然只有文檔上下文。

添加文檔鎖不起作用。有沒有辦法在會話命令中切換到在文檔上下文中運行代碼?

**編輯:示例代碼與Activationcontext:

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

您可以在這兩個應用程序(會話)或文檔上下文從你的命令執行,看到這些選項:

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

編輯

根據您的原始示例代碼,以下是代碼建議:

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

剛剛添加的示例代碼 –

相關問題