Jump to content

Saving global variable values in drawing session & load them again with dwg opening


MJLM

Recommended Posts

The idea is to store some global variables of some lisp routines in a drawing file so that when I load this drawing again these values are reloaded into session memory.

 

I found the

vlax-ldata-put & vlax-ldata-get

 

commands but these work with a specific entity or am I wrong?

I find it restrictive and inappropriate to having selecting

an entity to store values.

What would be the best way to do that?

Link to comment
Share on other sites

Hi ,

 

You can export the variable to a txt file and save it in the support folder or anywhere you 'd like and after that you can call that file by its name with its contents , or you can create dictionary which would increase the size of your drawing if you have many values to save with in many dictionaries .

 

It is up to you . :)

Link to comment
Share on other sites

You can also try the (vl-bb-set) and (vl-bb-ref) methods , there are plenty of documentation on those two, and they're really easy to use.

 

 

Which, FYI, stands for visual lisp, blackboard, set/ref.

Would have to check the documentation to determine where these values are stored, unless someone knows it off the top of their head.

Link to comment
Share on other sites

Thanks both of you. It seems the method bhull proposed is quite fast and flexible.

 

Edit: it seems that the (vl-bb-set) and (vl-bb-ref) work only during one autocad session and not per drawing file. Whatever file I open the same values I get loaded and only for one session. I will have to look deeper on this.

Edited by MJLM
Link to comment
Share on other sites

Problem with BB variables is they are lost when you close the drawing, I would suggest learning about creating a custom dictionary in each drawing. If you are saving simple values it will not increase drawing size by much. The issue with external text files is you will need to do a lot of coding to make sure all variables are changed if you do an undo command etc. (I tried that route not very nice). Also if you save the values right in the drawing within a dictionary you will not need to save the text file with the drawing file, especially if you want to send them to someone. I save approx 100 variable values within a custom dictionary with no issues, you will also need to learn the proper DXF codes to save your values to whether they be text, integer, reals point values etc. once you have things figured out it is very safe and reliable mothed of saving the values. Additionally if you undo the lisp function that created or set the variable values, the values will revert back automatically. (just as an undo should).

Link to comment
Share on other sites

The idea is to store some global variables of some lisp routines in a drawing file so that when I load this drawing again these values are reloaded into session memory.

 

 

 

Yeah, that's my bad. Hadn't tested the vl-bb-set/ref commands more thoroughly I would have known they're just for the current session.

Seems like snownuts method would be the best way to accomplish what you're asking.

Personally I would create a toolbar button and macro to access your dictionary, that way you can 1-click and then edit with whichever information you're adding.

Toolbar button or lisp routine, just something to make accessing and modifying the dictionary easy.

Perhaps snownut would help with this....although I'm sure we could find something working, that requires research and time...that I don't have atm

Link to comment
Share on other sites

Below is the code I use, there are several functions and sub-functions all included;

 

1.-Create New Dictionary

2.-Create new xrecord within the created dictionary

3.-Append an xrecord

4.-Read an xrecord

5.-Update an xrecord

 

I had a little assistance from Lee Mac on these functions, they work flawlessly in ACAD 2004 and up to at least 2011, and Bricscad 12 through current version.

 


;=============================  Begin < GM_Dict > Function =====================================;
;             Function to Create the "SADATA_DICT" within the NOD                ;
;        Syntax for function call;                                ;
;                  (GC_Dict  < dictname >)                             ;
;        Example - (GC_Dict "SADATA_DICT")                            ;
;                                  Created by B. Fillmore                                       ;

