System Variable save location detection

System Variable save location detection

Michiel.Valcke
Advisor Advisor
1,534 Views
4 Replies
Message 1 of 5

System Variable save location detection

Michiel.Valcke
Advisor
Advisor

Hello,

I'm looking for a way to determine from within AutoCAD whether a system variable is saved in a drawing or as a registry entry.

I realize you can look beforehand through the registry keys or use SYSVDLG / SYSVARMONITOR (help files) to see whether the value is stored in the drawing or in the registry.

But there are some cases where a variable is undocumented and a manual search through the registry keys is not possible/advisable.

For example in Map 3D, the system variable MAPNORTHARROWDEFAULTNORTH is undocumented (and can only be called from within a layout).

The idea would be to create a function which can tell me where a system variable is stored and when it is accessible. But I cannot start if I can't figure out how to get that information xD.


Thank you very much for your time and input.

Accepted solutions (1)
1,535 Views
4 Replies
Replies (4)
Message 2 of 5

marko_ribar
Advisor
Advisor

On Vanilla AutoCAD, these two sub-functions work... You should be looking in (envvars) for registry stored once...

 

(defun envvars ( / get-acad-regroots unique-sorted-strlst get-acad-envvars )

  (defun get-acad-regroots nil
    (vl-load-com)
    (mapcar (function (lambda ( root path ) (strcat root ((eval path)))))
            '("HKEY_LOCAL_MACHINE\\" "HKEY_CURRENT_USER\\")
            (cond (vlax-machine-product-key '(vlax-machine-product-key vlax-user-product-key))
                  ('(vlax-product-key vlax-product-key))
            )
    )
  )

  (defun unique-sorted-strlst ( strlst / prev test )
    (vl-remove-if (function (lambda ( next )
                              (cond ((setq test (eq next prev)))
                                    ((setq prev next))
                              )
                              test))
      (acad_strlsort strlst)
    )
  )

  (defun get-acad-envvars ( / read-envvars add-path )

    (defun add-path ( subkey ) (strcat path "\\" subkey))

    (defun read-envvars ( path )
      (apply 'append (cons (vl-remove-if-not 'getenv (vl-registry-descendents path t))
                           (mapcar 'read-envvars (mapcar 'add-path (vl-registry-descendents path))))
      )
    )
    
    (unique-sorted-strlst (apply 'append (mapcar 'read-envvars (get-acad-regroots))))
  )

  (get-acad-envvars)
)

 

(defun sysvars ( / car-str cmde logf qaf handle stream lst )

  (defun car-str ( text / lst )
    (substr
      (setq text (strcase (vl-string-trim " \t\r\n" text)))
      1
      (- (length (setq lst (vl-string->list text)))
         (length (member 32 lst))
      )
    )
  )

  (setq cmde (getvar 'cmdecho))
  (setq logf (getvar 'logfilemode))
  (setq qaf (getvar 'qaflags))
  (setvar 'cmdecho 0)
  (setvar 'logfilemode 1)
  (setvar 'qaflags 2)
  (vl-cmdf "_.setvar" "_?" "*")
  (setq handle (open (getvar 'logfilename) "r"))
  (while (setq stream (read-line handle))
    (setq lst (cons stream lst))
  )
  (close handle)
  (setvar 'cmdecho cmde)
  (setvar 'logfilemode logf)
  (setvar 'qaflags qaf)
  (mapcar 'car-str lst)
)

HTH.,

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 5

marko_ribar
Advisor
Advisor

If you want to filter from all system variables only those saved in registry, then simplest way is to go to REGEDIT - navigate to this branch (I use AutoCAD2018) : [HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R22.0\ACAD-1001\Variables\] , then do Export branches with variables to *.reg file... After that, open *.reg file from Notepad and save that file to *.txt UTF-8 encoding - you have option SaveAs from Notepad and then in filetypes, scroll until you find UTF-8... Finally go to AutoCAD, load this routine, start it and follow procedures - you should get *.txt file with all variables that are saved in registry...

 

(defun c:stripsysvarsfromregfile ( / regf fn outf f li )
  (setq regf (getfiled "Select *.reg file with exported sysvars branches that was converted to UTF-8 txt file..." "\\" "txt" 0))
  (setq fn (open regf "r"))
  (setq outf (getfiled "Specify filename you want to save stripped system variables from source *.txt file..." "sysvars-in-registry" "txt" 1))
  (setq f (open outf "w"))
  (while (setq li (read-line fn))
    (if (wcmatch li "`[HKEY_LOCAL_MACHINE\\SOFTWARE\\Autodesk\\AutoCAD\\R22.0\\ACAD-1001\\Variables\\*")
      (write-line (vl-string-right-trim "]" (substr li 73)) f)
    )
  )
  (close fn)
  (close f)
  (princ)
)

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 4 of 5

marko_ribar
Advisor
Advisor
Accepted solution

I've changed subs for envvars and sysvars accordingly... These system variables are stored in registry - look at second sub function...

 

(defun envvars ( / get-acad-regroots unique-sorted-strlst flatten get-acad-envvars )

  (vl-load-com)

  (defun get-acad-regroots nil
    (mapcar (function vl-filename-directory)
      (mapcar (function (lambda ( root path ) (strcat root ((eval path)))))
              '("HKEY_LOCAL_MACHINE\\" "HKEY_CURRENT_USER\\")
              (cond (vlax-machine-product-key '(vlax-machine-product-key vlax-user-product-key))
                    ('(vlax-product-key vlax-product-key))
              )
      )
    )
  )

  (defun unique-sorted-strlst ( strlst / prev test )
    (vl-remove-if (function (lambda ( next )
                              (cond ((setq test (eq next prev)))
                                    ((setq prev next))
                              )
                              test))
      (acad_strlsort (mapcar (function strcase) strlst))
    )
  )

  (defun flatten ( l )
    (if (atom l)
      (list l)
      (append (flatten (car l)) (if (cdr l) (flatten (cdr l))))
    )
  )

  (defun get-acad-envvars ( / read-envvars add-path dir )

    (defun add-path ( subkey ) (strcat path "\\" subkey))

    (defun read-envvars ( path )
      (if (vl-registry-descendents path)
        (mapcar (function read-envvars) (mapcar (function add-path) (vl-registry-descendents path)))
        path
      )
    )
    
    (unique-sorted-strlst
      (append
        (apply (function append)
          (mapcar
            (function (lambda ( x )
                        (vl-remove-if-not 
                          (function getenv)
                          (vl-registry-descendents x t)
                        )
                      )
            ) (setq dir (flatten (mapcar (function read-envvars) (get-acad-regroots))))
          )
        )
        (vl-remove-if-not (function getenv) (mapcar (function vl-filename-base) dir))
      )
    )
  )

  (get-acad-envvars)
)

 

(defun sysvars ( / get-acad-regroots unique-sorted-strlst flatten get-acad-sysvars )

  (vl-load-com)

  (defun get-acad-regroots nil
    (mapcar (function vl-filename-directory)
      (mapcar (function (lambda ( root path ) (strcat root ((eval path)))))
              '("HKEY_LOCAL_MACHINE\\" "HKEY_CURRENT_USER\\")
              (cond (vlax-machine-product-key '(vlax-machine-product-key vlax-user-product-key))
                    ('(vlax-product-key vlax-product-key))
              )
      )
    )
  )

  (defun unique-sorted-strlst ( strlst / prev test )
    (vl-remove-if (function (lambda ( next )
                              (cond ((setq test (eq next prev)))
                                    ((setq prev next))
                              )
                              test))
      (acad_strlsort (mapcar (function strcase) strlst))
    )
  )

  (defun flatten ( l )
    (if (atom l)
      (list l)
      (append (flatten (car l)) (if (cdr l) (flatten (cdr l))))
    )
  )

  (defun get-acad-sysvars ( / read-sysvars add-path dir )

    (defun add-path ( subkey ) (strcat path "\\" subkey))

    (defun read-sysvars ( path )
      (if (vl-registry-descendents path)
        (mapcar (function read-sysvars) (mapcar (function add-path) (vl-registry-descendents path)))
        path
      )
    )
    
    (unique-sorted-strlst
      (append
        (apply (function append)
          (mapcar
            (function (lambda ( x )
                        (vl-remove-if-not 
                          (function getvar)
                          (vl-registry-descendents x t)
                        )
                      )
            ) (setq dir (flatten (mapcar (function read-sysvars) (get-acad-regroots))))
          )
        )
        (vl-remove-if-not (function getvar) (mapcar (function vl-filename-base) dir))
      )
    )
  )

  (get-acad-sysvars)
)

I think that this is all relevant what can be pulled out from registry...

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 5 of 5

Michiel.Valcke
Advisor
Advisor

I'm sorry for my late reply, I got occupied otherwise. Your code really helped me understand what is possible to check from within AutoCAD.

0 Likes