Jump to content

How to save data attached to a variable


aloy

Recommended Posts

Hello everybody,

 

How can I save data accumulated by a variable at the end of running a lisp routine and save it along with the drawing to be used at a later session. In my case the data relates to a list of lists (about fifty) each with about twenty pairs of real numbers which again can be considered as lists.

 

Thanking in advance,

 

Aloy

Link to comment
Share on other sites

  • Replies 34
  • Created
  • Last Reply

Top Posters In This Topic

  • aloy

    17

  • BIGAL

    6

  • Roy_043

    4

  • Aftertouch

    4

Hi Lee Mac,

Actually I searched this topic before writing this post. I came across some discussions you had with industry specialists way back in 2009 where setenv and getenv were discussed. Since it did not give me a clue I searched further and came across the dictionary and Xrecords by AfraLisp. I found it too lengthy and thought there would be a ready made answer for the problem I have. Say, just one line of code, just like the solution to one of my problems previously.

Thanks for the reply.

Aloy

Link to comment
Share on other sites

Actually my data is not drawing specific as can be seen from my above description. I understand that a dictionary is a container that holds the data thst relate to various entities in the drawing. A layer has the code 8 and it can be retrieved by its name. What is the code for a variable that does not describe an entity. I will study AfraLisp's tutorial further as the process seem a bit complicates as can be seen from Marco's post.

Link to comment
Share on other sites

Setting up the dictionary should be feasible you could take each list and say make it into a csv text so can be easily pulled apart, just repeat for each inside list like L1 l2 l3 would be data variable name.

 

The code is a bit rough as I have not cleaned it up but shows how you can save a sequence of variables. It uses the afralisps core code. But is easy to understand as it saves some variables.

traffic-dictionary.lsp

Link to comment
Share on other sites

Hi Bigal,

No luck yet. I get the error message as:

; error: bad argument type: stringp ((0.0 10.5) (3.0 7.0) (6.0 6.0) (9.0 5.5) (12.0 7.6) (15.0 4.0) (18.0 6.5) (21.0 5.0) (24.0 2.8) (28.0 3.5))

What is given is my list that I want to save. When I change it to string "myname" it again gives error message to say that incorrect group code 40. But 40, I know is for text. How to solve this problem. I know the easiest way out is the cut and paste method which is manual. (sorry cannot get rid of the smiley which has replace .8 and ') ' )

Thanks,

Aloy

Link to comment
Share on other sites

Had a play with this code but need to look more into Vl-Ldata

 

(setq lst (List (list 0.0 10.5) (list 3.0 7.0) (list 6.0 6.0) (list 9.0 5.5) (list 12.0 7.6) (list 15.0 4.0) (list 18.0 6.5) (list 21.0 5.0) (list 24.0 2.0) (list 28.0 3.5)))
(setq lst (reverse lst)) ; so next bit does from 1st to last.

(defun savepts-dict (lst / y xy pt itemnum)
(if (not get-or-create-Dict)(Load "Dictionary example 2"))

(setq y 1) ; use with item+1
(repeat (setq x (length lst))
(setq pt (nth (setq x (- x 1)) lst))
(setq xy (list (car pt)  (cadr pt) 0.0 ))
(setq itemnum (strcat "Item"  (rtos y 2 0)))
(get-or-make-Xrecord itemnum xy)
(setq y (+ y 1))
)

) ; defun savepts-dict

; for testing
; now do reverse and get points
; need some help here how many in dictionary last point crashes
(setq Y 1)
(setq itemnum (strcat "Item"  (rtos y 2 0)))
(while  (/= nil (getBIGALvars itemnum 0))
(princ "\n")
(setq y (+ y 1))
(setq itemnum (strcat "Item"  (rtos y 2 0)))
)

 

; xdata sample code dictionary example 2
; By Alan H
; sample (setq varx "Item1")(setq valx "2,3")
; (get-or-make-Xrecord varx valx)

