Author Topic: Get field ?  (Read 11134 times)

0 Members and 1 Guest are viewing this topic.

new_mem

  • Newt
  • Posts: 67
Get field ?
« on: October 19, 2011, 03:34:37 AM »
Can someone tell me the way get field of cell in table. ?

Thank u.


irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #1 on: October 19, 2011, 05:44:57 AM »
Double click to edit the cell, then right-click --> Insert Field (or Ctrl+F). To get a formula, then as you've got it selected, right-click --> Insert --> Formula --> (Sum / Avg / Count / Cell / Equation)

Or do you mean you want to make a field through lisp? If so you simply need the field-codes and set that cell's value to the relevant field code. The field gets generated "automagically".

Or ... are you referring to making a field which points to a cell? If so I don't think there is a way other than formulas in the table itself.
« Last Edit: October 19, 2011, 05:48:56 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #2 on: October 19, 2011, 06:07:42 AM »
Actually doing some further investigation, you can generate the field-code programmatically. You need the table's ObjectId (see the function below). Then you need to insert a formula field with the following:
Code: [Select]
Table(###).A1Where ### is the object id of the table, and A1 is the address of the cell.

To get the object Id you could either go manually by making an object property field to that table then searching through the field code at the bottom for the number following %<\_ObjId ... e.g.
Quote
%<\_ObjId -1121994336>%
Or you could use this lisp function:
Code: [Select]
(defun c:GetObjId (/ en)
  (vl-load-com)
  (if (setq en (entsel))
    (princ (vla-get-ObjectId (vlax-ename->vla-object (car en))))
  )
  (princ)
)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get field ?
« Reply #3 on: October 19, 2011, 07:47:25 AM »
Do you mean retrieving the value of that cell?

If so...

Code: [Select]
(vla-gettext <Table> <row> <col>)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #4 on: October 19, 2011, 08:14:30 AM »
Here's what I mean by using the formula with the table object's Id. Note it only work on cells containing numbers (no text and / or blanks).

Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #5 on: October 19, 2011, 09:32:06 AM »
Here's what I mean by using the formula with the table object's Id. Note it only work on cells containing numbers (no text and / or blanks).

Great.
That is simple.  :ugly:
Thanks
But it not true with table extract data.! how can fix it.!?


irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #6 on: October 19, 2011, 09:36:43 AM »
You're welcome!
But it not true with table extract data.! how can fix it.!?
I'm unsure what you mean. The extract data should work fine AFAIK. Are you referring to Data Extract? That one generates a table, it doesn't read from a table (unfortunately).

Could you explain a bit more, what are you actually trying to achieve? Perhaps there's an alternative way of performing this.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #7 on: October 19, 2011, 09:43:00 AM »
Do you mean retrieving the value of that cell?

If so...

Code: [Select]
(vla-gettext <Table> <row> <col>)
Thanks.

True.  :lmao:

So how to select cell any by (entsel) and get ID and text of this cell.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get field ?
« Reply #8 on: October 19, 2011, 10:06:01 AM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #9 on: October 19, 2011, 10:07:26 AM »
Quick and dirty ... just copy to command line:
Code: [Select]
(setq tblObj (vlax-ename->vla-object (car (entsel))))Then to get the 1st row & 2nd column
Code: [Select]
(vla-gettext tblObj 0 1)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #10 on: October 19, 2011, 10:23:22 AM »
Also to obtain the cell's address from a point you can use the Table object's HitTest method. E.g.
Code: [Select]
(vlax-invoke-method tblObj 'HitTest (vlax-3D-point (getpoint)) (vlax-3D-point '(0.0 0.0 1.0)) 'r 'c)If the result is :vlax-true, then r will contain the row number and c the column number.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #11 on: October 19, 2011, 10:32:44 AM »
You're welcome!
But it not true with table extract data.! how can fix it.!?
I'm unsure what you mean. The extract data should work fine AFAIK. Are you referring to Data Extract? That one generates a table, it doesn't read from a table (unfortunately).

