Automatically execute command when open/create new file

Last week we covered about loading AutoLISP file. You learned how to load, and optionally put your LISP to startup suite. Startup suite will load LISP automatically every time you start AutoCAD.

In this post, you will learn how to load AutoLISP program using acaddoc.lsp.

We also had another post about excel datalink. The problem with datalink is, it’s not updated automatically. We have to update it manually. It means we may send or plot files that use old value.

In this post, you will also learn how to make AutoCAD automatically update datalink when we save or plot our files. We will define our own plot and save command.

Getting started with acaddoc.lsp

What is ACADDOC.LSP?

In short, it’s also an AutoLISP file. The difference is, AutoCAD will execute it every time we create or open a drawing file. If you want further explanation, Jimmy Bergmark explain about ACADDOC.LSP in details here.

Creating acaddoc.lsp

Create a new LISP file. You may use text editor or visual LISP editor to do it. Save it to a support file path. You may choose any existing path, or create your own support path.

I would recommend you to do the later, and you can also save all your LISP file there.

support_file_search_path

Loading AutoLISP program in acaddoc.lsp

You can load AutoLISP program using this line:

(LOAD "MYLISP")

This sample below will load cnlabel.lsp and zoomextend.lsp.

load_autolisp

Loading AutoLISP command is something quite basic we can do with acaddoc.lsp. Next, we will try to define our own command here.

Undefine and define PLOT function in acaddoc.lsp

In the next step, we will change how AutoCAD command work. Like we discuss before, we will change plot and save command. First, we need to undefine it.

Undefine and redefine

We can undefine AutoCAD command using UNDEFINE.

In AutoCAD command line, type UNDEFINE [enter] then PLOT [enter].

We just undefine plot command. Try to type plot in command line and see how AutoCAD will response.

Command: plot
Unknown command “PLOT”.  Press F1 for help.

After we undefine PLOT, AutoCAD will not recognize it anymore.

Now try type .PLOT [enter]. Notice the dot before PLOT command. Plot command should work. The dot means we use AutoCAD built in command.

After we finished, PLOT and .PLOT will give different result. The first one will use plot we define in acaddoc.lsp. The last one use AutoCAD built-in plot command.

You can activate PLOT command back using REDEFINE [enter] PLOT [enter]

Defining function in acaddoc.lsp

Now let’s work with acaddoc.lsp. First we undefine PLOT, then define new command. The new plot command should update datalink before start to plot.

Our code become like this:

; This line below will undefine existing PLOT command
(command "undefine" "plot")

; This line below will define new PLOT command
(defun c:PLOT ( )
(command "_DATALINKUPDATE" K)
(initdia)
(command ".plot")
)

We use INITDIA to load AutoCAD plot dialog. If we don’t use INITDIA before plot, then you will not see the dialog box. You will need to setup the plot using command line.

plot_config

Save this AutoLISP file. Create or open a new file then activate plot. You will see after I create a new file, our code will undefine plot. Then when we use PLOT command, it updates datalink first, then activate plot. Nice, isn’t it?


Regenerating model.

AutoCAD menu utilities loaded.undefine Enter command name: plot
Command:
Command:
Command: PLOT
_DATALINKUPDATE
Select an option [Update data link/Write data link] <Update data link>:

Command: .plot

 

 

Update datalink when saving file

Now we don’t have to worry when we plot files. It surely will use most updated value from excel files. But we haven’t finished. We need to make sure datalinks in our files are updated before we send them. We need to modify save and saveas command.

It’s very similar with plot. You can modify them as your exercise. Discuss it here if you have problem.

How do you think acaddoc.lsp can be useful?

Do you think you can make use of acaddoc.lsp? Do you prefer to load AutoLISP from startup suite or this method?

And what command you want to modify? What do you want it to do? Share it here, the others might find it useful too!

Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  1. hamid says

    dear master
    i need learning interactive autolisp & excel, i very searched in google , but unfortunately not find that explain step to step in case for me, i very confused , please help me.
    thanks a lot
    i hope your successful.

  2. cad says

    I redefined the close function with a datalinkupdate write, and found that this created issues when the dwg has no data links. Is there a way to make the datalinkupdate conditional on whether there are any datalinks in the dwg?