Jump to content

Rusty LISP Skills


Bill Tillman

Recommended Posts

Trying to dust off some code here for a new project and finding out how rusty I've gotten with LISP. So before I go to my real question can someone tell me why this works:

(defun c:sk (/ dxf ent)
  (defun dxf (code ent)(cdr (assoc code (entget ent))))  

 (if (and (setq ent (car (entsel "\nSelect an Attributed Block: ")))
          (eq "INSERT" (dxf 0 ent))
          (= 1 (dxf 66 ent)))

   (while (not (eq "SEQEND" (dxf 0 (setq ent (entnext ent)))))
     (princ (strcat "\n\nAtt_Tag:" (dxf 2 ent) "\nAtt_Value: " (dxf 1 ent)))))

 (princ))

But this doesn't

(defun c:sk (/ dxf ent)  

 (if (and (setq ent (car (entsel "\nSelect an Attributed Block: ")))
          (eq "INSERT" (dxf 0 ent))
          (= 1 (dxf 66 ent)))

   (while (not (eq "SEQEND" (dxf 0 (setq ent (entnext ent)))))
     (princ (strcat "\n\nAtt_Tag:" (dxf 2 ent) "\nAtt_Value: " (dxf 1 ent)))))

 (princ)
 )

(defun dxf (code ent)
 (cdr (assoc code (entget ent)))
)

The only difference is that the 2nd defun is outside of the first one in the version that does not work. I have seen code like this before with defun's inside of the main defun but never really understood it.

Link to comment
Share on other sites

Remove dxf from the list of local variables declared for your c:sk function and the second will work also; with the symbol dxf declared as a local symbol, new memory is allocated for this symbol within the scope of the c:sk function, with the symbol resuming its previous value following evaluation of the c:sk function.

Link to comment
Share on other sites

Some example in code of what Lee described:

(defun C:test ( / fun )
 (defun fun ( msg ) (alert msg))
 (fun "Im working with the localised 'fun'!")
 (princ)
)

(defun C:test ( / )
 (fun "Im working with the global 'fun'!")
 (princ)
)
(defun fun ( msg ) (alert msg))

(defun C:test ( / fun )
 (fun "Im expecting the localised 'fun' you declared and no, I won't use the global one!")
 (princ)
)
(defun fun ( msg ) (alert msg))

Link to comment
Share on other sites

This may also help you understand the concept of scope:

(setq var "Global")

(defun foo ( / var )

   (defun bar ( / var )
       (print var)
       (setq  var "Local to bar")
       (print var)
   )

   (print var)
   (setq  var "Local to foo")
   (print var)
   
   (bar)
   (print var)
   
   (princ)
)

See if you can predict the output before loading and running the above with (foo)

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...