Jump to content

Recommended Posts

Posted (edited)

Good day everyone!
Lisp displays data on the length and number of selected lines in a table.
But the table is obtained with one heading "Length",
and the heading "Quantity" is not displayed.
Please tell me how to fix it.
And one more question, how can I set a scale of 100 for a table in the code?

(defun c:CountLinesLengthTable (/ ss i ent len lenlist foundlen pt rowCount acadObj doc ms tableObj)
  (defun roundlen (l)
    (fix (+ l 0.5))
  )
  ;; Get the length of an object
  (defun getline (ent)
    (cond
      ((= (cdr (assoc 0 (entget ent))) "LINE")
        (distance (cdr (assoc 10 (entget ent))) (cdr (assoc 11 (entget ent))))
      )
      ((= (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
        (vlax-curve-getdistatparam (vlax-ename->vla-object ent) (vlax-curve-getendparam (vlax-ename->vla-object ent)))
      )
      (T 0)
    )
  )

  (setq ss (ssget '((0 . "LINE,LWPOLYLINE"))))
  (if ss
    (progn
      (setq lenlist nil)
      (setq i 0)
      (while (< i (sslength ss))
        (setq ent (ssname ss i))
        (setq len (roundlen (getline ent)))
        ;; Finding the length in the list
        (setq foundlen (assoc len lenlist))
        (if foundlen
          ;; If the length is already there, we increase the counter.
          (setq lenlist (subst (cons len (1+ (cdr foundlen))) foundlen lenlist))
          ;; If not, add a new pair.
          (setq lenlist (cons (cons len 1) lenlist))
        )
        (setq i (1+ i))
      )
    
      (setq pt (getpoint "
Specify the insertion point of the table: "))

      (setq acadObj (vlax-get-acad-object))
      (setq doc (vla-get-ActiveDocument acadObj))
      (setq ms (vla-get-ModelSpace doc))
      ;;Number of rows: 1 (header) + lengths
      (setq rowCount (+ 1 (length lenlist)))
      ;; Creating a table
      (setq tableObj (vla-AddTable ms
                                   (vlax-3d-point pt)
                                   rowCount
                                   2
                                   10.0 ; row height
                                   40.0 ; column width
                     )
      )
      ;; Filling in the headings
     (vla-SetText tableObj 0 0 "Length")
     (vla-SetText tableObj 0 1 "Quantity")

      ;;Sorting the list by length
      (setq lenlist (vl-sort lenlist '(lambda (a b) (< (car a) (car b)))))
      ;; Filling in the lines
      (setq i 1)
      (foreach pair lenlist
        (vla-SetText tableObj i 0 (itoa (car pair)))
        (vla-SetText tableObj i 1 (itoa (cdr pair)))
        (setq i (1+ i))
      )
      (princ "
The table is inserted.")
    )
    (princ "
There are no selected objects.")
  )
  (princ)
)


 

table.png

Edited by Nikon
Posted

I'd be tempted to make row 0 the title for the table, "Sum of Lengths", row 1 to be length and Quantity, with rows 2 - n being the data - you'd need to increase i by 1 and rowcount by 1 - without checking suspect that row 0 is set as a table title / header

Posted
4 hours ago, Steven P said:

I'd be tempted to make row 0 the title for the table, "Sum of Lengths", row 1 to be length and Quantity, with rows 2 - n being the data - you'd need to increase i by 1 and rowcount by 1 - without checking suspect that row 0 is set as a table title / header

I just need 2 columns: Length and Quantity.

Posted

Either of these any help

 

(vla-put-TitleSuppressed table :vlax-false)
(vla-put-HeaderSuppressed table :vlax-false)

 

  • Thanks 1
Posted

@Steven P went around in circles but false/true is only half of answer this helped

 

; UnmergeCells minRow, maxRow, minCol, maxCol

(vla-unMergeCells table 0 0 0 1)

Even though no Title it was still merged cells.

  • Like 1
  • Thanks 1
Posted

How can I programmatically change the scale when inserting a table?

Posted

Not so sure, I am assuming you mean the size of the table and not for example '1000' becoming '1' in your example.

 

Suspect you might need to create a table style with the settings you want - text sizes, line types, colours and so on, probably also add in the header row / merge cells of BigAls answer in table style too

  • Thanks 1
Posted (edited)

Like @Steven P you can make a table using say the "Standard" table and reset all the correct style values for the table.

Columnwidth, row height, text size, text alignment. When you make a table it just sets all column widths to one size so that is an obvious reset.

 

For any of us to help set the table to look like your image need a dwg with that table set up to how you want it, the correct size, then can work backwards getting all the required values to put into say a Lisp.

 

One of the lisps I have is make a new table style and make it current. I have posted code here but sorry can not remember which post.

Edited by BIGAL
  • Thanks 1
Posted

I don't want to create a new table style, as it will always need to be copied to a new file.
I only want to programmatically change the scale of the table when inserting.
Or is it impossible?
The code inserts a table with data, but does not change the scale and returns an error.
Specify the insertion point of the table: ; error: invalid argument type: lentityp #<VLA-OBJECT IAcadTable 0000000036da4e28>

(defun c:Lines_Len_Table_Scl (/ ss i ent len lenlist foundlen pt rowCount acadObj doc ms tableObj tableVla scaleFactor)
  (defun roundlen (l) (fix (+ l 0.5)))
  (defun getline (ent)
    (cond
      ((= (cdr (assoc 0 (entget ent))) "LINE")
        (distance (cdr (assoc 10 (entget ent))) (cdr (assoc 11 (entget ent)))))
      ((= (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
        (vlax-curve-getdistatparam (vlax-ename->vla-object ent)
                                   (vlax-curve-getendparam (vlax-ename->vla-object ent))))
      (T 0)
    )
  )

  (setq ss (ssget '((0 . "LINE,LWPOLYLINE"))))
  (if ss
    (progn
      (setq lenlist nil i 0)
      (while (< i (sslength ss))
        (setq ent (ssname ss i))
        (setq len (roundlen (getline ent)))
        (setq foundlen (assoc len lenlist))
        (if foundlen
          (setq lenlist (subst (cons len (1+ (cdr foundlen))) foundlen lenlist))
          (setq lenlist (cons (cons len 1) lenlist)))
        (setq i (1+ i)))
      (setq pt (getpoint "Specify the insertion point of the table: "))
      (setq acadObj (vlax-get-acad-object))
      (setq doc (vla-get-ActiveDocument acadObj))
      (setq ms (vla-get-ModelSpace doc))
      (setq rowCount (+ 2 (length lenlist))) ; 1 Title + 1 Header + data
      (setq tableObj (vla-AddTable ms
                                   (vlax-3d-point pt)
                                   rowCount
                                   2 
                                   10.0 ; row height
                                   40.0 ; column width
                     ))
     
      (vla-put-TitleSuppressed tableObj :vlax-false)
      (vla-put-HeaderSuppressed tableObj :vlax-false)
   
      (vla-SetText tableObj 0 0 "Table of line lengths")
    
      (vla-SetText tableObj 1 0 "Length")
      (vla-SetText tableObj 1 1 "Quantity")

      (setq lenlist (vl-sort lenlist '(lambda (a b) (< (car a) (car b)))))

      (setq i 2)
      (foreach pair lenlist
        (vla-SetText tableObj i 0 (itoa (car pair)))
        (vla-SetText tableObj i 1 (itoa (cdr pair)))
        (setq i (1+ i)))

      (setq tableVla (vlax-ename->vla-object tableObj))
      ;;  scale the object
      (vla-Scale tableVla (vlax-3d-point pt) 100.0)
          )
  )
  (princ)
)

 

Posted

The code I talked about makes a new table style in any dwg, so I put it at the start of any of my make a table solution. The code checks does the style exist if not then make it.

 

You don't need to scale the table just set all the parameters correctly and the table will be made at the size you want.

 

Again post a proper dwg with a table at the size you want, you just look at row height, column widths, text height being the 3 main things to change in an existing table.

  • Thanks 1
Posted
9 minutes ago, BIGAL said:

The code I talked about makes a new table style in any dwg, so I put it at the start of any of my make a table solution. The code checks does the style exist if not then make it.

 

You don't need to scale the table just set all the parameters correctly and the table will be made at the size you want.

Everything is clear with the table style, but is it possible to change the scale?

Posted

Maybe 'vla-transformBy'? 

  • Thanks 1
Posted
15 minutes ago, GLAVCVS said:

Maybe 'vla-transformBy'? 

Please explain in more detail

Posted

I don't understand same table just used SCALE.

 

image.png.6fc9a560103d8abb565e22e8ca320afb.png

 

Posted (edited)
12 minutes ago, BIGAL said:

I don't understand same table just used SCALE.

???

In AutoCAD, I can change the scale for any table, but I want to do it programmatically...

Edited by Nikon
Posted (edited)

"scale of 100 for a table in the code?" work out the correct scale value and just adjust the table. You know the default size of say text height and can work out what you want as text  height at 1:100. 

Edited by BIGAL
  • Like 1
  • Thanks 1
Posted (edited)
7 hours ago, BIGAL said:

"scale of 100 for a table in the code?" work out the correct scale value and just adjust the table. You know the default size of say text height and can work out what you want as text  height at 1:100. 

I often have to work with drawing at different scales, so it's inconvenient to set up a table for the desired drawing scale each time. It is easier to change the scale of the table when inserting.

Edited by Nikon
Posted

You can get the scale at the beginning of the insert / data collection, (getreal "Enter Text Height"), something like that and insert the table according to the value entered.

 

Table style can be created with LISP if it doesn't exist almost unnoticed in a routine to add a table to the drawing.

 

With regards to copying a table to another drawing - I don't use many tables so might be wrong - if you copy the table to another drawing where that table style exists then the inserted table will take on the formatting of the existing, it should then plot properly according to the new drawing?

  • Agree 1
Posted (edited)

Something like this code. You can change the scale to another one:  (command "_.SCALE" tableEnt "" pt "10")

Alternatively, you can add a request to select a scale from the list.

;; Determining line lengths and inserting the length and number of lines with the same length into the table
;; The table changes the scale of the table by 10 when inserted
(defun c:Lines-Len-Table-Scl10 (/ ss i ent len lenlist foundlen pt rowCount acadObj doc ms tableObj tableEnt)

  (defun roundlen (l) (fix (+ l 0.5)))

  (defun getline (ent)
    (cond
      ((= (cdr (assoc 0 (entget ent))) "LINE")
        (distance (cdr (assoc 10 (entget ent))) (cdr (assoc 11 (entget ent)))))
      ((= (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
        (vlax-curve-getdistatparam (vlax-ename->vla-object ent)
                                   (vlax-curve-getendparam (vlax-ename->vla-object ent))))
      (T 0)
    )
  )

  (setq ss (ssget '((0 . "LINE,LWPOLYLINE"))))
  (if ss
    (progn
      (setq lenlist nil i 0)
      (while (< i (sslength ss))
        (setq ent (ssname ss i))
        (setq len (roundlen (getline ent)))
        (setq foundlen (assoc len lenlist))
        (if foundlen
          (setq lenlist (subst (cons len (1+ (cdr foundlen))) foundlen lenlist))
          (setq lenlist (cons (cons len 1) lenlist)))
        (setq i (1+ i)))
      (setq pt (getpoint " Specify the insertion point of the table: "))
      (setq acadObj (vlax-get-acad-object))
      (setq doc (vla-get-ActiveDocument acadObj))
      (setq ms (vla-get-ModelSpace doc))
      (setq rowCount (+ 2 (length lenlist))) ; 1 Title + 1 Header + data
      (setq tableObj (vla-AddTable ms
                                   (vlax-3d-point pt)
                                   rowCount
                                   2 
                                   10.0 ; row height
                                   40.0 ; column width
                     ))
      ;;  Title - Header
      (vla-put-TitleSuppressed tableObj :vlax-false)
      (vla-put-HeaderSuppressed tableObj :vlax-false)
      
      (vla-SetText tableObj 0 0 "Table of line lengths")
    
      (vla-SetText tableObj 1 0 "Length")
      (vla-SetText tableObj 1 1 "Quantity")
     
      (setq lenlist (vl-sort lenlist '(lambda (a b) (< (car a) (car b)))))
    
      (setq i 2)
      (foreach pair lenlist
        (vla-SetText tableObj i 0 (itoa (car pair)))
        (vla-SetText tableObj i 1 (itoa (cdr pair)))
        (setq i (1+ i)))
      ;; Scaling the table
      (setq tableEnt (vlax-vla-object->ename tableObj))
   (command "_.SCALE" tableEnt "" pt "10")
         (princ " There are no selected objects."))
)
  (princ)
)

 

Edited by Nikon

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