Jump to content

Integrate lisp routines in AutoCAD template


EIA

Recommended Posts

Have you looked at Appload "start up suite" and added your lisps there. This will load all in the suite.

 

You would have to do a custom command to open a dwt and load certain lisps you would need a script. 

 

I made custom menu's for CIV3D so that when in drafting workspace I had access to CIV3D commands that I used often, you open CUI and copy the commands into a MNU made in notepad. Almost what you want.

Link to comment
Share on other sites

Not quite sure what you mean exactly, 

 

Do you want the LISPs available to use, use APPLOAD command and put the required LISPs in the startup suit - then they should be loaded and ready to use

 

If you want them to autorun, (not sure if this is what you want), you can put something like (c:MyLisp) line after the LISP definition, so when it loads LT sees the line (c:MyLisp) - or whatever the LISP is called (remember to include the brackets) - and it runs the LISP.

 

However if you want the LISP to AutoRun only when a specific file opens (like your dwt) then a small LISP might be necessary to check what the current file is and then run. 

 

My CAD is off for the evening but BigAl has just replied - probably has some little gem 

Link to comment
Share on other sites

There's one thing you need to understand. AutoLISP is not something that is "attached" to a dwt or dwg, it's a program that you load into an open drawing. Normally, users will simply load those files into the Startup Suite, with which they will be automatically loaded whenever a drawing is opened. This is ideal if you only have a few LISP routines to include.

 

However, if you are working with hundreds (if not thousands) of LISP files (as in my case), having to put each one into the Startup Suite is a pain. I'm pretty sure everyone has different solutions or there's probably a way, but my work-around is like this (it's probably not a healthy approach, but it works for me):

 

  1. Create a folder where you store all your LISP codes. You can use subfolders within it to organize your codes if you need to.
  2. In AutoCAD, go to Options and add the path to that folder into the Trusted Locations using the OPTIONS command. End the folder path with \... (as shown in the example Program Files path) to include subfolders too. (This is an optional step if you want to make it a trusted location.)
  3.  image.png.1466af77e48c166658b345da627e7099.png
  4. Save this code below into a new LISP file (for example, "LoadMe.lsp"), and change *LoadMe_Root* to the folder path: 
    (setq *LoadMe_Root* "C:\\Program Files\\Autodesk\\AutoCAD 2021\\Express")  ;; <--- Example Path, change it as desired
    
    ;; Directory Files  -  Lee Mac
    ;; Retrieves all files of a specified filetype residing in a directory (and subdirectories)
    ;; dir - [str] Root directory for which to return filenames
    ;; typ - [str] Optional filetype filter (DOS pattern)
    ;; sub - [bol] If T, subdirectories of the root directory are included
    ;; Returns: [lst] List of files matching the filetype criteria, else nil if none are found
    
    (defun LM:directoryfiles ( dir typ sub )
        (setq dir (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir)))
        (append (mapcar '(lambda ( x ) (strcat dir "\\" x)) (vl-directory-files dir typ 1))
            (if sub
                (apply 'append
                    (mapcar
                       '(lambda ( x )
                            (if (not (wcmatch x "`.,`.`."))
                                (LM:directoryfiles (strcat dir "\\" x) typ sub)
                            )
                        )
                        (vl-directory-files dir nil -1)
                    )
                )
            )
        )
    )
    
    (defun c:reload ( / i)
        (setq i 1)
        (mapcar
            '(lambda (x)
                (if
                    (   (lambda ( / i)
                            (vl-catch-all-error-p
                                (vl-catch-all-apply 'load (list x))
                            )
                        )
                    )
                    (progn
                        (alert (strcat "Unable to load \"" x "\" (item number " (itoa i) " in list). Please check if spelling is correct and if file directory exists!"))
                        T
                    )
                    (progn (setq i (1+ i)) nil)
                )
            )
            (vl-remove-if
                '(lambda (x)
                    (not (wcmatch (strcase x t) "*.lsp,*.fas"))
                )
                (LM:directoryfiles *LoadMe_Root* "*.*" T)
            )
        )
        T
    )
    
    (c:reload)
  5. Put this LoadMe.lsp file into the Startup Suite, and you're all set. Once loaded, all your LISP routines within that folder and its subfolders will be loaded, and any new LISP files created will then also be automatically loaded. You can use RELOAD if you need to reload all of them again (for whatever reason).
