January 13, 2012

Using Log4Net with Visual Lisp

Filed under: AutoCAD,Development — Tags: , , , — Darrin Maidlow @ 1:07 pm

If you’ve worked with me or talked technical with me in the past there is a good chance you area already very aware that I love .  There is also a really good chance that you know I still have a special place in my heart for .  Not only did I spend what may have been the “best years of my life” buried in VLIDE (or hey, maybe all those 30+ hour days and passing out under my desk were the best years of my life? =)) , I still firmly believe that Lisp is one of the most effective way to bang out even a relatively complex operation in AutoCAD when it comes to data manipulation.  ObjectARX (both original and .NET) is great – but the time and effort overhead is pretty high when you just need to bang out a quick routine.

<3 Visual Lisp

Even the quick routines need a little error handling and logging can make a huge difference in documenting the results or diagnosing problems.  Keep Reading

November 4, 2011

Introducing Question of the Week for ObjectARX.NET and Mapguide

Filed under: AutoCAD,Development,Mapguide — Tags: , , , — Darrin Maidlow @ 11:32 pm

Every good developer is constantly evolving and learning new technologies.  This can be a challenge when you’re hard at work in maintenance mode on familiar technology or when your every day needs don’t go as deeply into topics as you would like. 

If you take a step back and look at AutoCAD and – its a huge body of work and it is a challenge to get experience in all the various parts of it.  The recent request I received on how to or the command line and the subsequent research, code and blog post I wrote made me think,  what if I solicited small problems from the AutoCAD community that could make fun little 4 hour research projects.  These would allow me to dig into various portions of ObjectARX.NET once a week, write some fun code, and give me things to write about.  Everybody wins!

I’m looking to spend about 4-6 hours on each question.  I’m hoping to do one question per week, assuming I get enough requests.  I’ll provide the full source and project with each post.  All code will be written in c#.  Some examples of valid requests would be:

  • Submit a Visual Lisp routine to convert to ObjectARX.NET.
  • Request a particular routine that does some sort of drawing clean up or modifies something.
  • Identify some functionality that is missing in AutoCAD.
  • Request a piece of sample code explaining how to use API x of ObjectARX.NET or Mapguide Enterprise/Open Source.

The Do’s

  • Please keep your request realistic.   I’ve only got 4 to 6 hours and you’re not paying me =)
  • Please submit specific functionality along with sample data.
  • Please be available to answer any questions  I might have via e-mail, phone/skype or possibly gotomeeting.
  • Please keep the underlying technology somewhat current.  Don’ t request AutoCAD/Mapguide 2007.
  • Please review my code and comment if you see something wrong or that I could do more efficiently.  This is about me learning and I’m gonna make mistakes.

The Don’ts

  • Don’t expect days worth of work, I just don’t have that much spare time.
  • Don’t ask me to work on your project (unless of course you want to hire my team via ).
  • Please don’t get upset if I don’t pick your question.
  • I did my ten odd years in the trenches writing Visual Lisp – I don’t really want to write any more, so please don’t ask for lisp code.
  • Don’t be a ass.  I don’t have time for asses.

Let’s See What Happens

So I have no idea how this will play out.  I don’t know if 4-6 hours is a reasonable amount of time to allocate each week to do this right.   I don’t know if I will be able to make the time every week.  Maybe I’ll change it from “Question of the Week” to “Question of the Every Second Week”.  I’m going to put this out there and see what happens.   I reserve the right to change anything and everything! s  E-mail your requests to contact ~@~ geospecialling dot com.

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

February 24, 2011

AutoCAD VBA/VB6 to .NET Similar Function Comparison

Filed under: AutoCAD,Development — Tags: , — Darrin Maidlow @ 11:53 am

I’ve been working on supporting the team doing the VB6 code migration to .NET the past few weeks.   This code is many years worth of COM based AutoCAD development.   Came across this that will give you a good head start in the right direction on which functions are the new equivalents in .NET of the old VBA/COM  calls.

Technorati Tags: ,,

February 14, 2011

Debugging Visual Lisp and ObjectARX.NET Together

Filed under: AutoCAD,Development — Tags: , , — Darrin Maidlow @ 12:22 pm

I encountered the following error when trying to debug a new ObjectARX .NET assembly using loaderlockwasdetected

Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

This error would occur 95% of the time and pretty much took out AutoCAD each time it happened.  Fortunately this can be disabled manageddebuggingeceptionspretty easily within Visual Studio (I’ve tried both VS 2008 and VS 2010).   In Visual Studio bring up the exceptions dialog from the Debug / Exceptions menu (or via the keyboard shortcut Cntrl D+E). 

Expand the “Managed Debugging Exceptions” section and uncheck “LoaderLock” in the “Thrown” column.  Next time you start AutoCAD from Visual Studio and load Visual Lisp you should no longer get this exception and you should be merrily debugging away.

Powered by WordPress

Switch to our mobile site