Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 7 (474 Views)
Accepted Solution

repeat n times a function (urgent)

474 Views, 6 Replies
10-25-2012 08:23 PM

Hi friend, I don't know LISP language, but I need a program urgently

 

A program that prompts:"select objects:"

then I can select some objects like as text, line, circle,...

after that program find all texts in selected items and swap text

swap means:   xxx/yyy ===> yyy/xxx ;    "/" is a divisor

 

swap function for one text is here:

 

(defun c:swap ( / pos ent el txt)
(vl-load-com)
(if(null #div#)(setq #div# "/"))
(setq #div#
(if(=(setq tmp(getstring(strcat "\nDivisor character: <"#div#"> ")))"")
#div# tmp
)
)
(while(setq ent(entsel "\nSelect text: "))
(if(=(cdr(assoc 0(setq el(entget(car ent)))))"TEXT")
(entmod(subst(cons 1
(if(setq pos(vl-string-search #div#(setq txt(cdr(assoc 1 el)))))
(strcat(substr txt(+ 2 pos))#div#(substr txt 1 pos))
txt
)
)
(assoc 1 el)el))
)
)
(princ)
)

-------------------------------------------------------------------------------------------

 

but I want a program to prompt me like this:

(prompt "Select objects:")(terpri)
(setq a (ssget '((0 . "TEXT"))))

and gets divisor char </>:

after that:

(setq n (sslength a))
(repeat n

....

)

repeats n times, n=number of texts in my selection

 

Please help me!

Distinguished Mentor
_Tharwat
Posts: 599
Registered: ‎07-02-2010
Message 2 of 7 (461 Views)

Re: repeat n times a function (urgent)

10-26-2012 12:41 AM in reply to: aqdam1978
(defun c:Test (/ ss i e st p)
  ;;; Tharwat 26. Oct. 2012 ;;;
  (if (setq ss (ssget "_:L" (List '(0 . "*TEXT") (cons 1 "*/*"))))
    (repeat (setq i (sslength ss))
      (setq e (entget (ssname ss (setq i (1- i)))))
      (setq st (cdr (assoc 1 e)))
      (setq p (vl-string-search "/" st))
      (entmod
        (subst
          (cons
            1
            (strcat (substr st (+ p 2) (strlen st)) "/" (substr st 1 p))
          )
          (assoc 1 e)
          e
        )
      )
    )
  )
  (princ)
)

 

____________________________________________________
Get learn and learn and practice , to become experienced guy .
*Expert Elite*
Kent1Cooper
Posts: 5,370
Registered: ‎09-13-2004
Message 3 of 7 (435 Views)

Re: repeat n times a function (urgent)

10-26-2012 07:22 AM in reply to: _Tharwat

_Tharwat wrote:
....
  (if (setq ss (ssget "_:L" (List '(0 . "*TEXT") (cons 1 "*/*"))))
....
            (strcat (substr st (+ p 2) (strlen st)) "/" (substr st 1 p))
....

 


That's a good way to do it [you'll find various approaches to stepping through a selection set], though I notice two things about the above-quoted pieces that could be simplified slightly, if you're interested.

 

Wildcards in selection-set filtering can be used explicitly, that is, they are not considered to "require evaluation."  That is already applied above with the "*TEXT" entry in an apostrophe-prefixed ["quoted," no-evaluation-needed] dotted-pair list, and the same can also be used with the "*/*" entry.  That means you can make the filter list without the explicit (list) function or (cons), but rather using the ' shortcut of the (quote) function for the entire list instead of just part of it.  So the first quoted line above can be reduced to:
  (if (setq ss (ssget "_:L" '((0 . "*TEXT") (1 . "*/*"))))

 

And in (substr), the 'length' argument is optional, and if you don't give it a number of characters for that, it will just take it to the end of the string.  So as in the OP's original, the second quoted line above can be done without that argument in the first (substr) function:

            (strcat (substr st (+ p 2)) "/" (substr st 1 p))

Kent Cooper
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 4 of 7 (408 Views)

Re: repeat n times a function (urgent)

10-26-2012 07:50 PM in reply to: _Tharwat

Thank you so much. It works great!

however does not prompt for divisor char '/' , '|' , '-' , ....

but works fine for me.

 

Thanks

 

*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 5 of 7 (400 Views)

Re: repeat n times a function (urgent)

10-27-2012 12:38 AM in reply to: aqdam1978
(defun c:swap2 (/ _delFinder #div# ss i e st str fs p)
  (vl-load-com)
  (defun _delFinder (str md / d l str)
    (while (setq d (vl-string-position md str nil T))
      (setq l	(cons (substr str (+ 2 d)) l)
	    str	(substr str 1 d)
      )
    )
    (cons str l)
  )
  (if (null #div#)
    (setq #div# "/")
  )

  (if (and (setq #div#
		  (if (= (setq tmp (getstring
				 (strcat "\nDivisor character: <" #div# "> ")
			       )
			 )  "" ) #div# tmp
		  )
	   )
	   (setq ss
		  (ssget "_:L"
			 (list '(0 . "*TEXT") (cons 1 (strcat "*" #div# "*")))
		  )
	   )
      )
    (repeat (setq i (sslength ss))
      (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
      (setq st (_delfinder (setq str (vla-get-textstring e)) 32))
      (setq fs (vl-some	'(lambda (x)
			   (if (vl-string-position (ascii #div#) x)
			     x
			   )
			 )
			st
	       )
      )
      (setq fsr (reverse (_delfinder fs (ascii #div#))))
      (vla-put-textstring
	e
	(vl-string-subst
	  (strcat (car fsr) #div# (cadr fsr))
	  fs
	  str
	)
      )
    )
  )
  (princ)
)

 

command: swap2

Divisor character: </> [enter]

Select objects: Specify opposite corner: 2 found
Select objects:

< objects selected >

"xxx/yyy"

"asdf  xxx/yyy dfd"

 

result

"yyy/xxx"

"asdf  yyy/xxx dfdf"

 

HTH

 

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 6 of 7 (375 Views)

Re: repeat n times a function (urgent)

10-27-2012 10:28 AM in reply to: pbejse

Thanks my friend,

 

very completed program.

 

thanks.

*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 7 of 7 (359 Views)

Re: repeat n times a function (urgent)

10-28-2012 05:35 AM in reply to: aqdam1978

aqdam1978 wrote:

Thanks my friend,

 

very completed program.

 

thanks.


You're welcome, Glad you like it.

 

Cheers

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.