(defun get-or-create-Dict (/ adict)
  ;;test if "BIGAL" is already present in the main dictionary
 (if (not (setq adict (dictsearch (namedobjdict) "BIGAL")))
   ;;if not present then create a new one and set the main
   ;; dictionary as owner
   (progn
     (setq adict (entmakex '((0 . "DICTIONARY")(100 . "AcDbDictionary"))))
      ;;if succesfully created, add it to the main dictionary
     (if adict (setq adict (dictadd (namedobjdict) "BIGAL" adict)))
   )
    ;;if present then just return its entity name
   (setq adict (cdr (assoc -1 adict)))
 )
)

(defun get-or-make-Xrecord (varx valx / adict anXrec)
 (cond
    ;;first get our dictionary. Notice that "BIGAL" will be
   ;;created here in case it doesn't exist
   ((setq adict (get-or-create-Dict))
     (cond
       ;;if "BIGAL" is now valid then look for "BIGALVARS" Xrecord 
      ((not (setq anXrec (dictsearch adict varx))) 
;the variable BIGALvars is name of xrecord so need to be a variable to add lots
       ;;if "BIGALVARS" was not found then create it
       ;(setq anXrec (entmakex '((0 . "XRECORD")
       (setq anXrec (entmakex (list (cons 0  "XRECORD")
                    (cons 100  "AcDbXrecord")
                    (cons 1 varx)
                    (cons 10 valx)
                              )
                    )
       )
       ;;if creation succeeded then add it to our dictionary
       (if anXrec (setq anXrec (dictadd adict varx anXrec)))
      )
      ;;if it's already present then just return its entity name
      (setq anXrec
       (cdr (assoc -1 (dictsearch adict varx)))
      )
    )
   )
 )
)

; sample (getBIGALvars "Item1" 0)
(defun getBIGALvars (varx valx / vars varlist)
 ;;retrieve XRecord "varx" from dictionary "BIGAL"
 ;;which in turn calls both functions above
 (setq vars (get-or-make-Xrecord varx valx))
  ;;if our Xrecord is found, then get values in group code 
 (cond (vars
        (setq varlist  (entget vars))
        (setq Itemx (cdr (assoc 1 varlist)))
        (setq Itemans (cdr (assoc 10 varlist)))
       )
       ;;otherwise return nil
       (T nil)
 )
)

Link to comment
Share on other sites

After all of the above code try this 2 line answer.

 

(vlax-ldata-put "BIGAL" "PTS" lst)

 

(setq pts (vlax-ldata-get "BIGAL" "PTS"))

 

 ; just for reference if you have multiple entries under say "BIGAL" multiple pt sets. PTS1 PTS2 etc
(vlax-ldata-list "BIGAL" )

Link to comment
Share on other sites

I will check did have that during testing may have posted incorrect version very subtle changes to dxf codes, but use the second post 1 line does the same as about 20 lines of code. I know which I will use in future.

Link to comment
Share on other sites

Roy_043,

The list I have given in my previous post is a typical one to be generated by my program which makes use of large number of 3D points in the drawing. The 3D points are used to make a TIN surface as in Civil 3D. At the end I have a list of such lists. The dictionary I believe is autocad's contraption to save the the entities in the drawing which has DXF codes for each, but there is no way of keeping Xrecord for variable other than strings. This is what I am trying to solve with Bigal. Of course I can cut and paste to a new file. I need to save it and reuse over and over again with revised vertical alignments to generate cross section.

Sorry you may not get it if you are not familiar with road design.

Regards,

Link to comment
Share on other sites

The dictionary I believe is autocad's contraption to save the the entities in the drawing which has DXF codes for each, but there is no way of keeping Xrecord for variable other than strings.

 

I would suggest that you study Dictionaries in more detail...

Link to comment
Share on other sites

I also use dictionaries for both strings and ints.

 

(defun GetOrAddDict ( parentDict dictName )
(cond ((cdr (assoc -1 (dictsearch parentDict dictName))))((dictadd parentDict dictName (entmakex '(   (0 . "DICTIONARY")(100 . "AcDbDictionary"))))))
)

(defun AddOrReplaceXrec ( parentDict xrecName xrecData / xrec )
(if (setq xrec (dictsearch parentDict xrecName))
(entdel (cdr (assoc -1 xrec))))
(dictadd parentDict xrecName (entmakex (append '((0 . "XRECORD")(100 . "AcDbXrecord")) xrecData)))
)

(defun GetXrec ( xrec parentDict / dict)
(setq dict (GetOrAddDict (namedobjdict) parentDict))
(setq data (dictsearch dict xrec )) ; Entity list.
(setq data (cdr (member (assoc 280 data) data)))
(setq data (cdr (assoc 1 data)))
)

(defun BuildDictionary ( / dict1 dictfase fasecounter ribbonvalue )
(if (not (setq dict (dictsearch (namedobjdict) "YOUR_DICT")))
	(progn
		(setq dict (GetOrAddDict (namedobjdict) "YOUR_DICT"))
			(AddOrReplaceXrec dict "SETTINGS" (list (cons 1 "SOMETHING")))
	)
	(progn
		(princ "Drawing has a dictionary.")
	)
)
)

(BuildDictionary)

 

I stored a variable named SETTINGS in 'YOUR DICT")

 

Command: (getxrec "settings" "your_dict")
"SOMETHING"

Link to comment
Share on other sites

Hi, Aftertouch,

Yes, it would return "SOMETHING" when tested, however does not work with my list. I do not know which group code to use with the list.

 

Regards,

Aloy.

Link to comment
Share on other sites

Hi, Lee Mac,

Had a look at the code given in the 'SWAMP'. Yes the sample test given with code returns the entity lists of the two sub-dictioneries and the two predefined xrecords titled "KOOL". It doesn't seems to give a way out as to how to attache characters and spaces involved in my list with a code. Hence I am still at square number one.

 

Regards,

 

Aloy

Link to comment
Share on other sites

@ Lee Mac,

Correct, thats most of the source code.

Also found the GetXRec somewhere else, dunno where anymore.

 

@ Aloy

The groupcode doesnt matter at all.

You can use any number for that,

 

i always make many XRecords, with only a groupcode 1, and the content.

 

Code below is just a quick example. Just to give your the idea.

 

(AddOrReplaceXrec dict "Variable1" (list (cons 1 "2")))

(AddOrReplaceXrec dict "Variable2" (list (cons 1 "0")))

(AddOrReplaceXrec dict "Variable3" (list (cons 1 "1")))

(AddOrReplaceXrec dict "Variable4" (list (cons 1 "3")))

 

(setvar "LTSCALE" (GetXrec "YOUR_DICT" "Variable1"))

Link to comment
Share on other sites

Aloy did you not look at this one line solution the lst I used for testing was your LST

 

; saves a list
(setq lst (List (list 0.0 10.5) (list 3.0 7.0) (list 6.0 6.0) (list 9.0 5.5) (list 12.0 7.6) (list 15.0 4.0) (list 18.0 6.5) (list 21.0 5.0) (list 24.0 2.0) (list 28.0 3.5)))
(setq lst (reverse lst)) ; so next bit does from 1st to last.
(vlax-ldata-put "ALOY" "PTS" lst)

 

; saves lst2
(vlax-ldata-put "ALOY" "PTS2" LST2)

 

; get a list
(setq lst (vlax-ldata-get "ALOY" LST2 ))

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...