Search code examples
c#.netautocadautodeskautocad-plugin

Autocad right click event handler


I have written this code:

int count = 1;

while (true)
{

    pointOptions.Message = "\nEnter the end point of the line: ";
    pointOptions.UseBasePoint = true;
    pointOptions.BasePoint = drawnLine.EndPoint;
    pointResult = editor.GetPoint(pointOptions);

    if (pointResult.Status == PromptStatus.Cancel)
    {
        break;
    }

    if (count == 1)
    {
        drawnLine.AddVertexAt(count, pointResult.Value.Convert2d(new Plane()), 0, 0, 0);
        blockTableRecord.AppendEntity(drawnLine);
        transaction.AddNewlyCreatedDBObject(drawnLine, true);
    }
    else
    {
        stretch(drawnLine, pointResult.Value, Point3d.Origin);
    }

    editor.Regen();

    count++;
}

The code works fine but to coplete the drawing i have to type ESC, I want to make a right click or space bar click to close my loop.Can i do this?


Solution

  • It Was in PromptPointOptions See code example below:

    // Set promptOptions
    var pointOptions = new PromptPointOptions("\nSelect Next Point: ");
    pointOptions.SetMessageAndKeywords("\nSelect Next Point: or Exit [Y]","Yes");
    pointOptions.AppendKeywordsToMessage = true;
    pointOptions.AllowArbitraryInput = true;
    pointOptions.UseBasePoint = true;
    pointOptions.BasePoint = drawnLine.EndPoint;
    
    // While user wants to draw the polyline
    while (pointResult.Status != PromptStatus.Keyword)
    {
    // Get point
    pointResult = Editor.GetPoint(pointOptions);
    
    // stop creating polyline
    if (pointResult.Status == PromptStatus.Cancel)
        break;
    
    if (count == 1) {
    
        // Get base point and add to the modelspace
        drawnLine.AddVertexAt(count, pointResult.Value.Convert2d(new Plane()), 0, 0, 0);
        blockTableRecord.AppendEntity(drawnLine);
        transaction.AddNewlyCreatedDBObject(drawnLine, true);
    } else
    
        // Grow the polyline
        stretch(drawnLine, pointResult.Value, Point3d.Origin);
    
    // Regen
    editor.Regen();
    
    count++;
    }
    

    what you were looking for was PromptPointOptions.SetMessageAndKeywords and by changing your loop eval you will come out when the user selects yes and you can set that up for a spacebar press.

    Hope this helps :)