Jump to content

Retrieving attribute values


adbuckn2

Recommended Posts

So, here's the problem. I have been working on a lisp routine since July, and am rapidly approaching the finish line. At this point, I'm trying to make small improvements to make the program easier to use.

 

My last major hurdle is dealing with the local variables that are erased whenever a file is closed. I'm trying to create a block that will be inserted at the end of each iteration of the lisp routine. When inserted, the block will fill roughly a dozen individual attributes. The idea is that when the lisp routine is run it will:

 

1.) Look for this block.

 

2.) Read the attribute values contained in the block.

 

3.) Set a group of variables to the values of the attributes.

 

While I have seen many sites and threads devoted to setting attributes to a variable, I have not seen any that go in the reverse order (provided that the attributes actually have a value, of course). Any thoughts, suggestions? I sincerely doubt that anyone wants to thumb through 100 pages of text, but I do have a small file to practice with, if anyone is curious.

Link to comment
Share on other sites

Hi

 

Just a simple example for you to consider the way to retrieve the attributes values along with the tags strings:

(defun Group:tags:values (ent / get lst)
 ;; ent = Attributed Block entity name.	;;
   (while (/= (cdr (assoc 0 (setq get (entget (setq ent (entnext ent)))))) "SEQEND")
     (setq lst (cons (list (cdr (assoc 2 get)) (cdr (assoc 1 get))) lst))
     )
 lst
)
;;				;;
(if (setq att (car (entsel "\nSelect attributed block :")))
 (Group:tags:values att)
 )

Link to comment
Share on other sites

Why not look at your own dictionary its not that hard and gets saved in the dwg. Its almost same as the block has a name descriptions and a values. Now where did I put that code put it aside for a full rewrite of some code. Other way a bit limited is USERI USERR 1-5 you can store stuff in these variables (getvar 'useri1) an integer value Only problem is if some one else writes to these variables.

 

http://www.afralisp.net/autolisp/tutorials/dictionaries-and-xrecords.php

 

; xdata sample code dictionary example 2
; By Alan H

; Original code and example frpm Afralisp

; 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)
 )
)

 

To run only need a couple of lines of code, sample use in code above

(if (not get-or-create-Dict)(load "mydictionary"))
(getBIGALvars var1 "asdf")
(getBIGALvars var2 27.35)

Link to comment
Share on other sites

Thank you Tharwat! That was almost exactly what I was looking for. I do however have one more request. I would like the routine to select the block in question without stopping for user input. I have tried every combination of entsel, entname, and entlast that I can think of, but to no avail. I’m sure that I don’t fully understand these function enough to use them properly. A typical return when I use the functions would read:

 

; error: bad argument type: stringp

 

An example of the code used is:

 

(command "-insert" (strcat DESTINATION "DPVS") WVI 1 1 0 RCOUNT JNUM PNUM PTYPE PLENGTH INTSKN EXTSKN PTHICK NSF FLR_CON EACCESS CLG_CON NO_WOOD)

 

(if (setq att (cdr (entlast)))

 

(setq VALHALLA (GTV att))

 

)

 

Any tips or hints would be greatly appreciated.

Link to comment
Share on other sites

Hi,

 

If the block is not a Dynamic then you can select it via the ssget function as follows and change the name of the block that suits yours:

 

(setq s (ssget "_X" (list '(0 . "INSERT")'(2 . "MyBlockName") (cons 410 (getvar 'CTAB)))))

But if your block is Dynamic then you need to iterate a bit through the selection set to catch it as follows:

 

(setq Blk "MyBlockName")

(if (setq i -1 s (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," Blk)) (cons 410 (getvar 'CTAB)))))
 (while (and (setq sn (ssname s (setq i (1+ i))))
             (not (= (vla-get-effectivename (vlax-ename->vla-object sn)) Blk))
             )
   )
 )

In the first example you would have the selection set assigned to the variable 's' if found then in the second example and of course if your block is Dynamic then you would have your block entity name assigned to the variable 'sn'

 

Hope this helps.

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