Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP needed to delete text before or after a dash

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
Anonymous
3283 Views, 13 Replies

LISP needed to delete text before or after a dash

I am looking for a LISP routine that would delete a string of text before or after a DASH.

 

I have looked everywhere for such a thing and not sure if this exists, or not sure if there is an easy way to do this otherwise.

 

I have attached  a drawing to better explain.

Any help is very appreciated.

 

Thanks in advance!

13 REPLIES 13
Message 2 of 14
dmfrazier
in reply to: Anonymous

I would think this could be done fairly quickly with FIND.

You would have to do it in two groups - one for the prefix removal, one for the suffix removal.

In the "Find what" field use question marks as placeholders for the letters, followed by a dash.

For the prefix removal, use ?????-.

For the suffix, use -?????.

(Note: be sure to set the "use wildcards" search option; click the More Options arrow at the lower-left to access.)

Message 3 of 14
doni49
in reply to: dmfrazier

This will do what you've asked for but be careful as there's no error checking. I'm not sure what it'll do if there is no dash in the selected text. Run REMB to remove the BEFORE portion and run REMA to remove the AFTER portion. And select the appropriate text elements.

;;
;;  By Don Ireland
;;
;;  Takes two arguments:  A String to search for and a string to search in.
;;  Usage:  (charfind "ST" "TEST") ; This will return 3.
;;
;;  Example:  (if (> (setq pos (charfind "S" "TEST")) 0)(princ "Found the letter S at position:  " . pos)(Princ "One or both search parameters was blank"))
;;
;;  Return Values:
;;  -1 = character not found within given string.
;;  -2 = Search string is empty.  (srch)
;;  -4 = Test String is empty.  (str)
;;  -6 = Search String and Test String are both empty.

(defun strfind(srch str / pt pt2 cnt)
  (setq cnt 0 pt 0 pt2 nil)
  (if (EQ (strlen srch) 0) (setq pt -2))
  (if (EQ (strlen str) 0) (setq pt (+ pt (- 0 4))))
  (if (EQ pt 0)(setq pt -1))
  (while (and (< pt 0) (> (strlen str) 0)(< cnt (strlen str)))
    (if (eq srch (substr str (setq cnt (1+ cnt)) (strlen srch)))(setq pt cnt))
  )
  (setq pt2 pt)
)
(defun remTxt(ba)
  (setq ent(ssget '(
                   (-4 . "<OR")
                   (0 . "TEXT")(0 . "MTEXT")
                   (-4 . "OR>")
                   ))
  )

  (setq cnt -1)
  (repeat (sslength ent)
    (setq cent (entget (ssname ent (setq cnt (1+ cnt)))))
    (setq cur (cdr(assoc 1 cent)))
    (setq pos (strfind "-" cur))
    (if (EQ ba 1)
      (setq new (substr cur 1 (1- pos)))
      (setq new (substr cur (1+ pos) (strlen cur)))
    )
    (setq cent(subst (cons 1 New) (cons 1 cur) cent))
    (entmod cent)
  )
)
(defun c:remA()
  (remTxt 1)
)
(defun c:remB()
  (remTxt 0)
)

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 4 of 14
Anonymous
in reply to: doni49

Thanks to all of you! All of your solutions worked great!

Message 5 of 14
doni49
in reply to: Anonymous


@Anonymous wrote:

Thanks to all of you! All of your solutions worked great!


Glad to be of assistance.  But for future knowledge, you'd be better served posting questions about AutoLISP over in the AutoLISP forum.  There is a greater concentration of people who know how to use it over there.

http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/bd-p/130



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 6 of 14
abubakars
in reply to: Anonymous

Here is another workaround to get what you are looking for, I am using the free tool called LayoutManager available to download from Autodesk Exchange Apps store

 

To remove the string at right side of the - which ends with "-ABB, set the below conditions using command "Text-Find & Replace

 

LOM-Text Remove-1.PNG

 

To remove strings which starts with ABZ and - in between, set the below conditions using command "Text-Find & Replace

 

LOM-Text Remove-2.PNG

Message 7 of 14
thajmul2501
in reply to: doni49

hi don

 

your lisp is pretty so good but i have a problem with your lisp. why deleting after  or before a dash. there is no solution like that (12/12). i would like to  need this " this is a text" i would like to delete "this is" could you make for us. it would really appreciate.

Message 8 of 14
Kent1Cooper
in reply to: thajmul2501


@thajmul2501 wrote:

.... why deleting after  or before a dash. ....


Because that is the Subject of this thread.  If you need a different function, Search for a thread about what you are trying to do, or start a new one.

Kent Cooper, AIA
Message 9 of 14
thajmul2501
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@thajmul2501 wrote:

.... why deleting after  or before a dash. ....


Because that is the Subject of this thread.  If you need a different function, Search for a thread about what you are trying to do, or start a new one.


hi kent

 

thank you so much for kind reply.

 

i don't know how to start. i would like to need delete text suffix or prefix because i have no text with dash some time might be come. i want to delete txt suffix or prefix example "THIS IS A WORD"  i would like to delete "THIS IS" only. how to make it. you are familiar with lisp that i was posted here.

 

i hope you could help me. many thanks

 

best regards 

 

hussain

 

 

Message 10 of 14
pendean
in reply to: thajmul2501

>>>... example "THIS IS A WORD" i would like to delete "THIS IS" only...<<<

FIND command has always done this, the pop-up it presents is self explanatory IMHO: do you really need a lisp?
Message 11 of 14
thajmul2501
in reply to: pendean

hi

thank you so much for your kind reply. ok i will take find command no
problem. if we have this lisp its good for us that i asked to you. if you
have time. pls make it.

best regards

hussain
Message 12 of 14
ofrontera
in reply to: doni49

hi Family 🙂

can we have this command do the same for (~) instead of (-)? 

i have the same task of removing text before we give to our customer but i have to remove everything after (~).

for example:

IN: 328' 3/C #2BC 12KV (TO 2045E) ~ZOHC 709

IN: 328' 3/C #2BC 12KV (TO 2045E)

thanks Family!

IIRICANII
Message 13 of 14
ofrontera
in reply to: ofrontera

found and replaced it!

work to my needs now 🙂

IIRICANII
Message 14 of 14
ofrontera
in reply to: doni49

Hello Good sir!

i love the lisp!!

is there a way to add block attributes? i want this to work but i have to burst the block first then use the lisp you created.

either way its AWESOME lisp!!  just a little change (i assume little lol, i have no clue) to better suit my needs :slightly_smiling_face:

i want to select the block with attributes to work the same as selecting text strings :slightly_smiling_face: im crossing my fingers! 

;;
;; By Don Ireland
;;
;; Takes two arguments: A String to search for and a string to search in.
;; Usage: (charfind "ST" "TEST") ; This will return 3.
;;
;; Example: (if (> (setq pos (charfind "S" "TEST")) 0)(princ "Found the letter S at position: " . pos)(Princ "One or both search parameters was blank"))
;;
;; Return Values:
;; -1 = character not found within given string.
;; -2 = Search string is empty. (srch)
;; -4 = Test String is empty. (str)
;; -6 = Search String and Test String are both empty.

(defun strfind(srch str / pt pt2 cnt)
(setq cnt 0 pt 0 pt2 nil)
(if (EQ (strlen srch) 0) (setq pt -2))
(if (EQ (strlen str) 0) (setq pt (+ pt (- 0 4))))
(if (EQ pt 0)(setq pt -1))
(while (and (< pt 0) (> (strlen str) 0)(< cnt (strlen str)))
(if (eq srch (substr str (setq cnt (1+ cnt)) (strlen srch)))(setq pt cnt))
)
(setq pt2 pt)
)
(defun remTxt(ba)
(setq ent(ssget '(
(-4 . "<OR")
(0 . "TEXT")(0 . "MTEXT")
(-4 . "OR>")
))
)

(setq cnt -1)
(repeat (sslength ent)
(setq cent (entget (ssname ent (setq cnt (1+ cnt)))))
(setq cur (cdr(assoc 1 cent)))
(setq pos (strfind "~" cur))
(if (EQ ba 1)
(setq new (substr cur 1 (1- pos)))
(setq new (substr cur (1+ pos) (strlen cur)))
)
(setq cent(subst (cons 1 New) (cons 1 cur) cent))
(entmod cent)
)
)
(defun c:remA()
(remTxt 1)
)
(defun c:remB()
(remTxt 0)
)

IIRICANII

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

Post to forums  

Autodesk Design & Make Report

”Boost