Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Text Trimming

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
aqdam1978
457 Views, 5 Replies

Text Trimming

Hi,

 

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

suppose that the divisor character is: "/"

(selected text==>result:)

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

 

 

 

 

5 REPLIES 5
Message 2 of 6
hmsilva
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

EESignature

Message 3 of 6
aqdam1978
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

 

Message 4 of 6
Kent1Cooper
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, AIA
Message 5 of 6
hmsilva
in reply to: aqdam1978

You're welcome, aqdam1978

Henrique

EESignature

Message 6 of 6
hmsilva
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

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost