Nate Holt's Blog

August 13, 2009

How to execute a Lisp function, all dwgs / project-wide – AutoCAD Electrical

Filed under: Electrical, Tips — nateholt @ 5:23 pm

AutoCAD Electrical’s “project-wide utilities” command has an option to process a script file against each drawing in the active AutoCAD Electrical project. You set up the script file and then enter its name in the box as shown in the utility’s dialog:

script01

But, little known… instead of entering a script file name, you can enter a one-line AutoLISP function in this edit box (cannot browse to it!). Instead of launching a script file against each drawing in the project, the utility will apply this Lisp function against each drawing in the project set ( ! ). Neat and clean. No need to create a separate script file with the Lisp function embedded in it… just enter the single-line lisp function and off you go.

Example – Lisp utility to work-around Model/Paper space Mtext cross-reference issue

A user noticed a strange issue with his Paper space schematics created with AutoCAD Electrical. Whenever he would scoot a child component on a drawing that had its parent on some other drawing, the Mtext-based cross-reference annotation for the parent would disappear. He enlisted some help from N8 Consultants and discovered that AutoCAD Electrical was mistakenly pushing the new cross-reference Mtext out to Model space instead of the active Paper space tab.

Autodesk support was notified. But, in the meantime, the temporary work-around was to create a little AutoLISP utility that checks all cross-reference Mtext annotation on the active drawing and makes sure that it matches up with the “space” of the parent.

The complication is that this AutoLISP function needed to be run periodically against a lot of drawings (i.e. all schematic drawings in the active project). Launching it, one drawing at a time, was not something to look forward to.

The solution was to use AutoCAD Electrical’s “Project Wide Utilities” command as described above. But to keep it really simple, N8 set it up so that the user could enter in a one-line lisp function in the edit box normally reserved for a script file’s name. Launching the Project-Wide Utilities command then ran this lisp function against each selected dwg in the project set.

Setting up the Lisp function to auto-run as soon as it loads

This made it really simple. Here is the full “Paper space Cross-reference checker” utility. Note the two lines tacked on to the very end of the function…

; 13-Aug-2009 Nate Holt, http://n8consultants.com
; -------   P A P E R S P A C E _ X R E F . L S P  -----
; Process block inserts. Look for cross-ref Mtext tied via Xdata pointer. Make sure Mtext is in
; same space as the parent block insert. Work-around for problem with ACE2009, ACE2010 (and
; perhaps others) when parent is on a layout tab and insert/move related child on some other
; drawing. Cross-ref Mtext back on the parent sometimes ending up incorrectly pushed out to
; Model space even through the parent is in Paper space.
(defun c:paperspace_xref ( / ss ix slen parent_ed en mtext_ed newed hdl
              parent_410 mtext_410)
  ; Process all block inserts
  (setq ss (ssget "_X" '((0 . "INSERT"))))
  (if (/= ss nil)
    (progn
      (setq slen (sslength ss))
      (setq ix 0)
      (while (< ix slen)
        (setq en (ssname ss ix)) ; get next block insert instance
        ; Look for pointer to graphical cross-reference Mtext. This is carried as Xdata on
        ; the parent component and points to the "handle" of the cross-ref Mtext entity.
        (if (setq hdl (c:wd_get_1005_xdata en "VIA_WD_XREF_G"))
          (progn ; okay, found block insert instance that points at Mtext
            (if (handent hdl) ; valid handle, Mtext not deleted
              (progn
                ; Open the Mtext entity
                (setq mtext_ed (entget (handent hdl)))
                ; Open the block insert entity
                (setq parent_ed (entget en))
                ; Get the "space" each is in
                (setq mtext_410 (cdr (assoc 410 mtext_ed)))
                (setq parent_410 (cdr (assoc 410 parent_ed)))
                (if (/= mtext_410 parent_410)
                  (progn ; Problem. Try to force
                         ; the Mtext entity into the same space as
                         ; the parent. Adjust the 410 and 67 subrecords.
                    (setq newed (subst (assoc 410 parent_ed)(assoc 410 mtext_ed) mtext_ed))
                    ; 67 carries a value of 1 if paper space, 0 or missing if in model space
                    (if (AND (assoc 67 parent_ed) (assoc 67 mtext_ed))                   
                      (progn ; 67 subrecords different
                        (setq newed (subst (assoc 67 parent_ed)(assoc 67 mtext_ed) newed))
                        (entmod newed) ; update the Mtext entity
                        (princ "\nMoved Mtext hdl ")
                        (princ hdl)
                        (princ " from ")
                        (princ (cdr (assoc 410 mtext_ed)))
                        (princ " to ")
                        (princ (cdr (assoc 410 parent_ed)))
                ) ) ) )
            ) )
        ) )
        (setq ix (1+ ix))
      )
  ) )
  (setq ss nil) ; release the selection set
  (princ) ; quiet return
)    
; Set up to auto-run this function as soon as it is loaded
(c:paperspace_xref)
(princ)
 
 
script03
 
   Referencing the auto-start Lisp function in Project-Wide Utilities command

We don’t have to APPLOAD this lisp utility to make it run. We can do the same thing on the command line with a “(load …)” lisp function (tested on ACE2009 and ACE2010)

 

Let’s say that our above utility’s full file name is “c:/temp/paperspace_xref.lsp”. Here is how we reference it in the Project-Wide Utilities edit box (shown in yellow in the screen-shot).

We enter (load “c:/temp/paperspace_xref.lsp”) in the edit box. But we need to use forward slashes (or double backslashes) in the file’s name.

Hit OK. Select drawings to process… and that’s it. We don’t have to APPLOAD the function or embed it into a script file or add it to a Startup Suite of functions. We just reference it here in the “(load <filename>)” line and we’re good to go.

3 Comments »

  1. […] work! Now batch this tool against a bunch of symbols (make a backup first, just in case!). In short order your library should be modified. Possibly […]

    Pingback by Nate’s Simple AutoLISP – Lesson 006 « AutoCAD Electrical Etcetera — September 26, 2009 @ 4:14 pm

  2. I wants to know , how to write a program in lisp and how to execute program in lisp; and small programs

    Comment by Vijayakumar — November 11, 2009 @ 6:32 am

    • Hi,
      Pick one of the utilities described in this blog. Download it. Make sure it has a “.lsp” file extension. Then in AutoCAD, at the “Command:” prompt type APPLOAD [Enter]. Browse to the “.lsp” file and “Load” it. Now it is in memory for your active drawing. You can invoke the command by typing the command name as described in the blog posting.

      If you want to create your own lisp utility, find one on the blog (or elsewhere) that is “similar” to what you want to do. Copy it to a new name and then start editing. You can use the Visual Lisp editor (type VLIDE [Enter] at the “Command:” prompt) or use any ASCII text editor like Wordpad.exe.

      Comment by nateholt — November 13, 2009 @ 12:00 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 39 other followers

%d bloggers like this: