Insert Dynamic Block and then select Visibility before inserting it to drawing

Insert Dynamic Block and then select Visibility before inserting it to drawing

amrivasU38U2
Contributor Contributor
1,256 Views
18 Replies
Message 1 of 19

Insert Dynamic Block and then select Visibility before inserting it to drawing

amrivasU38U2
Contributor
Contributor

Hello everyone. Is it possible to create a lisp with this routine. 

1. Insert a dynamic block _AVSF-UTL PAGEFLAG in a particular layer _AVSF-LN AUDIO.

2. The block has a preview that can be drag while moving the mouse before inserting it to the drawing.

3. Choose visibility state either TO PAGE or FROM PAGE.

4. Lastly, Reset the current layer to DEFPOINTS.

Thank you in advance.

 
 
 
 
 
 
0 Likes
Accepted solutions (1)
1,257 Views
18 Replies
Replies (18)
Message 2 of 19

paullimapa
Mentor
Mentor

similar to this recent post...perhaps a bit of tweaking would do it for you

Solved: Repeated entry of Block, with Vis State selection before placement - Autodesk Community - Au...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 19

amrivasU38U2
Contributor
Contributor

Sorry but I don't have that much knowledge tweaking codes. Honestly, I am using ai to generate a code for me.

 
 
0 Likes
Message 4 of 19

paullimapa
Mentor
Mentor

Is the dynamic block _AVSF-UTL PAGEFLAG typically already inserted into the dwg?

If not, is it located in one of AutoCAD's Options>Files>Support File Search Path?

Could you share this dynamic block by uploading it here?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 19

amrivasU38U2
Contributor
Contributor
Yes, it already inserted in the drawing
0 Likes
Message 6 of 19

paullimapa
Mentor
Mentor

Could you share this dwg here?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 19

amrivasU38U2
Contributor
Contributor

Here is the dwg file of the block.

0 Likes
Message 8 of 19

amrivasU38U2
Contributor
Contributor
This is the code i am currently working but can't proceed with error in visibility states.



(defun c:PFA ()
(setq blockName "_AVSF-UTL PAGEFLAG")
(setq layerName "_AVSF-LN AUDIO")

;; Ensure the layer exists, if not, create it
(if (not (tblsearch "layer" layerName))
(command "._LAYER" "M" layerName "C" "7" "" "")
)

;; Set the current layer to the desired layer
(setvar "CLAYER" layerName)

;; Get the block definition
(setq blockDef (tblsearch "block" blockName))

(if blockDef
(progn
;; Insert block with drag functionality
(command "._-INSERT" blockName pause "1" "1" "0")
(princ "\nBlock inserted successfully.")

;; Select the inserted block
(setq ent (entlast))

;; Change the visibility state
(if ent
(progn
;; Prompt user for visibility state
(initget "TO PAGE FROM PAGE")
(setq visState (getkword "\nEnter visibility state [TO PAGE/FROM PAGE]: "))

;; Set the visibility state
(if (or (equal visState "TO PAGE") (equal visState "FROM PAGE"))
(progn
(vla-put-EffectiveName (vlax-ename->vla-object ent) visState)
(princ (strcat "\nVisibility state set to " visState "."))
)
(princ "\nInvalid visibility state.")
)
)
(princ "\nFailed to set visibility state.")
)
)
(princ "\nBlock definition not found.")
)

;; Reset the current layer to DEFPOINTS
(setvar "CLAYER" "DEFPOINTS")
(princ "\nCurrent layer reset to DEFPOINTS.")
(princ)
)

(princ "\nType PFA to run the command.")

0 Likes
Message 9 of 19

paullimapa
Mentor
Mentor
Accepted solution

Your code came pretty close...

These are the adjustments I made:

1. Added following 2 sub functions to get & then set dynamic block visibility states property written by @komondormrex 

