Author Topic: reuse saved variables  (Read 12922 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
reuse saved variables
« on: December 16, 2015, 12:35:47 PM »
Hi, I have question/request... Lets say I have stored some variables during my ACAD session as global variables... The question is : is there some ready routine that will prompt me to choose variables from memory and save them into external txt file from which with another similar routine I can safely exit ACAD session, start new one, load lisp, browse for a stored txt file and restore them back into ACAD memory - but in different session... All variable names should be the same as their corresponding data values...

Maybe if it doesn't exist this may be some challenge for some experience programmer to tackle this task...

Opinion or advice, I am hoping I am not asking too much, but I think it may be useful for all *CAD users...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: reuse saved variables
« Reply #1 on: December 16, 2015, 01:32:50 PM »
I had written something like that when I was a CAD mgr for trouble-shooting and what-not but I'd have to see if I have any of that stuff still (doubtful but worth a look). However, your request sounds a bit vague (the stuff I had written back when was more for trouble-shooting not session restoration); you may want to narrow your options.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: reuse saved variables
« Reply #2 on: December 16, 2015, 01:52:15 PM »
You would need to store the contents of (atoms-family) on drawing startup and then have your variable saving program compare the current content of (atoms-family) with the stored data, removing symbols present in the stored data in order to leave only global variables and variables defined within the scope of the program itself.

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: reuse saved variables
« Reply #3 on: December 16, 2015, 02:02:28 PM »
You would need to store the contents of (atoms-family) on drawing startup and then have your variable saving program compare the current content of (atoms-family) with the stored data, removing symbols present in the stored data in order to leave only global variables and variables defined within the scope of the program itself.

How difficult would it be if I wanted to save all variables (atoms) with (type) 'SYM, 'INT, 'LIST, 'REAL, 'STR? Would it take to much memory - I suppose I don't have to compare (atoms-family) at start of session with finish session when all needed variables are stored in memory - or I am wrong?
« Last Edit: December 16, 2015, 02:18:45 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: reuse saved variables
« Reply #4 on: December 16, 2015, 02:25:28 PM »
You are essentially describing the Express Tools 'LSP' command (lspdata.lsp).

You have two options:

1. Store the content of (atoms-family) at the start of the session (i.e. before any additional symbols are defined) and subsequently compare this with the list returned by (atoms-family).

2. Hard-code the known content of (atoms-family) and then compare the current list with this hard coded data - this is how lspdata.lsp operates.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: reuse saved variables
« Reply #5 on: December 16, 2015, 02:26:59 PM »
How difficult would it be if I wanted to save all variables (atoms) with (type) 'SYM, 'INT, 'LIST, 'REAL, 'STR? Would it take to much memory - I suppose I don't have to compare (atoms-family) at start of session with finish session when all needed variables are stored in memory - or I am wrong?

Memory isn't really the issue (you're storing them in a text file) however, you will be doing a bit of processing so you better make spend some time on making that code lean.  ...don't focus on the "how" yet. Focus on the "why". Why are wanting this (did something go wrong or are you looking to index your current lisps)?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: reuse saved variables
« Reply #6 on: December 16, 2015, 02:28:31 PM »
You are essentially describing the Express Tools 'LSP' command (lspdata.lsp).

You have two options:

1. Store the content of (atoms-family) at the start of the session (i.e. before any additional symbols are defined) and subsequently compare this with the list returned by (atoms-family).

2. Hard-code the known content of (atoms-family) and then compare the current list with this hard coded data - this is how lspdata.lsp operates

There was a built in tool for this?! Oops, I guess I shouldn't have made my own back then (dummy).
Pointy hat --> 7
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: reuse saved variables
« Reply #7 on: December 16, 2015, 05:36:06 PM »
I did something... I hope it's fine - it worked for me in my limited tests... Please read comments in header of lisp "variables.lsp"
(I was using Lee's 1st method, but I've stored preliminary atoms-family into *atoms* variable which is later just used, but not accounted into variables list...)

