CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2022 开发者帮助

选择集关键字 (.NET)

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

选择集关键字 (.NET)

在实际创建选择集之前,应用程序可能会使用关键字提示用户输入选择首选项。

可以通过创建对象的实例来将关键字分配给对象选择操作。创建对象后,该方法用于分配用户可以在命令提示符下输入的每个关键字。将关键字分配给对象后,必须将对象传递给编辑器的方法。PromptSelectionOptionsPromptSelectionOptionsSetKeywordsPromptSelectionOptionsPromptSelectionOptionsGetSelection

用户可以在提示符下输入的关键字的实现使用事件处理程序进行处理。当用户在生成的提示符下输入关键字时,将引发事件并调用应用程序的处理程序。Select objects:PromptSelectionOptions.KeywordInputKeywordInput

处理程序接收一个参数,该参数同时用作输入和输出参数。参数的属性指示所选的关键字。典型的处理程序将此关键字与应用程序关键字列表中的关键字进行比较,并调用适当的选择方法。如果选择方法返回实体,则应用程序会将这些实体添加到该方法的参数中。当原始调用返回时,它会向设置关键字列表的方法提供所选实体。KeywordInputSelectionTextInputEventArgsInputSelectionTextInputEventArgsSelectionTextInputEventArgsSelectionTextInputEventArgs.AddObjectsGetSelection

下面的示例定义了五个关键字,并添加了一个处理程序来支持用户选择的关键字。

VB.NET

Private Shared Sub SelectionKeywordInputHandler(ByVal sender As Object, ByVal eSelectionInput As SelectionTextInputEventArgs)
    '' Gets the current document editor and define other variables for the current scope
    Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
    Dim acSSPrompt As PromptSelectionResult = Nothing

    '' See if the user choose the myFence keyword
    Select Case eSelectionInput.Input
        Case "myFence"
            '' Uses the four points to define a fence selection
            Dim ptsFence As New Point3dCollection()
            ptsFence.Add(New Point3d(5.0, 5.0, 0.0))
            ptsFence.Add(New Point3d(13.0, 15.0, 0.0))
            ptsFence.Add(New Point3d(12.0, 9.0, 0.0))
            ptsFence.Add(New Point3d(5.0, 5.0, 0.0))

            acSSPrompt = acDocEd.SelectFence(ptsFence)
        Case "myWindow"
            '' Defines a rectangular window selection
            acSSPrompt = acDocEd.SelectWindow(New Point3d(1.0, 1.0, 0.0), _
                                              New Point3d(30.0, 20.0, 0.0))
        Case "myWPoly"
            '' Uses the four points to define a polygon window selection
            Dim ptsPolygon As New Point3dCollection()
            ptsPolygon.Add(New Point3d(5.0, 5.0, 0.0))
            ptsPolygon.Add(New Point3d(13.0, 15.0, 0.0))
            ptsPolygon.Add(New Point3d(12.0, 9.0, 0.0))
            ptsPolygon.Add(New Point3d(5.0, 5.0, 0.0))

            acSSPrompt = acDocEd.SelectWindowPolygon(ptsPolygon)
        Case "myLastSel"
            '' Gets the last object created
            acSSPrompt = acDocEd.SelectLast()
        Case "myPrevSel"
            '' Gets the previous object selection set
            acSSPrompt = acDocEd.SelectPrevious()
    End Select

    '' If the prompt status is OK, objects were selected
    If Not acSSPrompt Is Nothing Then
        If acSSPrompt.Status = PromptStatus.OK Then
            '' Objects were selected, so add them to the current selection
            Dim acSSet As SelectionSet = acSSPrompt.Value
            Dim acObjIds As ObjectId() = acSSet.GetObjectIds()
            eSelectionInput.AddObjects(acObjIds)
        Else
            Return
        End If
    End If
End Sub

<CommandMethod("SelectionKeywordInput")> _
Public Sub SelectionKeywordInput()
    '' Gets the current document editor
    Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor

    '' Setups the keyword options
    Dim acKeywordOpts As New PromptSelectionOptions()
    acKeywordOpts.Keywords.Add("myFence")
    acKeywordOpts.Keywords.Add("myWindow")
    acKeywordOpts.Keywords.Add("myWPoly")
    acKeywordOpts.Keywords.Add("myLastSel")
    acKeywordOpts.Keywords.Add("myPrevSel")

    '' Adds the event handler for keyword input
    AddHandler acKeywordOpts.KeywordInput, AddressOf SelectionKeywordInputHandler

    '' Prompts the user for a selection set
    Dim acSSPrompt As PromptSelectionResult = acDocEd.GetSelection(acKeywordOpts)

    '' If the prompt status is OK, objects were selected
    If acSSPrompt.Status = PromptStatus.OK Then
        '' Gets the selection set
        Dim acSSet As SelectionSet = acSSPrompt.Value

        '' Gets the objects from the selection set
        Dim acObjIds As ObjectId() = acSSet.GetObjectIds()
        Dim acCurDb As Database = Application.DocumentManager.MdiActiveDocument.Database

        '' Starts a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            Try
                '' Gets information about each object
                For Each acObjId As ObjectId In acObjIds
                    Dim acEnt As Entity = acTrans.GetObject(acObjId, OpenMode.ForWrite, True)
                    acDocEd.WriteMessage(vbLf + "Object selected: " + acEnt.GetType().FullName)
                Next acObjId
            Finally
                acTrans.Dispose()
            End Try
        End Using
    End If

    '' Removes the event handler for keyword input
    RemoveHandler acKeywordOpts.KeywordInput, AddressOf SelectionKeywordInputHandler
