Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HELP WITH INSERTING DYNAMIC BLOCK?

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
caddnima
1269 Views, 16 Replies

HELP WITH INSERTING DYNAMIC BLOCK?

Hello to all,

 

I have been a long time lurker but first time poster. I need help with Dynamic Block. I cannot wrap my head about VLISP thats why I am here hoping that maybe someone can point me to the right direction.

 

Please see attached file. Inside the file you will find the dynamic block in question.

 

Problem : 

1) I dont know how to disect the dynamic block. If its a regular entity or block, we can simply check the dxf group and simply edit it.  But with dynamic block, I cannot find the DXF group to edit.

2) I think I can say Im pretty good (not expert) with LISP but beyond that, Im totally DUMB. I dont know anything about VLISP and beyond.

3) When someone says dynamic block state, im totally clueless. Then there is this FLIPPED? If someone will check the dynamic block, there is the FLIP, then the OPTION then the TAIL LENGTH.

 

Conditions : This routine needs to happen fully automatic, no user intervention, so no dialog boxes, no prompts for what OPTION to use. Everything will be controlled with conditions and ifs.

 

History/Process :

 

 

The drawings will be coming off/export from REVIT. 

The dwg will contain regular blocks. Ex Elevation callout, Section Callout, Detail Callout.

 

The routine will do....

a) Open the dwg file. (I can do this)

b) Look for certain (non-dynamic) blocks, ie: elevation, section, callouts...(I can do this)

 

c) If the block is elevation block, then insert dynamic block, and then change the option to elevation. (need help on this)

 

I believe that If someone can point me or help me do one task like the one above, I can do the rest.

 

I tried several LISP, VLISP functions by LEE MAC from his websites but Im totally clueless on how to invoke them.

This is the first time i see a function other than c:.  Lee's functions are all with LM:. I dont know where to begin.

 

I hope I was clear explaining the situation and I know this subject have been asked so many times in this forum. 

I just thought that it would be easier to ask and create a new thread instead of trolling and wasting time figuring why I cant understand a thing about it.

 

Thank you in advance.

 

CT

 

 

 

16 REPLIES 16
Message 2 of 17
pbejse
in reply to: caddnima


@caddnima wrote:

Hello to all,

 

The drawings will be coming off/export from REVIT. 

The dwg will contain regular blocks. Ex Elevation callout, Section Callout, Detail Callout.

 

The routine will do....

a) Open the dwg file. (I can do this)

b) Look for certain (non-dynamic) blocks, ie: elevation, section, callouts...(I can do this)

 

c) If the block is elevation block, then insert dynamic block, and then change the option to elevation. (need help on this)

 

Thank you in advance.

 

CT 

 


Show us what you have  so far CT and we'll pick up from there.

 

Message 3 of 17
aliensinearth
in reply to: caddnima

Spoiler
CopyPaste the below code or download my attachment and run.
Spoiler
Give me the feedback if it solved your purpose or not.

 

(defun c:IDB (/ os pt vlae prop prop_list x)
  
  (command "ucs" "w")
  (setq os (getvar 'osmode))
  (setvar "attreq" 0)
  (setvar "osmode" 0)
  
  (setq pt (getpoint "\nPick Insertion point : "))

  (command "_.Insert" "GS-ViewCallout" pt "1" "" "0")

  (setq vlae	  (vlax-ename->vla-object (entlast))
	prop	  (vla-getdynamicblockproperties vlae)
	prop	  (vlax-variant-value prop)
	prop_list (vlax-safearray->list prop)
  )

  (foreach x prop_list
	    (if	(= (vla-get-propertyname x) "OPTION")
	      (vla-put-value x "Elevation") ;; You can change here for another option
	    )
  )

  (command "ucs" "p")
  (setvar "osmode" os)
  (setvar "attreq" 1)
  (princ)
)

 

Message 4 of 17
pbejse
in reply to: caddnima


@caddnima wrote:

 

Conditions : This routine needs to happen fully automatic, no user intervention, so no dialog boxes, no prompts for what OPTION to use. Everything will be controlled with conditions and ifs.

 

 

c) If the block is elevation block, then insert dynamic block, and then change the option to elevation. (need help on this)

 

 


What will be the insertion point for the block?

Attribute Value?

 

 

EDIT:

(defun c:demo (/ ref blk e state)
  (if
    (and (setq ref (vl-some '(lambda (nme)
			       (if (ssget "_X"
					  (list	'(0 . "INSERT")
						'(410 . "MODEL")
						(cons 2 nme)
					  )
				   )
				 nme
			       )
			     )
			    '("Ref_Elevation" "Ref_Section");<--- change the name to match your target block names
		   )
	 )				;<--- your block names
	 (tblsearch "BLOCK" "GS-ViewCallout")
	 (setq state (cadr (assoc ref
				  '(("Ref_Elevation" "Elevation");<-- the second elemet is the equivalent vis state per block
				    ("Ref_Section" "Section Cut")
				   )
			   )
		     )
	 )
;;; You can remove this line if you have a specific spot for insertion point 	;;;
	 (setq p (getpoint "\nPick Insertion Point:"))
;;;	 									;;;
    )
     (progn
       (setq blk (vlax-invoke
		   (vlax-get (vla-get-ActiveLayout
			       (vla-get-activedocument
				 (vlax-get-acad-object)
			       )
			     )
			     'Block
		   )
		   'InsertBlock
		   p
		   "GS-ViewCallout"
		   1
		   1
		   1
		   0
		 )
       )
       (if (and
	     (LM:getvisibilityparametername blk)
	     (/= state (LM:GETVISIBILITYSTATE blk))
	   )
	 (progn
	   (LM:SetVisibilityState blk state)
;;;	This is the spot where you will assign the attribute value	;;;
	   (foreach itm	(vlax-invoke blk 'GetAttributes)
	     (if (setq v
			(assoc (vla-get-tagstring itm)
			       '(("VIEW_ID" "Value") ("FROM_DWG#" "ThatDwg"))
			)
		 )
	       (vla-put-textstring itm (cadr v))
	     )
	   )
;;;									;;;
	 )
       )
     )
     (princ "\nIncomplete Data")
  )
  (princ)
)

 

