Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Export/Import attribute values and its color.

7 REPLIES 7
Reply
Message 1 of 8
kameron1967
2027 Views, 7 Replies

Export/Import attribute values and its color.

Hi guys.  I wonder if you can help me.  There is a BLOCKA that I'd like to extract its attributes - Tag and Color - into an external format (excel).  There after, I'd like to import it into the drawing and based on its color attribute value, change the value to that color in the drawing.

 

Any hints/suggestions would be appreciated.

7 REPLIES 7
Message 2 of 8
Hallex
in reply to: kameron1967

I think easier yet to read / write data to CSV file

You may want to use something like that

;;________________________ Write to file ___________________________;;

;;write attributes to CSV file
(defun c:bf(/ file lst)
  ;;list to file
(defun wrs (file lst  / filename atts blockname en obj ss)
         (setq filename (open file "W"))
         (foreach x lst 
           (write-line (lst2str x "\t") filename)
         )
         (close filename)
(princ)
)
  (defun lst2str (lst sep)
    (if (cadr lst)
      (strcat (vl-princ-to-string (car lst))
        sep
        (lst2str (cdr lst) sep)
      )
      (vl-princ-to-string (car lst))
    )
  )
  (setq file "C:\\Test\\attributes.csv");<-- change file name
  
  (setq blockname "BLOCKA");<-- change block name
  
  (setq lst nil)
  (if (setq ss (ssget "X" (list (cons 2 (strcat "`*U*," blockname))(cons 66 1))))

 (progn
  (while (setq en (ssname ss 0))
    (progn
      (setq obj (vlax-ename->vla-object en))
    (if (eq blockname (vla-get-effectivename obj))
      (progn
   (setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes obj))))
    (foreach att atts
      (setq lst (cons (list (vla-get-tagstring att) (itoa (vla-get-colorindex (vla-get-truecolor att)))) lst))
    )
   )
      )
    )
    (ssdel en ss)
    )
  )
    )
  (wrs file lst)
  (princ)
  )

;;______________________ Read from file _______________________;;

;;Read data from CSV file
(defun C:af(/ data file pos result x)
(defun file-to-list (filename / file result line)
  (if (findfile filename)
    (progn (setq file (open filename "r"))
	   (while (setq line (read-line file))
	     (setq result (cons line result))
	   )
	   (close file)
	   (reverse result)
    )
    (alert (strcat "File " filename " not found!"))
  )
)
  (defun split  (txt sep)
  (if (not (eq sep txt))
    (if	(setq pos (vl-string-search sep txt 0))
      (cons (substr txt 1 pos)
	    (split (substr txt (+ pos 1 (strlen sep))) sep))
      (list txt)) )
  )
  
  (setq file "C:\\Test\\attributes.csv")
  (setq data (file-to-list file))
  (setq result (mapcar '(lambda(x)(split x "\t")) data))
  (print result)
  (alert "Do whatever you need with attribute info here")
  (princ)
  )

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 8
kameron1967
in reply to: Hallex

Thank you very much, Hallex!  I'm sorry about the delay response, but I was out all day yesterday.  I tested the routine.  It exported fine into csv format.  However, when I opened it up using excel, it give me a weird ? in between the tag info and the color info.  When I opened it using notepad, it shows up fine.

 

This is what it gives me when I ran the routine.  I changed the number/color 7 to 5 to see if it will update the attribute property so that it's now blue, instead of white, but how do I incorporate this code?  Thanks.

 

(("TAG" "7"))

Message 4 of 8
pbejse
in reply to: kameron1967


@kameron1967 wrote:

....when I opened it up using excel, it give me a weird ? in between the tag info and the color info.  When I opened it using notepad, it shows up fine...

 


Use comma "," instead of  tab "\t"

Message 5 of 8
pbejse
in reply to: kameron1967


@kameron1967 wrote:

 

This is what it gives me when I ran the routine.  I changed the number/color 7 to 5 to see if it will update the attribute property so that it's now blue, instead of white, but how do I incorporate this code?  Thanks.

 

(("TAG" "7"))


 

 

(defun subkameron  (lst bn / a b)
      (vlax-for ent  (vla-item (vla-get-blocks (vla-get-ActiveDocument
                  (vlax-get-acad-object)))  bn)
            (if (and (eq (vla-get-ObjectName ent)
                         "AcDbAttributeDefinition")
                     (setq b (assoc (vla-get-TagString ent) lst)))
                  (vla-put-color ent (atoi (cadr b)))))

      (command "_attsync" "Name" bn)
      )

 (subkameron  result  "BLOCKA")

 

HTH

Message 6 of 8
kameron1967
in reply to: pbejse

Thanks, Pbejse!  Wow - I feel so honored to have the routine named after me! 🙂

 

I had a chance to incorporate your portion of the routine and put the last line at the end of Hallex's routine.  It ran successfully!  I had to update the way it writes to the csv file "," and the way it reads per your suggestion.  I initially only modified the read portion, but after changing the write portion, it worked great!  Thank you for your finishing touch, Pbejse - and thank you Hallex for the awesome routine!  Kudos to you both!! 🙂

Message 7 of 8
Hallex
in reply to: kameron1967

Glad to help

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 8
pbejse
in reply to: kameron1967


@kameron1967 wrote:

Thanks, Pbejse!  Wow - I feel so honored to have the routine named after me! 🙂

 


You like that huh?   Smiley Happy

 


@kameron1967 wrote:

 

  Thank you for your finishing touch, Pbejse - and thank you Hallex for the awesome routine!  Kudos to you both!! 🙂


You are wellcome

Good for you kameron.. It's all Hailex really...

 

Cheers

 

 

 


 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost