Jump to content

Recommended Posts

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • pmadhwal7

    13

  • Steven P

    4

  • hosneyalaa

    3

  • Tsuky

    2

Posted

Try this

(defun c:test ( / ss1 ss2 ss n ename l1 l2 l)
  (defun string-subst (nam_obj new_string old_string / value_string nbs tmp_nbs)
    (setq value_string (vlax-get nam_obj 'TextString) nbs 0)
    (while nbs
      (if (setq nbs (vl-string-search old_string value_string (setq tmp_nbs nbs)))
        (setq
          value_string (vl-string-subst new_string old_string value_string tmp_nbs)
          nbs (1+ nbs)
        )
      )
    )
    (vlax-put nam_obj 'TextString value_string)
    (vlax-put nam_obj 'Width 25.0)
  )
  (setq
    ss1 (ssget "_X" '((0 . "MTEXT") (1 . "Cable length-*")))
    ss2 (ssget "_X" '((0 . "MTEXT") (1 . "Pole to pole length-*")))
    ss (ssadd)
  )
  (cond
    ((and ss1 ss2)
      (repeat (setq n (sslength ss1))
        (setq
          ename (ssname ss1 (setq n (1- n)))
          l1 (cons (cons ename (cdr (assoc 10 (entget ename)))) l1)
        )
      )
      (repeat (setq n (sslength ss2))
        (setq
          ename (ssname ss2 (setq n (1- n)))
          l2 (cons (cons ename (cdr (assoc 10 (entget ename)))) l2)
        )
      )
      (setq
        l
        (vl-remove
          nil
          (mapcar
            '(lambda (z)
              (vl-remove nil z)
            )
            (mapcar
              '(lambda (x)
                (mapcar
                  '(lambda (y)
                    (if
                      (and
                        (< (distance (cdr x) (cdr y)) 2.6)
                        (> (distance (cdr x) (cdr y)) 2.5)
                      )
                      (cons (car x) (car y))
                    )
                  )
                  l2
                )
              )
              l1
            )
          )
        )
      )
      (cond
        (l
          (foreach e (mapcar 'car l)
            (setq
              str1 (cdr (assoc 1 (entget (car e))))
              str2 (strcat "\\P" (cdr (assoc 1 (entget (cdr e)))))
            )
            (entdel (cdr e))
            (string-subst (vlax-ename->vla-object (car e)) (strcat str1 str2) str1)
            (ssadd (car e) ss)
          )
          (sssetfirst nil ss)
        )
      )
    )
  )
)

 

Posted
13 hours ago, Tsuky said:

Try this

(defun c:test ( / ss1 ss2 ss n ename l1 l2 l)
  (defun string-subst (nam_obj new_string old_string / value_string nbs tmp_nbs)
    (setq value_string (vlax-get nam_obj 'TextString) nbs 0)
    (while nbs
      (if (setq nbs (vl-string-search old_string value_string (setq tmp_nbs nbs)))
        (setq
          value_string (vl-string-subst new_string old_string value_string tmp_nbs)
          nbs (1+ nbs)
        )
      )
    )
    (vlax-put nam_obj 'TextString value_string)
    (vlax-put nam_obj 'Width 25.0)
  )
  (setq
    ss1 (ssget "_X" '((0 . "MTEXT") (1 . "Cable length-*")))
    ss2 (ssget "_X" '((0 . "MTEXT") (1 . "Pole to pole length-*")))
    ss (ssadd)
  )
  (cond
    ((and ss1 ss2)
      (repeat (setq n (sslength ss1))
        (setq
          ename (ssname ss1 (setq n (1- n)))
          l1 (cons (cons ename (cdr (assoc 10 (entget ename)))) l1)
        )
      )
      (repeat (setq n (sslength ss2))
        (setq
          ename (ssname ss2 (setq n (1- n)))
          l2 (cons (cons ename (cdr (assoc 10 (entget ename)))) l2)
        )
      )
      (setq
        l
        (vl-remove
          nil
          (mapcar
            '(lambda (z)
              (vl-remove nil z)
            )
            (mapcar
              '(lambda (x)
                (mapcar
                  '(lambda (y)
                    (if
                      (and
                        (< (distance (cdr x) (cdr y)) 2.6)
                        (> (distance (cdr x) (cdr y)) 2.5)
                      )
                      (cons (car x) (car y))
                    )
                  )
                  l2
                )
              )
              l1
            )
          )
        )
      )
      (cond
        (l
          (foreach e (mapcar 'car l)
            (setq
              str1 (cdr (assoc 1 (entget (car e))))
              str2 (strcat "\\P" (cdr (assoc 1 (entget (cdr e)))))
            )
            (entdel (cdr e))
            (string-subst (vlax-ename->vla-object (car e)) (strcat str1 str2) str1)
            (ssadd (car e) ss)
          )
          (sssetfirst nil ss)
        )
      )
    )
  )
)

 

thanks very much this is what i need, but cna you tell me why this code not working on other dwgs i attached one more dwg please check thanks again

new.dwg

Posted

To also take the simple texts into consideration, this?
But depending on whether we apply the code to another drawing, it may require a lot of adjustments:
- Filter (ssget) with layers or others and the character string
- Change search distances between text insertions (here 3.0 and 2.0)

(defun c:test ( / ss1 ss2 ss n ename l1 l2 l)
  (defun string-subst (nam_obj new_string old_string / value_string nbs tmp_nbs)
    (setq value_string (vlax-get nam_obj 'TextString) nbs 0)
    (while nbs
      (if (setq nbs (vl-string-search old_string value_string (setq tmp_nbs nbs)))
        (setq
          value_string (vl-string-subst new_string old_string value_string tmp_nbs)
          nbs (1+ nbs)
        )
      )
    )
    (vlax-put nam_obj 'TextString value_string)
    (vlax-put nam_obj 'Width 25.0)
  )
  (setq
    ss1 (ssget "_X" '((0 . "*TEXT") (1 . "Cable length-*")))
    ss2 (ssget "_X" '((0 . "*TEXT") (1 . "Pole to pole length-*")))
    ss (ssadd)
  )
  (cond
    ((and ss1 ss2)
      (repeat (setq n (sslength ss1))
        (setq
          ename (ssname ss1 (setq n (1- n)))
          l1 (cons (cons ename (cdr (assoc 10 (entget ename)))) l1)
        )
      )
      (repeat (setq n (sslength ss2))
        (setq
          ename (ssname ss2 (setq n (1- n)))
          l2 (cons (cons ename (cdr (assoc 10 (entget ename)))) l2)
        )
      )
      (setq
        l
        (vl-remove
          nil
          (mapcar
            '(lambda (z)
              (vl-remove nil z)
            )
            (mapcar
              '(lambda (x)
                (mapcar
                  '(lambda (y)
                    (if
                      (and
                        (< (distance (cdr x) (cdr y)) 3.0)
                        (> (distance (cdr x) (cdr y)) 2.0)
                      )
                      (cons (car x) (car y))
                    )
                  )
                  l2
                )
              )
              l1
            )
          )
        )
      )
      (cond
        (l
          (foreach e (mapcar 'car l)
            (setq
              str1 (cdr (assoc 1 (entget (car e))))
              str2 (strcat "\\P" (cdr (assoc 1 (entget (cdr e)))))
            )
            (if (eq (cdr (assoc 0 (entget (car e)))) "TEXT")
              (progn
                (command "_.-MTEXT" (cdr (assoc 10 (entget (car e)))) "_Width" 25.0 str1 str2 "")
                (entdel (car e))
                (entdel (cdr e))
                (ssadd (entlast) ss)
              )
              (progn
                (entdel (cdr e))
                (string-subst (vlax-ename->vla-object (car e)) (strcat str1 str2) str1)
                (ssadd (car e) ss)
              )
            )
          )
          (sssetfirst nil ss)
        )
      )
    )
  )
)

 

Posted
On 8/28/2023 at 1:42 PM, Tsuky said:

To also take the simple texts into consideration, this?
But depending on whether we apply the code to another drawing, it may require a lot of adjustments:
- Filter (ssget) with layers or others and the character string
- Change search distances between text insertions (here 3.0 and 2.0)

(defun c:test ( / ss1 ss2 ss n ename l1 l2 l)
  (defun string-subst (nam_obj new_string old_string / value_string nbs tmp_nbs)
    (setq value_string (vlax-get nam_obj 'TextString) nbs 0)
    (while nbs
      (if (setq nbs (vl-string-search old_string value_string (setq tmp_nbs nbs)))
        (setq
          value_string (vl-string-subst new_string old_string value_string tmp_nbs)
          nbs (1+ nbs)
        )
      )
    )
    (vlax-put nam_obj 'TextString value_string)
    (vlax-put nam_obj 'Width 25.0)
  )
  (setq
    ss1 (ssget "_X" '((0 . "*TEXT") (1 . "Cable length-*")))
    ss2 (ssget "_X" '((0 . "*TEXT") (1 . "Pole to pole length-*")))
    ss (ssadd)
  )
  (cond
    ((and ss1 ss2)
      (repeat (setq n (sslength ss1))
        (setq
          ename (ssname ss1 (setq n (1- n)))
          l1 (cons (cons ename (cdr (assoc 10 (entget ename)))) l1)
        )
      )
      (repeat (setq n (sslength ss2))
        (setq
          ename (ssname ss2 (setq n (1- n)))
          l2 (cons (cons ename (cdr (assoc 10 (entget ename)))) l2)
        )
      )
      (setq
        l
        (vl-remove
          nil
          (mapcar
            '(lambda (z)
              (vl-remove nil z)
            )
            (mapcar
              '(lambda (x)
                (mapcar
                  '(lambda (y)
                    (if
                      (and
                        (< (distance (cdr x) (cdr y)) 3.0)
                        (> (distance (cdr x) (cdr y)) 2.0)
                      )
                      (cons (car x) (car y))
                    )
                  )
                  l2
                )
              )
              l1
            )
          )
        )
      )
      (cond
        (l
          (foreach e (mapcar 'car l)
            (setq
              str1 (cdr (assoc 1 (entget (car e))))
              str2 (strcat "\\P" (cdr (assoc 1 (entget (cdr e)))))
            )
            (if (eq (cdr (assoc 0 (entget (car e)))) "TEXT")
              (progn
                (command "_.-MTEXT" (cdr (assoc 10 (entget (car e)))) "_Width" 25.0 str1 str2 "")
                (entdel (car e))
                (entdel (cdr e))
                (ssadd (entlast) ss)
              )
              (progn
                (entdel (cdr e))
                (string-subst (vlax-ename->vla-object (car e)) (strcat str1 str2) str1)
                (ssadd (car e) ss)
              )
            )
          )
          (sssetfirst nil ss)
        )
      )
    )
  )
)

 

 

Still I was confused how we able to do in new file,can we make it simple as I will convert it into mtex and from "new pole" next paragraph will start,only this thing I am unable to do, when I convert it and change width factor this is not happening,

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