You need to download the following functions from Lee Macs website

 

LM:getvisibilityparametername
LM:SetVisibilityState
LM:GETDYNPROPVALUE
LM:GETDYNPROPALLOWEDVALUES
LM:SETDYNPROPVALUE

 

HTH

 

 

Message 5 of 17
bhull1985
in reply to: pbejse

An alternate, credits to CAB @ theswamp.org

This would just handle the dyn block insertion, you'd need to incorporate this into your larger routine or frankly, just use pbejses 🙂

But for if any reason you need an alternate, here's one....

 

;;; Function to insert block with Dynamic Parameters Set
(vl-load-com) ;Load VLisp extensions
;;; Use example:
;;;   (InsDynBlock "door-s" '(("Angle" . 120.0) ("Visibility" . "D")))
;;;
(defun InsDynBlock (bName params / en eo paramCol p v)
  (command "_.INSERT" bName) ;Start Insert command
  (while (> (getvar "CMDACTIVE") 0) (command pause)) ;Let user pick points, scale, rotation
  ;; Check if no error occured, e.g. wrong block
  (if (and (setq en (entlast)) ;Get last entity
           (setq eo (vlax-ename->vla-object en)) ;Convert to COM object
           (= (vla-get-ObjectName eo) "AcDbBlockReference") ;Check if block
           (= (strcase (vla-get-EffectiveName eo)) (strcase bName)) ;Correct block
           (setq paramCol (vla-GetDynamicBlockProperties eo)) ;Get the parameters
           (setq paramCol (vlax-variant-value paramCol)) ;Convert variant to SafeArray
           (>= (vlax-safearray-get-u-bound paramCol 1) 0) ;Check if there are parameters
           (setq paramCol (vlax-safearray->list paramCol)) ;Convert to list
      ) ;_ end of and
    (progn
      (foreach p paramCol ;Step through list
        ;; Check if current parameter is to be set
        (if (setq v (assoc (vla-get-PropertyName p) params)) ;Get the value
          (cond
            ((= (vla-get-UnitsType p) 0) ;String
             (vla-put-Value p (vlax-make-variant (cdr v) vlax-vbString)) ;Set the value
            )
            ((= (vla-get-UnitsType p) 1) ;Angle
             (vla-put-Value p (vlax-make-variant (* (/ (cdr v) 180.0) pi) vlax-vbDouble)) ;Set the value
            )
            ((= (vla-get-UnitsType p) 1) ;Distance
             (vla-put-Value p (vlax-make-variant (cdr v) vlax-vbDouble)) ;Set the value
            )
          ) ;_ end of cond
        ) ;_ end of if
        (vlax-release-object p) ;Release the COM object handle
      ) ;_ end of foreach
      (vlax-release-object eo) ;Release the COM object handle
    ) ;_ end of progn
    (princ "\nSome error occured, could not insert or set parameters.")
  ) ;_ end of if
  (princ)
) ;_ end of defun

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 6 of 17
caddnima
in reply to: caddnima

Thank you all for the help.  I will begin by disecting all your routines starting from the top and down.

 

I will start with aliensinearth. Looks simple enough. I will copy and paste it and report back.

 

CT

Message 7 of 17
caddnima
in reply to: caddnima

@aliensinearth,

I dont know why your code is so simple, but it worked.
Message 8 of 17
caddnima
in reply to: caddnima

This is for me so i can review this in the future, but please feel free to correct me.

 

Lets disect it line by line.

 

(command "_.Insert" "GS-ViewCallout" pt "1" "" "0")

 What is "_.Insert". what I mean is, what is the "_." does?

 

 

 

(setq vlae	  (vlax-ename->vla-object (entlast))
	prop	  (vla-getdynamicblockproperties vlae)
	prop	  (vlax-variant-value prop)
	prop_list (vlax-safearray->list prop)
  )

 So bottom line is we need to find the "prop_list"

 

 

(foreach x prop_list
	    (if	(= (vla-get-propertyname x) "OPTION")
	      ;(vla-put-value x "Elevation") ;; You can change here for another option
		  (vla-put-value x "Section Cut")
	    )
  )

 Then we have to look for each entry inside that list and see if there is a property name "OPTION"

All this time, I have been trying to put the value of OPTION which is ie: Elevation. totally wrong in my part, thats why it wont work.

 

 Then we have to assign a new value in "vla-put-value" to what ever we want it to be, ie: "Section Cut"

 

I think I got it?

 

Thank you 

 

CT

 

Message 9 of 17
pbejse
in reply to: caddnima


@caddnima wrote:

Thank you all for the help.  I will begin by disecting all your routines starting from the top and down.

 

CT


Reason the code i posted doesnt look as "simple" as the rest is the code incorporated catching the current "(non-dynamic) block" status and the capablity to fill up the attribute values. Notice that nowhere in the code you will see a test for a specifc parameter name. <Option> . Since you mentioned on your first post that  <<<I tried several LISP, VLISP functions by LEE MAC from his websites but Im totally clueless on how to invoke them.....I dont know where to begin.>>>  Hence the posted code

 

Hope somebody else can find a use for this code. 

 

Message 10 of 17
aliensinearth
in reply to: caddnima

//So bottom line is we need to find the "prop_list"//

Yes. Exactly.
Now you got it. you can play around in Dynamic Blocks like this.


As pbejse has mentioned. I haven't included any error checking code. Since you know to write the program, please include error checking codes before giving to others.

 

//What is "_.Insert". what I mean is, what is the "_." does?//

 

"_" means, Irrespective of any local language, Autocad will override to English (for commands).

"." means, Even if you have Undefined the "Insert" command, adding "." before the command name will make command work.

Message 11 of 17
aliensinearth
in reply to: pbejse

Hi pbejse,

 

Please don't feel Unhappy. You are a LISP Genius. You code is like doctorate level code. Only very few people in this world like you, Lee_Mac, Bhull1985 can write such a program.

With my small knowlege, I just guided caddnima with my simple program for the newbies to ActiveX (VLAX).

 

Your code is very very useful. Thank you.

 

Regards,

Alien

Message 12 of 17
aliensinearth
in reply to: caddnima

Thanks savinirsb4u and caddnima for giving Kudos.
Message 13 of 17
pbejse
in reply to: aliensinearth


@aliensinearth wrote:

Hi pbejse,

 

Please don't feel Unhappy. ....

 

Your code is very very useful. Thank you.

 

Regards,

Alien


Smiley LOL Its nothing like that Alien, I'm just surprised the OP did not see the beauty of the approach , doing away with trying to figure out the name of the visibility state parameter name among other things.

 

Thank you for the kind words dude Smiley Happy

 

Keep on coding.

Message 14 of 17
bhull1985
in reply to: aliensinearth


aliensinearth wrote:
Spoiler
CopyPaste the below code or download my attachment and run.
Spoiler
Give me the feedback if it solved your purpose or not.

 

 

(defun c:IDB (/ os pt vlae prop prop_list x)                                 ;define function 0 args 6 localized vars

(command "ucs" "w")                                                                ;set ucs to world

(setq os (getvar 'osmode))                                                         ;set var os to user osnap settings

(setvar "attreq" 0)                                                                      ;turn off command-line attribute filling

(setvar "osmode" 0)                                                                   ;turn off osnaps

(setq pt (getpoint "\nPick Insertion point : "))                             ;select insertionpoint and set to variable pt

(command "_.Insert" "GS-ViewCallout" pt "1" "" "0")                ;invoke insert command gs-viewcallout @ pt 1 x/y scale 0 rotation

(setq vlae   (vlax-ename->vla-object (entlast))                             ;convert last entity to vla object, assign to var vlae

        prop   (vla-getdynamicblockproperties vlae)                       ;assign var prop to dynamicblockproperties for vlae

        prop   (vlax-variant-value prop)                                          ;assign prop variant value to var prop

        prop_list (vlax-safearray->list prop)   )                                ;convert prop to a list and assign to prop_list

 

(foreach x prop_list                                                                    ;for each property in the property list

   (if (= (vla-get-propertyname x) "OPTION")                               ;cycle through until finding the option property

(vla-put-value x "Elevation")                                                       ;set option to elevation

     )   )

 

  (command "ucs" "p")                                                               ;restore ucs to previous

(setvar "osmode" os)                                                                  ;restore osmode to saved user settings

(setvar "attreq" 1)                                                                      ;restore command line attribute prompts

(princ)                                                                                        ;load this program cleanly

)           


 HTH

 

Also, to lump me into the same group as pbejse and lee mac is high praise but i can't accept that compliment, nor can I accept the credits for that code. i merely facilitate others needs as i increase my own understanding for my companies needs. Nevertheless, it's always good to spend time with code, *any* code, seriously, even if it's dealing with something completely not needed for what you do----the coding processes and functions used are invaluable knowledge to eventually learn if wanting to be able to code on your own- as such, i've spent a couple minutes to decipher aliensinearth's code fully so you or anyone else can see exactly what's occuring.

 

 

..

...

.....dang look at that sexy formatting! Total luck.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 15 of 17
caddnima
in reply to: aliensinearth

Hello and good morning pbejse,

 

My apology if I was misunderstood.  As i mentioned before, Im going to start from the top going to the bottom. And I saw your very detailed code. I just want to understand and learn as much on the first code before I get to yours which is the next code. 

 

And pbejse, get ready, because I am going to ask you a million question if thats ok with you or anybody reading this thread. Like I said, Im a total newbie in terms of VLISP and beyond. I dont even know if the code is VLISP or .NET or whatever.  As soon as I notice that its outside VANILLA LISP then im lost.

 

And based on Aliensinearth response, that I did understand his code. So that boils down to the next code which is yours. Are you ready?

 

Let me copy and paste your code and see what i can make of it.

 

Where are you guys located. for some reason all your responses came to me when I was sleeping.... And all this time I thought that my thread have been abandoned.

 

Im located West Coast, Pacific Time.

 

CT

 

 

Message 16 of 17
caddnima
in reply to: caddnima

Ok, here it goes PBEJSE.

 

Based on your demo code, you created an IF statement. And it says.... 

IF the AND is true then run the PROGN and if not then just do PRINC....  

 

haha I get that far. Lets continue...

 

Lets dig in deeper on the AND... inside the AND, you created 4 tasks or conditions.

1) setq ref

2) tblsearch

3) setq state