; add the following two sub functions  
; by komondormrex 
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/swap-the-visibility-state-in-a-block-using-lisp/m-p/12786544/highlight/true#M466339
(defun get_dyn_property_by_name (dyn_property_name insert_object / dyn_property_found)
	(if (vl-some '(lambda (dyn_property) (= dyn_property_name (vla-get-propertyname (setq dyn_property_found dyn_property))))
				  (vlax-invoke insert_object 'getdynamicblockproperties)
		)
		dyn_property_found
		nil
	)
)
; modified version:
(defun set_dyn_property (dyn_property_name value obj / stamp_property)
	(if (setq stamp_property (get_dyn_property_by_name dyn_property_name obj)) 
		(vla-put-value stamp_property value)
	)
)

2. Modified your initget statements since each selection must be a single word without spaces or underscores:

;; Prompt user for visibility state
; (initget "TO PAGE FROM PAGE")
; (setq visState (getkword "\nEnter visibility state [TO PAGE/FROM PAGE]: "))
(initget "TO-PAGE FROM-PAGE") ; use hyphen to preserve each choice as a single item - note underscore not supported
(setq visState (getkword "\nEnter visibility state [TO-PAGE/FROM-PAGE]: "))
; add this  
(if (equal visState "TO-PAGE") (setq visState "TO PAGE")(setq visState "FROM PAGE")) ; force to set to a correct vis state name
;

3. Apply the set_dyn_property subfunction:

(if (or (equal visState "TO PAGE") (equal visState "FROM PAGE"))
(progn
;(vla-put-EffectiveName (vlax-ename->vla-object ent) visState)
  (set_dyn_property "Visibility1" visState (vlax-ename->vla-object ent)) ; set to the Visibility1 paramenter

4. Before you set Defpoints as current layer:

;; Ensure the layer exists, if not, create it
(if (not (tblsearch "layer" "DEFPOINTS"))
(command "._LAYER" "M" "DEFPOINTS" "C" "7" "" "")
)

5. Add (princ) at end to remove double echo:

(princ "\nType PFA to run the command.")(princ) ; add princ at end to prevent double echo

6. When finish troubleshooting then localize variables:

(defun c:PFA (/ get_dyn_property_by_name set_dyn_property blockName layerName blockDef ent visState) ; localize variables

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 10 of 19

amrivasU38U2
Contributor
Contributor
Wow that's superb. Thank you so much.
0 Likes
Message 11 of 19

paullimapa
Mentor
Mentor

you are welcome...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 12 of 19

Sea-Haven
Mentor
Mentor

Another two versions.

 

; Change dynamic block properties when inserting look at properties available.
; By Alan H June 2022

;; Get Dynamic Block Property Allowed Values  -  Lee Mac
;; Returns the allowed values for a specific Dynamic Block property.
;; Set Dynamic Block Visibility State  -  Lee Mac
;; Sets the Visibility Parameter of a Dynamic Block (if present) to a specific value (if allowed)
;; Get Visibility Parameter Name  -  Lee Mac
;; Returns the name of the Visibility Parameter of a Dynamic Block (if present)

(defun insdynv (blkname / pt obj lst ans)
(if (not LM:setdynpropvalue )(load "Lee-mac Dynamic block get-put"))
(setq pt (getpoint "\nPick a pt for block "))
(command "-insert" blkname "s" 1.0  pt 0)
(setq obj (vlax-ename->vla-object (entlast)))
(setq visval (LM:getvisibilityparametername obj))
(setq lst (LM:getdynpropallowedvalues obj visval))
(setq lst (cons "Please choose" lst))
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not AHbut)(setq AHbut 1))
(setq ans (ah:butts 1 "v"  lst))
(LM:SetVisibilityState obj ans)
(princ)
)

; set existing block visibilty

(defun insdyne (blkname / pt obj lst ans)
(if (not LM:setdynpropvalue )(load "Lee-mac Dynamic block get-put"))
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(setq obj (vlax-ename->vla-object (car (entsel "\nPick a dynamic block "))))
(setq visval (LM:getvisibilityparametername obj))
(setq lst (LM:getdynpropallowedvalues obj visval))
(setq lst (cons "Please choose" lst))
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not AHbut)(setq AHbut 1))
(setq ans (ah:butts 1 "v"  lst))
(LM:SetVisibilityState obj ans)
(princ)
)

 reads visibility and puts into a dcl for choice. 

 

0 Likes
Message 13 of 19

amrivasU38U2
Contributor
Contributor
I want to try this one also, but I can't nail how to test this code.
0 Likes
Message 14 of 19

Sea-Haven
Mentor
Mentor

You need to either pre appload the multi radio and lee's dynamic code before running one of the defuns or save the lisps in a support path so they can autoload.

0 Likes
Message 15 of 19

amrivasU38U2
Contributor
Contributor
I tried but i can't crack it. I save the 2 file in a supported path. Save the last code separately. Load the 3 and then test using insdynv or insdyne command but it says unknown command.
0 Likes
Message 16 of 19

Sea-Haven
Mentor
Mentor

They are defuns so need to use, 

(insdynv "Yourblockname")

You can type that on the command line or make some command defuns. Add to the code at end.

(defun c:insd ( / blkname)
(setq blkname (getstring "\nEnter block name " T))
(insdynv blkname)
)

 

0 Likes
Message 17 of 19

amrivasU38U2
Contributor
Contributor
Can you please modify the code so there is no need to prompt to enter block name. My block name is _AVSF-UTL PAGEFLAG.
0 Likes
Message 18 of 19

amrivasU38U2
Contributor
Contributor
Can you modify this so that every 15 radio buttons it will add another column.
0 Likes
Message 19 of 19

Sea-Haven
Mentor
Mentor

Multi radio buttons is limited to around 20 max on my laptop. So the workaround is use a list box instead it can have 100+ etc. 

Lee-mac has a couple of list box functions this is another that I use.

 

Same thing if say length of visibilty is more than 15 then use the listbox method, not multi radio buttons.

0 Likes