Jump to content

Recommended Posts

Posted

Hi I write this code to export data from attribute blocks to txt

the block have this tags DIAT, KM, EO, EE

 

(defun c:foo ( / *error* del des ent idx lst obj ord out sel )
  (defun *error* (msg)
    (if (= 'file (type des))
        (close des))
    (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
        (princ (strcat "\nError: " msg)))
    (princ))

  (setq ord '("DIAT" "KM" "EO" "EE")
        del  ",")

  ;; Prompt the user to choose the file path for exporting
  (if (setq out (getfiled "Choose where to save the file" (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))) "txt" 1))
      (if (setq sel (ssget '((0 . "INSERT") (66 . 1))))
          (if (setq des (open out "w"))
              (progn
                (repeat (setq idx (sslength sel))
                  (setq ent (ssname sel (setq idx (1- idx)))
                        obj (vlax-ename->vla-object ent))
                  (setq lst
                        (mapcar '(lambda (x) (cons (strcase (vla-get-tagstring x)) (vla-get-textstring x)))
                                (vlax-invoke obj 'getattributes)))
                  (if (setq lst (vl-remove 'nil (mapcar '(lambda (x) (cdr (assoc x lst))) ord)))
                      (write-line (LM:lst->str lst del) des)))
                (setq des (close des)))
              (princ (strcat "\nUnable to open file: \"" out "\" for writing."))))
      (princ "No file path provided."))
  (princ))


(defun LM:lst->str (lst del)
  (if (cdr lst)
      (strcat (car lst) del (LM:lst->str (cdr lst) del))
      (car lst)))


(defun LM:uniquefilename (pth ext / fnm tmp)
  (if (findfile (setq fnm (strcat pth ext)))
      (progn
        (setq tmp 1)
        (while (findfile (setq fnm (strcat pth "(" (itoa (setq tmp (1+ tmp))) ")" ext))))))
  fnm)

(vl-load-com) (princ)

 

The code export the data from the attribiutes like

 

k1,135.25,4.25,2.54
y2,0.00,1.05,2.00
s2,20.00,1.25,2.35

 

I want to change the order of the export lines from smaller KM to bigger , like

 

y2,0.00,1.05,2.00
s2,20.00,1.25,2.35
k1,135.25,4.25,2.54

 

Can any one help?

 

Thanks

 

 

 

Posted (edited)

nada testing...

 

(defun c:foo ( / *error* ord fn del des ent idx lst obj out sel )
  (defun *error* (msg) (if (= 'file (type des)) (close des))
    (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))(princ (strcat "\nError: " msg)))(princ))
  (setq ord '("DIAT" "KM" "EO" "EE") del ",")
  (setq fn (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))))
  (cond
    ((not (setq out (getfiled "Choose where to save the file" fn "txt" 1)))
     (princ "\nNo file path provided."))
    ((not (setq sel (ssget '((0 . "INSERT") (66 . 1)))))
     (princ "\nNo blocks with attributes were found"))
    ((not (setq des (open out "w")))
     (princ (strcat "\nUnable to open file: \"" out "\" for writing.")))
    (t
     (repeat (setq idx (sslength sel))
       (setq ent (ssname sel (setq idx (1- idx))) obj (vlax-ename->vla-object ent))
       (setq lst
              (mapcar
                '(lambda (x) (cons (strcase (vla-get-tagstring x)) (vla-get-textstring x)))
                 (vlax-invoke obj 'getattributes)
              )
       )
     )
     (setq lst (vl-remove 'nil (mapcar '(lambda (x) (cdr (assoc x lst))) ord)))
     ;;; (setq lst '(("k1" "135.25" "4.25" "2.54") ("y2" "0.00" "1.05" "2.00") ("s2" "20.00" "1.25" "2.35")))
     (setq lst (vl-sort lst (function (lambda (a b) (< (atof (cadr a)) (atof (cadr b)))))))
     ;;; (("y2" "0.00" "1.05" "2.00") ("s2" "20.00" "1.25" "2.35") ("k1" "135.25" "4.25" "2.54"))
     (if (not (vl-consp lst))
       (princ "\nNo items to write")
       (foreach item lst (write-line (LM:lst->str item del) des))
     )
    )
  )
  (if (= 'file (type des)) (close des))(gc)
  (princ)
)

 

🐉

Edited by rlx
  • Like 1

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