Jump to content

Parse specific numbers on a text


Sambuddy

Recommended Posts

This is a sample of a *text I have. I am trying to select a bunch of Single and Multiline texts and then change their values from example this 311/312/313/314 to this 311/312/313/314 or from this A10/A11/A16/A18 to this A20/A21/A26/A28. In Summary the ones to twos or threes. What is consistant is the number 1 either in 3(1)1 or A(1)0 or E(1) in a set of numbers that are separated by / that I would like to change to 2s or 3s. Can someone help me either improve what I have so far or get me there please?

(defun c:sector ( / ss newValue ent entData text segments newSegments newText )
  (prompt "\nSelect Mtext objects to modify: ")
  (setq ss (ssget '((0 . "MTEXT")))) ; Select Mtext objects
  (if ss
    (progn
      (setq newValue (getint "\nEnter the new middle value (1, 2, or 3): "))
      (if (member newValue '(1 2 3))
        (progn
          (setq newValue (itoa newValue))
          (repeat (sslength ss)
            (setq ent (ssname ss (setq count (1- (sslength ss)))))
            (setq entData (entget ent))
            (setq text (cdr (assoc 1 entData)))
            (if (and text (> (strlen text) 0))
              (progn
                ;; Split the text into segments
                (setq segments (split-string text "/"))
                ;; Update each segment
                (setq newSegments
                      (mapcar
                        (lambda (seg)
                          (if (>= (strlen seg) 3)
                            ;; Replace the middle digit
                            (strcat (substr seg 1 1)
                                    newValue
                                    (substr seg 3))
                            seg
                          )
                        )
                        segments
                      )
                )
                ;; Join the segments back together
                (setq newText (apply 'strcat (mapcar (lambda (s) (strcat s "/")) newSegments)))
                ;; Remove the trailing "/"
                (setq newText (substr newText 1 (1- (strlen newText))))
                ;; Update the Mtext object
                (entmod (subst (cons 1 newText) (assoc 1 entData) entData))
                (entupd ent)
              )
              (prompt "\nSkipped: Mtext is empty.")
            )
          )
          (princ "\nMtext objects updated.")
        )
        (prompt "\nInvalid value. Please enter 1, 2, or 3.")
      )
    )
    (prompt "\nNo Mtext objects selected.")
  )
  (princ)
)

(defun split-string (str sep / pos start result)
  (setq result '() start 1)
  (while (setq pos (vl-string-search sep str start))
    (setq result (append result (list (substr str start (- pos start)))))
    (setq start (+ pos (strlen sep))))
  (append result (list (substr str start)))
)

 

Link to comment
Share on other sites

getkword will only allow user to input 1 2 or 3 before continuing.

Changed the lambda to work with either 3 or 2 string lengths.

UMV.lsp

Edited by mhupp
Link to comment
Share on other sites

10 hours ago, mhupp said:

getkword will only allow user to input 1 2 or 3 before continuing.

Changed the lambda to work with either 3 or 2 string lengths.

UMV.lsp 1.9 kB · 1 download

I could not get it to work. it tells me that the Mtexts are updated, but no changes for values.

Link to comment
Share on other sites

As usual, Lee Mac is way ahead of us. On this page you'll find a regular expression find/replace. Or you may only need to work the wcmatch command into your code.

Link to comment
Share on other sites

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