Jump to content

Doubting my LISP Logic...


Lee Mac

Recommended Posts

I have been trying to set a list of variables using mapcar, but can't seem to get them to hold the values. I am not sure why...

 

I have tried:

 

(defun c:test (/ vlst v1 v2 v3)
 (setq vlst (list v1 v2 v3))
 (mapcar '(lambda (x y) (setq x y)) vlst '(1 2 3))
 (princ))

 

to set the symbols "v1" "v2" & "v3" to 1, 2, 3 respectively, but this just keeps the variable "vlst" to (nil nil nil), and won't set them.

 

Maybe I am having a stupid moment, or maybe there is a completely different function needed to accomplish this, but any ideas or opinions are welcomed.

 

Cheers

 

Lee

Link to comment
Share on other sites

Hey Lee,

 

I think the problem lies with declaring the symbols V1,V2 &V3 as variable names using SETQ.

 

Using the function (setq x y) you are actually pass two arguments; X to create the variable and Y to assign it a value.

 

 

In this instance I think SET would seem to be more appropiate

 

From the acad support on SET

 

The set function is similar to setq except that set evaluates both of its arguments whereas setq only evaluates its second argument

 

 

Try the following

 

  
 (defun c:test (/ vlst)
 (setq vlst '(v1 v2 v3))
 (mapcar '(lambda (x y) (set x y)) vlst '(1 2 3))
 (princ))

 

Hope this is of some help,

 

Jammie

Link to comment
Share on other sites

Lambda isn't need:

 

Command: (mapcar 'set '(v1 v2 v3) '(1 17.2 "Lee Mac"))
(1 17.2 "Lee Mac")

 

Command: !v1
1
Command: !v2
17.2
Command: !v3
"Lee Mac"

Link to comment
Share on other sites

Hey Lee,

 

I think the problem lies with declaring the symbols V1,V2 &V3 as variable names using SETQ.

 

Using the function (setq x y) you are actually pass two arguments; X to create the variable and Y to assign it a value.

 

 

In this instance I think SET would seem to be more appropiate

 

From the acad support on SET

 

 

 

 

Try the following

 

  
 (defun c:test (/ vlst)
 (setq vlst '(v1 v2 v3))
 (mapcar '(lambda (x y) (set x y)) vlst '(1 2 3))
 (princ))

Hope this is of some help,

 

Jammie

 

Lambda isn't need:

 

Command: (mapcar 'set '(v1 v2 v3) '(1 17.2 "Lee Mac"))
(1 17.2 "Lee Mac")

Command: !v1
1
Command: !v2
17.2
Command: !v3
"Lee Mac"

 

 

Ahhh! Many thanks guys for your time and patience :)

 

I have never used "set" and so it didn't come to mind when thinking of which functions to use.

 

Thank you both for such clear explanations :)

 

Cheers

 

Lee

Link to comment
Share on other sites

Thanks guys, put the knowledge to good use :thumbsup:

 

;; ============ Num.lsp ===============
;;
;;  FUNCTION:
;;  Will sequentially place numerical
;;  text upon mouse click, with optional
;;  prefix and suffix.
;;
;;  SYNTAX: num
;;
;;  AUTHOR:
;;  Copyright (c) 2009, Lee McDonnell
;;  (Contact Lee Mac, CADTutor.net)
;;
;;  PLATFORMS:
;;  No Restrictions,
;;  only tested in ACAD 2004.
;;
;;  VERSION:
;;  1.0  ~  05.04.2009
;;
;; ====================================


(defun c:num  (/ vlst ovar dVars tmpVars pt)
 (setq    vlst '("OSMODE" "CLAYER")
   ovar (mapcar 'getvar vlst))
 (setvar "OSMODE" 0)
 (or (tblsearch "LAYER" "NumText")
     (vla-put-color
   (vla-add
     (vla-get-layers
       (vla-get-ActiveDocument
         (vlax-get-acad-object))) "NumText") acYellow))
 [color=Red][b](setq dVars '(sNum inNum Pref Suff))
 (mapcar '(lambda (x y) (or (boundp x) (set x y))) dVars '(1 1 "" ""))[/b][/color]
 (setq tmpVars (list (getreal (strcat "\nSpecify Starting Number <" (rtos sNum 2 2) ">: "))
             (getreal (strcat "\nSpecify Increment <" (rtos inNum 2 2) ">: "))
             (getstring (strcat "\nSpecify Prefix <" (if (eq "" Pref) "-None-" Pref) ">: "))
             (getstring (strcat "\nSpecify Suffix <" (if (eq "" Suff) "-None-" Suff) ">: "))))
 [b][color=Red](mapcar '(lambda (x y) (or (or (not x) (eq "" x)) (set y x))) tmpVars dVars)[/color][/b]
 (while (setq pt (getpoint "\nClick for Text... "))
   (Make_Text pt (strcat Pref (rtos sNum 2 2) Suff))
   (setq sNum (+ sNum inNum)))
 (mapcar 'setvar vlst ovar)
 (princ))

(defun Make_Text  (txt_pt txt_val)
 (entmake (list '(0 . "TEXT")
        '(8 . "NumText")
        (cons 10 txt_pt)
        (cons 40 (max 2.5 (getvar "TEXTSIZE")))
        (cons 1 txt_val)
        '(50 . 0.0)
        (cons 7 (getvar "TEXTSTYLE"))
        '(71 . 0)
        '(72 . 1)
        '(73 . 2)
        (cons 11 txt_pt))))

Link to comment
Share on other sites

Your welcome Lee,

 

Glad to be of help. I learned a bit more about Set and Setq myself from this post

 

Nice work with the routine as well

 

-Jammie

Link to comment
Share on other sites

Your welcome Lee,

 

Glad to be of help. I learned a bit more about Set and Setq myself from this post

 

Nice work with the routine as well

 

-Jammie

 

Cheers Jammie :)

Link to comment
Share on other sites

That's a really nice program, Lee, but in order for a leading zero to be retained, I have to make it the prefix, or include it as part of the prefix. Is there a way around this characteristic of the program? Prefix VB and number 0801 becomes VB801 unless I make the prefix VB0.

 

I ask because you have made the program easy to modify, so that if I don't need a prefix or suffix, those lines can be commented out, and they will not be a bother. But until 2010, I could sure use a leading zero.

 

Thanks,

Steve

Link to comment
Share on other sites

Well, this is a quick fix, but not ideal

 

;; ============ Num.lsp ===============
;;
;;  FUNCTION:
;;  Will sequentially place numerical
;;  text upon mouse click, with optional
;;  prefix and suffix.
;;
;;  SYNTAX: num
;;
;;  AUTHOR:
;;  Copyright (c) 2009, Lee McDonnell
;;  (Contact Lee Mac, CADTutor.net)
;;
;;  PLATFORMS:
;;  No Restrictions,
;;  only tested in ACAD 2004.
;;
;;  VERSION:
;;  1.0  ~  05.04.2009
;;
;; ====================================


(defun c:num  (/ vlst ovar dVars tmpVars pt)
 (vl-load-com)

 (setq Zer0 T)   ; Leading Zero (T or nil)
 
 (setq    vlst '("OSMODE" "CLAYER")
   ovar (mapcar 'getvar vlst))
 (setvar "OSMODE" 0)
 (or (tblsearch "LAYER" "NumText")
     (vla-put-color
   (vla-add
     (vla-get-layers
       (vla-get-ActiveDocument
         (vlax-get-acad-object))) "NumText") acYellow))
 (setq dVars '(sNum inNum Pref Suff))
 (mapcar '(lambda (x y) (or (boundp x) (set x y))) dVars '(1 1 "" ""))
 (setq tmpVars (list (getreal (strcat "\nSpecify Starting Number <" (rtos sNum 2 2) ">: "))
             (getreal (strcat "\nSpecify Increment <" (rtos inNum 2 2) ">: "))
             (getstring (strcat "\nSpecify Prefix <" (if (eq "" Pref) "-None-" Pref) ">: "))
             (getstring (strcat "\nSpecify Suffix <" (if (eq "" Suff) "-None-" Suff) ">: "))))
 (mapcar '(lambda (x y) (or (or (not x) (eq "" x)) (set y x))) tmpVars dVars)
 (while (setq pt (getpoint "\nClick for Text... "))
   (Make_Text pt (strcat Pref (if Zer0 "0" "") (rtos sNum 2 2) Suff))
   (setq sNum (+ sNum inNum)))
 (mapcar 'setvar vlst ovar)
 (princ))

(defun Make_Text  (txt_pt txt_val)
 (entmake (list '(0 . "TEXT")
        '(8 . "NumText")
        (cons 10 txt_pt)
        (cons 40 (max 2.5 (getvar "TEXTSIZE")))
        (cons 1 txt_val)
        '(50 . 0.0)
        (cons 7 (getvar "TEXTSTYLE"))
        '(71 . 0)
        '(72 . 1)
        '(73 . 2)
        (cons 11 txt_pt))))

Link to comment
Share on other sites

That did it. Thanks, Lee.

 

Steve

 

No Problem Steve - I am currently working on an updated version of the above - when complete, I shall be sure to post it :thumbsup:

 

Lee

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