Jump to content

Dynamic blocks, attribute names and values


Grrr

Recommended Posts

Hello guys, I'm working on some heavily modified lisp routine (original is from Lee Mac), and I'm trying to set quotes for effective blockname, and a number of attribute names with their values with lisp.

I know that theres LM's subfunciton to get attributes and it returns assoc list with:

((<Tag> . <Value>) ... )

I would prefer this, if someone could give me example how to set a quote for each Tag and Value. For example if I select a dynamic block to get a prompt of its effective name, and a list like:

"ATTname N" has a value "ATTvalue N"

 

Anyway, heres my unsuccessful try:

;Trying to obtain effective blockname, a attribute name and its attribute value
;I am going to use "foreach" function, and different conditions, based on that attribute value

(defun c:test ( / i o s )
(setq MyBlockName (getstring T "\nType the name of the Block to look for:"))
(setq MyAttributeName1 (getstring T "\nType the name of the Attribute1 to look for:"))
(setq MyAttributeName2 (getstring T "\nType the name of the Attribute2 to look for:"))
   (if (setq s (ssget "_X" '((0 . "INSERT") (66 . 1) (2 . "`*U*,MyBlockName"))))
       (repeat (setq i (sslength s))
           (setq o (vlax-ename->vla-object (ssname s (setq i (1- i)))))
           (if (and (vlax-property-available-p o 'effectivename)
                    (= MyBlockName (strcase (vla-get-effectivename o) t))
               )
               (progn
                   (princ (strcat "\nFound " MyBlockName " block "" (vla-get-handle o) "\"."))
                   (if (setq MyAttributeValue1 (LM:vl-getattributevalue o MyAttributeName1))
                       (progn
                          (princ (strcat "\nFound the block named " MyBlockName " !"))
                          (princ (strcat "\nFound the attribute named " MyAttributeName1 " !"))
                          (princ (strcat "\nThe value of that attribute is " MyAttributeValue1 " !"))
                       );progn
                       (princ "\n\"MyAttributeName1\" attribute not found in \"MyBlockName\" block.")
                   );if
                   (if (setq MyAttributeValue2 (LM:vl-getattributevalue o MyAttributeName2))
                       (progn
                          (princ (strcat "\nFound the block named " MyBlockName " !"))
                          (princ (strcat "\nFound the attribute named " MyAttributeName2 " !"))
                          (princ (strcat "\nThe value of that attribute is " MyAttributeValue2 " !"))
                       );progn
                       (princ "\n\"MyAttributeName2\" attribute not found in \"MyBlockName\" block.")
                   );if
               )
           )
       )
       (princ "\nNo attributed " MyBlockName " blocks found in the drawing.")
   )
   (princ)
)
(vl-load-com) (princ)


;; Get Attribute Value  -  Lee Mac
;; Returns the value held by the specified tag within the supplied block, if present.
;; blk - [vla] VLA Block Reference Object
;; tag - [str] Attribute TagString
;; Returns: [str] Attribute value, else nil if tag is not found.

(defun LM:vl-getattributevalue ( blk tag )
   (setq tag (strcase tag))
   (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att)))
       (vlax-invoke blk 'getattributes)
   )
)

Which should return this for each attribute:

(princ (strcat "\nFound the block named " MyBlockName " !"))
(princ (strcat "\nFound the attribute named " MyAttributeName2 " !"))
(princ (strcat "\nThe value of that attribute is " MyAttributeValue2 " !"))

and I get this error, no idea why:

Type the name of the Block to look for:VLD_Column
Type the name of the Attribute1 to look for:L1
Type the name of the Attribute2 to look for:L2
Error: bad argument type: FILE "VLD_Column"

Link to comment
Share on other sites

Some corrections:

;Trying to obtain effective blockname, a attribute name and its attribute value
;I am going to use "foreach" function, and different conditions, based on that attribute value

(defun c:test ( / i o s MyBlockName MyAttributeName1 MyAttributeName2 )
   (setq MyBlockName (getstring T "\nType the name of the Block to look for:"))
   (setq MyAttributeName1 (getstring T "\nType the name of the Attribute1 to look for:"))
   (setq MyAttributeName2 (getstring T "\nType the name of the Attribute2 to look for:"))
   (if (setq s (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (cons 2 (strcat "`*U*," MyBlockName)))))
       (repeat (setq i (sslength s))
           (setq o (vlax-ename->vla-object (ssname s (setq i (1- i)))))
           (if (and (vlax-property-available-p o 'effectivename)
                    (= (strcase MyBlockName) (strcase (vla-get-effectivename o) t))
               )
               (progn
                   (princ (strcat "\nFound " MyBlockName " block \"" (vla-get-handle o) "\"."))
                   (if (setq MyAttributeValue1 (LM:vl-getattributevalue o MyAttributeName1))
                       (progn
                          (princ (strcat "\nFound the block named " MyBlockName " !"))
                          (princ (strcat "\nFound the attribute named " MyAttributeName1 " !"))
                          (princ (strcat "\nThe value of that attribute is " MyAttributeValue1 " !"))
                       );progn
                       (princ (strcat "\n\"" MyAttributeName1 "\" attribute not found in \"" MyBlockName "\" block."))
                   );if
                   (if (setq MyAttributeValue2 (LM:vl-getattributevalue o MyAttributeName2))
                       (progn
                          (princ (strcat "\nFound the block named " MyBlockName " !"))
                          (princ (strcat "\nFound the attribute named " MyAttributeName2 " !"))
                          (princ (strcat "\nThe value of that attribute is " MyAttributeValue2 " !"))
                       );progn
                       (princ (strcat "\n\"" MyAttributeName2 "\" attribute not found in \"" MyBlockName "\" block."))
                   );if
               )
           )
       )
       (princ (strcat "\nNo attributed " MyBlockName " blocks found in the drawing."))
   )
   (princ)
)
(vl-load-com) (princ)


;; Get Attribute Value  -  Lee Mac
;; Returns the value held by the specified tag within the supplied block, if present.
;; blk - [vla] VLA Block Reference Object
;; tag - [str] Attribute TagString
;; Returns: [str] Attribute value, else nil if tag is not found.

(defun LM:vl-getattributevalue ( blk tag )
   (setq tag (strcase tag))
   (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att)))
       (vlax-invoke blk 'getattributes)
   )
)

