Author Topic: Passing a Single List into a function with Arguments - Script Generator  (Read 2049 times)

0 Members and 1 Guest are viewing this topic.

gumbtg

  • Mosquito
  • Posts: 12
Context:
I have a lisp function that takes 2 arguments. I also have a Script generator. I would like to use this lisp function with my script generator
The script generator takes 2 arguments; the lisp function name and a single argument.

I have long been unsure how to handle using my script generator with functions that take more than 1 argument.

Please help me adjust my script generator to handle multiple arguments:
The call I make to make script generator is: (C:BG_Scr_Generator "MyLispFunctionName" Argument)

I am so confused at this point after trying for hours on how to take a list of arguments and pass them to my script generator and for it to do the write line properly.
Due to my confusion right now Im not even going to say what I have tried.
Please help! Thank you!

Files Included:
Script Generator - I need help with line 136 (of the attached file, is a different number in the partial code below)
Block Replace - Calls the script Generator on line 49 (of attached file)
Image that clarifies the question - Makes it super easy to understand the question


Code - Auto/Visual Lisp: [Select]
  1. ;;--------------------------------Sub Functions--------------------------------------;;
  2. ;===========================================================
  3. ; 26/Jul/2019 01:50 PM[Friday]  AUTHOR: Brandon Gum
  4. ;--
  5. ;DESCRIPTION: Writes to the script file we are creating
  6. ;===========================================================
  7. (defun BG:ScrGen ( dwg lispFile arg fil / )
  8.     (if (= arg nil)
  9.                 (progn
  10.                 ;;;--- Send the open command to the script file
  11.                         (write-line
  12.                                 (strcat ;figured out the syntax from leeMac's BatchAttributeEditor, hardest thing of my life
  13.                                         "_.open \"" dwg "\" "
  14.                                         "(setvar \"filedia\" 0) "
  15.                                         "(load " (vl-prin1-to-string (vl-filename-base lispFile)) " nil) "
  16.                                         "(C:" lispfile ") "
  17.                                         "(while (= 1 (getvar \"cmdactive\")) (command pause)) "
  18.                                         "_.qsave"
  19.                                 );end of strcat
  20.                                 fil ;the file to write it to
  21.                         );end of write-line
  22.                 );end of progn
  23.                 ;--else--
  24.                 (progn
  25.                         (write-line
  26.                                 (strcat ;figured out the syntax from leeMac's BatchAttributeEditor, hardest thing of my life
  27.                                         "_.open \"" dwg "\" "
  28.                                         "(setvar \"filedia\" 0) "
  29.                                         "(load " (vl-prin1-to-string (vl-filename-base lispFile)) " nil) "
  30.                                         "(C:" lispfile " " (vl-prin1-to-string arg) ") " ;HELP ME FIGURE OUT HOW TO PASS MORE THAN 1 ARGUMENT HERE
  31.                                         "_.qsave"
  32.                                 );end of strcat
  33.                                 fil ;the file to write it to
  34.                         );end of write-line
  35.                 );end of progn
  36.         );end of if
  37.        
  38.         ;;;--- Send the open command to the script file
  39.    
  40.     ;(write-line (strcat "(setvar \"filedia\" 0)") fil);change the filedia before opening next dwg to surpress the open dcl box
  41.     ;(write-line "_.close" fil)
  42. );end of function
  43. ;===================================================================




EDIT (John): Fixed code tags.
« Last Edit: July 14, 2021, 04:03:47 PM by John Kaul (Se7en) »

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
I haven't looked over your code at the moment but to have access to the a Lisp's structure you may need to use DEFUN-Q (unless you are reading a file; in that case I imagine you'd have to do a little parsing of the function and just keep a list of arguments).

To give you something to check out; please look over a utility of Vladimir Nesterovsky's I posted a while back that does something similar.

https://www.theswamp.org/index.php?topic=48036.msg530747#msg530747


As far as general drawing scripting; I have a nice little batch script I have been using lately that uses ACCORECONSOLE.EXE I could share if you'd be interested.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
49
Code: [Select]
(C:BG_SCR_Generator "BG:BatchBlockReplace" (strcat (vl-prin1-to-string blkOG) " " (vl-prin1-to-string BlkFile)))


134
Code: [Select]
"(C:" lispfile " " arg ") "

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Alternatively:
Code - Auto/Visual Lisp: [Select]
  1. "(C:" lispfile (apply 'strcat (mapcar '(lambda ( x ) (strcat " " (vl-prin1-to-string x))) arg)) ") "

Just be aware that precision is lost if supplying reals - you may consider using rtos to perform the conversion in such cases, or better yet, write your own string conversion function to handle all possible types.

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Any reason that you have not set where your lisp files are saved, as a supported search path ? Much easier to program.