(defun GC_Dict ( dictname / adict)  
 (if (not (setq adict (dictsearch (namedobjdict) dictname))) 
   (progn 
     (setq adict (entmakex '((0 . "DICTIONARY")(100 . "AcDbDictionary")))) 
     (if adict (setq adict (dictadd (namedobjdict) dictname adict)))
   ) 
   (setq adict (cdr (assoc -1 adict))) 
 ) 
);defun

;(GC_Dict "SADATA_DICT") Calling Line                                ;
;                                                ;
;==============================  End < GM_Dict > Function ======================================;


;===========================  Begin < GM_Xrecord > Function ====================================;
;         Function to Create an XRECORD in the "SADATA_DICT" within the NOD        ;
;        Syntax for function call;                                ;
;            (GM_Xrecord < XRECORD NAME >)                        ;
;        Example -     (GM_Xrecord "LDATA_VARS")                        ;
;                                  Created by B. Fillmore                                       ;

(defun GM_Xrecord ( DATA_VARS / adict anXrec)
 (cond
   (
    (setq adict (GC_Dict "SADATA_DICT")) ; "SADATA_DICT" is the name of the dictionary
    (cond     
      (
   (not
     (setq anXrec (dictsearch adict DATA_VARS))
     )
   (setq anXrec (entmakex values))    
       (if anXrec (setq anXrec (dictadd adict DATA_VARS anXrec))
     );if
   );not
      (setq anXrec
   (cdr (assoc -1 (dictsearch adict DATA_VARS)))
   );setq
      );cond
    );setq
   );cond
 );defun

;(GM_Xrecord "EDATA_VARS") Calling Line     (setq DATA_VARS "EDATA_VARS")                ;
;                                                ;
;============================  End < GM_Xrecord > Function =====================================;


;===========================  Begin < Append_Xrecord >  Function ===============================;
;         Function to add  values to an existing XRECORD using the DXF Group Code        ;
;        Syntax for function call;                                ;
;                  (Append_Xrecord <XRECORD NAME> <DXF Group Code to Update> <Value to store>)    ;
;        Example - (Append_Xrecord "EDATA_VARS" 45 0.0)                        ;
;                                                                                               ;
;        Returns all associations of a key in an association list            ;
;                                  Created by B. Fillmore                                       ;


(defun Append_Xrecord (DATA_VARS DXF_VAL NEW_VAL / SAdict anXrec xRecFile)
 (setq DATA_VARS (strcat tabname DATA_VARS)
   SAdict (GC_Dict "SADATA_DICT")
   anXrec (dictsearch SAdict DATA_VARS)
   )
 (if (= (assoc DXF_VAL anXrec)nil)
   (progn
     (setq values (append anXrec
              (list(cons DXF_VAL NEW_VAL))
              )
       )
     (entdel (cdr (assoc -1 anXrec)))
     (GM_Xrecord DATA_VARS)
     )
   )
 );defun
 
;(Append_Xrecord "EDATA_VARS" 46 0.0) Calling Line     (setq DATA_VARS "EDATA_VARS")        ;
;                                                ;
;==========================  End < Append_Xrecord > Function ===================================;

     

;===========================  Begin < GetVar_Xrecord >  Function ===============================;
;         Function to retrieve values from an XRECORD using the DXF Group Code        ;
;        Syntax for function call;                                ;
;                  (GetVar_Xrecord <XRECORD NAME> <DXF Group Code to Update> <Nth Group Code>)    ;
;        Example - (GetVar_Xrecord "LDATA_VARS" 300 2)                        ;
;                                                                                               ;
;                  LM:mAssoc  -  By; Lee Mac                        ;
;        Returns all associations of a key in an association list            ;
;                                  Created by B. Fillmore                                       ;


(defun GetVar_Xrecord ( DATA_VARS DXF_VAL LnmBr / xRecFile vars )
 (defun LM:mAssoc ( DXF_VAL xRecFile / item )
   (if (setq item (assoc DXF_VAL xRecFile))
     (cons (cdr item) (LM:mAssoc DXF_VAL (cdr (member item xRecFile))))
     )
   );defun

 (if (/= DATA_VARS "ODATA_VARS") ; "ODATA_VARS is a set of DXF codes within the dictionary
   (setq DATA_VARS (strcat tabname DATA_VARS))
   )
 (setq vars (GM_Xrecord DATA_VARS)) 
 (cond (vars
        (setq xRecFile  (entget vars))
    (cdr (cons DXF_VAL(nth LnmBr (LM:mAssoc DXF_VAL xRecFile))))
       ) 
       (T nil)
 )
);defun

;(setq VARNAME (GetVar_Xrecord "LDATA_VARS" 300 2)) Calling Line                ;
;                                                ;
;==========================  End < GetVar_Xrecord > Function ===================================;


;==========================  Begin < Update_Xrecord > Function =================================;
;         Function to Update the values in an XRECORD using the nth DXF Group Code    ;
;        Syntax for function call;                                ;
;    (Update_Xrecord <XRECORD NAME> <DXF Group Code to Update> <New Value> <Nth Group Code>)    ;
;        Example - (Update_Xrecord "LDATA_VARS" 300 "Update test successful!" 3 )        ;
;                                                                                               ;
;               LM:mAssoc  -  By; Lee Mac                        ;
;        Returns all associations of a key in an association list            ;
;                                  Created by B. Fillmore                                       ;


(defun Update_Xrecord (DATA_VARS DXF_VAL MOD_VAL LnmBr / xRecFile Values SAdict)
 (defun LM:mAssoc ( DXF_VAL xRecFile / item )
   (if (setq item (assoc DXF_VAL xRecFile))
     (cons (cdr item) (LM:mAssoc DXF_VAL (cdr (member item xRecFile))))
     )
   );defun
 (if (/= DATA_VARS "ODATA_VARS")
   (setq DATA_VARS (strcat tabname DATA_VARS))
   )
 (setq    SAdict (dictsearch (namedobjdict) "SADATA_DICT")
   xRecFile (dictsearch (cdr (assoc -1 SAdict)) DATA_VARS)
   )  
 (setq values (subst (cons DXF_VAL MOD_VAL)
             (cons DXF_VAL (nth LnmBr (LM:mAssoc DXF_VAL xRecFile)))
             xRecFile)
   )
 (entdel (cdr (assoc -1 xRecFile)))
 (GM_Xrecord DATA_VARS)
 );defun

; (setq   DATA_VARS  "LDATA_VARS")

;(Update_Xrecord "LDATA_VARS" 300 "Nmbr" 2)  Calling line                    ;
;                                                ;
;==========================  End < Update XRecord > Function ===================================;


;============================  Begin < New_Xrecord > Function ==================================;
;         Function to Create an XRECORD with Default Values                ;
;    Syntax for function call;                                ;
;            (New_Xrecord <XRECORD NAME> )                        ;
;    Example -    (Update_Xrecord "LDATA_VARS")                        ;
;                                  Created by B. Fillmore                                       ;
;                                                                                               ;

(defun New_xRecord ( DATA_VARS / Values vars)  
 (if (/= DATA_VARS "EDATA_VARS" DATA_VARS "HDATA_VARS"
     DATA_VARS "LDATA_VARS" DATA_VARS "ODATA_VARS")          
   (progn
     (alert "There are no xRecord Values to Create xRecord for !
           \nYou must enter the Values Reference to proceed !")
     (exit)
     );progn
   );if
 
 (if (= DATA_VARS "EDATA_VARS" )
   (setq DATA_VARS (strcat tabname DATA_VARS)
        Values  (list(cons 0 "XRECORD"    )    ; used for EDA Data File Defaults in "XRECORDS"
              (cons 100  "AcDbXrecord"    )
              (cons   3 "0"        )
              (cons  40  0.0        )
              (cons  41  0.0        )
              (cons  42  0.0        )
              (cons  43  0.0        )
              (cons  44  0.0        )
              (cons  45  0.0        )
              (cons  46  0.0        )
              (cons  47  0.0        )               
              (cons  70  0        )
              (cons  71  0        )
              (cons  72  0        )
              (cons  73  0        )
              (cons  74  0        )
              (cons  75  0        )
              (cons  76  0        )
              (cons  77  0        )
              (cons  78  0        )
              (cons  79  0        )               
              (cons 270  0        )
              (cons 271  0        )
              (cons 272  0        )
              (cons 273  0        )               
              (cons 300 "0"        )
              (cons 301 "0"        )
              )
     );setq
   );if
 
 ;(setq DATA_VARS "EDATA_VARS")
 ;(New_Xrecord "EDATA_VARS")


 (if (= DATA_VARS "HDATA_VARS" )
   (setq DATA_VARS (strcat tabname DATA_VARS)
     Values  (list(cons 0 "XRECORD"    )    ; used for House Data File in "XRECORDS"
              (cons 100  "AcDbXrecord"    )
              (cons  40  0.0        )
              (cons  41  0.0        )
              (cons  70     0        )
              (cons  71    36        )
              (cons  72    10        )               
              (cons 270     1        )
              (cons 271     0         )
              (cons 272     0        )
              (cons 273     0        )               
              (cons 300 "Drain Outlet"    )
              )
     );setq
   );if

 ;(entmakex values)
 
 ;(setq DATA_VARS "HDATA_VARS")
 
 (if (= DATA_VARS "LDATA_VARS" )
   (setq DATA_VARS (strcat tabname DATA_VARS)
     Values  (list(cons 0 "XRECORD"    )    ; used for Lot Data File in "XRECORDS"
              (cons 100 "AcDbXrecord"    )
              (cons   3 "Septic Assistant")
              (cons  70   0        )
              (cons  71   0        )
              (cons 300 "Town"        )
              (cons 300 "County"    )
              (cons 300 "Book"     )
              (cons 300 "Page"     )
              (cons 300 "N/A"      )
              (cons 300 "Lot"         )
              (cons 300 "Map"         )
              (cons 300 "Area"     )
              (cons 300 "Block"    )
              (cons 300 "Unit"     )
              (cons 301 "Prev CA "    )
              (cons 301 "CA2012..."     )
              (cons 301 "Yes"      )
              (cons 302 "No"       )
              (cons 303 "No"       )
              (cons 304 "No"       )
              )
     );setq
   );if

  ;(setq DATA_VARS "LDATA_VARS")
  ;     (New_Xrecord "LDATA_VARS")
 
 (if (= DATA_VARS "ODATA_VARS" )
   (setq Values  (list(cons 0 "XRECORD")    ; used for Lot Owner File in "XRECORDS"
              (cons 100  "AcDbXrecord")
              (cons 3 "Septic Assistant")
              (cons 300 "O-Name"    )
              (cons 300 "O-Street"    )
              (cons 300 "O-State"     )
              (cons 300 "O-Town"     )
              (cons 300 "O-Zip"      )
              (cons 300 "O-Phone"     )
              (cons 300 "O-Email"     )
              (cons 301 "A-Name"    )
              (cons 301 "A-Street"    )
              (cons 301 "A-Town"    )
              (cons 301 "A-State"    )
              (cons 301 "A-Zip"    )
              (cons 301 "A-Phone"    )
              (cons 301 "A-Email"    )
              (cons 302 "Site Street"    )
              (cons 302 "Sub-Name"    )
              (cons 302 "SA2012..."    )
              )
     );setq
   );if
 (if (= (GM_Xrecord DATA_VARS) nil)
   (progn
     (Alert (strcat "\nThe xRecord < " DATA_VARS " > was not Created"
            "\n"
            "\nThe likely cause, there where no <Values> variables")
        )
     (exit)
     )
   );if
 T
 );defun


;(New_Xrecord "EDATA_VARS") Example Calling line                        ;
;                                                ;
;============================  End < New XRecord > Function ====================================;

Link to comment
Share on other sites

These are limited and may be altered by 3rd part software but may be usefull

 

Useri1 ... Useri5 5 integers

Users1... Users5 5 strings

Userr1 ... Userr5 5 reals

 

personally like above I would now use Xdata

Link to comment
Share on other sites

Below is the code I use, there are several functions and sub-functions all included;

 

1.-Create New Dictionary

2.-Create new xrecord within the created dictionary

3.-Append an xrecord

4.-Read an xrecord

5.-Update an xrecord

 

 

 

 

I ve found something similar in the afralisp website. I tried your code too. However both of them, although I made no changes, give me nil when I execute the (Append_Xrecord "EDATA_VARS" 45 0.0) command. I have run first on a new drawing the (GM_Xrecord "EDATA_VARS") function. Any ideas?

Edited by MJLM
Link to comment
Share on other sites

 

personally like above I would now use Xdata

 

 

yeah but to my knowledge Xdata are always attached to an entity. That I don't find practical for global parameters/settings.

Edited by MJLM
Link to comment
Share on other sites

MJLM I created Xdata no probs that was not attached to any entity using it to store current variables rather than Setvar USER?? also allowed lots more variables. Can post example if you want but it is similar to Snownut version just written a bit different.

Edited by BIGAL
Link to comment
Share on other sites

Ok I may have understood something else. I was actually thinking the associate dxf code -3 of an entity as xdata.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...