Link to comment
Share on other sites

Like @Jonathan Handojo if you look at the image this menu has like 130 lisps behind it but maybe 2 or 3 are autoloaded. The rest are loaded when a menu item is chosen. Ok you can have multiple workspaces the reason I mention is that workspaces can have different menu's loaded this would match your DWT idea.

 

 

Menu 16.png

Link to comment
Share on other sites

17 hours ago, EIA said:

I want to get the custom commands I write in an lsp file everytime I open a certain dwt.

Perhaps this theme will suit you

https://www.cadtutor.net/forum/topic/79014-put-the-text-in-the-command-string-without-copying-and-pasting/

Using the Lee Mac code, you can point to the line with the command and lisp will load and execute the command.
The Lee Mac code needs to be added to the startup.
Your lisps do not need to be added to the startup.

;; Lee Mac
;; https://www.cadtutor.net/forum/topic/79014-put-the-text-in-the-command-string-without-copying-and-pasting/
(defun c:ctext ( / ent enx str )
    (while
        (not
            (progn
                (setvar 'errno 0)
                (setq ent (car (entsel "\nSelect command text: ")))
                (cond
                    (   (= 7 (getvar 'errno))
                        (prompt "\nMissed, try again.")
                    )
                    (   (null ent))
                    (   (not (wcmatch (cdr (assoc 0 (setq enx (entget ent)))) "*TEXT"))
                        (prompt "\nThe selected object is not text or mtext.")
                    )
                    (   (setq str (cdr (assoc 1 enx))))
                )
            )
        )
    )
    (if str (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) (strcat str "\n")))
    (princ)
)
(vl-load-com) (princ)

 

file dwg or dwt.png

Link to comment
Share on other sites

You could add some kind of custom property to dwt, name for example, then when opening that dwt test by name which lisp file you want to load into that drawing
I assume this is because you have lisps with same name but do different things in different dwt? If that's the case, I'd make single lisp file, add to stratup suite, and in each function test condition for dwt custom property name

Link to comment
Share on other sites

Something like this (untested by the way, be aware of typos).

 

User needs to change a few details - drawing template names etc. and add this to the startup suit. It should load a LISP file and run routines in it if the current drawing is defined in this LISP. Could be neater coding but quick and easy works just as well.

 

From lastknownusers comment, you can have lisps with the same name saved in different files, this will load a specified file and so those LISPs will be the active ones as this runs.

 

(Defun C:TestForDrawing ( / MyTemplate1 MyTemplate2 MyTemplate3 )

  (setq MyTemplate1 "MyTemplateFile1.Dwt")  ;;User hard coded template file name 1
  (setq MyTemplate2 "MyTemplateFile2.Dwt")  ;;User hard coded template file name 1
  (setq MyTemplate3 "MyTemplateFile3.Dwt")  ;;User hard coded template file name 1


  (if (or  ;; If current drawing is in template list
      (= (strcase (getvar 'dwgname)) (strcase MyTemplate1))  ;;User updates these 3 lines according to
      (= (strcase (getvar 'dwgname)) (strcase MyTemplate2))  ;;'Mytemplate' defined above
      (= (strcase (getvar 'dwgname)) (strcase MyTemplate3))  ;;Should do this as a list, but quick and dirty
    )
    (progn ;; Do this
      (load "C:/SP/AutoCAD/Lisps/AutoRunLisps.lsp")  ;;Load this file: User hard codes this
       (c:AR1)                                       ;;Run this routine saved in above file
       (c:AR2)                                       ;;And this
       (c:AR3)                                       ;;And this...
      (load "C:/SP/AutoCAD/Lisps/TemplateLisps.lsp") ;;Another file to load and run
       (c:TL1)
       (c:TL1)
       (c:TL1)
    ) ; end progn
  ) ;  end if
) ; end defun
(C:TestForDrawing) ;; Autorun on load

 

 

 

  • Like 1
Link to comment
Share on other sites

When you open a dwt it loses its name as a dwt and becomes a dwg, as suggested by @lastknownuser you can store a variable in the dwt using LDATA. A startup lisp can look for that value. 

 

; inside dwt do this once
(vlax-ldata-put "DWTS" "DWTNAME" "actualdwtname")

; autoload lisp
(if (= (vlax-ldata-get "DWTS" "DWTNAME") nil)
(princ "\nNot a dwt")
(load "all the lisps here")
)

 

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...