Jump to content

Appending escape characters to escape characters


Recommended Posts

;; Preface: I've built a lisp routine that generates a script. The script then runs a SECOND lisp routine on a series of drawings.
;; I'm having serious trouble passing information from the first lisp routine to the second lisp routine.
;; The first lisp routine uses a DCL dialog box to gather user information, and the second lisp routine needs to apply this information to each drawing in a set.
;; I have assembled the information into a list of strings, which I'd like to pass as an argument into the second lisp (unless there's an easier way someone can share with me).
;; To do this, I've created the _StringifyList function, which creates a new list declaration string that gets written to the script file.
;; So here's the problem:
;; The strings in the arguments may contain quotes or backslashes since they are user entered.
;; The vl-string-translate commands I've added aren't properly appending extra backslash escape characters to make up for this.

;; Try running this example:
(defun C:ListTest ( / *error* simpleList simpleListString)
		;Return error descriptions to command line in the event of an error
		(defun *error* (/)
			(princ "\nInternal error")
	;test strings are A,B,C
	;to create a list:
	(setq simpleList (list "A" "B" "C"))
	(princ "simpleList: ")
	(princ simpleList)
	(princ "\n")
	;result is not what we need:
	;simpleList: (A B C)
	;now lets wrap it with the stringify function:
	(setq simpleListString (_StringifyList simpleList))
	(princ (strcat "simpleListString: " simpleListString "\n"))
	;result is good, it can be written to a script as an argument now:
	;simpleListString: (list "A" "B" "C")
	;how about something more complicated though?
	;test strings are one"quote, this\that, three"""quotes
	;to create a list:
	(setq complexList (list "one\"quote" "this\\that" "three\"\"\"quotes"))
	(setq complexListString (_StringifyList complexList))
	(princ (strcat "complexListString: " complexListString "\n"))	
	;result is bad. quotes have been replaced with backslashes, and backslashes haven't been duplicated.
	;complexListString: (list "one\quote" "this\that" "three\\\quotes")
	;what the result should look like (I think):
	;complexListString: (list "one\"quote" "this\\that" "three\"\"\"quotes")

;; _StringifyList - plackowski
;; takes a list of strings and converts them to a declaration of a list of strings.
;; lst - [lst] A list of strings, like ("A" "B" "C")
;; str - [str] Output string in the format "(list "A" "B" "C")"
(defun _StringifyList (lst / str)
	(setq str "(list")
	(foreach item lst 
		;any backslashes should be replaced with double slashes: \ -> \\
		(setq item (vl-string-translate "\\" "\\\\" item))
		;any quotes in the string should be replaced with slash quotes to escape them: \" -> \\\"
		(setq item (vl-string-translate "\\\"" "\\\\\\\"" item))

		(setq str (strcat str " \"" item "\""))
	(setq str (strcat str ")"))



Link to comment
Share on other sites

You might look into VL-PRIN1-TO-STRING for your 'stringify' function:

(alert (VL-PRIN1-TO-STRING '("A" "B" "C")))


  • Like 1
Link to comment
Share on other sites

That's just what I needed, thank you! I'll just use strcat to append apostrophes to the output so they are read as literal string lists.


Curious though, do you know if this is the appropriate method to use? Are there other more effective ways to pass information between the lisps?



Link to comment
Share on other sites

You need to think about what is global and what is local variables, in your defuns 


( /  local variables) no globals


or (global1 global2 / local variables) so this would pass a global variable from say another lisp.  In your case the second lisp.


But note global1 & global2 can be set as local variables in lisp 1.


Calling you variables like AH:numb or like lee-mac he has lots of LM:function, this helps avoiding contradiction with a global variable name. Say string v's a number.


I have some menu commands that call 3 lisps in sequence. So need to work out the hierarchy of global and local variables.


Link to comment
Share on other sites

I tried using global variables first, but no luck. To clarify, both lisp functions (lets just call them lispOne and lispTwo) reside in the same .lsp file. I'm using the following CUI macro to call load the .lsp and call lispOne:

^C^C^P(load "L:/lisp/lispOne.lsp")(princ)(lispOne)

Once the options are set by the user in the dialog box, lispOne generates a temporary script file, which looks like this:

_.OPEN "O:/Temp/pl/test/dwg1.dwg"
(load "L:/Lisp/lispOne.lsp")
(lispTwo "A" '("07/23/20" "CONSTRUCTION" "PL") "1")
_.OPEN "O:/Temp/pl/test/dwg2.dwg"
(load "L:/Lisp/lispOne.lsp")
(lispTwo "A" '("07/23/20" "CONSTRUCTION" "PL") "1")

This script then gets called before lispOne finishes:

(command "_.SCRIPT" scrFilePath)

When I tested it, I couldn't get the lispTwo function to interact with the global variables, because I believe the global variables are being reset when a new drawing is loaded.


Edited by plackowski
wrong code syntax
Link to comment
Share on other sites

Yes !


There are a few ways around the problem

write to file,

save as enviroment

put it in your script (setq val1 22) after open dwg2

use blackboard


  • Thanks 1
Link to comment
Share on other sites

Thanks for the suggestions! I managed to get it working by passing the string through the script, but If I have to do this again I'll probably give blackboard a try.

Edited by plackowski
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.

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