(progn
; Setvar version 2.0
; This program was created by GSA for use by contractors.
; It sets variables and performs drawing cleanup in
; accordance with GSA CAD policy and saves the drawing.
; Runs in AutoCAD r2002 and later
;
;
; audit the drawing for internal errors
(command "audit" "yes")
;
;
; purge unreferenced entities
(if (wcmatch (strcase (getvar "dwgname")) "O*")
  (progn) ; do not purge assignment drawings
  (progn
    (command "-purge"
             "All" ; Enter type of unused objects to purge
             "*"   ; Enter name(s) to purge <*>:
             "No"  ; Verify each name to be purged? [Yes/No] <Y>:
    )
  ) ;end progn
) ;end if
;
;
; remove xref paths
; select the first block
(setq blk (tblnext "BLOCK" 1))
; repeat for each block definition in the drawing
(while (/= blk nil)
  ; determine if the block is an xref
  (setq groupcode70 (cdr (assoc 70 blk)))
  ; determine if groupcode70 contains a bitwise 4
  ; this would indicate it is an xref
  (if (= 4 (logand 4 groupcode70))
    (progn
      ; if groupcode70 contains a bitwise 32 it is resolved
      (if (= 32 (logand 32 groupcode70))
        (progn
          ; it is resolved
          ; strip out path if present
          (setq xrefname (cdr (assoc 1 blk)))
          (if (wcmatch xrefname "*\\*")
            ; change the old name to the new
            (command "-xref"
                     "p"
                     (cdr (assoc 2 blk))
                     (strcat
                       (vl-filename-base xrefname)
                       (vl-filename-extension xrefname)
                     )
            ) ;end command
          ) ;end if
        ) ;end progn
        (progn) ; xref is unresolved - do not try to change it
      )
    )
    (progn) ; the block is not an xref
  )
  ; select the next block
  (setq blk (tblnext "BLOCK"))
)
;
; set TILEMODE according to the file name, if compliant
(setq discipline
        '("G"   "H"   "C"   "L"   "S"   "A"   "I"   "Q"   "F"   "P"
          "M"   "E"   "T"   "R"   "X"   "Z"   "V"   "W"   "B"   "D"
          "O"   "Y"
         )
)
(setq sheettype '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
(setq modeltype
        '("FP"   "SP"   "DP"   "QP"   "XP"   "RO"   "EL"
          "SC"   "DT"   "SH"   "3D"   "DG"   "EP"   "CP"
          "RP"   "NP"   "KY"   "SR"   "HP"   "PP"   "KP"
          "VP"   "LP"   "GP"   "TP"   "UP"   "SV"   "AP"
         )
)
;
(setq justname (strcase (vl-filename-base (getvar "dwgname"))))
;
(setq sheetormodel nil)
(if (and
      (member (substr justname 1 1) discipline)
      (member (substr justname 3 1) sheettype)
    ) ;end and
  (setq sheetormodel "SHEET")
)
(if (and
      (member (substr justname 1 1) discipline)
      (member (substr justname 3 2) modeltype)
    ) ;end and
  (setq sheetormodel "MODEL")
)
;
; set TILEMODE to 1 sheetormodel is MODEL
(if (= sheetormodel "MODEL")
  (setvar "tilemode" 1)
)
;
; set TILEMODE to 0 sheetormodel is SHEET
(if (= sheetormodel "SHEET")
  (setvar "tilemode" 0)
)
;
; if TILEMODE is 0, switch to paper space before zooming extents
; this prevents inadvertent rescaling of a viewport
(if (= (getvar "TILEMODE") 0)
  (command "PSPACE")
)
;
; zoom extents
(command "zoom" "extents")
;
;
; check version of AutoCAD for setting of limits
; does not reset limits of paperspace in 2000 or later
(if (> (atoi (getvar "acadver")) 14)
  (progn
    ; AutoCAD 2000 and later
    (if (= (getvar "tilemode") 0)
      (progn) ; do not reset limits in paper space
      (progn
        ; reset limits in model space
        (setvar "limmin" (list (car (getvar "extmin")) (cadr (getvar "extmin"))))
        (setvar "limmax" (list (car (getvar "extmax")) (cadr (getvar "extmax"))))
      )
    )
  )
  (progn
    ; AutoCAD 14 and earlier
    (setvar "limmin" (list (car (getvar "extmin")) (cadr (getvar "extmin"))))
    (setvar "limmax" (list (car (getvar "extmax")) (cadr (getvar "extmax"))))
  )
)
;
; set drawing variables
(setvar "ucsicon" 1)
(command "ucs" "world")
(setvar "insbase" (list 0 0 0))
(setvar "gridmode" 0)
(setvar "isavepercent" 0)
(setvar "clayer" "0")
(setvar "limcheck" 0)
(setvar "cecolor" "bylayer")
(setvar "celtype" "bylayer")
(if (> (atoi (getvar "acadver")) 14)
  (setvar "celweight" -1)
)
(setvar "pdmode" 0)
(setvar "pdsize" 0)
(setvar "qtextmode" 0)
(setvar "snapmode" 0)
(setvar "visretain" 1)
;
; save
(command "qsave")
)