Lisp that make selected text put in table arrangement

Lisp that make selected text put in table arrangement

Tolearnlisp
Enthusiast Enthusiast
10,713 Views
24 Replies
Message 1 of 25

Lisp that make selected text put in table arrangement

Tolearnlisp
Enthusiast
Enthusiast

Good Day everyone!

I have this idea in mind that need everyone's help. 

Is a LISP routine possible to make a selected text placed automatically in a table

Arrangement:

If Text 1 (blue text) and Text 2 (green text) was selected respectively, then at the end of the program, it can generate a table where all the selected text paste in it but it need to do by pairs.

 

I have attached the cad file for your reference, Please observe that the text in each box are transferred to the table by pairs.  Thank you

0 Likes
Accepted solutions (4)
10,714 Views
24 Replies
Replies (24)
Message 2 of 25

dlanorh
Advisor
Advisor
If the rectangle is always there and the text is always two colours you could just select the rectangles and from that get both text items.

I am not one of the robots you're looking for

0 Likes
Message 3 of 25

Tolearnlisp
Enthusiast
Enthusiast

Hi dlanorh,

Thank you. The rectangle is not present during actual, I just put in to show the grouping of each pair (consists of two text only per grouping. Imagine that the table is not yet present and could be automatically created after the selection of text made.

0 Likes
Message 4 of 25

Sea-Haven
Mentor
Mentor
Accepted solution

Please try this may need a bit of fine tuning but should be close. Needs Multi radio buttons.lsp

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-that-make-selected-text-put-in-table-arrangement/td-p/8801479
; By AlanH consulting May 2019 www.TBA June 2019 

(vl-load-com)

(defun AH:table_make (numcolumns  / numrows curspc colwidth numcolumns numrows rowheight sp doc)
(setq sp (vlax-3d-point (getpoint "Pick upper left")))
(setq doc  (vla-get-activedocument (vlax-get-acad-object) ))
(if (= (vla-get-activespace doc) 0)
(setq  curspc (vla-get-paperspace doc))
(setq curspc (vla-get-modelspace doc))
)
(setq numrows 2)
(setq rowheight (* 2.0 (getvar 'textsize)))
(setq colwidth (* 5 (getvar 'textsize)))
(setq objtable (vla-addtable curspc sp numrows numcolumns rowheight colwidth))
(vla-settext objtable 1 0 "Text1")
(vla-settext objtable 1 1 "Text2")
(vla-setcolumnwidth objtable 0 colwidth)
(vla-setcolumnwidth objtable 1 colwidth)
(setq notable 1)
)

(defun AH:table_add (tlst / txt rownum)
(setq rownum (vla-get-rows objtable))
(repeat (setq k (length tlst))
(vla-InsertRows objtable  rownum  (vla-GetRowHeight objtable (- rownum 1)) 1)
(setq txt  (nth 0 (nth (setq k (- k 1)) tlst)))
(vla-settext objtable rownum  0 txt)
(setq txt  (nth 1 (nth k tlst)))
(vla-settext objtable rownum  1 txt)
)
)

(defun AH:txt2tbl ( / ss x y tstr lst lst2 notable ans)
(alert "Pick a column or horizontal range of text\n\nThen Pick which type") 
(while (setq ss (ssget (list (cons 0 "*text"))))
(setq lst '())
(repeat (setq k (sslength ss))
(setq tobj (entget (ssname ss (setq k (- k 1)))))
(setq ins (cdr (assoc 10 tobj)))
(setq x (car ins))
(setq y (cadr ins))
(setq tstr (cdr (assoc 1 tobj)))
(setq lst (cons (list x y tstr) lst))
)
(if (not AH:Butts)(load "Multi radio buttons.lsp"))
(setq ans (ah:butts 1 "V"   '("Vertical or Hor text" "H" "V" )))
(cond
((= ans "H")
(setq lst (vl-sort lst
          (function (lambda (e1 e2)
          (< (cadr e1) (cadr e2)))))
))
((= ans "V")
(setq lst (vl-sort lst
          (function (lambda (e1 e2)
          (< (car e1) (car e2)))))
))
)
(setq lst2 '())
(setq k (length lst))
(repeat (/ k 2)
(if (= ans "V")
(setq lst2 (cons  (list (nth 2 (nth (setq k (- k 1)) lst))(nth 2 (nth (setq k (- k 1)) lst))) lst2))
(setq lst2 (cons (reverse (list (nth 2 (nth (setq k (- k 1)) lst))(nth 2 (nth (setq k (- k 1)) lst)))) lst2))
)
)
(if (= notable nil)(AH:table_make 2))
(if (= ans "H")
(AH:table_add (reverse lst2))
(AH:table_add lst2)
)
)
(setq notable nil)

)

(AH:txt2tbl)







0 Likes
Message 5 of 25

Tolearnlisp
Enthusiast
Enthusiast

Hi Sea.Haven,

Thank you for below code. I tested and so far it works but i have difficulty in running the program and I can't figure out the main command please help me on this. I need to app load the lisp every time I run the program in order to work. Thank you.

 

0 Likes
Message 6 of 25

Sea-Haven
Mentor
Mentor

If you change AH:txt2tbl to c:whatyouwant in the two locations in the code then you can run again without loading by typing whatyouwant.

 

The need for horizontal and vertical was to do simply, there is  a lot more in it to just pick in groups and work out vertical or horizontal.

0 Likes
Message 7 of 25

Tolearnlisp
Enthusiast
Enthusiast

Hi Sea.Haven,

 

Thank you but does't work on my approach:

I'm using appload command from Autocad to load the LISP, then none of the 3 c:below work highlighted in blue. 

 

Can you make it that all the blue text are always at text1 column and the green one in text2 column regardless of the arrangement of the text(vertical/horzontal) but base on the color. Thank you to consider this suggestion.

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-that-make-selected-text-put-in-...
; By AlanH consulting May 2019 www.TBA June 2019

(vl-load-com)

(defun AH:table_make (numcolumns / numrows curspc colwidth numcolumns numrows rowheight sp doc)
(setq sp (vlax-3d-point (getpoint "Pick upper left")))
(setq doc (vla-get-activedocument (vlax-get-acad-object) ))
(if (= (vla-get-activespace doc) 0)
(setq curspc (vla-get-paperspace doc))
(setq curspc (vla-get-modelspace doc))
)
(setq numrows 2)
(setq rowheight (* 2.0 (getvar 'textsize)))
(setq colwidth (* 5 (getvar 'textsize)))
(setq objtable (vla-addtable curspc sp numrows numcolumns rowheight colwidth))
(vla-settext objtable 1 0 "Text1")
(vla-settext objtable 1 1 "Text2")
(vla-setcolumnwidth objtable 0 colwidth)
(vla-setcolumnwidth objtable 1 colwidth)
(setq notable 1)
)

(defun AH:table_add (tlst / txt rownum)
(setq rownum (vla-get-rows objtable))
(repeat (setq k (length tlst))
(vla-InsertRows objtable rownum (vla-GetRowHeight objtable (- rownum 1)) 1)
(setq txt (nth 0 (nth (setq k (- k 1)) tlst)))
(vla-settext objtable rownum 0 txt)
(setq txt (nth 1 (nth k tlst)))
(vla-settext objtable rownum 1 txt)
)
)

(defun AH:whatyouwant ( / ss x y tstr lst lst2 notable ans)
(alert "Pick a column or horizontal range of text\n\nThen Pick which type")
(while (setq ss (ssget (list (cons 0 "*text"))))
(setq lst '())
(repeat (setq k (sslength ss))
(setq tobj (entget (ssname ss (setq k (- k 1)))))
(setq ins (cdr (assoc 10 tobj)))
(setq x (car ins))
(setq y (cadr ins))
(setq tstr (cdr (assoc 1 tobj)))
(setq lst (cons (list x y tstr) lst))
)
(if (not AH:Butts)(load "Multi radio buttons.lsp"))
(setq ans (ah:butts 1 "V" '("Vertical or Hor text" "H" "V" )))
(cond
((= ans "H")
(setq lst (vl-sort lst
(function (lambda (e1 e2)
(< (cadr e1) (cadr e2)))))
))
((= ans "V")
(setq lst (vl-sort lst
(function (lambda (e1 e2)
(< (car e1) (car e2)))))
))
)
(setq lst2 '())
(setq k (length lst))
(repeat (/ k 2)
(if (= ans "V")
(setq lst2 (cons (list (nth 2 (nth (setq k (- k 1)) lst))(nth 2 (nth (setq k (- k 1)) lst))) lst2))
(setq lst2 (cons (reverse (list (nth 2 (nth (setq k (- k 1)) lst))(nth 2 (nth (setq k (- k 1)) lst)))) lst2))
)
)
(if (= notable nil)(AH:table_make 2))
(if (= ans "H")
(AH:table_add (reverse lst2))
(AH:table_add lst2)
)
)
(setq notable nil)

)

(AH:whatyouwant)

0 Likes
Message 8 of 25

Sea-Haven
Mentor
Mentor

In response to your email if you look at code after this (if (= ans "V") it checks for ver or hor text in sorting out the text order if you make 4 choices Left Right Top Bot then this will imply the order you can use say left on the right if you swapped the right hand column to look like left. It will mean a bit extra coding have to do normal work now.

0 Likes
Message 9 of 25

pbejse
Mentor
Mentor
Accepted solution

@Tolearnlisp wrote:

Is a LISP routine possible to make a selected text placed automatically in a table


 

(defun c:Tablethisthingy ( / 	_DXF _isValidSelection _filterThisLayer _firstColumnText
                          	_SecondColumnText collectedData filteredLW i pl textinside )
	 
  (defun _DXF (el m ) (assoc m (entget el)))	
  (defun _isValidSelection (msg m / e)
    (if (and
          (setq e (car (entsel (strcat "\nSelect" msg "reference " m ))))
          (eq (Cdr (assoc 0 (entget e))) m)
         )
      e
    )
  )

  (if
    (and
		(setq _filterThisLayer (_isValidSelection " " "LWPOLYLINE"))          
          (setq _firstColumnText (_isValidSelection " First column " "TEXT"))
          (setq _SecondColumnText (_isValidSelection " Second column " "TEXT"))
    		(setq filteredLW (ssget  "_X" (list '(0 . "LWPOLYLINE")(cons 410 (Getvar 'ctab))
                                             (_DXF _filterThisLayer 8))))
          (setq basepoint (getpoint "\nPick point for Table:"))
          )
          
    (progn
      
      (setq _firstColumnText (_DXF _firstColumnText 8))
      (setq _SecondColumnText (_DXF _SecondColumnText 8))
      
      (repeat (setq i (sslength filteredLW))
        	(setq pl (ssname filteredLW (setq i (1- i))))
        		(if
                 (and
	     		(setq textinside (ssget "CP"
                             (mapcar 'cdr
                                     (vl-remove-if-not
                                       '(lambda (d) (= (car d) 10))
                                       (entget pl)
                                     )
                             )
                             (list '(0 . "TEXT")'(-4 . "<OR")
                                   _firstColumnText _SecondColumnText '(-4 . "OR>")
							)
                           )
                    )
                    (= (sslength textinside) 2)
                    )
                 
			(setq collectedData
                      (cons
                        (mapcar
                          '(lambda (n)
                             (mapcar
                               '(lambda (d)
                                  (cdr
                                    (_DXF (ssname textinside n)
                                          d
                                    )
                                  )
                                )
                               '(8 1)
                             )
                           )
                          '(0 1)
                        )
                        collectedData
                      )
               	)
                 )
        		)

	(if (and
             (setq filtered 
			(mapcar '(lambda (l)
	                 (mapcar '(lambda (f)(cadr (assoc l f))) collectedData))
	      				(mapcar 'cdr (list _firstColumnText _SecondColumnText))
	                  )
      	      )

      	   (setq collectedData
      		(vl-sort (mapcar 'list (Car filtered)(Cadr filtered))
                        '(lambda (a b)(< (cadr a)(cadr b))))
                   )
             )

	(progn
		(setq SupportDrawingTable
                           (vlax-invoke (vlax-get (vla-get-ActiveLayout (vla-get-ActiveDocument
                                                                          (vlax-get-acad-object))) 'Block)
                                                'Addtable basepoint
			2  2 0.1 0.8 	)
		)
          
          (vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-true)
       
		(mapcar '(lambda (y)
		  	(vla-settext SupportDrawingTable 1 (car y) (cadr y))
		  	(vla-setcelltextheight SupportDrawingTable 1 (car y) 0.08)
                               )
			 '((0 "Text 1")(1 "Text 2")))
                (vla-SetRowHeight SupportDrawingTable 1 0.19)
                (foreach col '((0 0.85)(1 0.85))
                                     (vla-SetColumnWidth SupportDrawingTable (Car col)(cadr col))
                      )
		
                (vla-InsertRowsAndInherit SupportDrawingTable 2 1 (setq i (length collectedData)))
                (setq row 1)
                (while (setq a (car collectedData))
                      (vla-settext SupportDrawingTable (setq row (1+ row)) 0 (car a))
                      (vla-settext SupportDrawingTable row 1 (cadr a))
                      (setq collectedData (cdr collectedData))
                      )
       		 
                (vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-false)
       		 (vla-DeleteRows SupportDrawingTable 0 1)
                )
             )
       	)
      )
   
	(princ)
    )

 

0 Likes
Message 10 of 25

Sea-Haven
Mentor
Mentor

Just a couple of comments. If you read the posts again you will see that the OP only put boxes around the text to clarify in reality they do not exist so using ssget "wp" can not be used.

 

There is also a bit of a stagger in the text in a couple of locations hence a wp may grab something else. In testing on left text did in two goes the ssget. I just plan to add the 4 variations for the moment a ultimate hint paid for would work out whats going on. 

 

Thanks for the vla-insertrowsandinherent hint that may be very useful. So many VL commands for a table.

0 Likes
Message 11 of 25

Tolearnlisp
Enthusiast
Enthusiast

thank you @pbejse,

I tried the program and run several trials, there are things that I've noticed like below:

1. From what the reference LWPOLYLINE referring to? because the rectangle is will not be there in actual.

1. The number of rows varies (on what part of the program defines this?)

2. What is the arrangement of the text? How is the program makes the order?

3. How's the data of column A & column B been collected in the model space, what is the limitation here?

Thank you in advance.

 

 

0 Likes
Message 12 of 25

Sea-Haven
Mentor
Mentor

Working on L R T B at moment hang in there. It will not recognise Green text etc but allows the swapping of the columns / rows as this is more a universal answer for any one else wanting to use.

0 Likes
Message 13 of 25

Sea-Haven
Mentor
Mentor

I have redone the code to use two picks of the text makes life easier to code.

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-that-make-selected-text-put-in-table-arrangement/td-p/8801479
; By AlanH consulting May 2019 www.TBA June 2019 

(vl-load-com)

(defun AH:table_make (numcolumns numrows / doc curspc colwidth numcolumns numrows rowheight sp doc)
  (setq sp (vlax-3d-point (getpoint "Pick upper left")))
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (if (= (vla-get-activespace doc) 0)
    (setq curspc (vla-get-paperspace doc))
    (setq curspc (vla-get-modelspace doc))
  )
  (setq rowheight 0.15)
  (setq colwidth (* 5 (getvar 'textsize)))
  (setq objtable (vla-addtable curspc sp numrows numcolumns rowheight colwidth))
  (vla-settext objtable 1 0 "Text1")
  (vla-settext objtable 1 1 "Text2")
  (vla-setcolumnwidth objtable 0 colwidth)
  (vla-setcolumnwidth objtable 1 colwidth)
  (setq notable 1)
)

(defun AH:table_add (tlst1 tlst2 / txt rownum)
  (setq rownum (vla-get-rows objtable))
  (repeat (setq k (length tlst1))
    (vla-InsertRows objtable rownum (vla-GetRowHeight objtable (- rownum 1)) 1)
    (setq txt (nth 2 (nth (setq k (- k 1)) tlst1)))
    (vla-settext objtable rownum 0 txt)
    (vla-setcelltextheight objtable rownum 0 0.1)
    (setq txt (nth 2 (nth k tlst2)))
    (vla-settext objtable rownum 1 txt)
    (vla-setcelltextheight objtable rownum 1 0.1)
  )
)

(defun sortlst1-2 (ans /)
  (if (or (= ans "B") (= ans "T"))
    (progn
      (setq lst (vl-sort lst (function (lambda (e1 e2) (< (car e1) (car e2))))))
      (setq lst2 (vl-sort lst2 (function (lambda (e1 e2) (< (car e1) (car e2))))))
    )
  )
  (if (or (= ans "L") (= ans "R"))
    (progn
      (setq lst (reverse (vl-sort lst (function (lambda (e1 e2) (< (cadr e1) (cadr e2)))))))
      (setq lst2 (reverse (vl-sort lst2 (function (lambda (e1 e2) (< (cadr e1) (cadr e2)))))))
    )
  )
)

; starts here

(defun AH:txt2tbl (/ ss x y tstr lst lst2 notable ans)
  (alert "Pick a column or horizontal range of text\n\n1st selection will be left column")
  (while (setq ss (ssget (list (cons 0 "*text"))))
    (setq lst '())
    (repeat (setq k (sslength ss))
      (setq tobj (entget (ssname ss (setq k (- k 1)))))
      (setq ins (cdr (assoc 10 tobj)))
      (setq x (car ins))
      (setq y (cadr ins))
      (setq tstr (cdr (assoc 1 tobj)))
      (setq lst (cons (list x y tstr) lst))
    )
    (alert "Pick a column or horizontal range of text\n\n2nd selection will be left column of table")
    (setq ss2 (ssget (list (cons 0 "*text"))))
    (if (/= (sslength ss) (sslength ss2))
      (progn
        (alert "You have not picked a equal number of texts\n\nwill now exit") (exit)
      )
      (progn
        (setq lst2 '())
        (repeat (setq k (sslength ss2))
          (setq tobj (entget (ssname ss2 (setq k (- k 1)))))
          (setq ins (cdr (assoc 10 tobj)))
          (setq x (car ins))
          (setq y (cadr ins))
          (setq tstr (cdr (assoc 1 tobj)))
          (setq lst2 (cons (list x y tstr) lst2))
        )
      )
    )

    (if (not AH:Butts) (load "Multi radio buttons.lsp"))
    (setq ans (ah:butts 1 "H" '( "Left Right Top Bot" "L" "R" "T" "B")))

    (sortlst1-2 ans)

    (if (= notable nil)
      (progn
        (AH:table_make 2 2)
        (vla-setrowheight objtable 0 0.12)
        (vla-setrowheight objtable 1 0.12)
        (vla-setcelltextheight objtable 0 0 0.1)
        (vla-setcelltextheight objtable 1 0 0.1)
        (vla-setcelltextheight objtable 1 1 0.1)
      )
    )

  (AH:table_add lst lst2)

  (setq notable nil)
  )
  (princ)
)


(AH:txt2tbl)
0 Likes
Message 14 of 25

pbejse
Mentor
Mentor

@Sea-Haven wrote:

... If you read the posts again you will see that the OP only put boxes around the text to clarify in reality they do not exist..

 


 

Good catch!

You are right, I dd not read the posts. I do have an idea on how to approach this condition. I'll look into this later

🙂

 

 

0 Likes
Message 15 of 25

pbejse
Mentor
Mentor

@Tolearnlisp wrote:

thank you @pbejse,

I tried the program and run several trials, there are things that I've noticed like below:

...


 

... there are things that I've noticed, why is there two 1.  🙂

 

Here's what we're going to do:

  1. There would be no selection for LWPOLYLINE [ Not existing as per posts ]
  2. Now it would depend on your selection
  3. The order would always be LEFT to RIGHT or TOP to BOTTOM
  4. We will remove the ssget filter "_X" 

 

The first two prompts will determine the order on the table. [ Horizontal or Vertical ]

 

Command: TABLETHISTEXTTHINGY

Select First column reference TEXT ( select the TEXT object that will be on the first column )

Select Second column reference TEXT ( select the TEXT object that will be on the second column )

Select objects ( select the TEXT objects that you want to be included on the table ) 

 

table this.PNG

(defun c:TablethisTextThingy ( /  _DXF _isValidSelection _firstColumnText _SecondColumnText
         FCT_lay SCT_lay basepoint collectedData  i pl SupportDrawingTable )

  (defun _DXF (el m ) (cdr (assoc m (entget el)))) 
  (defun _isValidSelection (msg m / e)
    (if (and
          (setq e (car (entsel (strcat "\nSelect" msg "reference " m ))))
          (eq (Cdr (assoc 0 (entget e))) m)
         )
     (list e (_DXF e 8))
      )
  )

  (if
    (and      
 (setq _firstColumnText (_isValidSelection " First column " "TEXT"))
 (setq _SecondColumnText (_isValidSelection " Second column " "TEXT"))
 
 (setq filteredLW (ssget (list '(0 . "TEXT")'(-4 . "<OR")
                                   (cons 8 (Strcat (Cadr _firstColumnText) "," (cadr _SecondColumnText)))
        '(-4 . "OR>")
       ))
       )
 (setq basepoint (getpoint "\nPick point for Table:"))
          )
          
    (progn
      
  (setq ppts (mapcar '(lambda (p) (_DXF (Car p) 10))
              (list _firstColumnText _SecondColumnText)))     
  (setq dist_XY (mapcar 'abs (mapcar '- (Car ppts)(cadr ppts))))
       (setq sym (if (< (car dist_XY)(cadr dist_XY))
                      (list < Caar)(list > cadar)))

       (repeat (setq i (sslength filteredLW))
          (setq pl (ssname filteredLW (setq i (1- i))))
    (setq data (list (_DXF pl 8)(_DXF pl 10)(_DXF pl 1)))
  
    (if (eq (Car data)  (cadr _firstColumnText))
       (setq FCT_lay  (cons (Cdr data) FCT_lay))
     (setq SCT_lay   (cons (Cdr data) SCT_lay))
            )
      )
       (if (and FCT_lay
                   SCT_lay
                   (= (length FCT_lay) (length SCT_lay))
                   (setq SortedData
                          (mapcar
                            '(lambda (l)
                               (mapcar
                                 'cadr
                                 (vl-sort
                                   l
                                   '(lambda (a b)
                                      ((Car sym) ((Cadr sym) a) ((Cadr sym) b))
                                    )
                                 )
                               )
                             )
                            (list FCT_lay SCT_lay)
                          )
                   )
                   (setq collectedData
                          (mapcar 'list
                                  (Car SortedData)
                                  (Cadr SortedData)
                          )
                   )
              )

 (progn
  (setq SupportDrawingTable
                           (vlax-invoke (vlax-get (vla-get-ActiveLayout (vla-get-ActiveDocument
                                                                          (vlax-get-acad-object))) 'Block)
                                                'Addtable basepoint
   2  2 0.1 0.8  )
  )
          
          (vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-true)
       
  (mapcar '(lambda (y)
     (vla-settext SupportDrawingTable 1 (car y) (cadr y))
     (vla-setcelltextheight SupportDrawingTable 1 (car y) 0.08)
                               )
    '((0 "Text 1")(1 "Text 2")))
                (vla-SetRowHeight SupportDrawingTable 1 0.19)
                (foreach col '((0 0.85)(1 0.85))
                                     (vla-SetColumnWidth SupportDrawingTable (Car col)(cadr col))
                      )
  
                (vla-InsertRowsAndInherit SupportDrawingTable 2 1 (setq i (length collectedData)))
                (setq row 1)
                (while (setq a (car collectedData))
                      (vla-settext SupportDrawingTable (setq row (1+ row)) 0 (car a))
                      (vla-settext SupportDrawingTable row 1 (cadr a))
                      (setq collectedData (cdr collectedData))
                      )
          
                (vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-false)
          (vla-DeleteRows SupportDrawingTable 0 1)
                )
             )
        )
      )
   
 (princ)
    )

 

 

 

 

0 Likes
Message 16 of 25

pbejse
Mentor
Mentor
Accepted solution

Command: TABLETHISTEXTTHINGY

Select First column reference TEXT [ The layer of this selected TEXT will be the first column value ]
Select Second column reference TEXT [ The layer of this selected TEXT will be the second column value ]
Select objects:  [ refer to image ]

Specify first corner: Specify opposite corner: 34 found

Pick point for Table:

 

 

table this.PNG

 

(defun c:TablethisTextThingy ( /  _DXF _isValidSelection _firstColumnText _SecondColumnText 
         FCT_lay SCT_lay basepoint collectedData  i pl ppts
                               dist_XY sym data SupportDrawingTable row a)

  (defun _DXF (el m ) (cdr (assoc m (entget el)))) 
  (defun _isValidSelection (msg m / e)
    (if (and
          (setq e (car (entsel (strcat "\nSelect" msg "reference " m ))))
          (eq (Cdr (assoc 0 (entget e))) m)
         )
     (list e (_DXF e 8))
      )
  )

  (if
    (and      
 (setq _firstColumnText (_isValidSelection " First column " "TEXT"))
 (setq _SecondColumnText (_isValidSelection " Second column " "TEXT"))
 
 (setq filteredLW (ssget (list '(0 . "TEXT")'(-4 . "<OR")
                                   (cons 8 (Strcat (Cadr _firstColumnText) "," (cadr _SecondColumnText)))
        '(-4 . "OR>")
       ))
       )
 (setq basepoint (getpoint "\nPick point for Table:"))
          )
          
    (progn
      
  (setq ppts (mapcar '(lambda (p) (_DXF (Car p) 10))
              (list _firstColumnText _SecondColumnText)))     
  (setq dist_XY (mapcar 'abs (mapcar '- (Car ppts)(cadr ppts))))
       (setq sym (if (< (car dist_XY)(cadr dist_XY))
                      (list < Caar)(list > cadar)))

       (repeat (setq i (sslength filteredLW))
          (setq pl (ssname filteredLW (setq i (1- i))))
    (setq data (list (_DXF pl 8)(_DXF pl 10)(_DXF pl 1)))
  
    (if (eq (Car data)  (cadr _firstColumnText))
       (setq FCT_lay  (cons (Cdr data) FCT_lay))
     (setq SCT_lay   (cons (Cdr data) SCT_lay))
            )
      )
       (if (and FCT_lay
                   SCT_lay
                   (= (length FCT_lay) (length SCT_lay))
                   (setq SortedData
                          (mapcar
                            '(lambda (l)
                               (mapcar
                                 'cadr
                                 (vl-sort
                                   l
                                   '(lambda (a b)
                                      ((Car sym) ((Cadr sym) a) ((Cadr sym) b))
                                    )
                                 )
                               )
                             )
                            (list FCT_lay SCT_lay)
                          )
                   )
                   (setq collectedData
                          (mapcar 'list
                                  (Car SortedData)
                                  (Cadr SortedData)
                          )
                   )
              )

 (progn
  (setq SupportDrawingTable
                           (vlax-invoke (vlax-get (vla-get-ActiveLayout (vla-get-ActiveDocument
                                                                          (vlax-get-acad-object))) 'Block)
                                                'Addtable basepoint
   2  2 0.1 0.8  )
  )
          
          (vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-true)
       
  (mapcar '(lambda (y)
     (vla-settext SupportDrawingTable 1 (car y) (cadr y))
     (vla-setcelltextheight SupportDrawingTable 1 (car y) 0.08)
                               )
    '((0 "Text 1")(1 "Text 2")))
                (vla-SetRowHeight SupportDrawingTable 1 0.19)
                (foreach col '((0 0.85)(1 0.85))
                                     (vla-SetColumnWidth SupportDrawingTable (Car col)(cadr col))
                      )
  
                (vla-InsertRowsAndInherit SupportDrawingTable 2 1 (setq i (length collectedData)))
                (setq row 1)
                (while (setq a (car collectedData))
                      (vla-settext SupportDrawingTable (setq row (1+ row)) 0 (car a))
                      (vla-settext SupportDrawingTable row 1 (cadr a))
                      (setq collectedData (cdr collectedData))
                      )
          
                (vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-false)
          (vla-DeleteRows SupportDrawingTable 0 1)
                )
             )
        )
      )
   
 (princ)
    )

 

 

 

0 Likes
Message 17 of 25

Tolearnlisp
Enthusiast
Enthusiast

Hi @Sea-Haven ,

Thank you for this revised code and it works, but again i can't figured out the command for this, I have to re appload lISP file in order to start the program.

0 Likes
Message 18 of 25

pbejse
Mentor
Mentor

That's weird, I knew i posted a revised code last night ( twice ) with a screencast and image, somehow it keeps disappearing. Smiley Frustrated

 

Oh well

 

 

0 Likes
Message 19 of 25

Tolearnlisp
Enthusiast
Enthusiast

Hi @pbejse ,

Thank you for the below code I appreciated the instruction you have provided.

Is it possible to make the text  order depend on the direction of how you will select the texts instead of just Top to Bottom and Left to right only. Thank you in advance.

 

Please refer to attached Cad file for your reference.

 

 

(defun c:TablethisTextThingy ( / _DXF _isValidSelection _firstColumnText _SecondColumnText
FCT_lay SCT_lay basepoint collectedData i pl ppts
dist_XY sym data SupportDrawingTable row a)

(defun _DXF (el m ) (cdr (assoc m (entget el))))
(defun _isValidSelection (msg m / e)
(if (and
(setq e (car (entsel (strcat "\nSelect" msg "reference " m ))))
(eq (Cdr (assoc 0 (entget e))) m)
)
(list e (_DXF e 8))
)
)

(if
(and
(setq _firstColumnText (_isValidSelection " First column " "TEXT"))
(setq _SecondColumnText (_isValidSelection " Second column " "TEXT"))

(setq filteredLW (ssget (list '(0 . "TEXT")'(-4 . "<OR")
(cons 8 (Strcat (Cadr _firstColumnText) "," (cadr _SecondColumnText)))
'(-4 . "OR>")
))
)
(setq basepoint (getpoint "\nPick point for Table:"))
)

(progn

(setq ppts (mapcar '(lambda (p) (_DXF (Car p) 10))
(list _firstColumnText _SecondColumnText)))
(setq dist_XY (mapcar 'abs (mapcar '- (Car ppts)(cadr ppts))))
(setq sym (if (< (car dist_XY)(cadr dist_XY))
(list < Caar)(list > cadar)))

(repeat (setq i (sslength filteredLW))
(setq pl (ssname filteredLW (setq i (1- i))))
(setq data (list (_DXF pl 8)(_DXF pl 10)(_DXF pl 1)))

(if (eq (Car data) (cadr _firstColumnText))
(setq FCT_lay (cons (Cdr data) FCT_lay))
(setq SCT_lay (cons (Cdr data) SCT_lay))
)
)
(if (and FCT_lay
SCT_lay
(= (length FCT_lay) (length SCT_lay))
(setq SortedData
(mapcar
'(lambda (l)
(mapcar
'cadr
(vl-sort
l
'(lambda (a b)
((Car sym) ((Cadr sym) a) ((Cadr sym) b))
)
)
)
)
(list FCT_lay SCT_lay)
)
)
(setq collectedData
(mapcar 'list
(Car SortedData)
(Cadr SortedData)
)
)
)

(progn
(setq SupportDrawingTable
(vlax-invoke (vlax-get (vla-get-ActiveLayout (vla-get-ActiveDocument
(vlax-get-acad-object))) 'Block)
'Addtable basepoint
2 2 0.1 0.8 )
)

(vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-true)

(mapcar '(lambda (y)
(vla-settext SupportDrawingTable 1 (car y) (cadr y))
(vla-setcelltextheight SupportDrawingTable 1 (car y) 0.08)
)
'((0 "Text 1")(1 "Text 2")))
(vla-SetRowHeight SupportDrawingTable 1 0.19)
(foreach col '((0 0.85)(1 0.85))
(vla-SetColumnWidth SupportDrawingTable (Car col)(cadr col))
)

(vla-InsertRowsAndInherit SupportDrawingTable 2 1 (setq i (length collectedData)))
(setq row 1)
(while (setq a (car collectedData))
(vla-settext SupportDrawingTable (setq row (1+ row)) 0 (car a))
(vla-settext SupportDrawingTable row 1 (cadr a))
(setq collectedData (cdr collectedData))
)

(vla-put-regeneratetablesuppressed SupportDrawingTable :vlax-false)
(vla-DeleteRows SupportDrawingTable 0 1)
)
)
)
)

(princ)
)

0 Likes
Message 20 of 25

pbejse
Mentor
Mentor
Accepted solution

@Tolearnlisp wrote:

Hi @pbejse ,

Thank you for the below code I appreciated the instruction you have provided.

Is it possible to make the text  order depend on the direction of how you will select the texts instead of just Top to Bottom and Left to right only. Thank you in advance.

 


 

 

So you did see the post. 

The selection order won't be a factor on this version, the program will compare the collected list with the two selected text and use that as the first row for the table.

 

Command: TABLETHISTEXTTHINGY

Select First column reference TEXT [ as the reference for the first column ]

Select Second column reference TEXT [ as the reference for the second column ]

Select objects: Specify opposite corner: 36 found [ Note that this should be an even number ]

Select objects: [ Enter to end selection ]

Pick point for Table:

 

As before:table this.PNG

 

HTH