End Sub

C#

private static void SelectionKeywordInputHandler(object sender, SelectionTextInputEventArgs eSelectionInput)
{
			 // Gets the current document editor and define other variables for the current scope
			 Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
    PromptSelectionResult acSSPrompt = null;
    SelectionSet acSSet = null;
    ObjectId[] acObjIds = null;

	   // See if the user choose the myFence keyword
	   switch (eSelectionInput.Input) {
        case "myFence":
			         // Uses the four points to define a fence selection
            Point3dCollection ptsFence = new Point3dCollection();
            ptsFence.Add(new Point3d(5.0, 5.0, 0.0));
            ptsFence.Add(new Point3d(13.0, 15.0, 0.0));
            ptsFence.Add(new Point3d(12.0, 9.0, 0.0));
            ptsFence.Add(new Point3d(5.0, 5.0, 0.0));

            acSSPrompt = acDocEd.SelectFence(ptsFence);
			         break;
        case "myWindow":
			         // Defines a rectangular window selection
            acSSPrompt = acDocEd.SelectWindow(new Point3d(1.0, 1.0, 0.0), new Point3d(30.0, 20.0, 0.0));
			         break;
        case "myWPoly":
			         // Uses the four points to define a polygon window selection
            Point3dCollection ptsPolygon = new Point3dCollection();
            ptsPolygon.Add(new Point3d(5.0, 5.0, 0.0));
            ptsPolygon.Add(new Point3d(13.0, 15.0, 0.0));
            ptsPolygon.Add(new Point3d(12.0, 9.0, 0.0));
            ptsPolygon.Add(new Point3d(5.0, 5.0, 0.0));

            acSSPrompt = acDocEd.SelectWindowPolygon(ptsPolygon);
			         break;
		      case "myLastSel":
			        // Gets the last object created
			        acSSPrompt = acDocEd.SelectLast();
			        break;
		      case "myPrevSel":
			        // Gets the previous object selection set
			        acSSPrompt = acDocEd.SelectPrevious();
			        break;
	   }

    // If the prompt status is OK, objects were selected and return
    if (acSSPrompt != null)
    {
        if (acSSPrompt.Status == PromptStatus.OK)
        {
            // Objects were selected, so add them to the current selection
            acSSet = acSSPrompt.Value;
            acObjIds = acSSet.GetObjectIds();
            eSelectionInput.AddObjects(acObjIds);
        }
    }
}

[CommandMethod("SelectionKeywordInput")]
public static void SelectionKeywordInput()
{
    // Gets the current document editor
    Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;

    // Setups the keyword options
    PromptSelectionOptions acKeywordOpts = new PromptSelectionOptions();
    acKeywordOpts.Keywords.Add("myFence");
    acKeywordOpts.Keywords.Add("myWindow");
    acKeywordOpts.Keywords.Add("myWPoly");
    acKeywordOpts.Keywords.Add("myLastSel");
    acKeywordOpts.Keywords.Add("myPrevSel");

    // Adds the event handler for keyword input
    acKeywordOpts.KeywordInput += new SelectionTextInputEventHandler(SelectionKeywordInputHandler);

    // Prompts the user for a selection set
    PromptSelectionResult acSSPrompt = acDocEd.GetSelection(acKeywordOpts);

    // If the prompt status is OK, objects were selected
    if (acSSPrompt.Status == PromptStatus.OK)
    {
        // Gets the selection set
        SelectionSet acSSet = acSSPrompt.Value;

        // Gets the objects from the selection set
        ObjectId[] acObjIds = acSSet.GetObjectIds();
        Database acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

        // Starts a transaction
        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            try
            {
                // Gets information about each object
                foreach (ObjectId acObjId in acObjIds)
                {
                    Entity acEnt = (Entity)acTrans.GetObject(acObjId, OpenMode.ForWrite, true);
                    acDocEd.WriteMessage("\nObject selected: " + acEnt.GetType().FullName);

                }
            }
            finally
            {
                acTrans.Dispose();
            }
        }
    }

    // Removes the event handler for keyword input
    acKeywordOpts.KeywordInput -= new SelectionTextInputEventHandler(SelectionKeywordInputHandler);
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 11:59

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部