Attributes written into file name when creating a WBLOCK

ingo360
Contributor

Attributes written into file name when creating a WBLOCK

ingo360
Contributor
Contributor

Hi,

 

I would like to have attributes automatically written into the file name when creating a WBLOCK.

Currently the plan is saved with placeholders:

 

(setq path "...\\000000_XX_0000_Cityname_Planname_Index-.dwg")

 

I want the following things to be inserted automatically. The attributes are all stored in a single dynamic block.

 

  • Current date --> YYMMDD
  • 2-digit country code --> attribute
  • 4-digit number --> attribute
  • city name --> attribute
  • plan name --> attribute
  • The index must be entered manually.

This is how the file name should be written.

(setq path "...\YYMMDD_attribute_attribute_attribute_attribute_Index-.dwg")


Greetings and thanks in advance for your help.

0 Likes
Reply
405 Views
6 Replies
Replies (6)

Kent1Cooper
Consultant
Consultant

@ingo360 wrote:

.... I would like to have attributes automatically written into the file name when creating a WBLOCK. ....


If 'blk' is a variable holding the Block's entity name [however you establish that], and if those are the real Attribute tags [with spaces replaced by hyphens here, because tags cannot include spaces], probably something like:

(setq
  Index (getstring "\n Enter Index: ")
  path
    (strcat
      ".../
      (substr (itoa (fix (getvar 'cdate))) 3) "_"
      (getpropertyvalue blk "2-digit-country-code") "_"
      (getpropertyvalue blk "4-digit-number") "_"
      (getpropertyvalue blk "city-name") "_"
      (getpropertyvalue blk "plan") "_"
      Index "-.dwg"
    ); strcat & path
); setq

Note the forward slash, which could be a double back-slash if you prefer.  File path strings in AutoLisp can't use a single back-slash separator, because that is a trigger for special codes, such as the "\n" in the prompt.

You don't say what the nature of the "Index" would be.  The above allows non-numerical text if that's a possibility.  If the Index would always be an integer, you can use (getint) in setting it, and (itoa Index) in use in the path string.  That would prevent a User from entering text content that does not represent an integer.  Or if always a number, but with decimal values allowed, (getreal) and (rtos Index).

 

Kent Cooper, AIA
0 Likes

ingo360
Contributor
Contributor

Thank you for your feedback.


Unfortunately, I do not understand it yet. In the attachment you will find a file that you can use to explain to me how I have to proceed. The attributes are now part of a normal block.

The index at the end of the file name is no longer needed.


Current date --> YYMMDD
STORE-NUMBER --> attribute
CITY --> attribute
PLANNAME --> attribute

 

The file name should look like this

"YYMMDD_STORE-NUMBER_CITY_PLANNAME.dwg"


 

0 Likes

EnM4st3r
Advocate
Advocate

i dont really understand whats your goal. Like you want that everytime you create that block its renaming your file?


This lisp here creates a copy with the new filename after you select the Block because afaik you cant rename opened files. One possible solution would be to delete the old file with the lisp after it saved the new copy.

 

 

(defun c:Entttest (/ ent path)
  (setq ent (ssname (ssget "_:S+." '((0 . "INSERT")))0))
  (setq path (getpath ent))
  (command "_saveas" "2018" path)
  (princ)
)

(defun getpath (blk / filename path) 
  (setq filename 
    (strcat 
      (substr (itoa (fix (getvar 'cdate))) 3)
      "_"
      (getpropertyvalue blk "number")
      "_"
      (getpropertyvalue blk "city")
      "_"
      (getpropertyvalue blk "planname")
    ) ;strcat
  ) ;setq
  (if (findfile (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME))) 
    (setq path (strcat (getvar 'DWGPREFIX) filename ".dwg"))
    (progn
      (princ)
      (alert "\nSave current drawing first.")
      (exit)
    )
  )
)

 

 

0 Likes

ingo360
Contributor
Contributor

This is a step in the right direction.

 

I want to save a WBLOCK from a complex drawing. For this I currently use the following code.

(defun c:wbtest ( / AWS N PATH VLAAWS)

(setq path "...\\000000_Store-Number_City_Planname.dwg")

  (if (and (princ "\nObjects:")
          (setq aws (ssget "_W" (list 0 0 0)(list 90900 61000 0)))
          (setq p (trans '(0 0) 1 0))
          )
    (progn
      (setq InsbaseOld (getvar "INSBASE"))
      (setvar "INSBASE" p)
      (setq vlaAws(vla-add
                    (vla-get-selectionsets
                      (vla-get-activedocument (vlax-get-acad-object)))
                    "testWblock"))
      (setq n 0)
      (repeat (sslength aws)
        (vlax-invoke vlaAws 'AddItems
          (list (vlax-ename->vla-object (ssname aws n))))
        (setq n (+ n 1))
        )
      (vlax-invoke
        (vla-get-activedocument (vlax-get-acad-object))
        'Wblock path vlaAws)
     
      (vla-delete vlaAws)
      (vlax-release-object vlaAws)
      (setvar "INSBASE" InsbaseOld)
      )
    )

  (princ)
  )

 

When saving the WBLOCK, the 3 attributes from the "test block" should be written into the file name.

The whole thing should be fully automated, without an input window.

 

Your code works so far, I just don't know how to implement it in my code.

0 Likes

MrJSmith
Advocate
Advocate

@Kent1Cooper wrote: If 'blk' is a variable holding the Block's entity name [however you establish that], and if those are the real Attribute tags [with spaces replaced by hyphens here, because tags cannot include spaces]....

Ken, they can have spaces if you create the attribute via entmake rather than AutoCADs normal user creation means. They even appear to work fine with spaces but AutoCAD will yell at you if you attempt to change their name at a later date using normal means.

0 Likes

MrJSmith
Advocate
Advocate

@ingo360See if this works, assuming the work @EnM4st3r provided is correct.

 

(defun c:exportBlocks (/ outputPath ss filename blk i)
	(setq outputPath (strcat (getvar "TempPrefix") "Export Dwgs\\")) ;Need to specify where you want the exports to go. For now just put them in temp files
	(setq ss (ssget "_A" '((2 . "testWblock")))) ;Select all "testWblocks" in drawing)
	(setq i -1)
	(while (setq  blk (ssname ss (setq i (1+ i))))
		(setq filename 
			(strcat 
				(substr (itoa (fix (getvar 'cdate))) 3)
				"_"
				(getpropertyvalue blk "number")
				"_"
				(getpropertyvalue blk "city")
				"_"
				(getpropertyvalue blk "planname")
			) ;strcat
		)
		(command "_.-WBLOCK" (strcat outputPath filename) blk)
	)
)
0 Likes