Could you explain a bit more, what are you actually trying to achieve? Perhaps there's an alternative way of performing this.

Okey,
I mean:
Step1: I get ID any cell by select table extract data but i want select cell by mouse.
Step2: Asign value text by field (some code lisp here)


new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #12 on: October 19, 2011, 10:54:27 AM »
My code:
(setq obj_tbl (vlax-ename->vla-object (car (entsel "\nSelect object table")))
;"how can get cell data table by select position cell at 3dpoint ?
(setq value_cell (?)
(setq obj_text (vlax-ename->vla-object (car (entsel "\nSelect object text")))
   field_data (strcat "%<\\AcObjProp Object(%<\\_ObjId " (rtos value_cell 2 0) ">%).TextString>%")
   )
  (vla-put-textstring obj_text field_data)
  (vla-update obd)
  (vl-cmdf "regen")

Realy thank u for your solution.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get field ?
« Reply #13 on: October 19, 2011, 11:05:16 AM »
To my knowledge you cannot directly reference a field cell value using a Field in another object.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #14 on: October 19, 2011, 11:15:55 AM »
To my knowledge you cannot directly reference a field cell value using a Field in another object.
You "can" but it's not recommended ... nested fields require more than one regen (or other type of update) to propagate a change. So you could run into a scenario where the plotted value is inconsistent.

Though this is again pointing me in strange directions. I thought you wanted to place a field inside a text to duplicate a value from the table? If I'm still on the same track then perhaps this could help:
Code: [Select]
(vl-load-com)

;;; Helper function to get the cell reference of a picked point in a table
;;; tbl - the ActiveX object of the table
;;; pt  - the 3d WCS point
;;; Returns nil if none found, else returns a string such as A1 for column A and row 1
(defun TblHitTest (tbl pt / r c s n letters)
  (setq letters (vl-string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  (if (= :vlax-true (vla-HitTest tbl (vlax-3D-point pt) (vlax-3D-point (getvar 'ViewDir)) 'r 'c))
    (progn
      (setq s "")
      (while (> c -1)
        (if (= c 0)
          (setq s (strcat "A" s) c -1)
          (setq s (strcat (chr (nth (rem c (length letters)) letters)) s)
                c (fix (/ c (length letters)))
          )
        )
        (if (= c 0) (setq c -1))
      )
      (setq s (strcat s (itoa (1+ r))))
    )
  )
  s
)
Then you'd need a further getpoint to pick the "cell". Or you could try using the point returned by the entsel function (though usually that would be on one of the borders and may give strange results).
Code: [Select]
(setq pt (getpoint "\nPick cell: "))
Also your field-code doesn't look correct. Shouldn't it be something like this:
Code: [Select]
(strcat "%<\\AcExpr (Table(%<\\_ObjId " (itoa (vla-get-ObjectId obj_tbl) ">%)." (TblHitTest obj_tbl pt) ")>%
« Last Edit: October 19, 2011, 11:19:40 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get field ?
« Reply #15 on: October 19, 2011, 11:33:14 AM »
Using your Field expression example, I couldn't get this field to evaluate:

Code: [Select]
(defun c:test ( / col en in obj p1 row ss tables utl )

    (defun LM:Num2Alpha ( n )
        (if (< n 27)
            (chr (+ 64 n))
            (strcat (LM:Num2Alpha (/ (1- n) 26)) (LM:Num2Alpha (1+ (rem (1- n) 26))))
        )
    )
   
    (if
        (setq ss
            (ssget "_X"
                (list
                    (cons 0 "ACAD_TABLE")
                    (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                )
            )
        )
        (repeat (setq in (sslength ss))
            (setq tables (cons (vlax-ename->vla-object (ssname ss (setq in (1- in)))) tables))
        )
    )

    (if tables
        (if
            (and
                (setq p1 (getpoint "\nPick Cell: "))
                (vl-some
                    (function
                        (lambda ( table )
                            (if (eq :vlax-true
                                    (vla-hittest table (vlax-3D-point (trans p1 1 0))
                                        (vlax-3D-point (trans (getvar 'VIEWDIR) 1 0)) 'row 'col
                                    )
                                )
                                (setq obj table)
                            )
                        )
                    )
                    tables
                )
                (progn
                    (while
                        (progn (setvar 'ERRNO 0) (setq en (car (entsel "\nSelect Text: ")))
                            (cond
                                (   (= 7 (getvar 'ERRNO))
                                    (princ "\nMissed, try again.")
                                )
                                (   (eq 'ENAME (type en))
                                    (if (not (wcmatch (cdr (assoc 0 (entget en))) "TEXT,MTEXT"))
                                        (princ "\nInvalid Object.")
                                    )
                                )
                            )
                        )
                    )
                    en
                )
            )
            (vla-put-textstring (vlax-ename->vla-object en)
                (strcat "%<\\AcExpr (Table(%<\\_ObjId "
                    (if
                        (and
                            (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
                            (vlax-method-applicable-p
                                (setq utl
                                    (vla-get-utility
                                        (vla-get-activedocument (vlax-get-acad-object))
                                    )
                                )
                                'getobjectidstring
                            )
                        )
                        (vla-getobjectidstring utl obj :vlax-false)
                        (itoa (vla-get-objectid obj))
                    )
                    ">%)." (LM:Num2Alpha (1+ col)) (itoa (1+ row)) ")>%"
                )
            )
        )
        (princ "\nNo Tables Found.")
    )
    (princ)
)

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #16 on: October 19, 2011, 12:03:03 PM »
To my knowledge you cannot directly reference a field cell value using a Field in another object.
You "can" but it's not recommended ... nested fields require more than one regen (or other type of update) to propagate a change. So you could run into a scenario where the plotted value is inconsistent.

Though this is again pointing me in strange directions. I thought you wanted to place a field inside a text to duplicate a value from the table? If I'm still on the same track then perhaps this could help:
Code: [Select]
(vl-load-com)
[/quote]

I tried your way, but it is not return true, can post another code.?
I think text A = constan, text B field A , text C field B return value. Why text don't return table cell.!!!\
Thanks
« Last Edit: October 19, 2011, 12:10:48 PM by new_mem »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #17 on: October 20, 2011, 03:54:11 AM »
Using your Field expression example, I couldn't get this field to evaluate:
As I think it was mentioned previously, this type of field code pointing to a table cell only works if the contents of that cell is a number (seeing as the field code uses a formula).

Anyhow, this seems to work fine for me (had to fix some typos from my previous code):
Code: [Select]
(vl-load-com)

;;; Helper function to convert a number into a base 26 ABC string
(defun Decimal->Base26 (Num / letters str minus)
  (setq letters (vl-string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ") minus (minusp Num) Num (fix (abs Num)) str "")
  (while (> Num 0)
    (setq str (strcat (chr (nth (rem Num (length letters)) letters)) str)
          Num (fix (/ Num (length letters)))
    )
  )
  (if (eq str "") (setq str (chr (car letters))))
  (if minus
    (strcat "-" str)
    str
  )
)

;;; Helper function to get the cell reference of a picked point in a table
;;; tbl - the ActiveX object of the table
;;; pt  - the 3d WCS point
;;; Returns nil if none found, else returns a string such as A1 for column A and row 1
(defun TblHitTest (tbl pt / r c)
  (if (= :vlax-true (vla-HitTest tbl (vlax-3D-point pt) (vlax-3D-point (getvar 'ViewDir)) 'r 'c))
    (strcat (Decimal->Base26 c) (itoa (1+ r)))
  )
)

;;; Command to create a field linking from a table cell into a text
(defun c:TblField (/ tbl cell txt)
  (if (setq tbl (ssget ":S" '((0 . "ACAD_TABLE"))))
    (progn
      (setq tbl (vlax-ename->vla-object (ssname tbl 0))
            cell (TblHitTest tbl (getpoint "\nPick cell in table: "))
      )
      (while (not cell)
        (setq cell (TblHitTest tbl (getpoint "\nThat was not inside a cell in the table, try again: ")))
      )
      (if (setq txt (ssget ":S" '((0 . "TEXT,MTEXT"))))
        (progn
          (setq txt (vlax-ename->vla-object (ssname txt 0)))
          (vla-put-TextString txt (strcat "%<\\AcExpr (Table(%<\\_ObjId " (itoa (vla-get-ObjectId tbl)) ">%)." cell ")>%"))
        )
        (princ "That is not text.")
      )
    )
    (princ "That is not a table.")
  )
  (princ)
)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #18 on: October 20, 2011, 06:30:17 AM »
So if cell not a number, we can't  get value cell.!

uhm, okey,
thank u.

 :lol:

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get field ?
« Reply #19 on: October 20, 2011, 08:04:55 AM »
So if cell not a number, we can't  get value cell.!
You can, but not as an auto-updating field-code. Only as a normal piece of text which won't update if you change the table. E.g.:
Code: [Select]
;;; Helper function to get the cell reference of a picked point in a table
;;; tbl - the ActiveX object of the table
;;; pt  - the 3d WCS point
;;; Returns nil if none found, else returns a list containing (row . column)
(defun TblHitTest (tbl pt / r c)
  (if (= :vlax-true (vla-HitTest tbl (vlax-3D-point pt) (vlax-3D-point (getvar 'ViewDir)) 'r 'c))
    (cons r c)
  )
)

Code: [Select]
;;; Command to create a field linking from a table cell into a text
(defun c:TblField (/ tbl cell txt val)
  (if (setq tbl (ssget ":S" '((0 . "ACAD_TABLE"))))
    (progn
      (setq tbl  (vlax-ename->vla-object (ssname tbl 0))
            cell (TblHitTest tbl (getpoint "\nPick cell in table: "))
      )
      (while (not cell)
        (setq cell (TblHitTest tbl (getpoint "\nThat was not inside a cell in the table, try again: ")))
      )
      (if (setq txt (ssget ":S" '((0 . "TEXT,MTEXT"))))
        (progn
          (setq txt (vlax-ename->vla-object (ssname txt 0))
                val (vla-GetCellValue tbl (car cell) (cdr cell))
          )
          (vla-put-TextString
            txt
            (cond
              ((or (member (vlax-variant-type val) '(2 3 4 5))
                   (and (= (vlax-variant-type val)
                        (vl-every '(lambda (c) (member c '(48 49 50 51 52 53 54 55 56 57 46 43 45)))
                                  (vl-string->list (vlax-variant-value val))
                        )
                   )
               )
               (strcat "%<\\AcExpr (Table(%<\\_ObjId "
                       (itoa (vla-get-ObjectId tbl))
                       ">%)."
                       (strcat (Decimal->Base26 (cdr cell)) (itoa (1+ (car cell))))
                       ")>%"
               )
              )
              ((= (vlax-variant-type val) (vlax-variant-value val))
              (t (vla-get-TextString txt))
            )
          )
        )
        (princ "That is not text.")
      )
    )
    (princ "That is not a table.")
  )
  (princ)
)
« Last Edit: October 20, 2011, 08:08:13 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #20 on: October 20, 2011, 09:03:48 AM »
Hi irneb,

If table change then text not auto update, so i use ctrl+ c cell and ctrl+ v text easy. :lol:
Any way another ?

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #21 on: November 15, 2011, 03:19:57 PM »
@Lee Mac !

Hello LM.
I used your code and remove "content locked" to unlock and put field value cell in text. Then update table extractdata and field effect/! Can u tell me why >?  :ugly:

new_mem

  • Newt
  • Posts: 67
Re: Get field ?
« Reply #22 on: November 16, 2011, 04:48:24 AM »
Ah, sorry, don't need remove lock. That is Style text.
Attached my test.
:D

Thank to irneb and LMac.!