Jump to content

Recommended Posts

Posted

Hi . I use this code to insert table from txt with coordinates in the drawing. I want to ask if is any way to round up the coordinates to 2 or 3 decimal places  before insert because the txt file some times have the coordinates to 8 decimal places like the example

 

 

1,311159.96991560,4244183.67645809,4.631
2,311162.47527926,4244177.41308898,5.156
3,311165.60772520,4244171.78492820,5.751
4,311168.98863420,4244168.37002316,6.080
5,311173.45298218,4244165.91270090,6.768
6,311177.82803504,4244162.50450169,7.273
7,311169.80214645,4244164.70754902,6.584
8,311167.89884639,4244165.03704983,6.369
9,311166.02291493,4244164.79504988,6.281
10,311162.84830354,4244162.21893261,6.324
11,311159.10740377,4244157.54691043,6.367
12,311155.00796711,4244160.54083855,5.877

 

 

 

The code I use is

 

;; 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:foo ( / *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 "Επιλέξτε το αρχείο  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  -  [url]www.lee-mac.com[/url]     ;;
;;------------------------------------------------------------;;
;;  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  -  [url]www.lee-mac.com[/url]            ;;
;;----------------------------------------------------------------------;;

(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 "Επιλέξτε το αρχείο  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  -  [url]www.lee-mac.com[/url]              ;;
;;----------------------------------------------------------------------;;
;;  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)

 

Posted

Look to Lee Macs rounding functions?

Posted (edited)

(rtos num 2 3) is what your after, look at (vla-settext obj i j val), you may have to do each column separately as 1st column is 1, 2, 3 etc (rtos num 2  0)

Edited by BIGAL

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