Extract a value from a Dynamic block using simple Autolisp code rather than Visual Lisp

Extract a value from a Dynamic block using simple Autolisp code rather than Visual Lisp

peter_thomson
Advocate Advocate
1,112 Views
10 Replies
Message 1 of 11

Extract a value from a Dynamic block using simple Autolisp code rather than Visual Lisp

peter_thomson
Advocate
Advocate

Hi All,

I see that there are a lot of posts for extracting values or properties from a dynamic block, however, all the code I have seen so far is Visual Lisp. Wondering if it is possible to extract a value from a dynamic block using just simple Autolisp functions. The reason is that I would like to re-jig some useful code that extracts attrributes to build a sensible PDF filename when plotting drawings but it can't be VL code as I want to run it as a script using AutoCAD Core Console. My understanding is that Core Console does not accept VL code....at least, there are no VL commands in the commands list for Core Console.

I would appreciate any advice. Just not sure that it is actually possible.

 

The code I am trying to replicate is...

 

;;now get status from separate dynamic block ONLY IF IT EXISTS
(if (/= "" XDYNBLNAME )
(progn
(setq BName XDYNBLNAME)
(setq VLAOBJ (vlax-ename->vla-object (SDynBe Bname)))
(LM:getdynprops VLAOBJ)
(setq XSTATUS XDYNSTATUSNAME)
(setq VSTATUS (LM:getdynpropvalue VLAOBJ XDYNSTATUSNAME))
);p
()
);f
(if (= VSTATUS "FOR TENDER / CONSTRUCTION")
(setq VSTATUS "FOR TENDER AND CONSTRUCTION")
()
)

 

also some Lee Mac functions..

(defun LM:getdynprops ( blk )
(mapcar '(lambda ( x ) (cons (vla-get-propertyname x) (vlax-get x 'value)))
(vlax-invoke blk 'getdynamicblockproperties)
)
)

 

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

 

Many thanks in advance,

Peter T

0 Likes
1,113 Views
10 Replies
Replies (10)
Message 2 of 11

pbejse
Mentor
Mentor

@peter_thomson wrote:

Wondering if it is possible to extract a value from a dynamic block using just simple Autolisp functions.


Look here

 

HTH

0 Likes
Message 3 of 11

peter_thomson
Advocate
Advocate
Thank you, however, there is still some VL code in there.
I will try and replace the VL-remove and the VL-remove-if-not code with simple autolisp. I guess it will be less efficient, but the benefit of being able to run the code in Core Console for plotting will be enormous.

Mant thanks,
Peter
0 Likes
Message 4 of 11

pbejse
Mentor
Mentor

@peter_thomson wrote:
Thank you, however, there is still some VL code in there.

You mean these two?

 

(mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 303 (car x))) elst))
(vl-remove-if-not
	  '(lambda (x) (= (car x) 360))
	  (entget enhancedBlock)
	)

 

 


@peter_thomson wrote:
I will try and replace the VL-remove and the VL-remove-if-not code with simple autolisp. I guess it will be less efficient, but the benefit of being able to run the code in Core Console for plotting will be enormous.


I'm pretty sure you can @peter_thomson , go for it then.

 

Glad it helps

 

BTW: What about taking the .Net approach?

 

0 Likes
Message 5 of 11

peter_thomson
Advocate
Advocate

The DotNet approach?

I have not ever ventured into DotNet and I am too old to start now...

 

Not even sure that I am clever enough to re-write that other code but will have a go anyway.

 

Many thanks.

Peter

0 Likes
Message 6 of 11

pbejse
Mentor
Mentor

@peter_thomson wrote:

Not even sure that I am clever enough to re-write that other code but will have a go anyway.


Let me know if you hit a snag, we'll be here.

 

 

0 Likes
Message 7 of 11

scot-65
Advisor
Advisor
Not sure what property you are looking for?
Have you tried:
DUMPALLPROPERTIES
GETPROPERTYVALUE
SETPROPERTYVALUE

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

Message 8 of 11

Sea-Haven
Mentor
Mentor

There is some info somewhere about load a ARX as part of using accoreconsole. Then VL can be used.

0 Likes
Message 9 of 11

_gile
Consultant
Consultant

