Tuesday, April 7, 2009

AutoCAD customization -- Search to replace Autolisp with Java or Python or such

I'm fluent in AutoLISP, and I have used it since the early '90s to customize AutoCAD. I sell custom tools, and people tell me my customizations have made a huge difference in their AutoCAD productivity. More recently I have been converted and have done several Software Libre projects using AutoLISP.

But I have not branched out much beyond basic AutoLISP. My tools include a couple of Visual Basic for Applications (VBA) forms (a GPL help tip form and a file list creator I still need to release under the GPL) I programmed a few years ago. I have dabbled here and there (but only recently begun to be conversant) in Dialog Control Language (DCL). And I have generally shied away from the "newer" Visual Lisp functions. But I now am seeing that those functions, and the Object Model they give AutoLISP the ability to access, are the window to a new world of possibilities, because the very same Object Model they control can be controlled by more popular and broadly useful (and Open Source) languages like Java and Python.

As a Software Libre (hypocritical) true believer, using broader languages is increasingly important and practical to me. It's important because I desire to be platform agnostic, which isn't well served by my programming only in AUTOlisp. It's practical because potential programming partners--fellow Software Libre believers--are not super likely to prefer a proprietary language like AutoLISP.

So I have been at the same time both exploring the AutoCAD and Civil3D Object Model and exploring how to program it with Java or Python (I recently made the acquaintance of civil programmer Darren Waschow who turned me on again to using Python for Turning Path Tracker) . I don't know either of those languages, but I can learn them. After seeing today this Python & Java: Side by Side Comparison and noting how friendly and AutoLISP-like Python is, I am definitely leaning toward Python. And to my delight, I found this explanation from Tim Riley on Automating AutoCAD with Python and COM, of which I will post a snippet here:

Riley says all you need for this to work (besides AutoCAD) is Python 2.4 and the win32all package. You can get both of these from ActiveState’s ActivePython product.

import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
doc.Utility.Prompt("Hello from Python\n")


Wow, Riley! That's extremely simple. And that's all it takes apparently (haven't tried yet) to control AutoCAD with Python.

Now, as for Java, I'm not so close yet, and to tell the truth, I am waning on the prospect, but I believe it would be done in 2009 using Jawin. I haven't found any examples specific to AutoCAD, but I believe this PowerPoint example is on the right track. Not as pretty as Python!


try {
Ole32.CoInitialize();
DispatchPtr app = new DispatchPtr("new:PowerPoint.Application");
app.put("Visible", true);
DispatchPtr preses = app.getObject("Presentations");
DispatchPtr pres = (DispatchPtr) preses.invoke("add", -1);
DispatchPtr slides = pres.getObject("Slides");
DispatchPtr slide = (DispatchPtr) slides.invoke("Add", 1, 2);
DispatchPtr shapes = slide.getObject("Shapes");
DispatchPtr shape = (DispatchPtr) shapes.invoke("Item", 1);
DispatchPtr frame = shape.getObject("TextFrame");
DispatchPtr range = frame.getObject("TextRange");
range.put("Text", "Using Jawin to call COM objects");
Ole32.CoUninitialize();
}
catch (Exception e) {
e.printStackTrace();
}


Next, see my successful connection to AutoCAD with Python in Part 2.

No comments: