Jump to content

Recommended Posts

Posted

I think it could. It might get tricky on which part of layer name you would remove.

Posted
I think it could. It might get tricky on which part of layer name you would remove.

 

 

 

I need to remove / replace the last 4 charaters

Posted
I need to remove / replace the last 4 charaters

Hi,

If you want to remove the last 4 characters , just replace number 5 to 4 in the my codes for the OP in this thread HERE

But if you want to replace , just use the function strcat to add any string you wish as prefix or suffix. easy right?

 

NOTE: but if you are after renaming layer names with these codes , you need to check first if the new layer name is not already existed.

 

Good luck.

  • 3 years later...
Posted (edited)

Hi all,

Just an amendments requested.

how about, if we want to remove the characters from left; or having an option from left or right side.

like I want to keep only the x characters from the right and remove all or remove the x charaters from the right.

Thanks

Edited by CADWORKER
Posted

Perhaps:

(setq str "ABCDEFOO")
(setq str (vl-string-left-trim "ABC" str))
(vl-string-right-trim "FOO" str)

 

Posted (edited)

Careful with the vl-string-*-trim functions @ronjonp 😉:

_$ (vl-string-left-trim "ABC" "ABCCBA")
""

I would always suggest substr for such purposes, e.g.:

_$ (setq s "ABCDEF")
"ABCDEF"
_$ (substr s 4) ;; Remove 3 chars from left
"DEF"
_$ (substr s 1 (- (strlen s) 3)) ;; Remove 3 chars from right
"ABC"

 

Edited by Lee Mac
Posted
11 minutes ago, Lee Mac said:

Careful with the vl-string-*-trim functions @ronjonp 😉:


_$ (vl-string-left-trim "ABC" "ABCCBA")
""

I would always suggest substr for such purposes, e.g.:

 

DOH! 

Posted (edited)

just 4fun a quickie before breakfast 🍞 🍳 ☕, don't see to much practical use for this 😄

; letter cutter
(defun c:le_cut ( / cut-size front-cut)
  (if (null (setq cut-size (getenv "cut-size")))(setenv "cut-size" (setq cut-size "1")))
  (if (null (setq front-cut (getenv "front-cut")))(setenv "front-cut" (setq front-cut "1")))
  (cut_dialog)
  (princ)
)

(defun cut_dialog ( / f p d r)
  (and (setq p (open (setq f (vl-filename-mktemp ".dcl")) "w"))
       (write-line "le_cut:dialog {label=\"Letter Cutter\";:concatenation {children_fixed_width=true;" p)
       (write-line ":text_part {key=\"tp1\";width=3;}:button {key=\"upper_cut\";label=\"+\";width=1;}" p)
       (write-line ":button {key=\"lower_cut\";label=\"-\";width=1;}" p)
       (write-line ":button {key=\"front_cut\";label=\"<>\";width=1;}" p)
       (write-line ":text_part {key=\"tp2\";width=5;}} spacer;ok_cancel;}" p)
       (not (setq p (close p)))(< 0 (setq d (load_dialog f)))(new_dialog "le_cut" d)
       (progn
         (set_tile "tp2" (if (= front-cut "1") "Front" "Back"))(set_tile "tp1" cut-size)
         (action_tile "front_cut" "(toggle_cut)")(action_tile "upper_cut" "(upper_cut 1)")
         (action_tile "lower_cut" "(upper_cut -1)")(action_tile "accept" "(setq r (get_tile \"lb\"))(done_dialog 1)")
         (action_tile "cancel" "(setq r nil)(done_dialog 0)")
         (start_dialog)(unload_dialog d)(vl-file-delete f))) (cond ((= r "") nil)(t (find_cut))))

(defun toggle_cut ()
  (set_tile "tp2" (if (= (setenv "front-cut" (if (= front-cut "1")(setq front-cut "0")(setq front-cut "1"))) "1") "Front" "Back"))
)

(defun upper_cut (i)
  (set_tile "tp1" (setenv "cut-size" (if (= "0" (setq cut-size (itoa (+ (atoi cut-size) i))))(setq cut-size "1") cut-size)))
)

; scan for string %p1=cornerpoint1 , %p2=cornerpoint2 , %fl=flag , if T include block text)
(defun sfs (%p1 %p2 %fl / _gip _pin _gbs ss i l e y )
  ; get insertionpoint e=ename
  (defun _gip (e) (if (assoc 11 (setq e (entget e)))(cdr (assoc 11 e))(cdr (assoc 10 e))))
  ; point inside a=point , b=cornerpoint1 , c=cornerpoint2
  (defun _pin (a b c) (if (and (>= (car a)(min (car b)(car c)))(<= (car a)(max (car b)(car c)))
    (>= (cadr a) (min (cadr b)(cadr c)))(<= (cadr a) (max (cadr b)(cadr c)))) t nil))
  ; get block string a = block (ename) , f=include (m)text , c=etype , r=result
  (defun _gbs (a f / c d r)
    (while (and (setq a (entnext a))(setq c (cdr (assoc 0 (entget a)))))
      (cond ((not (member c '("ATTRIB" "TEXT" "MTEXT")))) ((not (_pin (_gip a) %p1 %p2)))
     (t (if (= c "ATTRIB")(setq r (cons a r)) (if (and f (member c '("TEXT" "MTEXT")))(setq r (cons a r))))))) r)
  ; main routine
  (if (setq ss (ssget "c" %p1 %p2))
    (repeat (setq i (sslength ss)) (setq y (cdr (assoc 0 (entget (setq e (ssname ss (setq i (1- i))))))))
      (cond ((member y '("TEXT" "MTEXT" "ATTDEF"))(setq l (cons e l)))
     ((eq y "INSERT")(setq l (append l (_gbs e %fl)))))))
  l
)

; use (sfs p1 p2 T) for including block texts
(defun find_cut ( / p1 p2)
  (princ "\nSelect texts or attributes to cut")
  (while (and (setq p1 (getpoint "\nSelect first corner :"))(setq p2 (getcorner p1 "\nSelect second corner :")))
    (mapcar 'cut_me (mapcar 'vlax-ename->vla-object (sfs p1 p2 nil)))
    (vla-Regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport)
  )
  (prin1)
)

(defun cut_me (o / s)
  (if (and (vlax-property-available-p o 'textstring)(> (strlen (setq s (vla-get-textstring o)))(atoi cut-size)))
    (vla-put-textstring o
      (if (= front-cut "1") (substr s (1+ (atoi cut-size))) (substr s 1 (- (strlen s) (atoi cut-size)))))
  )
)
            
(vl-load-com)
; (c:le_cut)

🐉

Edited by rlx
Posted

THANKS LeeMac, Ronjonp and RLX

for your suggestions;

Busy for the day so let me try and amend the code and get back to you.

Thanks again.

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...