Author Topic: Get field ?  (Read 11009 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12916
  • 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.!