Code - Auto/Visual Lisp: [Select]
  1. ;;;
  2. ;;; put this lisp at line before last in acaddoc.lsp : (if (findfile "variables.lsp") (load "variables.lsp") (load (getfiled "Select variables.lsp file" "\\" "lsp" 16)))
  3. ;;;
  4. ;;; put this line as last one in acaddoc.lsp : (setq *atoms* (atoms-family 1))
  5. ;;;
  6.  
  7. (defun c:var-save ( / varlst f1 f2 n )
  8.   (setq varlst (vl-remove-if '(lambda ( x ) (vl-position x *atoms*)) (atoms-family 1)))
  9.   (setq varlst (vl-remove "*ATOMS*" varlst))
  10.   (foreach var varlst
  11.     (if (not (or (eq (type (eval (read var))) 'SYM) (eq (type (eval (read var))) 'INT) (eq (type (eval (read var))) 'STR) (eq (type (eval (read var))) 'LIST) (eq (type (eval (read var))) 'REAL)))
  12.       (setq varlst (vl-remove var varlst))
  13.     )
  14.   )
  15.   (setq f1 (open (getfiled "Save variable names file" "\\varnames" "txt" 1) "w"))
  16.   (setq n -1)
  17.   (foreach var varlst
  18.     (write-line (nth (setq n (1+ n)) varlst) f1)
  19.   )
  20.   (close f1)
  21.   (setq f2 (open (getfiled "Save variable values file" "\\varvalues" "txt" 1) "w"))
  22.   (foreach var varlst
  23.     (write-line (vl-prin1-to-string (eval (read var))) f2)
  24.     (princ "\n" f2)
  25.   )
  26.   (close f2)
  27.   (princ)
  28. )
  29.  
  30. (defun c:var-load ( / f1 f2 varlst varvallst l ll )
  31.   (setq f1 (open (getfiled "Load variable names file" "\\varnames" "txt" 16) "r"))
  32.   (while (setq l (read-line f1))
  33.     (setq varlst (cons (read l) varlst))
  34.   )
  35.   (close f1)
  36.   (setq ll "")
  37.   (setq f2 (open (getfiled "Load variable values file" "\\varvalues" "txt" 16) "r"))
  38.   (while (setq l (read-line f2))
  39.     (if (not (eq l ""))
  40.       (setq ll (strcat ll l))
  41.       (progn
  42.         (setq varvallst (cons (read ll) varvallst))
  43.         (setq ll "")
  44.       )
  45.     )
  46.   )
  47.   (close f2)
  48.   (mapcar 'set varlst varvallst)
  49.   (princ)
  50. )
  51.  

It helps me, so I thought it should be useful someone sometime...
M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: reuse saved variables
« Reply #8 on: December 17, 2015, 04:37:22 AM »
Some issues that are worth considering:
1.
Accuracy of stored reals.
2.
A list may need to be analysed before being stored. Example: storing a list of enames is not useful.
3.
The 'load' function will overwrite existing values. This may not always be wise.
4.
Why not use a single file to store both the names and the values of the variables?

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: reuse saved variables
« Reply #9 on: December 17, 2015, 07:14:42 AM »
Some issues that are worth considering:
1.
Accuracy of stored reals.
2.
A list may need to be analysed before being stored. Example: storing a list of enames is not useful.
3.
The 'load' function will overwrite existing values. This may not always be wise.
4.
Why not use a single file to store both the names and the values of the variables?

I can only confirm loosing of data with REAL numbers, and as far as I know there is no possibility to force *CAD to display long real values stored into 'REAL or 'LIST atoms... I don't know where to look to make (force) *CAD operates this way... So for ex. PI variable is actually truncated and when typed !PI in command prompt only 5 decimal digits are displayed - that's how much (read somenumberstring) can actually display even if (setq somenumberstring "99.9999999999999999999999999")... Only this problem occurs to me from those that you've stated, with other issues I can live... Maybe Autodesk should consider including some sysvar for this functionality, till then I think my lisp variables.lsp is actually correct - if and when Autodesk introduces this new functionality if I don't revise my above posted code, user should consider this lack and prevent loosing data when saving variable values comes into question... As for 2 separate *.txt files, I don't really mind - it was the way I wrote those (c:var-save) and (c:var-load) functions - when I was writing them firstly I had in mind 1 txt file, but then I realized that with (while (read-line f) ... ) where f is 'FILE I was in programming problem of how to obtain and reassign values to their corresponding symbol specifications...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: reuse saved variables
« Reply #10 on: December 17, 2015, 08:16:07 AM »
Well, I got a chance to look through some of my old USB sticks and I did find something (very early in the development it appears) but if anyone can glean or use anything from it, I'll be happy.

Code - Auto/Visual Lisp: [Select]
  1. ;;;===================================================================;
  2. ;;;===================================================================;
  3. ;;; Demmand loading of settings done from acad.lsp file for company   ;
  4. ;;; standards.                                                        ;
  5. ;;;===================================================================;
  6. ;;;===================================================================;
  7.  
  8. (
  9.  ;; first test run for a proced that will run a list of commands.
  10.  ;; TODO: add error trapping on the hookanddo portion of this app.
  11.    ( /        m-list
  12.               push->lst
  13.               list-pop
  14.               plug-in
  15.               plug-in
  16.               pop-out
  17.               hook
  18.               popout-hook-and-do
  19.               fill-list-from-file
  20.               )
  21.    ;; //-- Begin Support Procedures --//
  22.    (defun push->lst (sym lst)
  23.      ;; push-lst
  24.      ;; put an item first into existing list.
  25.      ;;
  26.      ;; Ex: (push->lst 'one 'masterlist)
  27.      ;;    > (ONE)
  28.      ;;    > !masterlist
  29.      ;;    > (ONE)
  30.      ;;
  31.      ;; Notes: One benifit for using this method is that
  32.      ;;    the list is created for you. better then;
  33.      ;;    (setq masterlist (cons ... masterlist))
  34.      ;;    This results in less confusion in final code.
  35.      ;;
  36.      ;; By: John (Se7en) K
  37.      ;;    (inspired by Vladimir Nesterovsky)
  38.      (set lst (cons sym (eval lst))) )
  39.  
  40.    (defun list-pop (lst / ~tval )
  41.      ;; list-pop
  42.      ;; remove the first item from a list and redefne the var
  43.      ;; containing that list For example:
  44.      ;; Given a list called "masterlist" with the
  45.      ;; value of: (TWO ONE)
  46.      ;;
  47.      ;; !masterlist
  48.      ;; (TWO ONE)
  49.      ;;
  50.      ;; (list-pop 'masterlist)
  51.      ;; TWO
  52.      ;;
  53.      ;; (list-pop 'masterlist)
  54.      ;; ONE
  55.      ;;
  56.      ;; !masterlist
  57.      ;; nil
  58.      ;;
  59.      ;; By: John (Se7en) K
  60.      ;;    (inspired by Vladimir Nesterovsky)
  61.      (setq ~tval (eval lst))
  62.      (set lst (cdr ~tval))
  63.      (car ~tval) )
  64.  
  65.    ;; NOTE: Removed the next two procedures because first
  66.    ;;       testing phase is over and they are not needed.
  67.    ;;
  68.    ;;   (defun plug-in ( proced )
  69.    ;;     ;; add an item to a global list
  70.    ;;     (push->lst proced 'proced-list) )
  71.    ;;
  72.    ;;   (defun pop-out ( )
  73.    ;;     ;; remove an item from a global list
  74.    ;;     (list-pop 'proced-list) )
  75.  
  76.    (defun hook (func)
  77.      ;; hook
  78.      ;; this function will take an argument and turn it into a
  79.      ;; lambda expression to evaluate. (You can assign an variable
  80.      ;; and assign it at a later time if you wish.)
  81.      ;;
  82.      ;; By: John (Se7en) K
  83.      ;;    (inspired by Vladimir Nesterovsky)
  84.      ;;
  85.      ;; Ex: (hook '(+ 1 2))
  86.      ;;     > ((LAMBDA nil (+ 1 2)))
  87.      ;;    
  88.      (list (cons 'lambda (cons nil (list func)))) )
  89.  
  90.    (defun popout-hook-and-do ( / x )
  91.      (setq x (list-pop 'proced-list))
  92.      (if x (eval (hook x))) )
  93.  
  94.    (defun fill-list-from-file ( filename-str / fp )
  95.      ;; fill-list-from-file
  96.      ;;
  97.      ;; (fill-list-from-file "c:\\test.dat")
  98.      ;; > ((ALERT "test2") (ALERT "test1"))
  99.      ;;
  100.      ;; TODO add a filter for the path variables
  101.      ;;      -ie ACAD => (setenv "ACAD" (strcat <pathstr> (getenv "ACAD")))
  102.      ;;
  103.      (setq fp (open filename-str "R"))
  104.      (while (setq line (read-line fp))
  105.             (push->lst (read line) 'proced-list))
  106.      (close fp)
  107.      my-lst
  108.      )
  109.    ;; //--End Support Procedures--//
  110.  
  111.  
  112.    ;; NOTE: I have removed the next four lines because the first test
  113.    ;;       was sucessfull, I am now moving on to the next phase of
  114.    ;;       of the procedure; reading from a file and filling the list
  115.    ;;       from there.
  116.    ;;
  117.    ;; (setq m-list '((alert "test1") (alert "test2") (alert "test3")))
  118.    ;; build the list of things to do
  119.    ;; build the list by using the ``fill-list-from-file'' proced
  120.    ;; (mapcar 'plug-in m-list)
  121.  
  122.  
  123.    (setq proced-list '((alert "test3") (alert "test2") (alert "test1")))
  124.  
  125.    ;; plug each of those items in the list into a master list
  126.    ;; c:\test.dat contains:
  127.    ;; (alert "test4")
  128.    ;; (alert "test5")
  129.    ;; (alert "test6")
  130.    ;; (alert "test7")
  131.    (fill-list-from-file "c:\\test.dat")
  132.  
  133.    (while proced-list
  134.           (popout-hook-and-do))
  135.    ;; evaluate each item in the list
  136.  
  137.    )
  138.  )

You may want to replace the file reader portion of the code above with something a little quicker which I think I gleaned from something ElpanovEvgeniy penned once.
Code - Auto/Visual Lisp: [Select]
  1. (defun file-get-list-from ( name / fp lst read-file-into-list )
  2.   ;; file-get-list-from
  3.   ;;
  4.   ;; GENERAL FILE READER
  5.   ;; given a file name this procedure will read the contents
  6.   ;; into a list
  7.   ;;
  8.   ;; EX:
  9.   ;;    (mapcar
  10.   ;;       'read (get-list-from-file
  11.   ;;         (findfile layers-to-load-from-file)))
  12.   ;;
  13. (defun read-file-into-list ( str file )         ;;/*{{{*/
  14.   (if str
  15.     (cons
  16.       (if str str)
  17.       (read-file-into-list (read-line file) file))))    ;;/*}}}*/
  18.   (setq fp (open name "R"))
  19.   (setq lst (read-file-into-list (read-line fp) fp))
  20.   (close fp)
  21.  lst )


I also found the function below which is in the same spirit of the above. This one looks like it was saving a few of the users environment variables in the same format as the above code expects (I must have had a whole setup at one time).
Code - Auto/Visual Lisp: [Select]
  1. (
  2.  ;; make a backup of users settings.
  3.  (lambda ()
  4.    (setq sys-var-list
  5.          '(
  6.            "ACAD" "ANSIHatch" "ANSILinetype" "ISOHatch" "ISOLinetype"
  7.            "StartUpType" "Measureinit" "InsertUnitsDefSource"
  8.            "InsertUnitsDefTarget" "LastTemplate" "Pickstyle" "Coords"
  9.            "ShowProxyDialog" "Osmode" "EdgeMode" "PAPERUPDATE" "ACADPLCMD"
  10.            "ImageHighlight" "Attdia" "Attreq" "Delobj" "Dragmode" "UseMRUConfig"
  11.            "PLSPOOLALERT" "PLOTLEGACY" "PSTYLEPOLICY" "OLEQUALITY" "Anyport"
  12.            "Validation Policy" "Validation Strategy" "CommandDialogs"
  13.            "TempDirectory" "PlotSpoolerDirectory" "DefaultLoginName" "MenuFile"
  14.            "NetLocation" "ACADDRV" "ACADHELP" "PrinterConfigDir"
  15.            "PrinterStyleSheetDir" "PrinterDescDir" "NewStyleSheet"
  16.            "DefaultFormatForSave" "DefaultConfig" "LastModifiedConfig"
  17.            "MRUConfig" "ACADLOGFILE" "MaxDwg" "AVEMAPS" "TemplatePath"
  18.            "DatabaseWorkSpacePath" "DefaultPlotStyle" "DefaultLayerZeroPlotStyle"
  19.            "LineWeightUnits" "LWDEFAULT" "CustomColors" "Blipmode" "ToolTips"
  20.            "acet-Enable" "acet-MenuLoad" "AcetRText:type" "CmdVisLines"
  21.            "MaxHatch" "AutoSnapColor" "AutomaticSaveMinutes"
  22.            "SDF_AttributeExtractTemplateFile" "AutoSnapPolarAng"
  23.            "AutoSnapPolarDistance" "AutoSnapPolarAddAng" "AutoSnapControl"
  24.            "AutoSnapTrackPath" "PickBox" "AutoSnapSize" "PickFirst" "PickAuto"
  25.            "MenuOptionFlags" "FontMappingFile" "LogFilePath"
  26.            "PSOUT_PrologFileName" "MainDictionary" "CustomDictionary"
  27.            "MTextEditor" "XrefLoadPath" "SaveFilePath" "AcadLspAsDoc"
  28.            "Background" "Layout background" "XhairPickboxEtc"
  29.            "LayoutXhairPickboxEtc" "Autotracking vector" "MonoVectors" "FontFace"
  30.            "FontHeight" "FontWeight" "FontItalic" "FontPitchAndFamily"
  31.            "CursorSize" "HideWarningDialogs" "SDIMode" "CmdLine.ForeColor"
  32.            "CmdLine.BackColor" "TextWindow.ForeColor" "TextWindow.BackColor"
  33.            "CmdLine.FontFace" "CmdLine.FontHeight" "CmdLine.FontWeight"
  34.            "CmdLine.FontItalic" "CmdLine.FontPitchAndFamily"
  35.            "TextWindow.FontFace" "TextWindow.FontHeight" "TextWindow.FontWeight"
  36.            "TextWindow.FontItalic" "TextWindow.FontPitchAndFamily"
  37.            )
  38.          )
  39.      ;; NOTE: Removed the next four lines because first
  40.      ;;       testing phase is over and they are not needed.
  41.      ;;
  42.         ;;(mapcar
  43.         ;; ;; create backup of orig sys-vars
  44.         ;; ;; ("pickfirst" "1") ("autosnapsize" "5") ("pickbox" "6") ...
  45.         ;;  '(lambda (x)
  46.         ;;     (setq sys-var-backup
  47.         ;;           (cons (list x (eval (list 'getenv x))) sys-var-backup))) sys-var-list)
  48.  
  49.    (setq fp (open "c:\\sys-var-bakup.dat" "A"))
  50.    (mapcar
  51.      '(lambda (x)
  52.         (setq y (eval (list 'getenv x)))
  53.         (write-line
  54.           (strcat "(setenv \"" x "\" \"" (if (not y) "nil" y) "\")") fp))
  55.      sys-var-list
  56.      )
  57.    (close fp)
  58.    )
  59.  )
  60.  

At any rate, I hope at least some of this code is helpful to someone.
« Last Edit: December 17, 2015, 08:19:27 AM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: reuse saved variables
« Reply #11 on: December 17, 2015, 09:00:08 AM »
as far as I know there is no possibility to force *CAD to display long real values stored into 'REAL or 'LIST atoms... I don't know where to look to make (force) *CAD operates this way... So for ex. PI variable is actually truncated and when typed !PI in command prompt only 5 decimal digits are displayed - that's how much (read somenumberstring) can actually display even if (setq somenumberstring "99.9999999999999999999999999")

Use rtos in place of vl-prin1-to-string, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. "3.14159"
  2. _$ (rtos pi 2 15)
  3. "3.141592653589793"

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: reuse saved variables
« Reply #12 on: December 17, 2015, 10:06:05 AM »
as far as I know there is no possibility to force *CAD to display long real values stored into 'REAL or 'LIST atoms... I don't know where to look to make (force) *CAD operates this way... So for ex. PI variable is actually truncated and when typed !PI in command prompt only 5 decimal digits are displayed - that's how much (read somenumberstring) can actually display even if (setq somenumberstring "99.9999999999999999999999999")

Use rtos in place of vl-prin1-to-string, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. "3.14159"
  2. _$ (rtos pi 2 15)
  3. "3.141592653589793"

Yes it's true, but actually it's not that simple: what shell we do with point lists?... Shouldn't (vl-prin1-to-string) be just enough good to do this task as built-in?... If not revision of this function then I suppose implementing new sysvar for more adequate representation of 'REAL numbers is necessity... Otherwise loosing data is unavoidable outcome...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: reuse saved variables
« Reply #13 on: December 17, 2015, 10:24:33 AM »
as far as I know there is no possibility to force *CAD to display long real values stored into 'REAL or 'LIST atoms... I don't know where to look to make (force) *CAD operates this way... So for ex. PI variable is actually truncated and when typed !PI in command prompt only 5 decimal digits are displayed - that's how much (read somenumberstring) can actually display even if (setq somenumberstring "99.9999999999999999999999999")

Use rtos in place of vl-prin1-to-string, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. "3.14159"
  2. _$ (rtos pi 2 15)
  3. "3.141592653589793"

Yes it's true, but actually it's not that simple: what shell we do with point lists?... Shouldn't (vl-prin1-to-string) be just enough good to do this task as built-in?... If not revision of this function then I suppose implementing new sysvar for more adequate representation of 'REAL numbers is necessity... Otherwise loosing data is unavoidable outcome...
o.0 ... I think you should look at the method I used in storing and retrieving variables.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: reuse saved variables
« Reply #14 on: December 17, 2015, 11:15:21 AM »
o.0... I don't see the method in above posted examples (link or similar), but beside this if 'LIST is entity DXF data, if I may also say (read) function returns error : extra cdr in dotted pair list... And for me this is odd too... So IMHO I think that at least 2 functions should be revised - or added : something similar to (vl-prin1-to-string) and something similar to (read)... So there it is - if someone wants to tackle it, it would be nice to see result of this challenge...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube