;Re-apply a template file to the current drawing (update layers, dimstyles, etc).
;Use DWS for frequent "live" changes of the template (and set STANDARDSVIOLATION to 1 or 2),
; but any DWT/DWG file will do as well
;
;CAD Studio, 2018 - www.cadstudio.cz  www.cadforum.cz
;

(defun C:ReTemplate ( / dwtfile )

;;;Michael Puckett
(defun cdrs (key lst / pair rtn)
(while (setq pair (assoc key lst))
(setq rtn (cons (cdr pair) rtn)
lst (cdr (member pair lst))))
(reverse rtn))

;;;R. Robert Bell
(defun AddDWS (FileN / DictN eDict XRInt)
 (setq DictN "AcStStandard"
	   eDict
	   (cond
	   ((cdr (assoc -1 (dictsearch (namedobjdict) DictN))))
		((dictadd (namedobjdict) DictN (entmakex '((0 . "DICTIONARY") (100 . "AcDbDictionary")))))
	   )
 )
 (if (setq XRInt (cdrs 3 (entget eDict)))
	(setq XRInt (1+ (apply 'max (mapcar 'atoi XRInt))))
	(setq XRInt 0)
 )
 (dictadd eDict (itoa XRInt) (entmakex
	(list '(0 . "XRECORD") '(100 . "AcDbXrecord") (cons 1 FileN)))
 )
)

;Usage: (AddDWS "c:\\program files\\AutoCAD 2014\\Sample\\MyStd.dws")

 (setq dwtfile 
      (if (= (getvar "FILEDIA") 1)(getfiled "Select the new template drawing to reapply " "" "dws;dwt;dwg" 16)(getstring "\nEnter the new template drawing name to reapply: "))
 )
 (if (and dwtfile (> dwtfile ""))(progn
	(AddDWS dwtfile)
	(command "_CHECKSTANDARDS") ; comment out if you want to set and use just automatic updates of nonstandard properties
 ))
 (princ)
)

(princ "\nReTemplate command loaded (CAD Studio, 2018)")
(princ)
