Access the AutoCAD Command Line訪問AutoCAD命令行
You can send commands directly to the AutoCAD command line by using the SendStringToExecute method. The SendStringToExecute method sends a single string to the command line. The string must contain the arguments to the command listed in the order expected by the prompt sequence of the executed command.
我們可以使用SendStringToExecute方法直接向AutoCAD命令行發送命令。SendStringToExecute方法將單個字符串發送給命令行。該字符串必須包含按所執行命令的提示序列所期望的順序排列的命令參數。
A blank space or the ASCII equivalent of a carriage return in the string is equivalent to pressing Enter on the keyboard. Unlike the AutoLISP environment, invoking the SendStringToExecute method with no argument is invalid.
字符串中的空格或代表回車符的ASCII碼等同於按下了鍵盤上的Enter鍵。和AutoLISP環境不同,調用不帶參數的SendStringToExecute方法是非法的。
Commands executed with SendStringToExecute are asynchronous and are not invoked until the .NET command has ended. If you need to execute a command immediately (synchronously), you should:
使用SendStringToExecute方法執行命令是異步的,知道.NET命令結束,所調用的AutoCAD命令才被執行。如果需要立即執行命令(同步),我們應該:
· Use the SendCommand method which is part of the COM Automation library which can be access using .NET COM Interop; 使用.NET COM互操作程序集能訪問的COM Automation庫的SendCommand方法;
· P/Invoke the unmanaged acedCommand or acedCmd method for native AutoCAD commands and commands defined with the ObjectARX or .NET API; 對於AutoCAD本地命令及由ObjectARX或.NET API定義的命令,使用非託管的acedCommand方法或acedCmd方法;
· P/Invoke the unmanaged acedInvoke method for commands defined through AutoLISP; 對於用AutoLISP定義的命令,使用非託管的acedInvoke方法;
Send a command to the AutoCAD command line 發送名利到AutoCAD命令行
The following example creates a circle with a center of (2, 2, 0) and a radius of 4. The drawing is then zoomed to all the geometry in the drawing. Notice that there is a space at the end of the string which represents the final Enter to begin execution of the command.
下面的例子用圓心(2,2,0)和半徑4創建一個圓,然後將圖形縮放到全部圖形都可見。注意字符串結尾有一個空格,代表最後按Enter鍵開始執行命令。
VB.NET
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
<CommandMethod("SendACommandToAutoCAD")> _
Public Sub SendACommandToAutoCAD()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
'' Draws a circle and zooms to the extents or
'' limits of the drawing
acDoc.SendStringToExecute("._circle 2,2,0 4 ", True, False, False)
acDoc.SendStringToExecute("._zoom _all ", True, False, False)
End Sub
C#
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
[CommandMethod("SendACommandToAutoCAD")]
public static void SendACommandToAutoCAD()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
// Draws a circle and zooms to the extents or limits of the drawing
//畫圓並縮放到圖形界限
acDoc.SendStringToExecute("._circle 2,2,0 4 ", true, false, false);
acDoc.SendStringToExecute("._zoom _all ", true, false, false);
}
VBA/ActiveX Code Reference
Sub SendACommandToAutoCAD()
' Draws a circle and zooms to the extents or
' limits of the drawing
ThisDrawing.SendCommand "_Circle 2,2,0 4 "
ThisDrawing.SendCommand "_zoom a "
End Sub
譯者:到此,《控制AutoCAD環境》這一章就翻譯完了,接下來的一章是《創建和編輯AutoCAD實體(圖元)》,敬請期待!