;Rename file with addition of "Xref-" in a begining of the file and current date in the end of file ..by Igal Averbuh 2015

(defun c:tpz (/ ss1 count emax en ed blkn found
thedate thetime plotby)
;define function and declare variables as local

(setvar "HIGHLIGHT" 0)
;switch off highlight

(setvar "CMDECHO" 0)
;switch off command echo

(setq ss1 (ssget "X" '((0 . "INSERT")(66 . 1))))
;filter for all blocks with attributes

(if ss1
;if any are found

;do the following

(setq count 0
;set the counter to zero

emax (sslength ss1)
;get the number of blocks


(while (< count emax)
;while the counter is less than the
;number of blocks

(setq en (ssname ss1 count)
;get the entity name

ed (entget en)
;get the entity list

blkn (dxf 2 ed)
;get the block name


(if (= "STAMP")
;if the block name is "STAMP"

(setq count emax
;stop the loop

found T
;set the flag


(setq count (1+ count))
;if not increment the counter

);end if

);while & if

(if found
;if the flag is set

;erase the block




(setvar "ATTDIA" 0)
;switch off dialogue boxes

(setq thedate (today))
;calculate and format date

(setq thetime (time))
;calculate and format time

(setq plotby (getvar "LOGINNAME"))
;get the users name

(setq file (strcat (getvar 'dwgprefix) "XREF-" (vl-filename-base (getvar 'dwgname)) "-TPZ-" thedate ))
(if (findfile file)
(command "_.-wblock" file "Y" "*")
(command "_.-wblock" file "*")

(setq filename file)
(setq vgao (vlax-get-acad-object))
(setq vgad (vla-get-activedocument vgao))
(setq vgd (vla-get-documents vgao))
(= 0 (getvar "SDI"))
(vla-activate (vla-open vgd filename)) ; if sdi = 0
(vla-sendcommand vgad (strcat "(command \"_open\")\n" filename "\n")); if sdi = 1

;(layerstate-save (strcat "LS " thedate " " thetime " by " plotby) nil nil)

;(command "ucs" "na" "s" (strcat "UCS " thedate " " thetime " by " plotby))

;(command "-view" "s" (strcat "VS " thedate " " thetime " by " plotby))

;(setq by (strcat "Updated by " plotby " " thedate " " thetime ))

;(setq by1 (strcat "NOPLOT - " " " thedate " " thetime ))

;(command "-layer" "filter" "N" "P" "" "PLOTTABLE==\"False\"" by1 "x" "")

;(command "-layer" "filter" "s" "All" "x" "")

;(command "-layer" "n" by "")

;insert the block and fill in the attribute data

(setvar "ATTDIA" 1)
;switch the dialogues back on

(setvar "HIGHLIGHT" 1)
;switch Highlight On

(setvar "CMDECHO" 1)
;switch Cmdecho On



(defun dxf(code elist)

(cdr (assoc code elist))
;finds the association pair, strips 1st element

(defun TODAY ( / d yr mo day)
(setq d (rtos (getvar "CDATE") 2 6)
yr (substr d 3 2)
mo (substr d 5 2)
day (substr d 7 2)
(strcat day "-" mo "-" yr)
(defun TIME ( / d hr m s)
(setq d (rtos (getvar "CDATE") 2 6)
hr (substr d 10 2)
m (substr d 12 2)
s (substr d 14 2)
(strcat hr "-" m "-" s)



;(strcat "_open \"" filename "\"")