PyAcad 0.4.1

Python Extension for Autocad/Intellicad

Pyacad embeds a Python interpreter into a running Autocad/Intellicad drawing session as an ADS/ARX based module, which loaded as a DLL (>=R13/Ic2000) or as a seperate application with IPC (<=R12).

This allows to execute Python code which may interact with the Autocad/Intellicad process and any loaded drawing file.

Loading Pyacad

First of all, you need to make sure that Autocad/Intellicad can find Pyacad. This is done by configuring the directory where the pyacad.lsp and the various DLLs are located in the right place. With Autocad, you can either add this directory to the support paths in the Options dialog, or you can set the ACAD environment variable of the operating system. For Intellicad, the same directory should be included in the PATH environment variable of the operating system. After changing any environment variable, you'll have to quit Autocad/Intellicad and start it again.

The easiest way to load Pyacad is through the pyacad.lsp file. This can be done at the Autocad/Intellicad commandline, by typing (load "pyacad"). The Autolisp code will try to find the right ADS/ObjectARX library for your version of Autocad/Intellicad, and load that. Once the library is loaded, Initialization of the Python interpreter takes place.

Lisp Functions

The following extension functions are currently implemented, that can be accessed through Autolisp or the ads_invoke() interface from other ADS/ARX/SDS applications.

(PyRun_SimpleStrings <string>...)
(PyRun_SimpleStrings '(<string>...))

Accepts a list of text strings, which will be executed as python statements in the global namespace. It is also possible to supply just the strings without the list as arguments, but use of this calling convention is not recommended, as it is likely to be removed in a future version, and the (pys) shortcut function defined by the pyacad.lsp file doesn't support it anyway. It is an error, if one of the strings is not a valid Python statement (eg. an incomplete statement or an expression).

(PyRun_String <string>)

Accepts one string as argument, which will be executed as a python expression or statement. If the return value of an expression can be converted, it is returned to the lisp environment. Unconvertable return values are treated as an error.

(PyRun_SimpleFile <string>)

Accepts a file name as argument, imports the file and executes it in the global namespace.

*pythonoptimize*

When the global Autolisp symbol *pythonoptimize* is set to an integer before Pyacad is loaded, then the internal variable Py_OptimizeFlag will be set to that value. Values other than zero cause the parser to strip clauses like the following from the code:

    if __debug__: ...

The generated byte code files will then get the extension *.pyo instead of *.pyc.
The alternative to this is to set the PYTHONOPTIMIZE environment variable before starting Autocad/Intellicad.

Python code executed in one of the PyRun_Xxx functions can import the ads module from the support library, which offers direct access to much of the Autocad/Intellicad functionality.