Get block parameters

Get block parameters

Anonymous
Not applicable
3,656 Views
5 Replies
Message 1 of 6

Get block parameters

Anonymous
Not applicable

Hi!

I've a block with diferent parameters such as distances and angles. So I need an eassy way to get and place them into a variable. For instance:

(command "insert" "blockname" point 1 1 0 attribute_list)

And now I have to get its properties and set it in a specific variable:

(setq dist1 (get_properties "Distance1"))
(setq ang1 (get_properties "Angle1"))

Where get_properties function is my unknown function... I'm lost and I've not found anything

Thanks you a lot

0 Likes
Accepted solutions (1)
3,657 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

Since AutoCAD 2012 the easiest way to get (or set) an attribute value is using the getpropertyvalue (or setpropertyvalue ) function.

This function considers the tag of a block attribute as a property of this block.

 

(setq dist1 (getpropertyvalue blockEname "Distance1"))

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6

serag.hassouna
Advocate
Advocate

Why not using the ActiveX approach of block insertion instead of the "INSERT" command ?
this will help you if there's another object created after the block insertion, which will make "entlast" not a best choice to track and fetch data from the inserted block.

what you can do is simply as the following

(vl-load-com) ;Load ActiveX Support
(setq modelspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))) ;get the model space object ;| Assume that the insertion point is declared before in a variable called "inspoint" |; ;| Make sure that there's a previously defined ((block definition)) called "blockname" |; (setq BlockRefobj (vla-insertblock modelspace inspoint "blockname" 1 1 1 0)) ;insert the block reference in the model space & get its vla-object name. ;The three 1s are the Xscale, Yscale & Zscale, and the last 0 is the rotation angle.

or you can harry up and get its entity name with "entlast" before doing anything else.

__________
if you want to get its properties/parameters, it's also very easy as the following

;get a list of all parameters/properties of the dynamic block [Every property is recognized as a vla-object] 
(setq allprops (vlax-safearray->list (vlax-variant-value (vla-getdynamicblockproperties BlockRefObj))))

To get the property name and value from its correspondent vla-object, simply use the functions vla-get-propertyname  and vla-get-value using the following

;| Assume that the vla-object of the property is stored in a variable
called "prop" |;
;| Note that "prop" is a member of "allprops" |; ;To get the property name (setq name (vla-get-propertyname prop)) ;To get the property value (setq val (vlax-variant-value (vla-get-value prop)))

__________
To recap, this makes the function "get-properties" defined as the following

;*Insert the block*
(vl-load-com) ;Load ActiveX support
(setq modelspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))) ;get the model space object
(setq inspoint (vlax-3d-point 0 0 0)) ;the zeroes can be replaced with the actual coordinates
(setq BlockRefObj (vla-insertblock modelspace inspoint "blockname" 1 1 1 0)) ;|insert the block reference in the model space
& get its vla-object name. |;

;*Define the function "get-properties"*
(defun get-properties (propname BRefObj / modelspace inspoint BlockRefobj allprops prop) (setq allprops (vlax-safearray->list (vlax-variant-value (vla-getdynamicblockproperties BRefObj)))) (foreach elem allprops (progn (setq name (vla-get-propertyname elem)) (if (eq name propname) (setq val (vlax-variant-value (vla-get-value elem))) );if );progn [of foreach] );foreach (progn val) ;what the function returns );end defun [get-properties]

;*Execute the function "get-properties"*
(setq dist1 (get-properties "Distance1" BlockRefObj))
(setq ang1 (get-properties "Angle1" BlockRefObj))
0 Likes
Message 4 of 6

_gile
Consultant
Consultant
Accepted solution

@serag.hassouna let me see my probable mistake.

If Distance1 and Angle1 are dynamic properties, you can also use the getpropertyvalue and setpropertyvalue functions:

(setq dist1 (getpropertyvalue blockEname "AcDbDynBlockPropertyDistance1"))
(setq ang1 (getpropertyvalue blockEname "AcDbDynBlockPropertyAngle1"))

 

(setpropertyvalue blockEname "AcDbDynBlockPropertyDistance1" 10.0)
(setpropertyvalue blockEname "AcDbDynBlockPropertyAngle1" (* pi 0.5)) ; angle in radians

  This is, IMO, much more simple than the ActiveX route.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 6

serag.hassouna
Advocate
Advocate

@_gile wrote:
...

This is, IMO, much more simple than the ActiveX route.




It works fine with getpropertyvalue, but setpropertyvalue returns the error (;error: ADS request error)!
If the workaround - in code - for that issue is so simple & small, then definitely they're much simpler than the ActiveX route.

0 Likes
Message 6 of 6

_gile
Consultant
Consultant

setpropertyvalue does work if the argument is a valid value for the specified parameter.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes