CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2021 开发者帮助

GetPoint 方法 (.NET)

2024-5-18 18:36| 发布者: admin| 查看: 87| 评论: 0|原作者: admin|来自: AutoCAD

GetPoint 方法 (.NET)

该方法提示用户在命令提示符下指定一个点。该对象允许您控制输入的输入以及提示消息的显示方式。对象的 和 属性控制是否从基点绘制橡皮筋线。该对象的属性允许您定义除了指定点之外还可以在命令提示符下输入的关键字。GetPointPromptPointOptionsUseBasePointBasePointPromptPointOptionsKeywordsPromptPointOptions

获取用户选择的点

下面的示例提示用户输入两个点,然后使用这些点作为起点和终结点绘制一条线。

VB.NET

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

    '' 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
        Using acLine As Line = New Line(ptStart, ptEnd)
            '' Add the line to the drawing
            acBlkTblRec.AppendEntity(acLine)
            acTrans.AddNewlyCreatedDBObject(acLine, True)
        End Using

        '' 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

C#

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;

    // 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
        using (Line acLine = new Line(ptStart, ptEnd))
        {
            // 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();
    }
}

VBA/ActiveX 代码参考

Sub GetPointsFromUser()
    Dim startPnt As Variant
    Dim endPnt As Variant
    Dim prompt1 As String
    Dim prompt2 As String
 
    prompt1 = vbCrLf & "Enter the start point of the line: "
    prompt2 = vbCrLf & "Enter the end point of the line: "
 
    ' Get the first point without entering a base point
    startPnt = ThisDrawing.Utility.GetPoint(, prompt1)
 
    ' Use the point entered above as the base point
    endPnt = ThisDrawing.Utility.GetPoint(startPnt, prompt2)
 
    ' Create a line using the two points entered
    ThisDrawing.ModelSpace.AddLine startPnt, endPnt
    ThisDrawing.Application.ZoomAll
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1   苏公网安备32011402011833)

GMT+8, 2024-12-15 13:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部