"(load " (vl-prin1-to-string (vl-filename-base lispFile)) " nil) "

"(load " "\"" filename "\"" ") ")
A man who never made a mistake never made anything

gumbtg

  • Mosquito
  • Posts: 12
Alternatively:
Code - Auto/Visual Lisp: [Select]
  1. "(C:" lispfile (apply 'strcat (mapcar '(lambda ( x ) (strcat " " (vl-prin1-to-string x))) arg)) ") "

Just be aware that precision is lost if supplying reals - you may consider using rtos to perform the conversion in such cases, or better yet, write your own string conversion function to handle all possible types.
Lee, thank you so much. I tried this exact approach yesterday but was a tad off basis and I was struggling with my testing in the command console. The way it prints the the output is confusing to me. I attached a screenshot to show what I mean.
BTW Lee, thank you for everything you do man. I've learned 80% of my knowledge from you. Thanks for giving back to us and helping us grow. 
------------
49
Code: [Select]
(C:BG_SCR_Generator "BG:BatchBlockReplace" (strcat (vl-prin1-to-string blkOG) " " (vl-prin1-to-string BlkFile)))


134
Code: [Select]
"(C:" lispfile " " arg ") "
Vovka, Thanks man. I tried this same thing yesterday and started getting really confused with the way that the vl-prin1-to-string was spitting this out. Very much appreciate your help.
------------
Any reason that you have not set where your lisp files are saved, as a supported search path ? Much easier to program.

"(load " (vl-prin1-to-string (vl-filename-base lispFile)) " nil) "

"(load " "\"" filename "\"" ") ")
Bigal, What line are you seeing this in? That is how I do it. In short, my thinking was that by not requiring the lisp to be in support path allows for most modularity.

gumbtg

  • Mosquito
  • Posts: 12
I haven't looked over your code at the moment but to have access to the a Lisp's structure you may need to use DEFUN-Q (unless you are reading a file; in that case I imagine you'd have to do a little parsing of the function and just keep a list of arguments).

To give you something to check out; please look over a utility of Vladimir Nesterovsky's I posted a while back that does something similar.

https://www.theswamp.org/index.php?topic=48036.msg530747#msg530747


As far as general drawing scripting; I have a nice little batch script I have been using lately that uses ACCORECONSOLE.EXE I could share if you'd be interested.
Would love to see your approach if you are willing to share. I didnt go CoreConsole route because of limited functions available for use.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Would love to see your approach if you are willing to share. I didnt go CoreConsole route because of limited functions available for use.

The only stipulations I have are: 1. be kind, and 2. contribute any improvements.

https://www.theswamp.org/index.php?topic=56891.new#new
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

gumbtg

  • Mosquito
  • Posts: 12
Would love to see your approach if you are willing to share. I didnt go CoreConsole route because of limited functions available for use.

The only stipulations I have are: 1. be kind, and 2. contribute any improvements.

https://www.theswamp.org/index.php?topic=56891.new#new
Wow, Thank you!!! Looking at now

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Alternatively:
Code - Auto/Visual Lisp: [Select]
  1. "(C:" lispfile (apply 'strcat (mapcar '(lambda ( x ) (strcat " " (vl-prin1-to-string x))) arg)) ") "
Lee, thank you so much. I tried this exact approach yesterday but was a tad off basis and I was struggling with my testing in the command console. The way it prints the the output is confusing to me. I attached a screenshot to show what I mean.

When printing AutoLISP data for debugging purposes, I find it useful to use the prin1 or print functions over the princ function, as the former will retain all control characters in the printed output, whereas the latter will evaluate such control characters - observe:

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq arg '("bob" "tom"))
  2. ("bob" "tom")
  3. _$ (princ (apply 'strcat (mapcar '(lambda ( x ) (strcat " " (vl-prin1-to-string x))) arg)))
  4.  "bob" "tom"" \"bob\" \"tom\""
  5. _$ (prin1 (apply 'strcat (mapcar '(lambda ( x ) (strcat " " (vl-prin1-to-string x))) arg)))
  6. " \"bob\" \"tom\""" \"bob\" \"tom\""

The data is returned twice because the princ/prin1/print functions will print the data to the command line or target file descriptor and will then return the supplied data. As such, when evaluating expressions directly at the Visual LISP IDE console, you can forego the use of the prin* functions:

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq arg '("bob" "tom"))
  2. ("bob" "tom")
  3. _$ (apply 'strcat (mapcar '(lambda ( x ) (strcat " " (vl-prin1-to-string x))) arg))
  4. " \"bob\" \"tom\""

BTW Lee, thank you for everything you do man. I've learned 80% of my knowledge from you. Thanks for giving back to us and helping us grow.

Thank you for your kind words, I'm delighted to have been of help even if indirectly!