TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hudster on November 12, 2014, 09:36:20 AM

Title: Extract Attribute Values and add to file name, and Save a copy?
Post by: hudster on November 12, 2014, 09:36:20 AM
Does any one know of a way I can extract three attribute values from a block we use as our title block, Drawing-Title-Line1, Drawing-Title-Line2 and Drawing-Title-Line3 and have AutoCAD append it to the drawing filename and save a copy to a Folder Called "Issued" in the same directory as the drawing?

I have a client who want this added to every file, every time we issue drawings, as we have over 500 drawings, It will be a pain in the butt.
I also can't rename our files to what he wants, as our document control restricts file characters to set parameters, and the file names would push our file paths beyone the allowable character file limit is we add the titles.

There is no way I fancy having to type this in every time we issue, but he's not budging on this one.

**edit**
Oh, and I need to add a space after Title Line 1 and Title line 2, so the file name remains readable.
Title: Re: Extract Attribute Values and add to file name, and Save a copy?
Post by: ronjonp on November 12, 2014, 11:00:30 AM
Here's something to get you started.  :)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ _getattributevalue b f nf t1 t2 t3)
  2.   (defun _getattributevalue (block tag / att out)
  3.     (foreach att (vlax-invoke block 'getattributes)
  4.       (if (eq (strcase tag) (strcase (vla-get-tagstring att)))
  5.         (setq out (vla-get-textstring att))
  6.       )
  7.     )
  8.     out
  9.   )
  10.   (if (and (setq b (ssget "_x" (list (cons 2 "nameofyourtitleblock") '(66 . 1))))
  11.            (setq b (vlax-ename->vla-object (ssname b 0)))
  12.            (setq t1 (_getattributevalue b "Drawing-Title-Line1"))
  13.            (setq t2 (_getattributevalue b "Drawing-Title-Line2"))
  14.            (setq t3 (_getattributevalue b "Drawing-Title-Line3"))
  15.       )
  16.     (progn (setq f (strcat (getvar 'dwgprefix) "Issued\\"))
  17.            (setq nf (strcat f (vl-filename-base (getvar 'dwgname)) "_" t1 "_" t2 "_" t3 ".dwg"))
  18.            (vl-mkdir f)
  19.            (and (findfile nf) (vl-file-delete nf))
  20.            (vl-file-copy (strcat (getvar 'dwgprefix) (getvar 'dwgname)) nf)
  21.     )
  22.   )
  23.   (princ)
  24. )
Title: Re: Extract Attribute Values and add to file name, and Save a copy?
Post by: Tharwat on November 12, 2014, 11:21:09 PM
@ ronjonp , I guess there won't be any evaluation after the quote function that you used for the block name .  ;-)
Title: Re: Extract Attribute Values and add to file name, and Save a copy?
Post by: ronjonp on November 13, 2014, 12:58:42 AM
@ ronjonp , I guess there won't be any evaluation after the quote function that you used for the block name .  ;)

Ah yes the ' over list ... I'll fix in the morning. Should not post code I don't test  :oops:


*FIXED
Title: Re: Extract Attribute Values and add to file name, and Save a copy?
Post by: hudster on November 13, 2014, 11:24:14 AM
Thanks, That works perfectly.

I won't tell the client just yet, I'll let him think I had to do it manually  ;-)
Title: Re: Extract Attribute Values and add to file name, and Save a copy?
Post by: ronjonp on November 13, 2014, 11:37:58 AM
Thanks, That works perfectly.

I won't tell the client just yet, I'll let him think I had to do it manually  ;)


Glad to help out.  8)  What is this word you speak of ?? maannnooooleeee?  ;D