Author Topic: Combine a variable value with a text string in an alert dialog box  (Read 3445 times)

0 Members and 1 Guest are viewing this topic.

jpcadconsulting

  • Newt
  • Posts: 56
Hi gang,

I have written the short code below to check for AECC objects in a drawing.  Works great, but I'd like to report how many objects when it finds them.

I'm a little unclear on the syntax to combine the value of ss with the string "AECC objects found in this drawing." in an alert dialog box.

Any guidance is appreciated.

Code: [Select]
(vl-load-com)
(defun c:chkaecc (/ ss)
(If (setq ss (ssget "x" '((0 . "AECC*"))))
(alert "AECC objects found in this drawing.")
(alert "No AECC objects found in this drawing.")
)
)
Technology will save us all! *eyeroll*

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Combine a variable value with a text string in an alert dialog box
« Reply #1 on: December 02, 2015, 01:27:44 PM »
You'll require the following functions:

sslength
itoa
strcat

jpcadconsulting

  • Newt
  • Posts: 56
Re: Combine a variable value with a text string in an alert dialog box
« Reply #2 on: December 02, 2015, 01:51:25 PM »
OK I've got this. (additions in red)

Code: [Select]
(vl-load-com)
(defun c:chkaecc (/ ss a1 a2 a3)
(If (setq ss (ssget "x" '((0 . "AECC*"))))
(
(setq a1 (sslength ss))
(setq a2 (itoa a1))
(setq a3 (strcat a2 " AECC objects found in this drawing."))
(alert a3)
)
(alert "No AECC objects found in this drawing.")
)
)

I'm getting the error: "error: bad argument type: fixnump: nil" which would indicate that something is not getting the format it expects.

If I'm correct:

(setq a1 (sslength ss))     takes value of ss and changes to an integer in a1
(setq a2 (itoa a1))     changes integer to a string in a2
(setq a3 (strcat a2 " AECC objects found in this drawing."))     combines a2 and the string into a3

I'm obviously missing something...
« Last Edit: December 02, 2015, 01:59:10 PM by jpcadconsulting »
Technology will save us all! *eyeroll*

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Combine a variable value with a text string in an alert dialog box
« Reply #3 on: December 02, 2015, 02:09:00 PM »
Oh, so many variables.
Code - Auto/Visual Lisp: [Select]
  1. (strcat (itoa (sslength (ssget "x" '((0 . "*"))))) " objects found in this drawing.")
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

jpcadconsulting

  • Newt
  • Posts: 56
Re: Combine a variable value with a text string in an alert dialog box
« Reply #4 on: December 02, 2015, 02:14:03 PM »
Ok, well that works but its returning a value of 3, when there are actually 199 objects in the selection set. ???
Technology will save us all! *eyeroll*

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Combine a variable value with a text string in an alert dialog box
« Reply #5 on: December 02, 2015, 02:41:27 PM »
You're missing a PROGN
Code - Auto/Visual Lisp: [Select]
  1. (defun c:chkaecc (/ ss a1 a2 a3)
  2.   (if (setq ss (ssget "x" '((0 . "AECC*"))))
  3.     ((setq a1 (sslength ss))
  4.       (setq a2 (itoa a1))
  5.       (setq a3 (strcat a2 " AECC objects found in this drawing."))
  6.       (alert a3)
  7.     )
  8.     (alert "No AECC objects found in this drawing.")
  9.   )
  10. )
« Last Edit: December 02, 2015, 03:07:20 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Combine a variable value with a text string in an alert dialog box
« Reply #6 on: December 02, 2015, 02:57:07 PM »
You're missing a PROGN

ronjonp,
Check out the help section I wrote on using our custom code tags. Section 1.3.2 lists how to highlight a line.
https://www.theswamp.org/index.php?topic=48309.0

Also, theSwamp has a built in AutoLisp help section you can link to (just type the alisp function name after the hashtag in the following link).
http://www.theswamp.org/~john/avlisp/#progn
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Combine a variable value with a text string in an alert dialog box
« Reply #7 on: December 02, 2015, 03:01:31 PM »
It's been awhile since I've written any AutoLisp but give the following a try.
Code - Auto/Visual Lisp: [Select]
  1. ( (lambda ( / ss )
  2.     (strcat
  3.       (itoa
  4.         (cond
  5.           ((setq ss (ssget "x" '((0 . "aecc*"))))
  6.            (sslength ss))
  7.           (0)))
  8.       " objects found in this drawing.")) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Combine a variable value with a text string in an alert dialog box
« Reply #8 on: December 02, 2015, 03:07:50 PM »
You're missing a PROGN

ronjonp,
Check out the help section I wrote on using our custom code tags. Section 1.3.2 lists how to highlight a line.
https://www.theswamp.org/index.php?topic=48309.0

Also, theSwamp has a built in AutoLisp help section you can link to (just type the alisp function name after the hashtag in the following link).
http://www.theswamp.org/~john/avlisp/#progn
Cool :) .. did not know about the highlighting. Thanks!

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Combine a variable value with a text string in an alert dialog box
« Reply #9 on: December 02, 2015, 03:15:23 PM »
No problem.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

jpcadconsulting

  • Newt
  • Posts: 56
Re: Combine a variable value with a text string in an alert dialog box
« Reply #10 on: December 08, 2015, 05:51:50 PM »
Thanks Everyone... as always.

Working code:

Code: [Select]
(vl-load-com)
(defun c:chkaecc (/ ss a1 a2 a3)
  (If (setq ss (ssget "x" '((0 . "AECC*"))))
    (alert (strcat (itoa (sslength (ssget "x" '((0 . "AECC*"))))) " AECC objects found in this drawing."))
    (alert "No AECC objects found in this drawing.")
  )
)
Technology will save us all! *eyeroll*

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Combine a variable value with a text string in an alert dialog box
« Reply #11 on: December 08, 2015, 06:01:21 PM »
Well done  :)

Some minor points:

Since you have already defined the variable 'ss', you needn't retrieve the selection set a second time.  :wink:

Also, you may want to include a (princ) or (prin1) at the end of your code to suppress the nil returned by the alert function.

(vl-load-com) is also not required in this instance, as you are not using ActiveX.

You also no longer need to declare the symbols a1, a2 & a3 as local variables, as these are no longer used.

And to be really pernickety, if you really wanted to be grammatically correct, you could use:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:chkaecc ( / sel )
  2.     (if (setq sel (ssget "_X" '((0 . "AECC*"))))
  3.         (alert
  4.             (strcat
  5.                 (itoa (sslength sel))
  6.                 " AECC object"
  7.                 (if (= 1 (sslength sel)) " was" "s were")
  8.                 " found in this drawing."
  9.             )
  10.         )
  11.         (alert "No AECC objects were found in this drawing.")
  12.     )
  13.     (princ)
  14. )
« Last Edit: December 08, 2015, 06:07:54 PM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Combine a variable value with a text string in an alert dialog box
« Reply #12 on: December 09, 2015, 11:22:09 AM »
Glad you got it working, jpcadconsulating. You should implement Lee's suggestions (All of his comments up to the grammar part are necessary). Now, the real question is, do you understand your code (that's the important part)?

The basic structure (the conditional--IF in this case-) to your code now will be used in many, many situations. I had used another conditional statement, COND, and they are essentially the same at this point in your learning but so you know there is an important difference in how the two conditionals (IF and COND) operate. As an exercise/lesson take the two different structures in this thread (the IF and the COND) and convert them to simple statements (try to explain them using your own words).

For example, here is your code with Lee's necessary suggestions made.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:chkaecc ( / ss )
  2.   (if (setq ss (ssget "x" '((0 . "AECC*"))))
  3.     (alert (strcat (itoa (sslength ss)) " AECC objects found in this drawing."))
  4.     (alert "No AECC objects found in this drawing.")
  5.   )
  6. )

Quote
- Based on the condition of a variable; the existence of AECC objects,
        - Convert the number of objects to a string and prompt the user of the number of AECC objects found.
        - Otherwise, prompt the user that there are not any AECC members found.

And here is my COND structure for reference.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:chkaecc ( / ss )
  2.   (alert
  3.     (strcat
  4.       (itoa
  5.         (cond
  6.           ((setq ss (ssget "x" '((0 . "aecc*"))))
  7.            (sslength ss)
  8.            )
  9.           (0)
  10.           )
  11.         )
  12.       " AECC objects found in this drawing."
  13.       )
  14.     )
  15.   )

The COND structure I posted would be:
Quote
- Convert the result of a conditional and prompt the user.
        - First condition (the existence of AECC objects): The number of AECC objects found.
        - Second condition (fail-safe/default): 0 if none were found.

This exercise is more important then the code you write. It is an essential task, and one that often gets overlooked by people just learning.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org