lisp/script help

Discussion in 'AutoCAD' started by MR.DEE, Mar 8, 2005.

  1. MR.DEE

    MR.DEE Guest

    in a prior post it was suggested that in order to have a script load into every dwg I would need to add a lisp to my acad.lsp doc. I've never written one before so heres my feeble attempt

    ;;;-----------------------------------------------------------------------------
    ;Loads Scirpt File H:documentation\users\scra\startup

    (defun (script h:\documentation\users\scra\startup))

    what I don't know is after calling for the script file do I also need to write a line to run it? also the little I've done doesn't seem to load anything, in fact I get a "; error: syntax error" which can't be good. like I said I'm not very good at this so I'm probably not even close to what I need, s any advice is greatly appreciated
     
    MR.DEE, Mar 8, 2005
    #1
  2. MR.DEE

    Mikel Martin Guest

    Try adding this to the end of the Acad2005doc.lsp

    (if (findfile "C:/temp/test.scr")
    (command "script" "C:/temp/test.scr")
    )

    Note: you will want to change the name and location of the file.



    every dwg I would need to add a lisp to my acad.lsp doc. I've never written
    one before so heres my feeble attempt
    write a line to run it? also the little I've done doesn't seem to load
    anything, in fact I get a "; error: syntax error" which can't be good. like
    I said I'm not very good at this so I'm probably not even close to what I
    need, s any advice is greatly appreciated
     
    Mikel Martin, Mar 8, 2005
    #2
  3. MR.DEE

    Jim Dee Guest

    Try this...

    (command "_.script" "h:\\documentation\\users\\scra\\startup")

    Jim Dee
    www.caddee.com
     
    Jim Dee, Mar 8, 2005
    #3
  4. MR.DEE

    wkiernan Guest

    It's trickier than you'd think. First, you have to understand the distinction between ACAD.LSP and ACADDOC.LSP. ACAD.LSP _always_ loads when you first open the _first_ drawing in an AutoCAD session. But sometimes you can open more than one drawing file in a single AutoCAD session. (And sometimes you can't; if you use Land Development Desktop, for example. See the online help regarding the SDI system variable.)

    Now depending upon the setting of the ACADLSPASDOC system variable either ACAD.LSP will run (ACADLSPASDOC = 1) or it won't (ACADLSPASDOC = 0) when you open a second, third, etc. drawing in a given AutoCAD session. However, regardless of how the ACADLSPASDOC variable is set, if a file called ACADDOC.LSP exists and is in the search path, AutoCAD will always run ACADDOC.LSP whenever you open any drawing file, whether it is the first one you open in an AutoCAD session or the second or third or whatever. So if you have code you want to load with every drawing, put it in ACADDOC.LSP instead of ACAD.LSP.

    Here's the catch: both ACAD.LSP and ACADDOC.LSP run before the drawing editor is initialized. Until the drawing editor initializes, you can't run any commands, such as the SCRIPT command. So you can't run any commands directly from ACAD.LSP or ACADDOC.LSP. There's a workaround, however. If your ACADDOC.LSP defines the function S::STARTUP (note two colons) then as soon as the drawing editor is initialized, S::STARTUP will run.

    So here's ACADDOC.LSP, which will run your script whenever you open a drawing:

    (defun S::STARTUP()
    (command "script" "H:\\documentation\\users\\scra\\startup")
    )

    Two notes. In AutoLISP, a single backslash is an escape character. For example, in a string in AutoLISP, "\n" represents a carriage return - line feed; you might write:

    (princ "Here is a line of text and here\nwe drop down and start a new line.")

    which will display

    Here is a line of text and here
    we drop down and start a new line.

    But what if you want to put a file name which includes backslashes into a string in AutoLISP? The special code for a literal backslash is "\\" So to specify a path name which includes backslashes, you have to do double-backslashes, thus:

    (command "script" "H:\\documentation\\users\\scra\\startup")

    Second, another application (such as Land Development Desktop) might have already defined S::STARTUP. When your ACADDOC.LSP redefines S::STARTUP it overwrites the previously existing S::STARTUP. Your new S::STARTUP will execute, but whatever functions were in the original version don't get executed, which will probably break the other program which defined S::STAARTUP earlier. So we've got to use yet another work-around. Here's the final version of ACADDOC.LSP, including the work-around which saves your old S::STARTUP function as OLD_S::STARTUP, then redefines S::STARTUP so it runs OLD_S::STARTUP.

    ; ACADDOC.LSP
    (if S::STARTUP
    (setq old_s::Startup S::STARTUP)
    (setq old_s::startup nil)
    )
    (defun-q S::STARTUP()
    (if old_s::startup
    (old_s::startup)
    )
    (command "script" "H:\\documentation\\users\\scra\\startup")
    )
     
    wkiernan, Mar 8, 2005
    #4
  5. MR.DEE

    mr.dee Guest

    holy crap, now my head hurts. most of that made sense though. thanks for the help it worked great. so if I want to exicute a command its always (command "type command here" then the path)?
     
    mr.dee, Mar 8, 2005
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.