Toggling properties true/false

Discussion in 'AutoCAD' started by David Kozina, Apr 28, 2004.

  1. David Kozina

    David Kozina Guest

    For the groups' consideration:

    Is the following a decent way to toggle an object's property true/false?
    (I realize that if the property doesn't use a boolean value, something's
    gonna blow.)

    Still... I'm curious if I'm missing a better/more elegant way to do this?
    Thanks


    ; Toggle object property true/false
    (defun djkBooleToggleObjectProperty
    (_obj ; object to apply toggled property
    Property ; property to toggle - NOTE: NO validity checking
    )
    (vlax-put-property
    _obj
    Property
    (if
    (eq (vlax-get-property _obj Property)
    :vlax-true
    )
    :vlax-false
    :vlax-true
    );_end if
    )
    );_end defun
     
    David Kozina, Apr 28, 2004
    #1
  2. David Kozina

    PG. Guest

    Looks good but before trying to modify a property check if it is available
    and modifiable using (vlax-property-available-p) function.

    For example,

    (if (vlax-property-available-p _obj Property T) ;; where
    Property = 'Visible
    (vlax-put-property _obj Property (if ...

    -PG.
     
    PG., Apr 29, 2004
    #2
  3. David Kozina

    Tom Smith Guest

    You can do it with a plain oldfashioned

    (setvar <variable> (boole 6 1 (getvar <variable>)))

    but that also assumes a 0/1 toggle.

    FWIW it isn't necessary to comment every closing parenthesis.
    :) ;_end smiley
     
    Tom Smith, Apr 29, 2004
    #3
  4. David Kozina

    Doug Broad Guest

    David,
    What you've got looks good. I can't see any better way.

    Here's another way but probably not better than you posted.
    It uses the 0 and -1 alternatives to :vlax-true and :vlax-false

    (defun toggleprop (obj prop) ;;D. C. Broad
    (vlax-put obj prop (- (abs (vlax-get obj prop)) 1)))

    ;;example (toggleprop <vla-object> 'visible)

    BTW, is there any particular reason for using the leading
    underscore in your arguments/local variables? Seems
    less readable that way.

    Not sure that toggles are generally useful though. I'd
    just as soon (vlax-put-property obj 'visible :vlax-false)
    or (vlax-put-property obj 'visible :vlax-true) and be
    sure which way I'm going. No particular need for
    another wrapper.

    Regards,
    Doug
     
    Doug Broad, Apr 29, 2004
    #4
  5. David Kozina

    David Kozina Guest

    Doug,

    My formatting has been specifically formulated to drive everyone here
    bananas.
    <muhahaha>

    Really though - thanks for the comments (same goes for Tom Smith and PG)

    Actually, the leading underscored variables are just a mnemonic for me to
    help me remember what type of variable they represent. (I couldn't
    recall/decide what type of variable 'property' was, so I gave up at this
    point.) But if a more complex section of code deals with a block and a
    circle, say, I may use blk_obj and circ_obj amongst others. _pt - point.
    _alst - association list. _int - integer. _etc. :)

    IIRC, Reini Urban used these types of variable names in much of his public
    code, and I thought it helped make it easier to follow what was happening to
    a variable. IOW, I could probably expect an error if I tried: (strcat
    First_int Second_int)... But who knows what might happen with just (strcat
    First Second)?

    For a routine this small, of course I don't need the underscore.
    Just force of habit now, and helps keep me consistent elsewhere.

    Same with the ;_comments.
    It appears that most everyone here would think that

    (if x
    ; then
    y
    ; else
    ;
    );_end if

    ....is way-WAY-OVER-overkill. I understand.
    But the 'y' may possibly span more than a single line someday.
    And as for the 'totally idiotic'

    ; else
    ;

    bit - well, what it shows is that at least the programmer *thought about*
    the possibility of 'x' proving to be 'false'. In *this* case -
    *presently* - there's nothing more to do.
    But someday, perhaps, I'll come across some case where something *does* need
    to be done here.
    So I've left a spot available, should that ever happen.

    I am sorry if these formatting preferences bother some here.
    Truly not my intent, nor am I trying to set forth 'righteous-code'. HA!
    Me? Don't make me laugh.
    I've just been trying at a *personal* level to do things in a more
    consistent and disciplined fashion.
    And in a way that I might be able to more easily figure out what I was
    smoking a few months from now.


    Now, as for the 'toggle' - well basically this was a result of trying to
    accomplish two things with one function. Perhaps this is generally a no-no?

    In this case, I started with two command line functions: C:DTON and
    C:DTOFF, which activated or deactivated a particular function, respectively,
    just as you wrote, by either:
    (vlax-put-property obj 'Active :vlax-true)
    or (vlax-put-property obj 'Active :vlax-false)

    The way I looked at it, if I created a different function C:DTOG that just
    toggled the 'Active property, I could delete the other two functions and
    only have to remember one function name. Thus my reasoning and thus my
    original question.

    I am curious as to how others would handle this type of situation.

    Again many thanks for the comments. It helps alot. Sorry for the
    eye-strain, everyone.

    :)
    David Kozina
     
    David Kozina, Apr 29, 2004
    #5
  6. Why would you have to check to see if a property is
    available and modifiable? vlax-put-property already
    does that. Are you suggesting that every single call
    to vlax-get/put-property should be guarded by a test
    involving vlax-property-available-p?

    If not, then what makes this particular case special?
     
    Tony Tanzillo, Apr 29, 2004
    #6
  7. (defun djkBooleToggleObjectProperty

    There's certainly benefit to more self-descriptive
    function names, but I think this is a bit of overkill,
    mainly because 'toggle' implies a boolean type.

    For what it's worth, using the logical 'xor' operation
    is the succinct way to toggle a value but in this case,
    the values you must toggle between are not 0 and 1, but
    rather, -1 and 0, so:

    (defun toggle-property (obj prop)
    (vlax-put-property obj prop
    (boole 6 -1 (vlax-get-property obj prop))
    )
    )

    Also, there is no point whatsoever to validating the
    name of the property. If you pass the wrong value as
    the property, the function should just fail.
     
    Tony Tanzillo, Apr 29, 2004
    #7
  8. David Kozina

    Doug Broad Guest

    Hi Tony,

    Boole 6 is good but your function always
    returns an error(at least it does here):

    (boole 6 -1 (vlax-get-property a 'visible))
    ; error: bad argument type: fixnump: :vlax-true

    Perhaps this is what you meant(similar to mine)
    Code:
    (defun toggle-property (obj prop)
    (vlax-put obj prop
    (boole 6 -1 (vlax-get obj prop))
    )
    )
    
     
    Doug Broad, Apr 29, 2004
    #8
  9. David Kozina

    Doug Broad Guest

    Hi David,
    Glad to help. While I can understand blk_obj or BlkObj and
    Circ_Obj or CircObj, I think _obj is not helpful. But it
    ultimately is what you enjoy and can read that counts. ;-)

    As to commenting, I prefer comments that explain, in general,
    what each main section of code does. Line ending comments
    are helpful where complex if and cond sections end but
    spacing and indents are often enough.

    Regards,
    Doug



    <snip>
     
    Doug Broad, Apr 29, 2004
    #9
  10. David Kozina

    David Kozina Guest


    Roger that.
    Thanks for this suggestion and your other comments.

    Best regards,
    David Kozina
     
    David Kozina, Apr 29, 2004
    #10
  11. Doug - Thanks for pointing that out.

    After looking at my own 5+ year-old boolean property
    'toggler', I see it still uses (vlax-get/put), rather
    than (vlax-get/put-property):

    (defun toggle-property (obj prop)
    (vlax-put obj prop
    (boole 6 -1 (vlax-get obj prop))
    )
    )





     
    Tony Tanzillo, Apr 29, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.