I have decided to move my blog from here to my SliceHost slice. I looked at a few different blogging solutions and decided on enki
I picked enki primarily because it was written in Rails which meant that I could hack on it and learn from it. So I played around with it for awhile locally, added an AutoTweet function just to see if I could, and deployed it today.
I have copied over most of the posts from the other site and added a few wiki pages from my github projects.
Overall, it has been a good learning experience and I hope to blog my heart for many years to come.
Now that Ruby and AutoCAD are playing nicely together, I thought it would be fun to try to get AutoCAD to talk to a Ruby on Rails application.
My first thought was to use ActiveResource. (Seemed logical to me)
First, I used the IronRuby igems.bat to install rails. That went well so now I was off to the races.
Next I added
1 |
require 'activeresource'
|
to an .rb file and tried to load it into autocad. That didn’t work too well because, it seems, when creating a new hosted IronRuby run-time, the search paths are set to the local Autocad install directory and “.”. No worries, I added the appropriate paths (just copied the paths from my local ir.exe paths).
I tried again to load the file was rewarded for my efforts with a 10 line backtrace. I tracked the problem down to a call to
1 |
require 'iconv'
|
in the likeliest of places, C:\ironruby-0.9.0\lib\IronRuby\gems\1.8\gems\activesupport-2.3.4\lib\active_support\inflector.rb
Long story made short, there was, in the local Autocad installation directory, a file called iconv.dll that was the culprit. So I remove the Autocad directory from the search path and from then on everything was too-easy, dead-simple.
I created a block whose attribute tags matched exactly the attributes of the model (Products, in this case) of my rails apps.
(manufacturer:string, modelnumber:string, color:string, size:string).
The helper method that I posted here uses an OpenStruct to mimic the attribute structure. Once that struct was created, I used the OpenStruct#marshal_dump method to create a hash to pass to the Product constructor of my ActiveResource object and then saved it. (It is actually harder to explain than it was to code. Is that a good thing or a bad thing?)
Here is the code. Of course, it is just proof of concept. It doesn’t handle errors very well or authentication (at all), but I think it does show how easy it can be to have Autocad communicate with a Ruby on Rails app.
The Rails application did not require anything special. I used script/generate scaffold and rake db:migrate. I did zero coding.
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 32 |
require 'acad2009.rb' require 'AcadHelper' include AcadHelper #add a Loading message because requiring activeresource can be slow puts "\nLoading" require 'activeresource' class Product < ActiveResource::Base #connect to my MacBook Pro which is running my rails app self.site = "http://192.168.1.102:3000/" end def save_to_remote_db begin puts "\nSelect Block References to add to remote database:" select_on_screen.each do |ss_ent| get_block_ref(ss_ent.ObjectId, :Read) do |attribs| #all the magic happens in one line of code if Product.new(attribs.marshal_dump).save puts "\nProduct saved" else puts "\nProblem saving product" end end end rescue Exception => e puts_ex e end end |
If the tables didn’t match exactly, one would just need a simple map of the fields before saving
1 2 3 4 5 |
prod = Product.new
prod.field1 = attrib.corresponding_field1
...
prod.fieldN = attrib.corresponding_fieldN
prod.save
|
There are no connection strings or any information about the database needed other than a URL and some knowledge of the table to which you are storing. ActiveResource automagically handles the rest. (no pun intended)
If you are playing along at home, the current version of acad2009.rb has the search path edits that worked on my system. You may (and probably will) have to adjust those paths to my your setup. You can easily use different acadXXXX.rb files to experiment with various setups.
Coming up next, AutoCAD talks to Google Maps!!!
Have fun!