Jump to content

Delete table row


zwonko

Recommended Posts

Is there option to delete row in AutoCAD table in autolisp? For example row number 4.

Didn't find anything on web. Maybe anybody know easy workaround? 

Link to comment
Share on other sites

I don't know how to ask user to Select a cell or test that a cell has been selected. so just do that before running this command. maybe someone can figure that out.

 

;;----------------------------------------------------------------------------;;
;; Delete current row from table _O = Operation _RO = delete ROw _Q = Quit
(defun C:DCR(\)
  ;ask user to select a cell and wait for selection
  (setvar 'cmdecho 0)
  (command "_EditTableCell" "_O" "_RO" "_Q") 
  (setvar 'cmdecho 1)
  (princ)
)

 

 

 

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

thank You!!! Renember that "pause" gives user option to choose. But I want to implement it in other code. I've I manage I will paste code here.

 

Link to comment
Share on other sites

Lee-mac has a Hit-test that returns the cell address of a table. Have a look for it.

 

 

(setq obj (vlax-ename->vla-object (car  (entsel "Pick table obj"))))
(vla-DeleteRows obj 3 1) ; row how many
(vla-put-regeneratetablesuppressed obj :vlax-false) ; regen tablea

 

Edited by BIGAL
  • Like 2
Link to comment
Share on other sites

As an option, you can do it with one click using HitTest,

This is ActiveX, so it should be translatable to Autolisp

 

import traceback
import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed

import AxApp24 as Ax

def PyRxCmd_deleterow():
    try:
        axApp = Ax.getApp()
        axDoc = axApp.ActiveDocument
        
        #select the cell but also make a slection fence
        normal = (0, 0, 1) #z
        hitPnt = axDoc.Utility.GetPoint("\nSelect cell: ")
        minmax = hitPnt + axDoc.GetVariable("VSMAX")
 
        #select using out new fence
        axSs = axDoc.SelectionSets.Add("AXTBLSS")
        axSs.SelectByPolygon(Ax.constants.acSelectionSetFence,
                             minmax, [0], ["ACAD_TABLE"])
        
        #run the hittest
        for axEnt in axSs:
            axTable = Ax.IAcadTable(axEnt)
            hit = axTable.HitTest(hitPnt, normal)
            if hit[0]:
                axTable.DeleteRows(hit[1],1)

    except Exception as err:
        traceback.print_exception(err)
    finally:
        axSs.Delete()
        

 

  • Like 2
Link to comment
Share on other sites

Like this

 

(setq obj (vlax-ename->vla-object (car  (entsel "Pick obj"))))
(setq pt  (getpoint "\nPick point inside cell "))
(If (Eq :Vlax-true (Vla-hittest Obj (Vlax-3d-point (Trans Pt 1 0))(Vlax-3d-point (Trans (Getvar 'Viewdir) 1 0)) 'R 'C)) (List O R C) )

(NIL 3 0) = (nil row column)

  • Like 2
Link to comment
Share on other sites

Great thanks for everyone. For me the best was @BIGAL from third post. There is not so much info about vla-DeleteRows, but that gives me everything.
 

(vla-DeleteRows obj 3 1) ; row how many

 

My goal was to automatically clean/delete row from table, if values are same in next rows. Maybe somebody will find this code usefull.
 

(defun c:RemoveDuplicateRows_WORKING_clean_old4 (/ ent row tbl cell1 cell2 col)
  (if
    (setq ent (car (entsel "\nSelect a table: "))) ; allows the user to select a table
    (progn
      (princ "\nTable selected successfully.") ; displays a message after selecting a table
      (setq tbl (vlax-ename->vla-object ent))
      (setq col 1)
      (setq row 0)
      (princ "\nRow 0 selected.") ; displays a message after selecting a row
      (while (< row (vla-get-rows tbl)) ; recalculates the number of rows after each deletion
        (setq cell1 (vla-GetText tbl row col)) ; compares only the first column
        (setq cell2 (vla-GetText tbl (+ row 1) col)) ; compares only the first column
        (princ "\nCells read.") ; displays a message after reading the cells
        (if (and cell1 cell2 (= cell1 cell2))
          (progn
            (vla-DeleteRows tbl (+ row 1) 1)  ; vla-DeleteRows objectTable rowNumber HowManyRowsToDelete
            (princ (strcat "\nRow " (itoa row) " and row " (itoa (+ row 1)) " are the same.")) ; displays a message if the rows are the same
          )
          (setq row (+ row 1))
        )
      )
      (princ (strcat "\nNumber of rows after removing duplicates: " (itoa (vla-get-rows tbl)))) ; displays the number of rows after removing duplicates
    )
  )
  (princ)
)



Code is cleaning table/deleting rows If the data in subsequent rows is identical and repeated several times in a row. For example data:

1 1520

2 1520

3 1520

4 1530

5 1540

6 1520

7 1520

8 1545

we will revive:

1 1520

4 1530

5 1540

6 1520

8 1545

 

The next goal to achieve is that in the first column you will get something resembling a rep count. If I manage to achieve this, I will post the code here. Wan't to get it like:

1to3 1520

4 1530

5 1540

6to7 1520

8 1545

Edited by zwonko
Link to comment
Share on other sites

Ok rather than delete rows you can remove duplicates in a list that would be easier, then make the table. You do though need to sort the list for it to work. yes can get 1520 3, 1540 4 and so on as a count then put in table. Will see what I can do later working on something similar at moment.

Link to comment
Share on other sites

 

Yes, I also thought it's easier to make a list and prepare the table. I even have a code for it. However, the problem is that I need to set the appropriate styles/height/etc in the generated table - to make it looks like source. So I've get back to deleting. Code for new table is:
 

(defun c:NewTableWithRemovedDuplicateRow_WORKING (/ ent row tbl cell1 cell2 newtbl pt rowmax inspt uniqueData)
  (if
    (setq ent (car (entsel "\nSelect a table: "))) ; allows the user to select a table
    (progn
      (princ "\nTable selected successfully.") ; displays a message after selecting a table
      (setq tbl (vlax-ename->vla-object ent))
      (setq inspt (vlax-get tbl 'InsertionPoint)) ; reads the insertion point of the table
	(setq colWidth1 (vla-getColumnWidth tbl 0))
	(setq rowHeight (vla-getRowHeight tbl 2))
      (setq row 0)
      (setq rowmax (vla-get-rows tbl)) ; reads the number of rows in the table
      (setq uniqueData '()) ; initializes the list of unique data
      (while (< row 2) ; copies the first two rows without checking
        (setq uniqueData (append uniqueData (list (list (vla-GetText tbl row 0) (vla-GetText tbl row 1))))) ; adds the row to the end of the list
        (setq row (+ row 1))
      )
      (while (< row rowmax) ; recalculates the number of rows after each removal
        (setq cell1 (vla-GetText tbl row 1)) ; compares only the second column
        (if (not (member cell1 (mapcar 'cadr uniqueData))) ; checks if the value is already in uniqueData
          (setq uniqueData (append uniqueData (list (list (vla-GetText tbl row 0) (vla-GetText tbl row 1)))))) ; adds the unique row to the end of the list
        (setq row (+ row 1))
      )
      (setq pt (getpoint "\nEnter the insertion point for the new table: ")) ; asks the user for the insertion point
      (setq newtbl (vla-addtable (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) (vlax-3d-point (list (car pt) (cadr pt))) (length uniqueData) 2 rowHeight colWidth1)) ; creates a new table based on the length of uniqueData
      (setq row 0) ; starts from the first row
      (foreach item uniqueData
        (vla-SetText newtbl row 0 (nth 0 item)) ; copies the unique data to the new table
        (vla-SetText newtbl row 1 (nth 1 item)) ; copies the unique data to the new table
        (setq row (+ row 1))
      )
      (princ (strcat "\nNumber of rows in the new table: " (itoa (vla-get-rows newtbl)))) ; displays the number of rows in the new table
      (princ (strcat "\nInsertion point of the selected table: " (vl-princ-to-string inspt))) ; displays the insertion point of the selected table
    )
  )
  (princ)
)

 

Can't manage to get same textsize inside new table, so get back for deleting. And actually this code is not exactly what it meant to be. Problem was that it is making just UniqueData. And I wan't to have only the same deleted if there is new value only in previous cell. Probably I've manage to do it but somewhere I've lost the code..

If somebody knows way to set same textHeight like in source table, code for new table is probably faster. 
 

Edited by zwonko
Link to comment
Share on other sites

When you make a table it uses the default table style. Once you make a table you can reset a lot of things like column sizes, text size & alignment. I make a table with Title heading and 1 data row then reset it all to how I want it to look. Then use insertrow method to add rows.

 

Post a dwg with a sample table set up the way you want it to look.

Link to comment
Share on other sites

  • 2 weeks later...

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