Author Topic: coordinate point list (*.txt file) and table  (Read 8677 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
coordinate point list (*.txt file) and table
« on: November 24, 2013, 04:00:21 PM »
I found at CadTutor forum , a Lee Mac lisp code. It is very useful but a have a little problem with the text size. I dont know a lot of things about lisp that's way  i ask for you if any one can add a commad from the text size.

thank you

Code: [Select]
;; Text File to Table  -  Lee Mac
;; Prompts the user to select a text file containing 4 columns of comma-delimited data
;; and generates an AutoCAD Table containing the file data at the point specified.

(defun c:txt2tab ( / *error* des ins lin lst txt )

    (defun *error* ( msg )
        (if (= 'file (type des)) (close des))
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
   
    (if (setq txt (getfiled "choose a file  P,X,Y,Z (*.txt)" "" "txt" 16))
        (if (setq des (open txt "r"))
            (progn
                (while (setq lin (read-line des))
                    (if (= 4 (length (setq lin (LM:str->lst lin ","))))
                        (setq lst (cons lin lst))
                    )
                )
                (setq des (close des))
                (if lst
                    (if (setq ins (getpoint "\nSpecify point for table: "))
                        (LM:addtable
                            (vlax-get-property (vla-get-activedocument (vlax-get-acad-object))
                                (if (= 1 (getvar 'cvport))
                                    'paperspace
                                    'modelspace
                                )
                            )
                            (trans ins 1 0)
                            nil
                            (cons '("A/A" "X" "Y" "Z") (reverse lst))
                            nil
                        )
                    )
                    (princ "\nNo valid data found in selected file.")
                )
            )
            (princ "\nUnable to open selected file for reading.")
        )
    )
    (princ)
)

;;---------------------=={ Add Table }==----------------------;;
;;                                                            ;;
;;  Creates an AutoCAD Table Object at the specified point,   ;;
;;  populated with the given data and optional title.         ;;
;;------------------------------------------------------------;;
;; Author:  Lee Mac, Copyright © 2013  -  www.lee-mac.com     ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  spc - VLA Block Object                                    ;;
;;  ins - WCS Insertion Point for Table                       ;;
;;  ttl - [Optional] Table title                              ;;
;;  lst - Matrix list of data to populate the table           ;;
;;  eqc - If T, columns are of equal width                    ;;
;;------------------------------------------------------------;;
;;  Returns:  VLA Table Object                                ;;
;;------------------------------------------------------------;;

(defun LM:AddTable ( spc ins ttl lst eqc / dif hgt i j obj stn sty wid )
    (setq sty
        (vlax-ename->vla-object
            (cdr
                (assoc -1
                    (dictsearch
                        (cdr
                            (assoc -1
                                (dictsearch (namedobjdict) "acad_tablestyle")
                            )
                        )
                        (getvar 'ctablestyle)
                    )
                )
            )
        )
    )
    (setq hgt (vla-gettextheight sty acdatarow))
    (if (LM:Annotative-p (setq stn (vla-gettextstyle sty acdatarow)))
        (setq hgt (/ hgt (getvar 'cannoscalevalue)))
    )
    (setq wid
        (mapcar
            (function
                (lambda ( col )
                    (apply 'max
                        (mapcar
                            (function
                                (lambda ( str )
                                    (   (lambda ( box ) (if box (+ (* 2.5 hgt) (- (caadr box) (caar box))) 0.0))
                                        (textbox
                                            (list
                                                (cons 01 str)
                                                (cons 40 hgt)
                                                (cons 07 stn)
                                            )
                                        )
                                    )
                                )
                            )
                            col
                        )
                    )
                )
            )
            (apply 'mapcar (cons 'list lst))
        )
    )
    (if
        (and ttl
            (< 0.0
                (setq dif
                    (/
                        (-
                            (   (lambda ( box ) (if box (+ (* 2.5 hgt) (- (caadr box) (caar box))) 0.0))
                                (textbox
                                    (list
                                        (cons 01 ttl)
                                        (cons 40 hgt)
                                        (cons 07 stn)
                                    )
                                )
                            )
                            (apply '+ wid)
                        )
                        (length wid)
                    )
                )
            )
        )
        (setq wid (mapcar '(lambda ( x ) (+ x dif)) wid))
    )
    (setq obj
        (vla-addtable spc
            (vlax-3D-point ins)
            (1+ (length lst))
            (length (car lst))
            (* 2.0 hgt)
            (if eqc
                (apply 'max wid)
                (/ (apply '+ wid) (float (length (car lst))))
            )
        )
    )
    (vla-put-regeneratetablesuppressed obj :vlax-true)
    (vla-put-stylename obj (getvar 'ctablestyle))
    (setq i -1)
    (if (null eqc)
        (foreach col wid
            (vla-setcolumnwidth obj (setq i (1+ i)) col)
        )
    )
    (if ttl
        (progn
            (vla-settext obj 0 0 ttl)
            (setq i 1)
        )
        (progn
            (vla-deleterows obj 0 1)
            (setq i 0)
        )
    )
    (foreach row lst
        (setq j 0)
        (foreach val row
            (vla-settext obj i j val)
            (setq j (1+ j))
        )
        (setq i (1+ i))
    )
    (vla-put-regeneratetablesuppressed obj :vlax-false)
    obj
)

;; Annotative-p
;; Returns T if the given Textstyle is annotative

(defun LM:annotative-p ( sty )
    (and (setq sty (tblobjname "style" sty))
         (setq sty (cadr (assoc -3 (entget sty '("AcadAnnotative")))))
         (= 1 (cdr (assoc 1070 (reverse sty))))
    )
)

;; String to List
;; Separates a string using a given delimiter
;; str - [str] string to process
;; del - [str] delimiter by which to separate the string

(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)
(vl-load-com) (princ)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;----------------------=={ Text File to Table }==----------------------;;
;;                                                                      ;;
;;  Prompts the user to select a text file and generates an AutoCAD     ;;
;;  Table at the point specified, representing the file data.           ;;
;;----------------------------------------------------------------------;;
;;    Author:  Lee Mac, Copyright © 2013  -  www.lee-mac.com            ;;
;;----------------------------------------------------------------------;;

(defun c:txt2tab2 ( / *error* del des hdl ins lin lst txt )

    (setq ttl nil ;; Table Title (nil for no title)
          hdl '("A/A" "X" "Y") ;; Table Headings
          del "," ;; Data Delimiter String
    )
   
    (defun *error* ( msg )
        (if (= 'file (type des)) (close des))
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
   
    (if (setq txt (getfiled "choose a file   P,X,Y (*.txt)" "" "txt" 16))
        (if (setq des (open txt "r"))
            (progn
                (while (setq lin (read-line des))
                    (setq lst (cons (mapcar '(lambda ( a b ) a) (LM:str->lst lin del) hdl) lst))
                )
                (setq des (close des))
                (if lst
                    (if (setq ins (getpoint "\nSpecify point for table: "))
                        (LM:addtable
                            (vlax-get-property (vla-get-activedocument (vlax-get-acad-object))
                                (if (= 1 (getvar 'cvport))
                                    'paperspace
                                    'modelspace
                                )
                            )
                            (trans ins 1 0) ttl (cons hdl (reverse lst)) nil
                        )
                    )
                    (princ "\nNo valid data found in selected file.")
                )
            )
            (princ "\nUnable to open selected file for reading.")
        )
    )
    (princ)
)

;;---------------------------=={ Add Table }==--------------------------;;
;;                                                                      ;;
;;  Creates an AutoCAD Table Object at the specified point, populated   ;;
;;  with the given data and optional title.                             ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright © 2013  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Arguments:                                                          ;;
;;  spc - VLA Block object                                              ;;
;;  ins - WCS Insertion point for table                                 ;;
;;  ttl - [Optional] Table title                                        ;;
;;  lst - Matrix list of data to populate the table                     ;;
;;  eqc - If T, columns are of equal width                              ;;
;;----------------------------------------------------------------------;;
;;  Returns:  VLA Table Object                                          ;;
;;----------------------------------------------------------------------;;

(defun LM:AddTable ( spc ins ttl lst eqc / dif hgt i j obj stn sty wid )
    (setq sty
        (vlax-ename->vla-object
            (cdr
                (assoc -1
                    (dictsearch
                        (cdr
                            (assoc -1
                                (dictsearch (namedobjdict) "acad_tablestyle")
                            )
                        )
                        (getvar 'ctablestyle)
                    )
                )
            )
        )
    )
    (setq hgt (vla-gettextheight sty acdatarow))
    (if (LM:Annotative-p (setq stn (vla-gettextstyle sty acdatarow)))
        (setq hgt (/ hgt (getvar 'cannoscalevalue)))
    )
    (setq wid
        (mapcar
            (function
                (lambda ( col )
                    (apply 'max
                        (mapcar
                            (function
                                (lambda ( str )
                                    (   (lambda ( box ) (if box (+ (* 2.5 hgt) (- (caadr box) (caar box))) 0.0))
                                        (textbox
                                            (list
                                                (cons 01 str)
                                                (cons 40 hgt)
                                                (cons 07 stn)
                                            )
                                        )
                                    )
                                )
                            )
                            col
                        )
                    )
                )
            )
            (apply 'mapcar (cons 'list lst))
        )
    )
    (if
        (and ttl
            (< 0.0
                (setq dif
                    (/
                        (-
                            (   (lambda ( box ) (if box (+ (* 2.5 hgt) (- (caadr box) (caar box))) 0.0))
                                (textbox
                                    (list
                                        (cons 01 ttl)
                                        (cons 40 hgt)
                                        (cons 07 stn)
                                    )
                                )
                            )
                            (apply '+ wid)
                        )
                        (length wid)
                    )
                )
            )
        )
        (setq wid (mapcar '(lambda ( x ) (+ x dif)) wid))
    )
    (setq obj
        (vla-addtable spc
            (vlax-3D-point ins)
            (1+ (length lst))
            (length (car lst))
            (* 2.0 hgt)
            (if eqc
                (apply 'max wid)
                (/ (apply '+ wid) (float (length (car lst))))
            )
        )
    )
    (vla-put-regeneratetablesuppressed obj :vlax-true)
    (vla-put-stylename obj (getvar 'ctablestyle))
    (setq i -1)
    (if (null eqc)
        (foreach col wid
            (vla-setcolumnwidth obj (setq i (1+ i)) col)
        )
    )
    (if ttl
        (progn
            (vla-settext obj 0 0 ttl)
            (setq i 1)
        )
        (progn
            (vla-deleterows obj 0 1)
            (setq i 0)
        )
    )
    (foreach row lst
        (setq j 0)
        (foreach val row
            (vla-settext obj i j val)
            (setq j (1+ j))
        )
        (setq i (1+ i))
    )
    (vla-put-regeneratetablesuppressed obj :vlax-false)
    obj
)

;; Returns T if the given Textstyle is annotative

(defun LM:annotative-p ( sty )
    (and (setq sty (tblobjname "style" sty))
         (setq sty (cadr (assoc -3 (entget sty '("AcadAnnotative")))))
         (= 1 (cdr (assoc 1070 (reverse sty))))
    )
)

;; String to List
;; Separates a string using a given delimiter
;; str - [str] string to process
;; del - [str] delimiter by which to separate the string

(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)

;;----------------------------------------------------------------------;;

(vl-load-com)
(princ
    (strcat
        "\n:: Text2Table.lsp | Version 1.1 | \\U+00A9 Lee Mac "
        (menucmd "m=$(edtime,0,yyyy)")
        "\n:: Type \"txt2tab\" to Invoke ::"
    )
)
(princ)

;;----------------------------------------------------------------------;;
;;                             End of File                              ;;
;;----------------------------------------------------------------------;;
« Last Edit: November 25, 2013, 05:43:49 AM by pedroantonio »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: coordinate point list (*.txt file) and table
« Reply #1 on: November 24, 2013, 05:56:57 PM »
The text height comes from the current Table Style. You need to update the table style.
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.

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #2 on: November 25, 2013, 02:37:30 AM »
I am trying to add this lines in the code
Code: [Select]
(command "_layer" "m" "table" "c" "7" "" )
(setq scl(/ (getreal  "\n Specify the scale (100,200,500,etc) : ") 100))
(setq hgt(* 0.25 scl))

but is not working

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: coordinate point list (*.txt file) and table
« Reply #3 on: November 25, 2013, 03:00:00 AM »
Lee's code is using the Annotation Scale factor. Perhaps you could simply change that when running this code.

Edit: Just a question though. Why go this route? You know you can open that TXT file in Excel (it's the same thing as a CVS file). Then you can select the columns & rows you want, copy to clipboard (Ctrl+C) and paste special into ACad (paste it as ACad Objects - which turns the spreadsheet into an acad table).

Unless I'm missing something, I don't see how it's that much more work.
« Last Edit: November 25, 2013, 03:05:23 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #4 on: November 25, 2013, 03:06:52 AM »
Can any one tranform this lisp to add my code in ? please  :embarrassed:

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #5 on: November 25, 2013, 04:10:01 AM »
any ideas ? :-o

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #6 on: November 25, 2013, 05:48:48 AM »
Quote
Edit: Just a question though. Why go this route? You know you can open that TXT file in Excel (it's the same thing as a CVS file). Then you can select the columns & rows you want, copy to clipboard (Ctrl+C) and paste special into ACad (paste it as ACad Objects - which turns the spreadsheet into an acad table).

yes i know but with this lisp is faster.I tryed and the Table Style setting and Annotation Scale factor but the result is not i want ...please someone help :embarrassed:

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #7 on: November 25, 2013, 09:53:57 AM »
i am tryind to add this lines

Code: [Select]
(command "_layer" "m" "table" "c" "7" "" )
(setq scl(/ (getreal  "\n Specify the scale (100,200,500,etc) : ") 100))
(setq hgt(* 0.25 scl))

somebody helps me ..... please :cry:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: coordinate point list (*.txt file) and table
« Reply #8 on: November 25, 2013, 07:11:59 PM »

Since you found this at CadTutor ; have you asked in the thread that you acquired the code from ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
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.

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #10 on: November 26, 2013, 02:41:14 AM »
Quote
Since you found this at CadTutor ; have you asked in the thread that you acquired the code from ?

First of all same people answer in the posts in the both of this sites. And second i don't think that it's the problem. I think that if somebody really know how to fix this lisp will help .... I said

Quote
I found at CadTutor forum , a Lee Mac lisp code. It is very useful but a have a little problem with the text size. I dont know a lot of things about lisp that's way  i ask for you if any one can add a commad from the text size.

So if somebody really knows please help me... If you don't , not hurt feelings

Thanks ....

ymg

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #11 on: November 26, 2013, 05:39:11 AM »
pedroantonio,


Shouting in the forum won't help you.

Cab's answer is perfectly valid simply modify the tablestyle.

This being said, if you are deadset about doing it in the program, why don't you creates your layer before the table is created and simply issue a scale command after creation.

ymg
« Last Edit: November 26, 2013, 05:49:24 AM by ymg »

pedroantonio

  • Guest
Re: coordinate point list (*.txt file) and table
« Reply #12 on: November 26, 2013, 10:48:33 AM »
Sorry about Shouting bur i explain that I dont know a lot of things about lisp that's way  i ask for you if any one can add a commad from the text size. I don't know how to change this code. If you can help me...

thanks

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: coordinate point list (*.txt file) and table
« Reply #13 on: November 26, 2013, 11:39:55 AM »
Have you tried this?
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.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: coordinate point list (*.txt file) and table
« Reply #14 on: November 26, 2013, 12:23:22 PM »
Sorry about Shouting bur i explain that I dont know a lot of things about lisp that's way  i ask for you if any one can add a commad from the text size. I don't know how to change this code. If you can help me...

thanks

Thats one of the points of this community.  We won't necessarily do it for you, but we can hold your hand (and give the occaisonal kick in the caboose) to help you learn so you can do it yourself.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}