October 28, 2011

Control Active AutoCAD Ribbon Tab from Visual Lisp – RibbonNinja!

Filed under: AutoCAD,Development — Tags: , , , — Darrin Maidlow @ 5:18 pm

A friend asked me recently if there was some way to control the active ribbon tab from a macro.  After a little bit of digging I could not find any lisp or command line command that would do this.  ObjectARX however does provide the functionality I needed so I decided to whip something together in c#.  I’ve named it RibbonNinja – because lets face it, ninjas are awesome and should play a much larger part of everyday life.

How’s it work?

Attached is a zip file that contains the dll.  To use this simply netload the dll into your AutoCAD session and issue the all powerful RibbonNinja lisp call.  It takes a single string parameter that is the name of the tab you want to activate.  If it finds and activates the tab it will return T, if not it returns nil.  For example given the following AutoCAD ribbon:

mapribbon

Issuing the following lisp statement will change the active tab from “Map Setup” to “Analyze”.  Then it would switch to “Home"

   1: (RibbonNinja “Analyze”)
   2: (RibbonNinja “Home”)

This code is not case sensitive.  If its even possible to have multiple menus within AutoCAD with the same spelling but in different cases you’re probably S.O.L. and it will always choose the last one.  I’ve assumed unique tab names.

This dll has been compiled to work against AutoCAD 2012.  It will support both x86 and x64 versions.  It should also work on any AutoCAD vertical (i.e AutoCAD Map, Civil, ADT etc etc).  If you get the following  error when you try to netload the dll we have one last change to make:

Cannot load assembly. Error details: System.IO.FileLoadException: Could not load file or assembly ‘file:///C:\Program Files\Autodesk\AutoCAD 2012 – English\RibbonNinja.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0×80131515) File name: ‘file:/// C:\Program Files\Autodesk\AutoCAD 2012 – English\RibbonNinjadll ‘ —> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch.

Please read and try the following posts.  .NET 4.0 is “protecting” us so we need to fix that.  First you can try and “unblock” the dll following the steps outlined on .  If that doesn’t work for you again shut down AutoCAD and edit your acad.exe.config file as explained by Kean Walmsley on .  Remember to run notepad or whatever editor you’re using as an administrator – or you will get permission errors when you try to save the config file.

Turn up the nerd

For those of you more technically inclined the source is pretty simple once you figure out the mess of  references that are needed:

   1: [LispFunction("RibbonNinja")]
   2: public TypedValue GoNinjaGoNinjaGo(ResultBuffer lispArgs)
   3: {
   4:     //check the params
   5:     Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
   6:     if (lispArgs == null)
   7:     {
   8:         ed.WriteMessage("\nIncorrect number of arguments.  Expected 1 parameter, received none\n");
   9:         return new TypedValue((int)LispDataType.Nil);
  10:     }
  11:  
  12:     if(lispArgs.AsArray().Length != 1)
  13:     {
  14:         ed.WriteMessage("\nRibbonNinja - Incorrect number of arguments.  Expected String - tab keyboard shortcut\n");
  15:         return new TypedValue((int)LispDataType.Nil);
  16:     }
  17:     //get the tab name
  18:     String tabKey = Convert.ToString(lispArgs.AsArray()[0].Value);
  19:  
  20:     //get the ribbon
  21:     Autodesk.Windows.RibbonControl ribCntrl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
  22:     
  23:     //find the custom tab using the Id
  24:     foreach (RibbonTab tab in ribCntrl.Tabs)
  25:     {
  26:         //is this the one we're looking for?
  27:         if(tab.AutomationName.ToUpper() == tabKey.ToUpper())
  28:         {
  29:             //yup - get it active and bail. weeeeee
  30:             tab.IsActive = true;
  31:             return new TypedValue((int)LispDataType.T_atom);
  32:         }
  33:     }
  34:     return new TypedValue((int)LispDataType.Nil);
  35: }

Thanks for the challenge Shawn!  Here is your shiny new

.  Feel free to use this commercially – I always appreciate links to my posts in exchange !  If you have an AutoCAD project that you could use some help with – please feel free to contact me via !

p.s. Two posts in two days.  that’s crazy talk I tell ya!

Update 2012.04.13 – Shawn Dillon sent me a copy of his tabs.lsp file. I’ve included it here Tabs.lsp

5 Comments »

  1. Expanding on this I created a little LISP routine creating commands for all the default tabs in ACAD 2012. Feel free to add to it if you have created custom tabs for your install. I used the naming convention of tabXXX where XXX is the first three letters of every tab.

    Grab the LISP routine here.

    http://www.box.net/shared/8ft79uf8e105au8pfm46

    Shawn

    Comment by Shawn — October 28, 2011 @ 9:51 pm

  2. Cool! Thanks for sharing that Shawn.

    Comment by Darrin Maidlow — October 28, 2011 @ 9:52 pm

  3. [...] to get experience in all the various parts of it.  The recent request I received on how to control the AutoCAD ribbon from Visual Lisp or the command line and the subsequent research, code and blog post I wrote made me think,  [...]

    Pingback by Introducing Question of the Week for ObjectARX.NET and Mapguide « Geospecialling — November 4, 2011 @ 11:32 pm

  4. I tried that RibbonNinja routine and it work great ! Thanks for that ! I tried to download Shawn’s routine to see the difference but no more available. I am looking for something similar. I have an enterprise menu that have one ribbon tab. But that tab does not show automaticaly so i have to go into the cui interface and enable it manualy.

    Michel

    Comment by Michel Trottier — April 25, 2013 @ 9:53 am

  5. Hi Michel,

    thanks for commenting! I’ve contacted Shawn and he sent me his original lisp file. I’ve edited the article to include his lisp. Hope it helps.

    Comment by Darrin Maidlow — April 25, 2013 @ 8:47 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress

Switch to our mobile site