Link to comment
Share on other sites

Thank you Lee, quickly noticed!

I might have mistaken parenthesis somewhere, since I get:

No attributed VLD_Column blocks found in the drawing.

But without the desired result I can't be sure that it would be fully functioning.

 

I hope you don't mind for giving an example on how to return in separate "setq"s effective bname, attname and attvalue

Link to comment
Share on other sites

This is not an error but would imply that the selection set is empty; are you testing the program with the relevant attributed dynamic blocks present in your drawing?

Link to comment
Share on other sites

Lee,

Sure I have the current dynamic block in my drawing.

Perhaps I'm misunderstanding the definition of "attribute", the attributes L1 and L2 I was looking for, are actually "action parameters" for the block.

 

I have a little question, because I still can't figure it out by myself (and obviously you are one of the less people on the internet who would know the answer of this):

When you select a dynamic block, in the quick properties menu are listed all of its "parameters"

But do they all count as attributes? (like: block table, visibility state, flip state) or attributes are the only one invoked by ATTDEF ?

Link to comment
Share on other sites

Success!

 

block-return-parameter-test.jpg

 

Now only if I could window select all the blocks with that name and get every individual parameter. (I know about "foreach" and "LM:effectivename" functions, but I don't know how to use them)

;Returns Parameter
(defun c:test ( / ent idx sel paramval MyParameterName )
   (setq MyParameterName (getstring T "\nType the name of the Parameter to look for:"))
   (prompt "\nSelect dynamic block")
   (if (setq sel (ssget "_+.:E:S" '((0 . "INSERT"))))
       (repeat (setq idx (sslength sel))
           (setq ent (ssname sel (setq idx (1- idx))))
           (if (setq paramval (LM:getdynpropvalue (vlax-ename->vla-object ent) MyParameterName))
               (princ (strcat "\nBname:" (cdr (assoc 2 (entget ent))) "    ParamName:" MyParameterName "    ParamValue:" paramval ))
               (princ (strcat "\nParameter " MyParameterName " not found in block " (cdr (assoc 2 (entget ent)))))
           )
       )
   )
   (princ)
)

;; Get Dynamic Block Property Value  -  Lee Mac
;; Returns the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)

(defun LM:getdynpropvalue ( blk prp )
   (setq prp (strcase prp))
   (vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'value)))
       (vlax-invoke blk 'getdynamicblockproperties)
   )
)



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