Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

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

Text Trimming

258 Views, 5 Replies
02-02-2013 11:30 AM

Hi,

 

I need a program to trim selected text(s) with a divisor.

suppose that the divisor character is: "/"

(selected text==>result:smileyhappy:

textL/textR ==> textR

xxx/yyy     ==>yyy

t12x/5678  ==>5678

 

I should use some commands like:

;;;;div="/":

(setq str "xxx/yyy")
(setq pos (vl-string-position (ascii "/") str))
(setq len (strlen str))
(setq RGT (substr str (+ pos 2) (-(- len pos)1))) ;;=text after "/"

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

but I need a LISP program that:

1-ask user to give a divisor char: (Divisor character: </>)

2-ask user to select text(s) but program should filter selected text(s) with divisor (*/* pattern)

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

(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# "*")))))
)

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

Can anybody help me?

 

Thanks

 

 

 

 

aqdam1978 wrote

... I would like to know is there any changes that I should do but I forgotted? some programming tips that made our LISP program as a optimum code....

 

sorry,read too quickly, and not read to the end, regarding code changes and tips, after Kent Cooper's "Expert Elite* advices, is difficult to provide better advices, but I saw a few things that can improve the code performance, so, is good practice to declare the variables that we use as local variables, when they are local, and notice that I have not declared local the variable #div#, to be available for the next time you run the code, in the same dwg and in the same AutoCad session, the other variables have been declared local,so they are not occupying memory, and will not remain set values to these variables, and will not enterin in conflict with other code that uses the same variables.

 

(defun C:p074 (/ tmp ss itm obj str pos)
(vl-load-com)
(if (not #div#) (setq #div# "/"))
(setq tmp (getstring (strcat "\nDivisor character: <" #div# "> ")))
(if (= tmp "") (setq tmp #div#) (setq #div# tmp))
(setq ss (ssget "_:L" (list '(0 . "*TEXT") (cons 1 (strcat "*" #div# "*")))))
(if (and #div# SS)
(repeat (setq itm (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss (setq itm (1- itm)))))
(setq str (vla-get-textstring obj))
(setq pos (vl-string-position (ascii #div#) str))
(vla-put-textstring obj (substr str (+ pos 2)))
);; repeat
);if
(princ)
)

 

 

hope that helps

 

Henrique

*Expert Elite*
hmsilva
Posts: 3,368
Registered: ‎12-17-2004
Message 2 of 6 (247 Views)

Re: Text Trimming

02-02-2013 01:02 PM in reply to: aqdam1978

aqdam1978,

perhaps something like

 

 

  (if (or (not #div#) (/= (type #div#) 'STR)) (setq #div# "/"))
  (setq tmp (getstring (strcat "\nDivisor character: <" #div# "> ")))
  (if (= tmp "") (setq tmp #div#) (setq #div# tmp))
  (setq ss (ssget "_:L" (list '(0 . "*TEXT") (cons 1 (strcat "*" #div# "*")))))
  (setq str (cdr (assoc 1 (entget (ssname ss 0)))))
  (setq pos (vl-string-position (ascii #div#) str))
  (setq len (strlen str))
  (setq RGT (substr str (+ pos 2) (-(- len pos)1)))

 

 

Henrique

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

Re: Text Trimming

02-02-2013 04:07 PM in reply to: hmsilva

Hi Hmsilva,

 

Thank you for your help.

I change the LISP program to:

(defun C:p074()
(vl-load-com)
  (if (or (not #div#) (/= (type #div#) 'STR)) (setq #div# "/"))
  (setq tmp (getstring (strcat "\nDivisor character: <" #div# "> ")))
  (if (= tmp "") (setq tmp #div#) (setq #div# tmp))
  (setq ss (ssget "_:L" (list '(0 . "*TEXT") (cons 1 (strcat "*" #div# "*")))))
  (if (and #div# SS)
      (repeat (setq itm (sslength ss))
	(setq obj (vlax-ename->vla-object (ssname ss (setq itm (1- itm)))))
	(setq str (vla-get-textstring obj))
        (setq pos (vl-string-position (ascii #div#) str))
        (setq len (strlen str))
	(vla-put-textstring obj (substr str (+ pos 2) (-(- len pos)1)))
      );; repeat
  );if
  (princ)
)

 I would like to know is there any changes that I should do but I forgotted?

some programming tips that made our LISP program as a optimum code....

 

Thank you for your consideration

 

*Expert Elite*
Kent1Cooper
Posts: 5,879
Registered: ‎09-13-2004
Message 4 of 6 (231 Views)

Re: Text Trimming

02-02-2013 07:50 PM in reply to: aqdam1978

aqdam1978 wrote:

....

....
  (if (or (not #div#) (/= (type #div#) 'STR)) (setq #div# "/"))
....
	(vla-put-textstring obj (substr str (+ pos 2) (-(- len pos)1)))
....

 I would like to know is there any changes that I should do but I forgotted?

some programming tips that made our LISP program as a optimum code....

....


Two things come to mind:

 

1)  You could probably eliminate the (/= (type #div#) 'STR) test, and the (or) that includes it.  I don't see any way, if it exists, that it could be any other type [unless you're also using the same variable name in some other routine, in which case you should change the name of one of them].  Try just:

 

(if (not #div#) (setq #div# "/"))

 

And you could shorten it further by doing it this way:

 

(or #div# (setq #div# "/"))

 

which would work, though I prefer the first one, which seems more "descriptive" of what's going on.

 

2)  The (substr) function's 'length' argument is optional.  If you omit it, it will just go from the 'start' position to the end of the string, however long that is, which seems like what you want to do.  Try just:

 

(vla-put-textstring obj (substr str (+ pos 2)))

Kent Cooper
*Expert Elite*
hmsilva
Posts: 3,368
Registered: ‎12-17-2004
Message 5 of 6 (216 Views)

Re: Text Trimming

02-03-2013 06:49 AM in reply to: aqdam1978

You're welcome, aqdam1978

Henrique

*Expert Elite*
hmsilva
Posts: 3,368
Registered: ‎12-17-2004
Message 6 of 6 (209 Views)

Re: Text Trimming

02-03-2013 07:58 AM in reply to: aqdam1978

aqdam1978 wrote

... I would like to know is there any changes that I should do but I forgotted? some programming tips that made our LISP program as a optimum code....

 

sorry,read too quickly, and not read to the end, regarding code changes and tips, after Kent Cooper's "Expert Elite* advices, is difficult to provide better advices, but I saw a few things that can improve the code performance, so, is good practice to declare the variables that we use as local variables, when they are local, and notice that I have not declared local the variable #div#, to be available for the next time you run the code, in the same dwg and in the same AutoCad session, the other variables have been declared local,so they are not occupying memory, and will not remain set values to these variables, and will not enterin in conflict with other code that uses the same variables.

 

(defun C:p074 (/ tmp ss itm obj str pos)
(vl-load-com)
(if (not #div#) (setq #div# "/"))
  (setq tmp (getstring (strcat "\nDivisor character: <" #div# "> ")))
  (if (= tmp "") (setq tmp #div#) (setq #div# tmp))
  (setq ss (ssget "_:L" (list '(0 . "*TEXT") (cons 1 (strcat "*" #div# "*")))))
  (if (and #div# SS)
      (repeat (setq itm (sslength ss))
	(setq obj (vlax-ename->vla-object (ssname ss (setq itm (1- itm)))))
	(setq str (vla-get-textstring obj))
        (setq pos (vl-string-position (ascii #div#) str))
	(vla-put-textstring obj (substr str (+ pos 2)))
      );; repeat
  );if
  (princ)
)

 

 

hope that helps

 

Henrique

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.