GETKWORD Lisp Help

GETKWORD Lisp Help

gccdaemon
Collaborator Collaborator
1,068 Views
4 Replies
Message 1 of 5

GETKWORD Lisp Help

gccdaemon
Collaborator
Collaborator

So I found this routine in some black hole in the interwebs (don't know where or when) but wanted to modify it to fit our purposes. The problem I'm having is that the the ARG is not working propperly. I have replaced ARG in line 47 with the strings produced from GETKWORD and it works correctly. What am I missing?

 

(defun C:JOINTXT (/ *error* END ENT LST OBJ WD STR ARG)
	(defun *error* (errmsg)
		(if	(not	(wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
			(princ	(strcat "\nError: " errmsg)))
		(command "_.regen" "_.undo" "_end")
		(setvar 'cmdecho cmde)
		(princ)
	)
	(setq	cmde (getvar 'cmdecho))
	(setvar 'cmdecho 0)
	(command "_.undo" "_begin")
	(initget "1 2 3 4 5")
	(setq KW (getkword "\nSelect a spacing type: [1 - None |/ 2 - Single Space |/ 3 - Single Return |/ 4 - Double Return |/ 5 - Custom] (1): "))
	(IF	(= KW 2)
		(setq ARG " ")
		(IF	(= KW 3)
			(setq ARG "\\P")
			(IF	(= KW 4)
				(setq ARG "\\P\\P")
				(IF	(= KW 5)
					(setq ARG (GETSTRING T "\nEnter custom spacing text: "))
					(setq ARG "")
	)	)	)	)
	(while	(not END)
		(setvar 'errno 0)
		(setq ENT (car (entsel "\nSelect Text/Mtext to join: ")))
		(if	(and	(= (getvar 'errno) 0); picked something
				(wcmatch (cdr (assoc 0 (entget ENT))) "TEXT,MTEXT"))
			(progn	(if	(not (member (vlax-ename->vla-object ENT) LST))
					(setq LST (cons (vlax-ename->vla-object ENT) LST)))
				(redraw ENT 3))
			(if	(= (getvar 'errno) 52)
				(setq END T)
				(prompt "\nNothing selected --")
	)	)	)
	(setq OBJ (last LST))
	(if	(= (substr (vla-get-ObjectName OBJ) 5) "Text")
		(progn	(setq WD	(apply 'max	(mapcar 'vla-get-Width (vl-remove-if '(lambda (x) (= (vla-get-ObjectName x) "AcDbText")) LST)
          		)		)		)
			(command "_.txt2mtxt" (vlax-vla-object->ename OBJ) "")
			(setq OBJ (vlax-ename->vla-object (entlast)))
			(if	(> WD 0)
				(vla-put-Width OBJ WD))
	)	)
	(setq STR (vla-get-TextString OBJ))
	(foreach x	(cdr (reverse LST))
			(setq STR (strcat STR ARG (vla-get-TextString x)))
			(vla-delete x))
	(vla-put-TextString OBJ STR)
	(command "_.undo" "_end")
	(setvar 'cmdecho cmde)
	(princ)
)
Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
0 Likes
Accepted solutions (1)
1,069 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

I didn't go thru it all... but main problem is that (getkword) returns a string, not an integer. So your conditions for IF fuction has to look like this:
(= KW "2")
(= KW "3")
and so on.
I tried quick test and it works.

0 Likes
Message 3 of 5

Kent1Cooper
Consultant
Consultant

@gccdaemon wrote:

So I found this routine in some black hole in the interwebs (don't know where or when) but wanted to modify it ....

 

....
	(IF	(= KW 2)
		(setq ARG " ")
		(IF	(= KW 3)
			(setq ARG "\\P")
			(IF	(= KW 4)
				(setq ARG "\\P\\P")
				(IF	(= KW 5)
					(setq ARG (GETSTRING T "\nEnter custom spacing text: "))
					(setq ARG "")
         )       )       )      )
....

A lot of that looked very familiar -- I would guess that much of it came from OneMtext.lsp, here, or an earlier version on that same thread.  It has separate commands for different spacings, rather than options within one command [and fewer choices about it than yours].

 

The part quoted above is an example of something that is better [or at least much more concisely] done with a (cond) function, which can eliminate a lot of code duplication compared to all those nested (if) functions [e.g. only one (setq) function covers all possibilities], such as this:

 

....
	(setq ARG
(cond
((= KW "2") " ")
((= KW "3") "\\P")
((= KW "4") "\\P\\P") ((= KW "5")(GETSTRING T "\nEnter custom spacing text: ")) (""); [none of the above]
); cond ); setq ....

 Or, another way, with a list of values:

 

....
	(setq ARG
(cond
((< (atoi KW) 5) (nth (atoi KW) '("" "" " " "\\P" "\\P\\P")))
((GETSTRING T "\nEnter custom spacing text: ")); for "5" option ); cond ); setq .... 

 [The first "" in the list is just a place-holder, since KW will never be "0" but (nth) needs a zero-index (first) item in the list.]

Kent Cooper, AIA
Message 4 of 5

gccdaemon
Collaborator
Collaborator

I knew there was something I was missing, Thanks guys.

 

Kent, I think I'll use that Condition example instead to minimize language. Have a great holiday fellas!

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
0 Likes
Message 5 of 5

gccdaemon
Collaborator
Collaborator

Now I'm getting an error. "Too few arguments" ?

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
0 Likes