The GetPoint method prompts the user to specify a point at the Command prompt. The PromptPointOptions object allows you to control the input entered and how the prompt message appears. The UseBasePoint and BasePoint properties of the PromptPointOptions object controls if a rubber-band line is drawn from a base point. The Keywords property of the PromptPointOptions object allows you to define keywords that can be entered at the Command prompt in addition to specifying a point.
GetPoint 方法提示用户在 AutoCAD 命令提示下给出点的定义。PromptPointOptions 对象允许用户控制输入和如何显示提示信息。PromptPointOptions 对象的 UseBasePoint 和 BasePoint 属性控制是否从基点绘制一条橡皮线。PromptPointOptions 对象的 Keywords 属性允许用户定义用户可以在命令提示中输入除指定点外的关键字。
The following example prompts the user for two points, then draws a line using those points as the start point and endpoint.
下面的样例提示用户输入两个点,然后以这两个点为起点和端点绘制一条直线。
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
<CommandMethod("GetPointsFromUser")> _
Public Sub GetPointsFromUser()
'' 获得当前数据库并启动事务管理器 Get the current database and start the Transaction Manager
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim pPtRes As PromptPointResult
Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
'' 提示开始点 Prompt for the start point
pPtOpts.Message = vbLf & "Enter the start point of the line: "
pPtRes = acDoc.Editor.GetPoint(pPtOpts)
Dim ptStart As Point3d = pPtRes.Value
'' 如果用户按了 ESC 键或取消了命令就退出 Exit if the user presses ESC or cancels the command
If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
'' 提示结束点 Prompt for the end point
pPtOpts.Message = vbLf & "Enter the end point of the line: "
pPtOpts.UseBasePoint = True
pPtOpts.BasePoint = ptStart
pPtRes = acDoc.Editor.GetPoint(pPtOpts)
Dim ptEnd As Point3d = pPtRes.Value
If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As BlockTable
Dim acBlkTblRec As BlockTableRecord
'' 以写的方式打开模型空间 Open Model space for write
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
OpenMode.ForRead)
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 定义新的直线 Define the new line
Dim acLine As Line = New Line(ptStart, ptEnd)
acLine.SetDatabaseDefaults()
'' 添加直线到图形中去 Add the line to the drawing
acBlkTblRec.AppendEntity(acLine)
acTrans.AddNewlyCreatedDBObject(acLine, True)
'' 缩放到图形的范围或界限 Zoom to the extents or limits of the drawing
acDoc.SendStringToExecute("._zoom _all ", True, False, False)
'' 提交修改并销毁事务 Commit the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[CommandMethod("GetPointsFromUser")]
public static void GetPointsFromUser()
{
// 获得当前数据库并启动事务管理器 Get the current database and start the Transaction Manager
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示开始点 Prompt for the start point
pPtOpts.Message = "\nEnter the start point of the line: ";
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
Point3d ptStart = pPtRes.Value;
// 如果用户按了 ESC 键或取消了命令就退出 Exit if the user presses ESC or cancels the command
if (pPtRes.Status == PromptStatus.Cancel) return;
// 提示结束点 Prompt for the end point
pPtOpts.Message = "\nEnter the end point of the line: ";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = ptStart;
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
Point3d ptEnd = pPtRes.Value;
if (pPtRes.Status == PromptStatus.Cancel) return;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
BlockTableRecord acBlkTblRec;
// 以写的方式打开模型空间 Open Model space for write
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 定义新的直线 Define the new line
Line acLine = new Line(ptStart, ptEnd);
acLine.SetDatabaseDefaults();
// 添加直线到图形中去 Add the line to the drawing
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
// 缩放到图形的范围或界限 Zoom to the extents or limits of the drawing
acDoc.SendStringToExecute("._zoom _all ", true, false, false);
// 提交修改并销毁事务 Commit the changes and dispose of the transaction
acTrans.Commit();
}
}