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



    « Adding a context menu to AutoCAD objects using .NET | Main | Automating batch printing of DWF files using Design Review 2008 »

    May 07, 2007

    TrackBack

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

    Listed below are links to weblogs that reference It's all in the context: adding a default menu to your AutoCAD application using .NET:

    » [AutoCAD2008新功能介绍3]在AutoCAD中使用.NET来添加一个新的缩放比例 from C#才鸟
    在本贴中,我们来看一下只有AutoCAD2008才有的一个功能——给图形添加一个新的缩放比例。下一篇将会讨论如何用编程的方式添加一个比例来让一个实体可以被缩放。 [Read More]

    » [AutoCAD2008新功能介绍4]利用.NET使AutoCAD对象可缩放 from C#才鸟
    在上一篇帖子中,我们看到了如何为AutoCAD图形添加一个新的缩放比例。 在这一篇帖子中,我们来看一下如何使一个对象可缩放——当然前提条件是它必须是一个支持缩放比例的类型。重复一下,这一篇帖子也是基于AutoCAD 2008中引入的功能。... [Read More]

    Comments

    Hi Kean,

    I've a question: how do I pass parameters to a command invoked from the context menu?

    I can use 'SendStringToExecute' with a command strings and its parametrs (as string) but how does the invoked command method retrieve those parameters?

    Any other available method than 'SendStringToExecute' to recall methods from a menu?

    TIA,
    Cabbi

    Hi Cabbi,

    You'd use Editor.GetString()/GetDouble()/GetInteger()/GetPoint() etc. to pick up the command-line input fed in through SendStringToExecute().

    You might opt to call the functions directly, but generally it's safer to use the SendStringToExecute() to invoke commands like this (it helps avoid re-entrancy issues, for example).

    Regards,

    Kean

    >You might opt to call the functions directly
    But if I call functions directly from the menu command and do any transaction with the DB I got an AutoCAD crash. I was assuming this is due to the different contexts of the menu and the drawing, isn't it?
    How do I avoid this crash by calling my methods directly?

    TIA,
    Cabbi

    You might try locking the document, by I *strongly* recommend sticking to using SendStringToExecute. It's a bit more work to expose your functions as commands, but in my opinion the extra effort is worthwhile and they can then be used in many different places (menus, toolbars, etc.).

    Kean

    Thanks a lot!

    Yet another stupid question: how do I insert a menu separator?

    I tried with "-", "--", "_" but... nothing. Even Goolgeing around didn't helped.

    Cabbi

    Try using a blank string:

    MenuItem sep = new MenuItem("");
    cme.MenuItems.Add(sep);

    Regards,

    Kean

    Oh, nooooo, the most stupid option I did not try!?!?
    It seems it's time for me to take a little vacation! :oD

    Thanks again,
    Cabbi

    Good morning Kean.

    I am new to programming AutoCAD via .NET and I have a question for you. I used to have a custom context menu in ACAD2005. I would like to do the same using the above method, but my inexperience is frustrating me.

    How would I set the on1st method to draw a vertical xline? Or to call any command from the command line. (For example: dist) This would definately reduce my frustration.

    Thanks for your help.

    Jason

    Hi Jason,

    For calling commands, just search this blog for "SendStringToExecute" - you should find plenty of examples.

    To create an XLine, you should be able to pick up some standard code to create and post another type of entity (a polyline, for instance), and then you modify it to use the Autodesk.AutoCAD.DatabaseServices.Xline class.

    Regards,

    Kean

    Thanks for the info. I've got it working now. I am a little more comfortable working with VB.NET. Do you know if someone has converted this to VB at all?

    These days I generally don't post in both C# and VB, mainly because I prefer C#, myself, and the translation tools available are pretty good.

    Here are two that I've used:

    Carlos Aguilar's "Code Translator"
    Kamal Patel's "Convert C# to VB.NET"

    Neither provide 100% perfect results, but they do get you most of the way there.

    I also use a Visual Studio plugin that uses either of these sites for the conversion directly in Visual Studio 2005.

    Kean

    Good morning Kean.

    I do have another question for you. I've got this code working perfectly, just the way I want it. Is there a way to add accelerator keys to the context menu? Just like in the image above, the first letters in Copy and Paste are underlined. Can that be done in this code as well?
    Thanks.

    Hi Jason,

    Preceding the accelerator character with an ampersand is the typical way to do this. I gave this a try, and found it works, but not perfectly...

    cme = new ContextMenuExtension();
    cme.Title = "&&Kean's commands";
    MenuItem mi1 = new MenuItem("&&1st");
    mi1.Click += new EventHandler(On1st);
    cme.MenuItems.Add(mi1);

    The accelerator works in both cases, but only displays for the menu, not for the menu item. This isn't ideal, but hopefully it's enough for you.

    Regards,

    Kean

    Good morning Kean.

    Thanks for that info. I never thought to try two & in front of what I wanted accelerated. I did try one, but that didn't work. Must be a VB thing. (Although I haven't tried there either.)

    Thanks again.

    Hello Kean,

    Nice option to create in memory context-menu parts. But the addition will be situated in the lower part of the menu and that's not quit userfriendly for our application. So I was wondering if it's possible to change the order within a context menu.

    Thanks for you input.

    Hi John,

    There's no way to control this through the API, although you might be able to do something with some low-level APIs in the OS (I haven't done so, but it *might* be possible, if you don't mind creating a potential future maintenance problem for yourself).

    Regards,

    Kean

    Hi Kean,
    Is it possible to have my own menu with out adding to default menu ?
    Regards,
    Razmin.

    Hi Razmin,

    Do you mean you want to replace the default context menu, or have your own pull-down menu in the AutoCAD application frame?

    Regards,

    Kean

    Hi Kean,
    I'm getting some strange (to me) behavior out of ads_queueexpr...

    When I first translated this example, It triggered the appropriate events, but the SendString wasn't executed... So I tried a couple of other ways. When I tried calling the methods directly, I had lock mode issues, so I tried ads_queueexpr and at first it seemed nothing was happening, then I found that if I clicked one of my menuitems, then (do anything, like ESC, or pick a button, or type a command) and then my "queued expression" would fire. Strange, no?

    Depends on how your command string is formatted - have you remembered to call it via "(command \"MYCMD\")" ?

    Kean

    I prefer using vl-cmdf, but yes, I called it as a lisp statement.
    ex. ads_queueexpr("(vl-cmdf ""STARTRUN"")")

    I even tried putting a line feed in after the closing paren on the lisp, and just for giggles I tried it with 'command'. No difference.

    I don't really know what was happening with the SendString... because it seems to be working that way now, but this behavior on ads_queueexpr is still repeatable.

    The oddest thing is that I use ads_queueexpr to respond to DocumentActivated in order to get saved values out of an extension dictionary, and it works fine there, though so much is going on at that time, it may be that it's getting flushed out by the things going on after it, just like when I hit ESC or something.

    Anyway, at this point, my stuff is working with SendStringtoExecute, so there's no need for me to waste any more of your time with it, but it is still strange!

    Hi Ken, try pass parameters to command, but I can´t because generate an error
    the code is:

    _
    Public Sub Arma_Dado(ByVal sss As String)

    code...

    end sub

    how a pass parameter using SendStringtoExecute, for example

    SendStringtoExecute("arma_dado"& chr(13) & "D-1", ???,???,???)

    Can I do this?

    You need to use Editor.GetString() (or one of the other GetXXX functions) from within your command to pick up the values passed in via the command-line.

    Kean

    Sorry kean, but I need take a value from a usercontrol, when I execute for a first time everything is well, but when I change it on other window and try to run it, don´t recive the parametes: my code is:

    (funtion en the command class)
    Public Class LU01Commands
    _
    Public Sub ins_varilla(ByVal pto As Point3d, ByVal varilla As String)
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
    Dim tr As Transaction = db.TransactionManager.StartTransaction()
    Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
    Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
    'more code......
    End Sub
    End Class

    And My code on then pallete (user control) is:
    when I press button1.


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
    Try
    doc.SendStringToExecute("ins_varilla" & Chr(13), False, False, False)
    ed = Nothing
    db = Nothing
    doc = Nothing
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try

    End Sub

    Can I use the "SendStringtoExecute" to do or there are other way to do.

    Luis -

    You can't just declare the function with arguments: it needs to be a standard AutoCAD command (defined as always) and then you use GetString() etc. to retrieve the arguments from the command-line.

    See this post.

    Regards,

    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