Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
kameron1967
Posts: 186
Registered: ‎09-05-2008
Message 1 of 8 (534 Views)

Export/Import attribute values and its color.

534 Views, 7 Replies
04-24-2012 08:35 AM

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 8 (528 Views)

Re: Export/Import attribute values and its color.

04-24-2012 11:20 AM 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
Distinguished Contributor
kameron1967
Posts: 186
Registered: ‎09-05-2008
Message 3 of 8 (500 Views)

Re: Export/Import attribute values and its color.

04-25-2012 01:24 PM 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"))

*Expert Elite*
pbejse
Posts: 2,502
Registered: ‎11-24-2009
Message 4 of 8 (490 Views)

Re: Export/Import attribute values and its color.

04-26-2012 01:17 AM 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"

*Expert Elite*
pbejse
Posts: 2,502
Registered: ‎11-24-2009
Message 5 of 8 (482 Views)

Re: Export/Import attribute values and its color.

04-26-2012 02:05 AM 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

Distinguished Contributor
kameron1967
Posts: 186
Registered: ‎09-05-2008
Message 6 of 8 (469 Views)

Re: Export/Import attribute values and its color.

04-26-2012 08:42 AM in reply to: pbejse

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

 

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!! :smileyhappy:

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 8 (465 Views)

Re: Export/Import attribute values and its color.

04-26-2012 09:09 AM in reply to: kameron1967

Glad to help

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
pbejse
Posts: 2,502
Registered: ‎11-24-2009
Message 8 of 8 (462 Views)

Re: Export/Import attribute values and its color.

04-26-2012 09:27 AM in reply to: kameron1967

kameron1967 wrote:

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

 


You like that huh?   :smileyhappy:

 


kameron1967 wrote:

 

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


You are wellcome

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

 

Cheers

 

 

 


 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!