4) setq p

 

No1. - This is the first time I encountered VL-SOME. See Question1.

No2. - I understand this. Look for the block name.

No3. - Looks for assoc in REF. See Question2.

No4. - I am surprised, or did not know that you can insert a setq inside an if condition. This one ask for a point.

 

Question1.

1A. What does VL-SOME do? is it like an IF then OR?

1B. When I ran the lambda code, it gives me... #<SUBR @0000000032e9b7c8 -lambda->, is this normal?

(lambda (nme)
	(if (ssget "_X"
		(list	'(0 . "INSERT")
			'(410 . "MODEL")
		(cons 2 nme)
		)
	)
	nme
	)
)

 

Question2.

2A. When I ran this code, it only gives me ELEVATION. What happens to SECTION CUT? Is this normal?

(setq state 
	(cadr 
		(assoc ref
			'(
				("GS-ViewCallout" "Elevation");<-- the second element is the equivalent vis state per block
				("GS-ViewCallout" "Section Cut")
			)
		)
	)
)

 

Question3.

I assume that this AND conditions need to be TRUE on all 4 counts? and if one of them is NIL, then the if condition will go PRINC?

 

 

Thank you in advance.

 

CT

Message 17 of 17
pbejse
in reply to: caddnima


@caddnima wrote:

 

Question1.

1A. What does VL-SOME do? is it like an IF then OR?

 


Vl-some is just a fancy way of doing while function with an option to termiate the evaluation when the desired condition is met. 

 

From the help file

The vl-some function passes the first element of each supplied list as an argument to the test function, then the next element from each list, and so on.
Evaluation stops as soon as the predicate function returns a non-nil value for an argument combination, or until all elements have been
processed in one of the lists.

 

Example:

(setq target 3)
(setq _list '((1 . "Red")(2 . "Yellow")(3 . "Green")(4 . "Cyan")(5 . "Cyan")))

 

_$ (vl-some '(lambda (a)(if (eq (car a) target)(cdr a))) _list)
"Green"
_$ _list
((1 . "Red") (2 . "Yellow") (3 . "Green") (4 . "Cyan") (5 . "Cyan"))
_$

Notice how vl-some derived the result without redefining the _list variable? Whilst with the while command

 

_$ (while (setq a (car _list)) (if (eq (car a) target)  (setq b (cdr a) _list nil)(setq _list (cdr _list))))
nil
_$ b
"Green"
_$ _list
nil

 

or even wth recursion programming. [ but that will open another can of worms ]

 

I had free time to check the net this time, I'll get back to you on the other items as soon as i can.

 

Cheers

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost