See the top rated post in this thread. Click here

Results 1 to 10 of 10

Thread: Help Please - Switch visibility state of blocks in selection set

  1. #1
    Login to Give a bone
    0

    Default Help Please - Switch visibility state of blocks in selection set

    Hello, I'm putting together a lisp that will create a selection set of blocks based on a window defined by the user, and, depending on the effective name of the block, will either switch visibility states of, change the layer of, or erase each block. The blocks that I want to switch the visibility state of, have text that is either turned on or off by using a visibility state. The name of the visibility state is "Text", and it's options are "On" and "Off". I've been trying to figure this out for a few days now. Can someone please help? Thank you!



    (defun c:windowblockchange ( / bb1 bb2 blks num ent effname blockstochangelayer blockstoerase)
    (setq BB1 (getpoint "\nWindow select area to edit:\n")) ;asks LL point of area
    (setq BB2 (getcorner BB1)) ;asks UR point of area
    (setq blockstochangelayer (ssadd) blockstoerase (ssadd)) ;creates empty selection sets
    (if (setq blks (ssget "w" bb1 bb2 (list (cons 0 "insert")))) ;if insert blocks are found, create selection set of blocks
    (progn ;do all of these things
    (setq num 0) ;set block number to 0, aka first
    (repeat (sslength blks) ;repeat this the number of blocks found
    (setq ent (ssname blks num)) ;set current entity to the name of the current block
    (setq effname (vla-get-EffectiveName (vlax-ename->vla-object ent))) ;gets effective name of current entity (block)
    (cond ;conditional that puts the current entity into the correct selection set based on effective name
    ((= effname "block1") (wbcpt2))
    ((= effname "block2") (wbcpt2))
    ((= effname "block3") (ssadd ent blockstochangelayer))
    ((= effname "block4") (ssadd ent blockstochangelayer))
    ((= effname "block5") (ssadd ent blockstoerase))
    ((= effname "block6") (ssadd ent blockstoerase))
    )
    (setq num (+ 1 num)) ;adds 1 to block number so can go to next block
    )
    )
    )
    (if (/= (sslength blockstoerase) 0) ;if blockstoerase is found
    (command "erase" blockstoerase "") ;erase selection set
    )
    (if (/= (sslength blockstochangelayer) 0) ;if blockstochangelayer is found
    (command "chprop" blockstochangelayer "" "la" "Layer1" "") ;change layer of blocks in selection set
    )
    )

    (defun wbcpt2 (/ obj props) ;subroutine to turn on text of dynamic block
    (vl-load-com)
    (setq obj (entlast))
    (= "AcDbBlockReference" (vla-get-objectname (setq obj (vlax-ename->vla-object obj))))
    (= :vlax-true (vla-get-isdynamicblock obj))
    (setq props (vlax-invoke obj 'getdynamicblockproperties))
    (foreach prop props
    (if (eq "Text" (vla-get-propertyname prop))
    (vla-put-value prop (vlax-make-variant (cdr prop) "On"))
    )
    )
    (princ)
    )

  2. #2
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    In Visual LISP there are three syntax types to be familiar with.
    Code:
    (vlax-get obj "Value")
    (vla-get-value obj)
    (vlax-get-property obj "value")
    The first above is what is sometimes called vital lisp.

    The other two are visual LISP and the last is a deeper variant based visual LISP.

    In order to change the Visibility property of a dynamic block you need to use variants.

    The kind of variant is important and being one of the allowed values (spelling and case is important)

    When I change them I get the variant type from the old value and use


    Code:
           (setq Value            (vlax-get objDynamicProperty "Value"))
           (setq varValue         (vlax-get-property objDynamicProperty "Value"))
           (setq intVariantType   (vlax-variant-type varValue))
           (setq lstAllowedValues (vlax-get objDynamicProperty "AllowedValues"))
    Code:
      
     (vlax-put-property 
      objDynamicProperty 
      "Value" 
      (vlax-make-variant Value intVariantType)
     )
    But that is the trick.
    AutomateCAD

  3. #3
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    Thanks for the reply, but sorry, I still don't get it

  4. #4
    Active Member
    Join Date
    2015-12
    Location
    Western Europe
    Posts
    57
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    Your wbcpt2 defun doesn't work because :

    1. you are using (entlast) which is the last non deleted entlty in the drawing database, NOT the entity (block) you are iterating

    2.
    Code:
    (= "AcDbBlockReference" (vla-get-objectname (setq obj (vlax-ename->vla-object obj))))
    (= :vlax-true (vla-get-isdynamicblock obj))
    These two lines are conditionals yet are not in any conditional statement so won't filter non dynamic blocks from hitting the foreach loop

    You need to pass in the current entity (ent) from the main lisp

    Code:
    ( (or (= effname "block1") (= effname "block2")) (wbcpt2 ent))
    then

    Code:
    (defun wbcpt2 ( obj / props)
    and remove (setq obj (entlast))

    then sort out a (cond) or (if) statement to filter non dynamic blocks

    Then look at Lee Macs dynamic properties functions

  5. #5
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    It would help if you posted a test drawing (earliest save format available)

    P=
    AutomateCAD

  6. #6
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    I'll take those notes into account, dlanor, thank you. Also, in the meantime, I have attached an example block of one of the items that i wish to turn the text on. Thanks again for all your help!
    Attached Files Attached Files

  7. #7
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    Dlanor, I made the changes you suggested, and went to Lee Mac's code and tried to incorporate the section that seemed to be relevant, but I'm still getting errors. ; error: bad argument type: VLA-OBJECT <Entity name: 1ca9e36ecd0>

    Here is the current code, please see the previous post for an example block.

    Code:
    (defun c:wbc ( / bb1 bb2 blks num ent effname blockstochangelayer blockstoerase) ;
    	(setq bb1 nil bb2 nil blks nil num nil ent nil effname nil blockstochangelayer nil blockstoerase nil)
    	(setq BB1 (getpoint "\nWindow select area to edit:\n"))										;asks LL point of area
    	(setq BB2 (getcorner BB1))													;asks UR point of area
    	(setq blockstoturnontext (ssadd) blockstochangelayer (ssadd) blockstoerase (ssadd))						;creates empty selection sets
    	(if (setq blks (ssget "w" bb1 bb2 (list (cons 0 "insert"))))									;if insert blocks are found, create selection set of blocks
    		(progn															;do all of these things
    			(setq num 0)													;set block number to 0, aka first
    			(repeat (sslength blks)												;repeat this the number of blocks found
    				(setq ent (ssname blks num))										;set current entity to the name of the current block
    				(setq effname (vla-get-EffectiveName (vlax-ename->vla-object ent)))					;gets effective name of current entity (block)
    				(cond													;conditional that puts the current entity into the correct selection set based on effective name
    					;((= effname "P") (wbcpt2 ent))    
    					((= effname "P") (LM:setdynpropvalue ent))
    					;((= effname "block2") (wbcpt2 ent))
    					((= effname "block3") (ssadd ent blockstochangelayer))
    					((= effname "block4") (ssadd ent blockstochangelayer))
    					((= effname "block5") (ssadd ent blockstoerase))
    					((= effname "block6") (ssadd ent blockstoerase))
    				)
    				(setq num (+ 1 num))											;adds 1 to block number so can go to next block
    			)
    		)
    	)
    	(if (/= (sslength blockstoerase) 0)												;if blockstoerase is found
    		(command "erase" blockstoerase "")											;erase selection set
    	)
    	(if (/= (sslength blockstochangelayer) 0)											;if blockstochangelayer is found
    		(command "chprop" blockstochangelayer "" "la" "Layer1" "")								;change layer of blocks in selection set
    	)
    )
    
    (defun wbcpt2  ( obj / props) ;subroutine to turn on text of dynamic block
    	(vl-load-com)
    	(setq props (vlax-invoke obj 'getdynamicblockproperties))
    	(foreach prop props
    		(if (eq "Text" (vla-get-propertyname prop))
    			(vla-put-value prop (vlax-make-variant (cdr prop) "On"))
    		)
    	)
    	(princ)
    )
    
    (defun LM:setdynpropvalue ( ent / prp val )
        (setq prp (strcase "Text"))
        (vl-some
           '(lambda ( x )
                (if (= prp (strcase (vla-get-propertyname x)))
                    (progn
                        (vla-put-value x (vlax-make-variant "On" (vlax-variant-type (vla-get-value x))))
                        (cond ("On") (t))
                    )
                )
            )
            (vlax-invoke ent 'getdynamicblockproperties)
        )
    )
    Thanks everyone for all the help.

  8. #8
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    Here is a different approach to manipulating dynamic blocks.

    I include several functions

    The one you can use is

    Code:
    (DynamicPropertyPut objSelection strProperty Value)
    Include block instance, propertyname and new value.

    P=


    Code:
    ;___________________________________________________________________________________________________________|
    ;
    ; Written By: Peter Jamtgaard C.E., P.E., S.E. copyright 2020 All Rights Reserved
    ;___________________________________________________________________________________________________________|
    ;___________________________________________________________________________________________________________|
    ;
    ; General Function Header List
    ;___________________________________________________________________________________________________________|
    
    ;* (DynamicProperty objSelection strProperty)
    ;* Function to put a dynamic property value given Block Reference object, propertyname and new value
    
    ;* (DynamicPropertyPut objSelection strProperty Value)
    ;* Function to put a dynamic property value given Block Reference object, propertyname and new value
    
    ;* (DynamicSublist objDynamic  strWCProperty)
    ;* Function to get a sublist of Propertyname, Value as Variant, Value, VariantType, vla-object
    
    ;* (DynamicSublists objSelection strWCProperty )
    ;* Function to get a dynamic block property value given block instance object, wildcard dynamic propertyname
    
    ;* (ErrorTrap symFunction)
    ;* Function to trap error. Syntax (errortrap '(setq A (/ 1 0)))
    
    ;$ Header End
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to put a dynamic property value given Block Reference object, propertyname and new value
    ;___________________________________________________________________________________________________________|
    
    (defun DynamicProperty (objSelection strProperty / lstOfSublists)
     (if (setq lstOfSublists (DynamicSublists objSelection (strcase strProperty )))
      (cadar lstOfSublists)
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to put a dynamic property value given Block Reference object, propertyname and new value
    ;___________________________________________________________________________________________________________|
    
    (defun DynamicPropertyPut (objSelection strProperty Value / intVariantType lstSublist objDynamic)
     (if (and (setq lstOfSublists  (DynamicSublists objSelection (strcase strProperty )))
              (setq lstSublist     (car lstOfSublists))
              (setq objDynamic     (car (reverse lstSublist)))
              (setq intVariantType (nth 3 lstSublist))
         )
      (errortrap (quote (vlax-put-property objDynamic 
                                           "Value" 
                                           (vlax-make-variant Value 
                                           intVariantType
                        )
                  )
       )
      )
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to get a sublist of Propertyname, Value as Variant, Value, VariantType, vla-object
    ; Given a Dynamic Block Property object and wildcard propertyname
    ; Returns a sublist for success and nil for failure
    ;___________________________________________________________________________________________________________|
    
    (defun DynamicSublist (objDynamic
                            strWCProperty
                            /
                            intVariantType
                            strPropertyName
                            varValue
                           )
     (if (and 
          (= (type objDynamic) 'vla-object)
          (vlax-property-available-p objDynamic "PropertyName")
          (setq strPropertyName (strcase (vla-get-propertyname objDynamic)))
          (wcmatch (strcase strPropertyName)(strcase strWCProperty))
         )
      (list strPropertyName
            (setq varValue (vlax-get-property objDynamic "Value"))
            (vlax-variant-value varValue)
            (vlax-variant-type  varValue)
            objDynamic
      )      
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to get a dynamic block property value given block instance object, wildcard dynamic propertyname
    ;___________________________________________________________________________________________________________|
    
    (defun DynamicSublists (objSelection 
                            strWCProperty  
                            / 
                            lstDynamicBlockProperties
                            lstReturn
                            objDynamic
                            strProperty
                           )  
     (if (and
          (= (type objSelection) 'vla-object)
          (vlax-property-available-p objSelection "IsDynamicBlock")   
          (= (vlax-get objSelection "IsDynamicBlock") -1)
          (setq lstDynamicBlockProperties (vlax-invoke objSelection "GetDynamicBlockProperties"))
         )
      (foreach objDynamic (reverse lstDynamicBlockProperties)
       (and (setq lstDynamicSublist (DynamicSublist objDynamic strWCProperty))
            (setq lstReturn         (cons lstDynamicSublist lstReturn))
       )
      )
     )
     lstReturn
    )
    
    ;___________________________________________________________________________________________________________|
    ; 
    ; Function to trap error. Syntax (errortrap '(setq A (/ 1 0)))
    ;___________________________________________________________________________________________________________|
    
    (defun ErrorTrap (symFunction / objError result)
     (if (not
          (vl-catch-all-error-p
           (setq objError (vl-catch-all-apply
                          '(lambda (X)(set X (eval symFunction)))
                           (list 'result)))))
      (if result result 'T)
     )
    )
    
    (princ "!")
    (vl-load-com)
    Attached Files Attached Files
    AutomateCAD

  9. #9
    Active Member
    Join Date
    2015-12
    Location
    Western Europe
    Posts
    57
    Login to Give a bone
    1

    Default Re: Help Please - Switch visibility state of blocks in selection set

    Apologies, but I am unable to access AutoCAD at the moment as I'm still awaiting the arrival of my new monitor.

    I think the error is caused by the fact that both (defun wbcpt2) and (LM:setdynpropvalue) require an object to be passed, and you are passing an entity

    Try changing

    Code:
    (setq effname (vla-get-EffectiveName (vlax-ename->vla-object ent)))
    to

    Code:
    (setq effname (vla-get-EffectiveName (setq obj (vlax-ename->vla-object ent))))
    and then pass obj not ent i.e.

    Code:
    ;((= effname "P") (wbcpt2 obj))    
    ((= effname "P") (LM:setdynpropvalue obj))
    ;((= effname "block2") (wbcpt2 obj))
    don't forget to localise variable obj

  10. #10
    Login to Give a bone
    0

    Default Re: Help Please - Switch visibility state of blocks in selection set

    SUCCESS! Dlanor's list of changes was the key. Thank you both for your help!

Similar Threads

  1. Can't Deselect certain items from selection set - Deadline! Please help!
    By pixeltje914124 in forum NavisWorks - General
    Replies: 0
    Last Post: 2012-01-11, 02:40 PM
  2. Visibility State selection upon insert of block?
    By Mamma Jamma in forum Dynamic Blocks - Technical
    Replies: 5
    Last Post: 2010-09-01, 11:41 AM
  3. Replies: 1
    Last Post: 2008-11-04, 05:48 PM
  4. Replies: 8
    Last Post: 2006-10-11, 07:36 PM
  5. Set a certain visibility state (not the first) to default?
    By sinc in forum Dynamic Blocks - Technical
    Replies: 1
    Last Post: 2006-02-24, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •