Jump to content

Recommended Posts

Posted

Hi,

 

I have a large number of strings that contain between 7 and 16 characters.

 

Does someone have a lisp that will remove all but the last 3 or 4 characters. The 3 or 4 characters being defined by the user

eg 3 character  original "123 456 789" = "789"

or 4 characters original "123 10.5 15 1234" = "1234"

 

Thanks in advance for any assistance.

 

Posted

Give this a try:

(defun c:foo (/ el s str)
  (if (setq s (ssget ":L" '((0 . "text") (1 . "* *"))))
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (setq str (cdr (assoc 1 (setq el (entget e)))))
      (entmod (append el (list (cons 1 (substr str (+ 2 (vl-string-position 32 str 0 t)))))))
    )
  )
  (princ)
)

 

  • 2 weeks later...
Posted

Hi,

 

First define a function that returns a suffix of a given length from string

 

(defun string-suffix (len str / start)
  (setq start (1+ (- (strlen str) len)))
  (if (<= start 0) (setq start 1))
  (substr str start))

 

Test it

 

> (string-suffix 0 "abcdefg")
""
> (string-suffix 1 "abcdefg")
"g"
> (string-suffix 3 "abcdefg")
"efg"
> (string-suffix 100 "abcdefg")
"abcdefg"

 

Then use the function whatever you feel like

 

(setq lists '("123 456 789" "123 10.5 15 1234"))

 

Map over your strings

 

> (mapcar '(lambda (str) (string-suffix 3 str)) lists)
("789" "234")

 

Or print them

 

> (foreach str lists (princ (strcat (string-suffix 3 str) "\n")))
789
234

 

Posted (edited)

Maybe something like this Mutli radio buttons.lsp (in Cadtutor Downloads) supports up to about 20 buttons screen dependant ie a string with 20 values. Just make a list of the items in a string, 

 

image.png.b888160aad3449ec26bc0ced942f63a1.png

 

Happy to provide more info, would just pick a string 

 

(setq lst '("Choose to save" "123" "345" "567")) ; make this from a string
(ah:butts but "V"   lst)

 

Edited by BIGAL
Posted

Thanks for the reply's.

 

I was unable to get the below to work. It does not appear to do anything after selecting the text.

On 11/05/2021 at 10:48, ronjonp said:

Give this a try:


(defun c:foo (/ el s str)
  (if (setq s (ssget ":L" '((0 . "text") (1 . "* *"))))
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (setq str (cdr (assoc 1 (setq el (entget e)))))
      (entmod (append el (list (cons 1 (substr str (+ 2 (vl-string-position 32 str 0 t)))))))
    )
  )
  (princ)
)

 

 

 

This level of information is great but also far beyond my skill level to interpret and use. My effective level with all this is about zero.

On 21/05/2021 at 02:35, ten0s said:

Hi,

 

First define a function that returns a suffix of a given length from string

 


(defun string-suffix (len str / start)
  (setq start (1+ (- (strlen str) len)))
  (if (<= start 0) (setq start 1))
  (substr str start))

 

Test it

 


> (string-suffix 0 "abcdefg")
""
> (string-suffix 1 "abcdefg")
"g"
> (string-suffix 3 "abcdefg")
"efg"
> (string-suffix 100 "abcdefg")
"abcdefg"

 

Then use the function whatever you feel like

 


(setq lists '("123 456 789" "123 10.5 15 1234"))

 

Map over your strings

 


> (mapcar '(lambda (str) (string-suffix 3 str)) lists)
("789" "234")

 

Or print them

 


> (foreach str lists (princ (strcat (string-suffix 3 str) "\n")))
789
234

 

 

Thanks for the suggestion about the radio buttons. Unfortunately I need to be also to select to keep either 3 or 4 characters on the right side of the text as the numbers do not repeat within a drawing. There are area a different number of characterless in each drawing is I can just delete the first 6 characters and keep whats left.

 

On 22/05/2021 at 10:11, BIGAL said:

Maybe something like this Mutli radio buttons.lsp (in Cadtutor Downloads) supports up to about 20 buttons screen dependant ie a string with 20 values. Just make a list of the items in a string, 

 

image.png.b888160aad3449ec26bc0ced942f63a1.png

 

Happy to provide more info, would just pick a string 

 


(setq lst '("Choose to save" "123" "345" "567")) ; make this from a string
(ah:butts but "V"   lst)

 

 

 

Below is a small sample of the text:

drawing 1.

201 0.5 14 123

201 1 13 124

201 0.7 11 158

201 0.5 10 162

201 1.2 18 114

 

Drawing 2.

201 0.5 14 2301

201 1 13 2485

201 0.7 11 2689

201 0.5 10 2547

201 1.2 18 2519

 

Thanks again for all the help so far.

 

Posted
(defun _last_str (str / n)
  (if
    (setq n (vl-string-position 32 str nil T))
    (substr str (+ n 2))
    str
  )
)

_$ (_LAST_STR "201 1 13 124")
"124"
_$ (_LAST_STR "201 0.5 14 2301")
"2301"
_$ 

 

Posted
16 hours ago, jon123 said:

Thanks for the reply's.

 

I was unable to get the below to work. It does not appear to do anything after selecting the text.

 

 

This level of information is great but also far beyond my skill level to interpret and use. My effective level with all this is about zero.

 

Thanks for the suggestion about the radio buttons. Unfortunately I need to be also to select to keep either 3 or 4 characters on the right side of the text as the numbers do not repeat within a drawing. There are area a different number of characterless in each drawing is I can just delete the first 6 characters and keep whats left.

 

 

 

Below is a small sample of the text:

drawing 1.

201 0.5 14 123

201 1 13 124

201 0.7 11 158

201 0.5 10 162

201 1.2 18 114

 

Drawing 2.

201 0.5 14 2301

201 1 13 2485

201 0.7 11 2689

201 0.5 10 2547

201 1.2 18 2519

 

Thanks again for all the help so far.

 

Strange .. works here. Remove all characters at the last space.

 

foo.gif

Posted

A bit late to the party. Post did not go through

 

If you pick the string you want to keep it will not change text where string does not exist. So try this. Pick 18 as a test pick 201. 

 

(defun c:savetxt ( / ss txt txtlst ans obj but )
(setq ss (ssget '((0 . "TEXT"))))
(setq txt (cdr (assoc 1 (entget (ssname ss 0)))))
(setq txtlst (_csv->lst txt))
(setq txtlst (append (list "Please choose txt ") txtlst))
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq ans (ah:butts but "v" txtlst))
(repeat (setq x (sslength ss))
(setq obj (entget (ssname ss (setq x (- x 1)))))
(setq txt (cdr (assoc 1 obj)))
(setq txtlst (_csv->lst txt))
(if (= (nth (- but 1) txtlst) ans)
(entmod (subst (cons 1 ans) (assoc 1 obj) obj))
)
)
(princ)
)

 

Taking Stefans idea a bit further.

 

(defun c:txtlast ( / ss txt txtlst obj)
; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma
; 32 is space
(defun _csv->lst ( str / pos )
    (if (setq pos (vl-string-position 32 str))
        (cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
        (list str)
    )
)

(setq ss (ssget '((0 . "TEXT"))))
(repeat (setq x (sslength ss))
(setq obj (entget (ssname ss (setq x (- x 1)))))
(setq txt (cdr (assoc 1 obj)))
(setq txtlst (_csv->lst txt))
(entmod (subst (cons 1 (last txtlst)) (assoc 1 obj) obj))
)
(princ)
)

Multi radio buttons.lsp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...