@peter_thomson wrote:
Thank you, however, there is still some VL code in there.
I will try and replace the VL-remove and the VL-remove-if-not code with simple autolisp. I guess it will be less efficient, but the benefit of being able to run the code in Core Console for plotting will be enormous.

Mant thanks,
Peter

Are you sure the vl-* functions are not supported by the Core Console? vl functions, unlike vla, vlax or vlr functions, are simple AutoLISP functions.

Anyway, you can use the following function:

(defun gc:massoc (code alst)
  (if (setq alst (member (assoc code alst) alst))
    (cons (cdar alst) (gc:massoc code (cdr alst)))
  )
)

 

and replace:

(mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 303 (car x))) elst))

with

(gc:massoc 303 elst)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 11

_gile
Consultant
Consultant

Assuming you want to get all the dynamic property values of a block reference, you can try the following:

(defun GetDynamicPropertyValues	(blockReference / blockRecord xDictionary enhancedBlock elst param name lst)
  (if (and
	(setq blockRecord (getDynBlockRecord blockReference))
	(setq xDictionary
	       (cdr
		 (assoc	360
			(member '(102 . "{ACAD_XDICTIONARY") blockRecord)
		 )
	       )
	)
	(setq enhancedBlock
	       (cdr
		 (assoc	360
			(member '(3 . "ACAD_ENHANCEDBLOCK") (entget xDictionary))
		 )
	       )
	)
      )
    (foreach prop (gc:massoc 360 (entget enhancedBlock))
      (setq elst  (entget prop)
	    param (cdr (assoc 0 elst))
	    name  (cond
		    ((= param "BLOCKPOINTPARAMETER")
		     (cdr (assoc 303 elst))
		    )
		    ((= param "BLOCKLINEARPARAMETER")
		     (cdr (assoc 305 elst))
		    )
		    ((= param "BLOCKPOLARPARAMETER")
		     (cdr (assoc 305 elst))
		    )
		    ((= param "BLOCKXYPARAMETER")
		     (cdr (assoc 305 elst))
		    )
		    ((= param "BLOCKROTATIONPARAMETER")
		     (cdr (assoc 305 elst))
		    )
		    ((= param "BLOCKFLIPPARAMETER")
		     (cdr (assoc 305 elst))
		    )
		    ((= param "BLOCKVISIBILITYPARAMETER")
		     (cdr (assoc 301 elst))
		    )
		    ((= param "BLOCKLOOKUPPARAMETER")
		     (cdr (assoc 303 elst))
		    )
		    ((= param "BLOCKALIGNMENTPARAMETER")
		     (cdr (assoc 300 elst))
		    )
		  )
      )
      (if name
	(setq lst (cons	(cons name
			      (getpropertyvalue blockReference (strcat "AcDbDynBlockProperty" name))
			)
			lst
		  )
	)
      )
    )
  )
  (reverse lst)
)

(defun getDynBlockRecord (blockRef / elst xDictionary blockRep repData blockRecord block)
  (if (= (cdr (assoc 0 (setq elst (entget blockRef)))) "INSERT")
    (if	(and (setq xDictionary (cdr (assoc 360 (member '(102 . "{ACAD_XDICTIONARY") elst))))
	     (setq blockRep (cdr (assoc 360 (member '(3 . "AcDbBlockRepresentation") (entget xDictionary)))))
	)
      (setq repData	(cdr (assoc 360 (member '(3 . "AcDbRepData") (entget blockRep))))
	    blockRecord	(entget (cdr (assoc 340 (entget repData))))
      )
      (setq block	(entget (tblobjname "block" (cdr (assoc 2 elst))))
	    blockRecord	(entget (cdr (assoc 330 block)))
      )
    )
  )
)

(defun gc:massoc (code alst)
  (if (setq alst (member (assoc code alst) alst))
    (cons (cdar alst) (gc:massoc code (cdr alst)))
  )
)

To get the value of a dynamic property you know the property name, simply use the getpropertyvalue function:

 (getpropertyvalue blockReferenceEname (strcat "AcDbDynBlockProperty" name))


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 11

peter_thomson
Advocate
Advocate

Thank you all for your responses. Will try and get back to my code this week if I can. 

Cheers, Peter

0 Likes