Autolisp to select the Alternate Text from the selected text.

Autolisp to select the Alternate Text from the selected text.

arpansark0544TCX
Advocate Advocate
398 Views
7 Replies
Message 1 of 8

Autolisp to select the Alternate Text from the selected text.

arpansark0544TCX
Advocate
Advocate

Dear All,

Happy New Year to you!

I am working on a Lisp which can select the alternate text regardless of its coordinate.

The use has to input the axiz of the selection and then select the text in that axis.

The code should select only the alternate text regardless of the y axis.

 

Below is the code:

(Kindly guide me to make it a working one.)

(defun c:SelAltText (/ ss count index selected)
  (prompt "\nSelect text entities to process: ")
  ;; Prompt user to select text entities
  (setq ss (ssget '((0 . "TEXT,MTEXT")))) ; Filter for TEXT and MTEXT objects
  (if ss
    (progn
      ;; Initialize variables
      (setq count (sslength ss))  ; Total number of selected entities
      (setq index 0)              ; Start index
      (setq selected (ssadd))     ; Create an empty selection set
      ;; Loop through the selection and pick alternate entities
      (while (< index count)
        (if (= 0 (rem index 2)) ; Select even-indexed entities
          (ssadd (ssname ss index) selected))
        (setq index (1+ index))) ; Move to the next index
      ;; Highlight the selected entities
      (if (> (sslength selected) 0)
        (progn
          (sssetfirst nil selected) ; Highlight alternate entities
          (prompt (strcat "\nSelected " (itoa (sslength selected)) " alternate text entities.")))
        (prompt "\nNo alternate text entities found."))
    )
    (prompt "\nNo text entities selected."))
  (princ)) ; Exit cleanly

 

 

arpansark0544TCX_0-1737460460452.png

 

 

0 Likes
Accepted solutions (1)
399 Views
7 Replies
Replies (7)
Message 2 of 8

ВeekeeCZ
Consultant
Consultant

I wrote something similar to so long ago... have a preselected some objects (not necessary texts), make a vector in which direction would be every other object left in selection.

 

;-----
(defun c:SelectEveryOther ( / *error* os si p1 p2 i a lst p ll ur)  ; SEO
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if os (setvar 'osmode os))
    (if (and e (entget e)) (entdel e))
    (princ))


  (setq si (ssget "_I"))
  
  (if (and (setq os (getvar 'osmode))
	   (setvar 'osmode 0)
	   (setq p1 (getpoint "\nSpecify first point of direction: "))
	   (setq p2 (getpoint p1 "\nSpecify second: "))
	   (setq a (+ (angle p1 p2) (/ pi 2)))
	   (setvar 'osmode os)
	   (or si
	       (setq si (ssget "_f" (list p1 p2))))
	   (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex si))))
	   (setq lst (mapcar '(lambda (x) (cons (mapcar '/ (apply 'mapcar (cons '+ (progn (vla-getboundingbox (vlax-ename->vla-object x) 'll 'ur) (mapcar 'vlax-safearray->list (list ll ur))))) '(2 2)) x)) lst))
	   (setq lst (mapcar '(lambda (x) (cons (inters p1 p2 (car x) (polar (car x) a 1) nil) (cdr x))) lst))
	   (setq p (mapcar '(lambda (x) (apply 'min x)) (list (mapcar 'caar lst) (mapcar 'cadar lst))))
	   (setq lst (vl-sort lst '(lambda (x1 x2) (< (distance p (car x1)) (distance p (car x2))))))
	   (setq lst (mapcar 'cdr lst))
	   )
    (while (setq e (cadr lst))
      (ssdel e si)
      (setq lst (cdr (cdr lst)))))

  (if (and si (> (sslength si) 0))
    (sssetfirst nil si))
  (princ)
  )

 

Message 3 of 8

Moshe-A
Mentor
Mentor

@arpansark0544TCX  hi,

 

i think you can not relay on every 2nd object in ss because the order the objects are hold in memory are the same as they were entered to drawing and in reverse order so some sorting in XY direction must be done here.

 

Moshe

Message 4 of 8

arpansark0544TCX
Advocate
Advocate

OK.

But how can that be done.

 

0 Likes
Message 5 of 8

Moshe-A
Mentor
Mentor

@arpansark0544TCX ,

 

have you test what @ВeekeeCZ  send you?

 

the algorithm should go this way:

1. for horizontal texts objects, select them in a row, process them (ename) into a list, sort them by X coordinates value, add even texts to 'Selected' 

2. for vertical texts objects, the same but sorting by Y coordinates.

 

Moshe

 

0 Likes
Message 6 of 8

Moshe-A
Mentor
Mentor
Accepted solution

@arpansark0544TCX ,

 

Here is my version that determine automatically the text direction (sorted from 0,0 to positive X, positive Y)

 

enjoy

Moshe

 

(defun c:SelAltText (/ build_data determine_direction ; local functions
                       ss points^ selected item i)
  
  (defun build_data (p)
    (mapcar
      (function
        (lambda (ename / p0 p1 MinPoint MaxPoint)
          (vla-GetBoundingBox (vlax-ename->vla-object ename) 'MinPoint 'MaxPoint)
          (setq p0 (vlax-safearray->list MinPoint))
          (setq p1 (vlax-safearray->list MaxPoint))
          (list (mapcar (function (lambda (x0 x1) (/ (+ x0 x1) 2))) p0 p1) ename)
        ); lambda
      ); function
      (vl-remove-if 'listp (mapcar 'cadr (ssnamex p)))
    ); mapcar
  ); build_data


  (defun determine_direction (lst / _xsort _ysort ; local functions
                                    xlsy ylst x0 x1 y0 y1)
   ; anonymous functions
   (setq _xsort (lambda (lst) (vl-sort lst (function (lambda (e0 e1) (< (caar e0 ) (caar  e1)))))))
   (setq _ysort (lambda (lst) (vl-sort lst (function (lambda (e0 e1) (< (cadar e0) (cadar e1)))))))  
    
    
   (setq xlst (_xsort lst))
   (setq x0 (caaar xlst)  x1 (caar  (last xlst)))
   (setq ylst (_ysort lst))
   (setq y0 (cadaar ylst) y1 (cadar (last ylst)))
    
   (if (> (- x1 x0) (- y1 y0)) xlst ylst)
  ); determine_direction

  ; here start c:SelAltText
  (if (and
        (setq ss (ssget '((0 . "text,mtext"))))
        (setq points^ (build_data ss))
      )
   (progn
    (setq selected (ssadd) i -1)
    (foreach item (reverse (determine_direction points^))
      (setq i (1+ i))
      (if (= (rem i 2) 0)
        (ssadd (cadr item) selected)
	  ) 
     ); foreach
     
     (if (> (sslength selected) 0)
      (sssetfirst nil selected)
      (prompt "\nNo alternate text text(s) found.")
	 )
   ); progn
  ); if

  (princ)
); c:SelAltText

 

Message 7 of 8

arpansark0544TCX
Advocate
Advocate

Thank you. It is working so great. I will look and study what changes you have included.

0 Likes
Message 8 of 8

Moshe-A
Mentor
Mentor

@arpansark0544TCX ,

 

thanks you for that, i am here if you have questions about the code 😀

0 Likes