Author Topic: Select text only or else loop  (Read 4057 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
Select text only or else loop
« on: July 05, 2017, 03:57:32 PM »
I want to know how to make the user select text or mtext only, if they select anything else it will loop until it is true.

My feeble attempt
Code: [Select]
(defun c:foo()
  (while
  (setq txt(cdr(assoc 1(entget (ssname (ssget "_:S+." '((0 . "*TEXT"))) 0)))))
  (princ "error")
 
)
  )

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Select text only or else loop
« Reply #1 on: July 05, 2017, 04:08:37 PM »
http://www.lee-mac.com/selectif.html


Why are you selecting text one at a time? Could you process all selected at once?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

dubb

  • Swamp Rat
  • Posts: 1105
Re: Select text only or else loop
« Reply #2 on: July 05, 2017, 04:31:02 PM »
Perhaps I can try to approach the method differently.
I have a batch of survey points that each contain a tag id. The survey is in a delimited format which is imported into a gis (QGIS) program to populate the points. From the GIS program I export the points and the corresponding labels as a dxf file. However the generated point is a block separate from the label which is mtext. To merge block and mtext together. I replace those exported blocks with an attributed block then I have to copy the exported text contents into the attribute. 

I hope that explains it.

The initial script I have been writing:

Code: [Select]
;select text or mtext
;select attribute
;edits attribute with text
(defun c:tat (/ txt loop)
  (while
  (princ "Select Text or Mtext <press enter to exit>")(setq txt(cdr(assoc 1(entget (ssname (ssget "_:S+." '((0 . "*TEXT"))) 0)))))
  (attinject txt)
  )
  (princ)
)
;;function
(defun attinject(txt / e)
      (princ "Select attribute text\n")(if (and (setq e (car (nentsel))) (= "ATTRIB" (cdr (assoc 0 (setq e (entget e))))))
    (entmod (subst (cons 1 txt) (assoc 1 e) e))
  )
  )

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Select text only or else loop
« Reply #3 on: July 05, 2017, 04:32:39 PM »
Just for fun, recursive:

Code - Auto/Visual Lisp: [Select]
  1. ; (seltxt)
  2. (defun seltxt ( / rec )
  3.   (defun rec ( msg pat / e )
  4.     (setvar 'errno 0) (setq e (car (entsel msg)))
  5.     (cond
  6.       ( (= 52 (getvar 'errno)) e)
  7.       ( (null e) (princ "\nMissed.") (rec msg pat) )
  8.       ( (not (wcmatch (cdr (assoc 0 (entget e))) pat)) (princ "\nInvalid.")  (rec msg pat) )
  9.       ( e )
  10.     ); cond
  11.   ); defun
  12.   (rec "\nPick text/mtext: " "*TEXT")
  13. ); defun seltxt
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Select text only or else loop
« Reply #4 on: July 05, 2017, 04:35:41 PM »
Perhaps I can try to approach the method differently.
I have a batch of survey points that each contain a tag id. The survey is in a delimited format which is imported into a gis (QGIS) program to populate the points. From the GIS program I export the points and the corresponding labels as a dxf file. However the generated point is a block separate from the label which is mtext. To merge block and mtext together. I replace those exported blocks with an attributed block then I have to copy the exported text contents into the attribute. 

I hope that explains it.

The initial script I have been writing:

Code: [Select]
;select text or mtext
;select attribute
;edits attribute with text
(defun c:tat (/ txt loop)
  (while
  (princ "Select Text or Mtext <press enter to exit>")(setq txt(cdr(assoc 1(entget (ssname (ssget "_:S+." '((0 . "*TEXT"))) 0)))))
  (attinject txt)
  )
  (princ)
)
;;function
(defun attinject(txt / e)
      (princ "Select attribute text\n")(if (and (setq e (car (nentsel))) (= "ATTRIB" (cdr (assoc 0 (setq e (entget e))))))
    (entmod (subst (cons 1 txt) (assoc 1 e) e))
  )
  )
Can you post a sample drawing ?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ChrisCarlson

  • Guest
Re: Select text only or else loop
« Reply #5 on: July 05, 2017, 04:51:29 PM »
I switched over to lee-mac's "select-if" function.

Simply pass arguments and it handles the rest.

http://www.lee-mac.com/selectif.html

Code - Auto/Visual Lisp: [Select]
  1. ;;---------------------=={ Select if }==----------------------;;
  2. ;;                                                            ;;
  3. ;;  Provides continuous selection prompts until either a      ;;
  4. ;;  predicate function is validated or a keyword is supplied. ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  msg  - prompt string                                      ;;
  10. ;;  pred - optional predicate function [selection list arg]   ;;
  11. ;;  func - selection function to invoke                       ;;
  12. ;;  keyw - optional initget argument list                     ;;
  13. ;;------------------------------------------------------------;;
  14. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  15. ;;------------------------------------------------------------;;
  16.  
  17. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  18.   (while
  19.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  20.       (cond
  21.         ( (= 7 (getvar 'ERRNO))
  22.           (princ "\nMissed, Try again.")
  23.         )
  24.         ( (eq 'STR (type sel))
  25.           nil
  26.         )
  27.         ( (vl-consp sel)
  28.           (if (and pred (not (pred sel)))
  29.             (princ "\nInvalid Object Selected.")
  30.           )
  31.         )
  32.       )
  33.     )
  34.   )
  35.   sel
  36. )

dubb

  • Swamp Rat
  • Posts: 1105
Re: Select text only or else loop
« Reply #6 on: July 05, 2017, 05:15:06 PM »
Sure. I had to strip out some things but this should be good enough.

Quote
Can you post a sample drawing ?

dubb

  • Swamp Rat
  • Posts: 1105
Re: Select text only or else loop
« Reply #7 on: July 05, 2017, 05:15:36 PM »
Sweet! I'm gonna try this one!

I switched over to lee-mac's "select-if" function.

Simply pass arguments and it handles the rest.

http://www.lee-mac.com/selectif.html

Code - Auto/Visual Lisp: [Select]
  1. ;;---------------------=={ Select if }==----------------------;;
  2. ;;                                                            ;;
  3. ;;  Provides continuous selection prompts until either a      ;;
  4. ;;  predicate function is validated or a keyword is supplied. ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  msg  - prompt string                                      ;;
  10. ;;  pred - optional predicate function [selection list arg]   ;;
  11. ;;  func - selection function to invoke                       ;;
  12. ;;  keyw - optional initget argument list                     ;;
  13. ;;------------------------------------------------------------;;
  14. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  15. ;;------------------------------------------------------------;;
  16.  
  17. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  18.   (while
  19.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  20.       (cond
  21.         ( (= 7 (getvar 'ERRNO))
  22.           (princ "\nMissed, Try again.")
  23.         )
  24.         ( (eq 'STR (type sel))
  25.           nil
  26.         )
  27.         ( (vl-consp sel)
  28.           (if (and pred (not (pred sel)))
  29.             (princ "\nInvalid Object Selected.")
  30.           )
  31.         )
  32.       )
  33.     )
  34.   )
  35.   sel
  36. )

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Select text only or else loop
« Reply #8 on: July 05, 2017, 05:24:14 PM »
Another, to prompt for the text contents:

Code - Auto/Visual Lisp: [Select]
  1. ; (TestPick "\nPick text: " (lambda (x / o) (if (and x (vlax-property-available-p (setq o (vlax-ename->vla-object x)) 'TextString)) (vla-get-TextString o))))
  2. (defun TestPick ( msg tf / rec )
  3.   (defun rec ( msg tf ) (setvar 'errno 0) (cond ( (tf (car (entsel msg))) ) ( (= 52 (getvar 'errno)) nil) ( (rec msg tf) ) ) )
  4.   (rec msg tf)
  5. ); defun TestPick
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Select text only or else loop
« Reply #9 on: July 06, 2017, 09:42:47 AM »
Sure. I had to strip out some things but this should be good enough.

Quote
Can you post a sample drawing ?
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b p text txt x)
  2.   (cond ((null (setq txt (ssget "_x" '((0 . "mtext"))))) (print "Text not found!"))
  3.         ((null (setq b (ssget "_x" '((0 . "insert") (2 . "_symbol")))))
  4.          (print "Block '_symbol' not found!")
  5.         )
  6.         ((setq txt
  7.                 (mapcar '(lambda (x) (cons (cdr (assoc 10 (entget x))) x)) (mapcar 'cadr (ssnamex txt)))
  8.          )
  9.          (if (null (tblobjname "block" "xpole"))
  10.            (progn (entmake '((0 . "BLOCK")
  11.                              (100 . "AcDbEntity")
  12.                              (67 . 0)
  13.                              (8 . "0")
  14.                              (100 . "AcDbBlockReference")
  15.                              (66 . 1)
  16.                              (2 . "xpole")
  17.                              (10 0.0 0.0 0.0)
  18.                              (70 . 2)
  19.                             )
  20.                   )
  21.                   (entmake '((0 . "CIRCLE")
  22.                              (100 . "AcDbEntity")
  23.                              (67 . 0)
  24.                              (8 . "0")
  25.                              (62 . 0)
  26.                              (100 . "AcDbCircle")
  27.                              (10 0.0 0.0 0.0)
  28.                              (40 . 6.24999949999751)
  29.                             )
  30.                   )
  31.                   (entmake '((0 . "ATTDEF")
  32.                              (100 . "AcDbEntity")
  33.                              (67 . 0)
  34.                              (8 . "0")
  35.                              (62 . 0)
  36.                              (100 . "AcDbText")
  37.                              (10 -11.13331384264566 8.407499455614502 0.0)
  38.                              (40 . 3.999999679998406)
  39.                              (1 . "NA")
  40.                              (50 . 0.0)
  41.                              (41 . 1.0)
  42.                              (51 . 0.0)
  43.                              (7 . "Standard")
  44.                              (71 . 0)
  45.                              (72 . 0)
  46.                              (11 0.0 0.0 0.0)
  47.                              (100 . "AcDbAttributeDefinition")
  48.                              (280 . 0)
  49.                              (3 . "Enter pole #")
  50.                              (2 . "POLE-NO")
  51.                              (70 . 1)
  52.                              (73 . 0)
  53.                              (74 . 0)
  54.                              (280 . 0)
  55.                             )
  56.                   )
  57.                   (entmake '((0 . "LINE")
  58.                              (100 . "AcDbEntity")
  59.                              (67 . 0)
  60.                              (8 . "0")
  61.                              (62 . 0)
  62.                              (100 . "AcDbLine")
  63.                              (10 4.419417028860769 -4.419417028860769 0.0)
  64.                              (11 -4.419417028860769 4.419417028860769 0.0)
  65.                             )
  66.                   )
  67.                   (entmake '((0 . "LINE")
  68.                              (100 . "AcDbEntity")
  69.                              (67 . 0)
  70.                              (8 . "0")
  71.                              (62 . 0)
  72.                              (100 . "AcDbLine")
  73.                              (10 4.419417028860769 4.419417028860769 0.0)
  74.                              (11 -4.419417028860769 -4.419417028860769 0.0)
  75.                             )
  76.                   )
  77.                   (entmake '((0 . "ENDBLK") (100 . "AcDbBlockEnd") (8 . "0")))
  78.            )
  79.          )
  80.          (command "_.attsync" "_Name" "xpole")
  81.          (foreach blk (mapcar 'cadr (ssnamex b))
  82.            (setq p (cdr (assoc 10 (entget blk))))
  83.            (if (setq
  84.                  text (car (vl-sort txt '(lambda (a b) (< (distance p (car a)) (distance p (car b))))))
  85.                )
  86.              (progn (setq txt (vl-remove text txt))
  87.                     (entmod (subst '(2 . "xpole") (assoc 2 (entget blk)) (entget blk)))
  88.                     (mapcar '(lambda (x) (vla-put-textstring x (cdr (assoc 1 (entget (cdr text))))))
  89.                             (vlax-invoke (vlax-ename->vla-object blk) 'getattributes)
  90.                     )
  91.                     ;; This line is to check that correct content was found
  92.                     ;; (entmakex (list '(0 . "line") '(8 . "check") (cons 10 p) (cons 11 (car text))))
  93.                     (entdel (cdr text))
  94.              )
  95.            )
  96.          )
  97.         )
  98.   )
  99.   (princ)
  100. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ziele_o2k

  • Newt
  • Posts: 49
Re: Select text only or else loop
« Reply #10 on: July 06, 2017, 11:03:35 AM »
Another from CADPL-Pack-v1:
Code - Auto/Visual Lisp: [Select]
  1. ; =========================================================================================== ;
  2. ; Wybiera zadane obiekty / Select a desired object                                            ;
  3. ;  Msg   [LIST]     - lista komunikatow / list of messages                                    ;
  4. ;  Obj   [LIST/nil] - LIST = lista zadanych obiektow / list of desired objects                ;
  5. ;                     nil  = wybiera dowolne obiekty / selects any objects                    ;
  6. ;  Init  [STR/nil]  - STR = slowa kluczowe / keywords                                         ;
  7. ;                     nil = bez slow kluczowych / no keywords                                 ;
  8. ;  Lock  [T/nil] - pomija obiekty na zamknietej warstwie / ignored objects in a locked layer  ;
  9. ;                  nil = tak / yes                                                            ;
  10. ;                  T   = nie / no                                                             ;
  11. ;  Enter [T/nil] - zakoncz prawy klik/spacja/enter / exit right click/space/enter             ;
  12. ;                  nil = nie / no                                                             ;
  13. ;                  T   = tak / yes                                                            ;
  14. ; =========================================================================================== ;
  15. (defun cd:USR_EntSelObj (Msg Obj Init Lock Enter / res)
  16.   (while
  17.     (progn
  18.       (setvar "ERRNO" 0)
  19.       (if Init (initget Init))
  20.       (setq res (entsel (car Msg)))
  21.       (cond
  22.         ( (= (getvar "ERRNO") 7)
  23.           (princ (cadr Msg))
  24.         )
  25.         ( (null res)
  26.           (if Enter
  27.             (not (princ (caddr Msg)))
  28.             (princ (caddr Msg))
  29.           )
  30.         )
  31.         ( (listp res)
  32.           (if
  33.             (if Obj
  34.               (member (cdr (assoc 0 (entget (car res)))) Obj)
  35.               T
  36.             )
  37.             (if Lock
  38.               (if (vlax-write-enabled-p (car res))
  39.                 (not (setq res res))
  40.                 (princ (last Msg))
  41.               )
  42.               (not (setq res res))
  43.             )
  44.             (princ (cadddr Msg))
  45.           )
  46.         )
  47.         ( (= (type res) (quote STR))
  48.           nil
  49.         )
  50.         (T nil)
  51.       )
  52.     )
  53.   )
  54.   res
  55. )
Example for text/mtext
Code - Auto/Visual Lisp: [Select]
  1. (cd:USR_EntSelObj
  2.   (list
  3.     "\nSelect Text or Mtext : " "Please select text/mtext " "Nothing selected "
  4.     "This is not text/mtext " "Object on closed layer "
  5.   )
  6.   (list "TEXT" "MTEXT") nil T T
  7. )

dubb

  • Swamp Rat
  • Posts: 1105
Re: Select text only or else loop
« Reply #11 on: July 06, 2017, 11:13:46 AM »
Wholly cow! I thats amazing. I didn't expect you to write all that. But it works great!

Sure. I had to strip out some things but this should be good enough.

Quote
Can you post a sample drawing ?
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b p text txt x)
  2.   (cond ((null (setq txt (ssget "_x" '((0 . "mtext"))))) (print "Text not found!"))
  3.         ((null (setq b (ssget "_x" '((0 . "insert") (2 . "_symbol")))))
  4.          (print "Block '_symbol' not found!")
  5.         )
  6.         ((setq txt
  7.                 (mapcar '(lambda (x) (cons (cdr (assoc 10 (entget x))) x)) (mapcar 'cadr (ssnamex txt)))
  8.          )
  9.          (if (null (tblobjname "block" "xpole"))
  10.            (progn (entmake '((0 . "BLOCK")
  11.                              (100 . "AcDbEntity")
  12.                              (67 . 0)
  13.                              (8 . "0")
  14.                              (100 . "AcDbBlockReference")
  15.                              (66 . 1)
  16.                              (2 . "xpole")
  17.                              (10 0.0 0.0 0.0)
  18.                              (70 . 2)
  19.                             )
  20.                   )
  21.                   (entmake '((0 . "CIRCLE")
  22.                              (100 . "AcDbEntity")
  23.                              (67 . 0)
  24.                              (8 . "0")
  25.                              (62 . 0)
  26.                              (100 . "AcDbCircle")
  27.                              (10 0.0 0.0 0.0)
  28.                              (40 . 6.24999949999751)
  29.                             )
  30.                   )
  31.                   (entmake '((0 . "ATTDEF")
  32.                              (100 . "AcDbEntity")
  33.                              (67 . 0)
  34.                              (8 . "0")
  35.                              (62 . 0)
  36.                              (100 . "AcDbText")
  37.                              (10 -11.13331384264566 8.407499455614502 0.0)
  38.                              (40 . 3.999999679998406)
  39.                              (1 . "NA")
  40.                              (50 . 0.0)
  41.                              (41 . 1.0)
  42.                              (51 . 0.0)
  43.                              (7 . "Standard")
  44.                              (71 . 0)
  45.                              (72 . 0)
  46.                              (11 0.0 0.0 0.0)
  47.                              (100 . "AcDbAttributeDefinition")
  48.                              (280 . 0)
  49.                              (3 . "Enter pole #")
  50.                              (2 . "POLE-NO")
  51.                              (70 . 1)
  52.                              (73 . 0)
  53.                              (74 . 0)
  54.                              (280 . 0)
  55.                             )
  56.                   )
  57.                   (entmake '((0 . "LINE")
  58.                              (100 . "AcDbEntity")
  59.                              (67 . 0)
  60.                              (8 . "0")
  61.                              (62 . 0)
  62.                              (100 . "AcDbLine")
  63.                              (10 4.419417028860769 -4.419417028860769 0.0)
  64.                              (11 -4.419417028860769 4.419417028860769 0.0)
  65.                             )
  66.                   )
  67.                   (entmake '((0 . "LINE")
  68.                              (100 . "AcDbEntity")
  69.                              (67 . 0)
  70.                              (8 . "0")
  71.                              (62 . 0)
  72.                              (100 . "AcDbLine")
  73.                              (10 4.419417028860769 4.419417028860769 0.0)
  74.                              (11 -4.419417028860769 -4.419417028860769 0.0)
  75.                             )
  76.                   )
  77.                   (entmake '((0 . "ENDBLK") (100 . "AcDbBlockEnd") (8 . "0")))
  78.            )
  79.          )
  80.          (command "_.attsync" "_Name" "xpole")
  81.          (foreach blk (mapcar 'cadr (ssnamex b))
  82.            (setq p (cdr (assoc 10 (entget blk))))
  83.            (if (setq
  84.                  text (car (vl-sort txt '(lambda (a b) (< (distance p (car a)) (distance p (car b))))))
  85.                )
  86.              (progn (setq txt (vl-remove text txt))
  87.                     (entmod (subst '(2 . "xpole") (assoc 2 (entget blk)) (entget blk)))
  88.                     (mapcar '(lambda (x) (vla-put-textstring x (cdr (assoc 1 (entget (cdr text))))))
  89.                             (vlax-invoke (vlax-ename->vla-object blk) 'getattributes)
  90.                     )
  91.                     ;; This line is to check that correct content was found
  92.                     ;; (entmakex (list '(0 . "line") '(8 . "check") (cons 10 p) (cons 11 (car text))))
  93.                     (entdel (cdr text))
  94.              )
  95.            )
  96.          )
  97.         )
  98.   )
  99.   (princ)
  100. )

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Select text only or else loop
« Reply #12 on: July 06, 2017, 11:17:50 AM »
Wholly cow! I thats amazing. I didn't expect you to write all that. But it works great!

Sure. I had to strip out some things but this should be good enough.

Quote
Can you post a sample drawing ?
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b p text txt x)
  2.   (cond   ((null (setq txt (ssget "_x" '((0 . "mtext"))))) (print "Text not found!"))
  3.    ((null (setq b (ssget "_x" '((0 . "insert") (2 . "_symbol")))))
  4.     (print "Block '_symbol' not found!")
  5.    )
  6.    ((setq txt
  7.       (mapcar '(lambda (x) (cons (cdr (assoc 10 (entget x))) x)) (mapcar 'cadr (ssnamex txt)))
  8.     )
  9.     (if (null (tblobjname "block" "xpole"))
  10.       (progn (entmake '((0 . "BLOCK")
  11.               (100 . "AcDbEntity")
  12.               (67 . 0)
  13.               (8 . "0")
  14.               (100 . "AcDbBlockReference")
  15.               (66 . 1)
  16.               (2 . "xpole")
  17.               (10 0.0 0.0 0.0)
  18.               (70 . 2)
  19.              )
  20.         )
  21.         (entmake '((0 . "CIRCLE")
  22.               (100 . "AcDbEntity")
  23.               (67 . 0)
  24.               (8 . "0")
  25.               (62 . 0)
  26.               (100 . "AcDbCircle")
  27.               (10 0.0 0.0 0.0)
  28.               (40 . 6.24999949999751)
  29.              )
  30.         )
  31.         (entmake '((0 . "ATTDEF")
  32.               (100 . "AcDbEntity")
  33.               (67 . 0)
  34.               (8 . "0")
  35.               (62 . 0)
  36.               (100 . "AcDbText")
  37.               (10 -11.13331384264566 8.407499455614502 0.0)
  38.               (40 . 3.999999679998406)
  39.               (1 . "NA")
  40.               (50 . 0.0)
  41.               (41 . 1.0)
  42.               (51 . 0.0)
  43.               (7 . "Standard")
  44.               (71 . 0)
  45.               (72 . 0)
  46.               (11 0.0 0.0 0.0)
  47.               (100 . "AcDbAttributeDefinition")
  48.               (280 . 0)
  49.               (3 . "Enter pole #")
  50.               (2 . "POLE-NO")
  51.               (70 . 1)
  52.               (73 . 0)
  53.               (74 . 0)
  54.               (280 . 0)
  55.              )
  56.         )
  57.         (entmake '((0 . "LINE")
  58.               (100 . "AcDbEntity")
  59.               (67 . 0)
  60.               (8 . "0")
  61.               (62 . 0)
  62.               (100 . "AcDbLine")
  63.               (10 4.419417028860769 -4.419417028860769 0.0)
  64.               (11 -4.419417028860769 4.419417028860769 0.0)
  65.              )
  66.         )
  67.         (entmake '((0 . "LINE")
  68.               (100 . "AcDbEntity")
  69.               (67 . 0)
  70.               (8 . "0")
  71.               (62 . 0)
  72.               (100 . "AcDbLine")
  73.               (10 4.419417028860769 4.419417028860769 0.0)
  74.               (11 -4.419417028860769 -4.419417028860769 0.0)
  75.              )
  76.         )
  77.         (entmake '((0 . "ENDBLK") (100 . "AcDbBlockEnd") (8 . "0")))
  78.       )
  79.     )
  80.     (command "_.attsync" "_Name" "xpole")
  81.     (foreach blk (mapcar 'cadr (ssnamex b))
  82.       (setq p (cdr (assoc 10 (entget blk))))
  83.       (if (setq
  84.        text (car (vl-sort txt '(lambda (a b) (< (distance p (car a)) (distance p (car b))))))
  85.           )
  86.         (progn (setq txt (vl-remove text txt))
  87.           (entmod (subst '(2 . "xpole") (assoc 2 (entget blk)) (entget blk)))
  88.           (mapcar '(lambda (x) (vla-put-textstring x (cdr (assoc 1 (entget (cdr text))))))
  89.              (vlax-invoke (vlax-ename->vla-object blk) 'getattributes)
  90.           )
  91.           ;; This line is to check that correct content was found
  92.           ;; (entmakex (list '(0 . "line") '(8 . "check") (cons 10 p) (cons 11 (car text))))
  93.           (entdel (cdr text))
  94.         )
  95.       )
  96.     )
  97.    )
  98.   )
  99.   (princ)
  100. )
Glad to help out :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

dubb

  • Swamp Rat
  • Posts: 1105
Re: Select text only or else loop
« Reply #13 on: July 06, 2017, 11:19:25 AM »
Thanks! This is very useful.

Another from CADPL-Pack-v1:
Code - Auto/Visual Lisp: [Select]
  1. ; =========================================================================================== ;
  2. ; Wybiera zadane obiekty / Select a desired object                                            ;
  3. ;  Msg   [LIST]     - lista komunikatow / list of messages                                    ;
  4. ;  Obj   [LIST/nil] - LIST = lista zadanych obiektow / list of desired objects                ;
  5. ;                     nil  = wybiera dowolne obiekty / selects any objects                    ;
  6. ;  Init  [STR/nil]  - STR = slowa kluczowe / keywords                                         ;
  7. ;                     nil = bez slow kluczowych / no keywords                                 ;
  8. ;  Lock  [T/nil] - pomija obiekty na zamknietej warstwie / ignored objects in a locked layer  ;
  9. ;                  nil = tak / yes                                                            ;
  10. ;                  T   = nie / no                                                             ;
  11. ;  Enter [T/nil] - zakoncz prawy klik/spacja/enter / exit right click/space/enter             ;
  12. ;                  nil = nie / no                                                             ;
  13. ;                  T   = tak / yes                                                            ;
  14. ; =========================================================================================== ;
  15. (defun cd:USR_EntSelObj (Msg Obj Init Lock Enter / res)
  16.   (while
  17.     (progn
  18.       (setvar "ERRNO" 0)
  19.       (if Init (initget Init))
  20.       (setq res (entsel (car Msg)))
  21.       (cond
  22.         ( (= (getvar "ERRNO") 7)
  23.           (princ (cadr Msg))
  24.         )
  25.         ( (null res)
  26.           (if Enter
  27.             (not (princ (caddr Msg)))
  28.             (princ (caddr Msg))
  29.           )
  30.         )
  31.         ( (listp res)
  32.           (if
  33.             (if Obj
  34.               (member (cdr (assoc 0 (entget (car res)))) Obj)
  35.               T
  36.             )
  37.             (if Lock
  38.               (if (vlax-write-enabled-p (car res))
  39.                 (not (setq res res))
  40.                 (princ (last Msg))
  41.               )
  42.               (not (setq res res))
  43.             )
  44.             (princ (cadddr Msg))
  45.           )
  46.         )
  47.         ( (= (type res) (quote STR))
  48.           nil
  49.         )
  50.         (T nil)
  51.       )
  52.     )
  53.   )
  54.   res
  55. )
Example for text/mtext
Code - Auto/Visual Lisp: [Select]
  1. (cd:USR_EntSelObj
  2.   (list
  3.     "\nSelect Text or Mtext : " "Please select text/mtext " "Nothing selected "
  4.     "This is not text/mtext " "Object on closed layer "
  5.   )
  6.   (list "TEXT" "MTEXT") nil T T
  7. )

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Select text only or else loop
« Reply #14 on: July 06, 2017, 11:37:31 AM »
Ron, my 2 cents is that you could mapcar the entmake, when creating the block definiton.  :nerdystraight:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg