Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « Making AutoCAD objects annotative using .NET | Main | It's all in the context: adding a default menu to your AutoCAD application using .NET »

    May 04, 2007

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200d834ff381e53ef

    Listed below are links to weblogs that reference Adding a context menu to AutoCAD objects using .NET:

    Comments

    Great!

    Is this possible through ObjectARX C++ too?

    Regards.

    Hi Fernando,

    Sure - if you grep the ObjectARX samples folder for "acedAddObjectContextMenu", you'll find a few samples demonstrating this feature (CurveText, SubEntity & contextmenu).

    Regards,

    Kean

    I noticed that after I call 'PromptSelectionResult psr = ed.GetSelection()' my selection is reset. Is there a way to leave the current selection unchanged?

    I played a bit with 'PromptSelectionOptions' but... nothing! ):

    TIA,
    Cabbi

    The technique in this post should help.

    Kean

    Hey there. My question is: Can we insert a command in the LAYOUT(sheet) context menu ( in C++) ? And can that command be executed without clearing the layout selection?

    10x,
    gabriel

    Hi Gabriel,

    Sorry - I'm not aware of any way to do this.

    Regards,

    Kean

    I converted the code to Vb.net. It compiled well, but did not add context menu on right click... What can be the reason??
    Best regards,
    Sajjad

    Here is the code:

    Namespace ContextMenuApplication

    Public Class Commands
    Implements IExtensionApplication

    Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
    CountMenu.Attach()
    End Sub

    Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
    CountMenu.Detach()
    End Sub


    Public Sub CountSelection()
    Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
    Dim psr As PromptSelectionResult = ed.GetSelection()
    If (psr.Status = PromptStatus.OK) Then
    ed.WriteMessage("\nSelected {0} entities.", psr.Value.Count())
    End If

    End Sub
    End Class


    Public Class CountMenu
    Private Shared cme As ContextMenuExtension


    Public Shared Sub Attach()
    cme = New ContextMenuExtension
    Dim mi As MenuItem = New MenuItem("Count")
    AddHandler mi.Click, AddressOf OnCount
    cme.MenuItems.Add(mi)
    Dim rxc As RXClass = Entity.GetClass(GetType(Entity))
    Application.AddObjectContextMenuExtension(rxc, cme)
    End Sub


    Public Shared Sub Detach()
    Dim rxc As RXClass = Entity.GetClass(GetType(Entity))
    Application.RemoveObjectContextMenuExtension(rxc, cme)
    End Sub

    Private Shared Sub OnCount(ByVal o As Object, ByVal e As EventArgs)
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    doc.SendStringToExecute("_.COUNT ", True, False, False)
    End Sub

    End Class

    End Namespace

    Sajjad,

    It works for me. What type of entity are you clicking on to get the menu to appear?

    If you're just right-clicking on the drawing (not an entity) then you won't get the menu item - it's registered against the Entity class.

    Kean

    I learned that if system variable PICKFIRST=0 then custom context menu does not show up. I set it to 1 and it worked.

    Sajjad

    all this discussion helped me a lot.

    can you please provide a code snippet to display the short cut menu corresponding the selected entity using autocad vba

    Hi,

    It's Possible to have the new context menu without select a object ?

    Hi Thib,

    Yes - see this post.

    Kean

    Hi Kean,

    Can we add or remove the contextmenu in runtime, I'm working with entities that have special xrecords, my contextmenu should appear depending on what's in the xrecord?

    Do you see a way to achieve this?

    Thanks
    Kristof

    Hi Kristof,

    Sorry - I don't see how that's possible with the existing mechanism.

    Regards,

    Kean

    "Can we add or remove the contextmenu in runtime?" - What about using Popup event of myContextMenuExtension object?

    I'm actually thinking about making this a subject of a post. You should be able to use one of these COM (not .NET) events from the Document object:

    BeginShortcutMenuCommand event

    Triggered after the user right-clicks on the drawing window, and before the shortcut menu appears in command mode.

    BeginShortcutMenuDefault event

    Triggered after the user right-clicks on the drawing window, and before the shortcut menu appears in default mode.

    BeginShortcutMenuEdit event

    Triggered after the user right-clicks on the drawing window, and before the shortcut menu appears in edit mode.

    BeginShortcutMenuGrip event

    Triggered after the user right-clicks on the drawing window, and before the shortcut menu appears in grip mode.

    BeginShortcutMenuOSnap event

    Triggered after the user right-clicks on the drawing window, and before the shortcut menu appears in osnap mode.

    Kean

    Hi Kean,

    What about using Popup event of ContextMenuExtension object in .NET?

    Test my code.

    public class CountMenu

    {

    private static ContextMenuExtension cme;


    public static void Attach()

    {

    cme = new ContextMenuExtension();

    cme.Popup += new EventHandler(OnPopup);

    RXClass rxc = Entity.GetClass(typeof(Entity));

    Application.AddObjectContextMenuExtension(rxc, cme);

    }

    public static void Detach()

    {

    RXClass rxc = Entity.GetClass(typeof(Entity));

    Application.RemoveObjectContextMenuExtension(rxc, cme);

    }

    private static void OnCount(Object o, EventArgs e)

    {

    Document doc =

    Application.DocumentManager.MdiActiveDocument;

    doc.SendStringToExecute("_.COUNT ", true, false, false);

    }

    private static void OnPopup(Object o, EventArgs e)

    {
    cme.MenuItems.Clear();

    if (Analyse Something)
    {

    MenuItem mi = new MenuItem("Count");

    mi.Click += new EventHandler(OnCount);

    cme.MenuItems.Add(mi);
    }
    }

    }

    }

    That`s all :)

    That seems to work very well - thanks, al!

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search