• Home
  • Training Books
  • Subscribe to Our Email Newsletter
  • About
    • Contributors
    • Feedback
    • Contact
    • Privacy policy
    • Cookie Policy

CADnotes

CAD Tutorials and Best Practices for professionals and students

  • Featured
  • AutoCAD
    • AutoLISP
  • Revit
    • Revit Architecture Basic
    • Revit MEP Basic Tutorial
  • Inventor
  • MicroStation
    • MicroStation Basic Tutorial
You are here: Home / AutoCAD / AutoLISP / Automatically execute command when open/create new file

Automatically execute command when open/create new file

October 25, 2011 by Edwin Prakoso 4 Comments

In this Article...

  • Getting started with acaddoc.lsp
    • What is ACADDOC.LSP?
    • Creating acaddoc.lsp
    • Loading AutoLISP program in acaddoc.lsp
  • Undefine and define PLOT function in acaddoc.lsp
    • Undefine and redefine
    • Defining function in acaddoc.lsp
  • Update datalink when saving file
  • How do you think acaddoc.lsp can be useful?

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!

About Edwin Prakoso

I work as a Sr. Consultant in PT Cipta Satria Informatika. I've been using AutoCAD since R14 and Revit since Revit Building 9. I occasionally write for AUGIWorld magazine and I am also active in Autodesk discussion forum. I'm a member of Autodesk Expert Elite, an appreciation for individuals who give contributions to the Autodesk community.
Connect with me on twitter or LinkedIn.

Filed Under: AutoLISP Tagged With: acaddoc.lsp, define function, load autolisp, undefine

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Inline Feedbacks
View all comments
soma narayan
soma narayan
8 years ago

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.

0
Reply
hamid
hamid
10 years ago

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.

0
Reply
cad
cad
12 years ago

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?

0
Reply
Edwin Prakoso
Edwin Prakoso
Reply to  cad
12 years ago

You can try this:

(command "undefine" "CLOSE")
(defun c:CLOSE ()
(if (/= (dictsearch (namedobjdict) "ACAD_DATALINK") nil)
(command "_DATALINKUPDATE" U K)
)
(command ".CLOSE")
)

0
Reply
wpdiscuz   wpDiscuz

Featured

AutoCAD Essentials: 27 CADnotes Best Tutorials and Tips

This compilation list 27 tutorials, tips and trick that CADnotes published in the last 8 years. A great place to start if you want to learn advanced features or learn new cool tricks! And it’s free!

Recent Articles

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available

Advertisement

New on CADnotes

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available
  • Autodesk Forma Design Contest
  • Revit 2025: Toposolid Enhancements

Meet the Authors

avatar for
avatar for
avatar for
avatar for
avatar for
avatar for

Get Connected

CADnotes on FacebookCADnotes on InstagramCADnotes on TwitterCADnotes on YouTube

© 2009 – 2025 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz