Author Topic: Use of %1 in autolisp routine  (Read 5914 times)

0 Members and 1 Guest are viewing this topic.

mhakim

  • Guest
Use of %1 in autolisp routine
« on: May 11, 2005, 09:25:24 PM »
;;; Following is part of lydelmrg.lsp by AutoCAD


(prompt (ACET-STR-FORMAT "\nThere are %1 other layouts that reference one or more of the selected layers."  
(length LAYOUTLST)   ))
;;;;;;
;;;;;; my notations ;;;;;
(length LAYOUTLST) ==> integer
(itoa (length LAYOUTLST) ) ==> string

;;-----
Please can someone show me,
how does the routine ACET-STR-FORMAT,
replace the value %1 ,
with the string value in expression : (itoa (length LAYOUTLST)) ?

Thank you in advance

whdjr

  • Guest
Use of %1 in autolisp routine
« Reply #1 on: May 12, 2005, 08:43:15 AM »
FINALLY!!!!!!!!!!!

I'm glad to see someone else like that functionality.  I read thru the express tools and made my own to mimic theirs.  Try this:
Code: [Select]
(defun str-format (str lst)
  (mapcar '(lambda (x y)
    (while (vl-string-search y str 0)
      (setq str (vl-string-subst x y str))
    )
  )
 lst
 (cond ((= (length lst) 1) '("%1"))
((= (length lst) 2) '("%1" "%2"))
((= (length lst) 3) '("%1" "%2" "%3"))
 )
  )
  str
)

Usage:
(str-format "This is for %1 and you." '("me"))
"This is for me and you."

(str-format "This is for %2 and %1." '("you" "us"))
"This is for us and you."

(str-format "This is %3 for %2 and %1." '("our" "them" "not"))
"This is not for them and our."

CADaver

  • Guest
Use of %1 in autolisp routine
« Reply #2 on: May 12, 2005, 08:55:20 AM »
okay now that just hurts... what does a brain aneurysm feel like.

so i can build a list by some method '("stuff" "stuff2" "stuff3")

then extract elements from that list by position and do somehing else with them.

heh heh heh heh... thanks.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Use of %1 in autolisp routine
« Reply #3 on: May 12, 2005, 08:59:17 AM »
Nice little util function there Will.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

whdjr

  • Guest
Use of %1 in autolisp routine
« Reply #4 on: May 12, 2005, 09:04:18 AM »
Thanks guys.  It's only set up for 3 items to search for, but with a little more thought it could work off the lendth of the list and thus be limitless; however I didn't see too much of a need to do more than 3 replacements.  The cool thing is you can use an item more than once.

Example:

Code: [Select]
(str-format "This is %3 for %2, %1, %2, %1, %2 or %1." '("our" "them" "not"))
"This is not for them, our, them, our, them or our."

SMadsen

  • Guest
Re: Use of %1 in autolisp routine
« Reply #5 on: May 12, 2005, 09:44:45 AM »
Nice work, Will. Could it also be unlimited as to number of different text replacements? :)

Quote from: mhakim
Please can someone show me,
how does the routine ACET-STR-FORMAT,
replace the value %1 ,
with the string value in expression : (itoa (length LAYOUTLST)) ?

Can't show you how in code (very much like Will's tool I'll bet) but this is from the help file on ACET-STR-FORMAT (I assume it's the rules for conversion you're interested in):

"A list of argument values to be converted and inserted. Only INT, REAL and STR arguments can be converted. Other argument types must be converted to one of these three. REAL arguments are converted using the current LUNITS and LUPREC values; you may wish to first use the (angtos) or (rtos) functions for proper display."

When it says a "list", it means a series of string, real and/or integer arguments (not a list data type).

mhakim

  • Guest
Use of %1 in autolisp routine
« Reply #6 on: May 12, 2005, 11:15:38 AM »
Thank U so much guys, esp. Will.
Couldn't hv done it in a million yrs.
Not with my level of autolisp anyway... hehe

Now, why do  Mark call this place ,
D SWAMP ?? :wink:

whdjr

  • Guest
Use of %1 in autolisp routine
« Reply #7 on: May 12, 2005, 11:23:00 AM »
See here

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Use of %1 in autolisp routine
« Reply #8 on: May 12, 2005, 12:49:53 PM »
Here's my take on this. Unless I missed something what this really distills down to is a replace function.

Given that, here's my contribution --

Code: [Select]
(defun replace ( old new text / _replace _stringp )
    (defun _replace ( oldtext newtext text / i )
        (if (/= oldtext newtext)
            (while (setq i (vl-string-search oldtext text))
                (setq text
                    (vl-string-subst
                          newtext
                          oldtext
                          text
                          i
                    )
                )
            )
        )
        text
    )
    (defun _stringp (x)
        (eq 'str (type x))
    )
    (cond
        (   (apply 'and (mapcar 'listp (list old new)))
            (foreach pair (mapcar 'cons old new)
                (setq text
                    (_replace
                        (car pair)
                        (cdr pair)
                        text
                    )                    
                )
            )
        )    
        (   (apply 'and (mapcar 'stringp args))
            (_replace old new text)
        )
        ;;  any other type of input is undefined      
    )
)

Examples --

Code: [Select]
(replace "%1" "Howdy" "%1, how ya'll doin?")

=> "Howdy, how ya'll doin?"

(replace
    '("%1" "%2" "%3")
    '("Howdy" "ya'll" "America")
    "%1, how %2 doin' in %3?"
)

=> "Howdy, how ya'll doin' in America?"

The %1 convention is merely for convenience, but I like it -- it clearly identifies the replacable parameters.

Cheers.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Use of %1 in autolisp routine
« Reply #9 on: May 12, 2005, 02:05:45 PM »
Very nice Michael, lots of flexibility.
Would be nice to accept types, INT, REAL and STR without pre conversion.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Use of %1 in autolisp routine
« Reply #10 on: May 12, 2005, 02:23:54 PM »
Ok.

Code: [Select]
(defun replace ( old new text / _plaque _tostring _replace _stringp )

    (defun _plaque ( x lst )
        (cons (car lst)
            (apply 'append
                (mapcar
                   '(lambda (item) (list x item))
                    (cdr lst)
                )
            )
        )
    )

    (defun _tostring ( x / typex )
        (cond
            (   (eq 'str (setq typex (type x))) x)
            (   (eq 'real typex)
                (rtos x 2 (if (zerop (- x (fix x))) 1 15))
            )
            (   (eq 'list typex)
                (strcat
                    (chr 40)
                    (if (vl-list-length x)
                        (apply 'strcat
                            (_plaque " "
                                (mapcar '_tostring x)
                            )
                        )
                        (strcat
                            (_tostring (car x))
                            " . "
                            (_tostring (cdr x))
                        )
                    )
                    (chr 41)
                )
            )
            ((vl-princ-to-string x))
        )
    )

    (defun _replace ( oldtext newtext text / i )
        (if (/= oldtext newtext)
            (while (setq i (vl-string-search oldtext text))
                (setq text
                    (vl-string-subst
                          newtext
                          oldtext
                          text
                          i
                    )
                )
            )
        )
        text
    )
   
    (defun _stringp (x)
        (eq 'str (type x))
    )
   
    (cond
        (   (apply 'and (mapcar 'listp (list old new)))
            (foreach pair (mapcar 'cons old (mapcar '_tostring new))
                (setq text
                    (_replace
                        (car pair)
                        (cdr pair)
                        text
                    )                    
                )
            )
        )    
        (   (apply 'and (mapcar 'stringp args))
            (_replace old new text)
        )
        ;;  any other type of input is undefined        
    )
)

Example:

Code: [Select]
(replace
    '("%1" "%2" "%3")
     (list 1 pi "number")
    "As you wish number %1, %2 is my favorite %3."
)

=> "As you wish number 1, 3.141592653589793 is my favorite number."

:)

Edit 1: Fixed up a typo.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Use of %1 in autolisp routine
« Reply #11 on: May 12, 2005, 02:45:36 PM »
Aaaah, Nice.
I'm off to squirrel this one away in my treasure chest. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Use of %1 in autolisp routine
« Reply #12 on: May 12, 2005, 03:05:00 PM »
Thanks for the idea Alan. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Use of %1 in autolisp routine
« Reply #13 on: May 12, 2005, 04:22:04 PM »
Hey Alan, you might wish to "re-squirrel" this function, there was a tpyo.

:oops:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Use of %1 in autolisp routine
« Reply #14 on: May 12, 2005, 04:34:38 PM »
Done, Thanks :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Use of %1 in autolisp routine
« Reply #15 on: August 31, 2009, 07:13:26 PM »
Just revisited this function & made some modifications.
Code: [Select]
;;  CAB 08.31.09
(defun str-format (str lst / r)
  (mapcar '(lambda (x y)
    (while (vl-string-search y str 0)
      (setq str (vl-string-subst (vl-princ-to-string x) y str)))
  )
lst
         (mapcar '(lambda(x)
           (strcat "%" (itoa(if r (setq r (1+ r))(setq r 1))))) lst)
  )
  str
)

 
Code: [Select]
(str-format "As you wish number %1, %2 is my favorite %3." (list 1 pi "number"))"As you wish number 1, 3.14159 is my favorite number."
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Use of %1 in autolisp routine
« Reply #16 on: September 01, 2009, 07:07:53 AM »
Very nice Alan, definitely keeping that one   ;-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Use of %1 in autolisp routine
« Reply #17 on: September 01, 2009, 09:15:03 AM »
Code: [Select]
(defun str-format-recursion (s l)
 (if (wcmatch s "*%#*")
  (if (wcmatch s "%#*")
   (strcat (vl-princ-to-string (nth (1- (atoi (substr s 2))) l))
           (str-format (substr s 3) l)
   ) ;_  strcat
   (strcat (substr s 1 1) (str-format (substr s 2) l))
  ) ;_  if
  s
 ) ;_  if
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Use of %1 in autolisp routine
« Reply #18 on: September 01, 2009, 09:16:10 AM »
Thanks Lee.
As you probably know it was inspired by the Express Tools subroutine.
Code: [Select]
(princ (acet-str-format "\nCurrent settings: Mode = %1, Radius = %2" sTrim sRad))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.