aridzv Posted January 13, 2023 Posted January 13, 2023 Hi. I need to check if an attribute property exsit,and if yes to update it. this the code I'm using: (if (getpropertyvalue obj "QUANTITY") (setpropertyvalue obj "QUANTITY" (rtos (* di ht) 2 3)) ) is this code correct or could I use only this: (setpropertyvalue obj "QUANTITY" (rtos (* di ht) 2 3)) and if there is no such property the lisp will just ignor it and continue? thanks, aridzv. Quote
Steven P Posted January 13, 2023 Posted January 13, 2023 try it and see... maybe in a small test LISP ? 1 Quote
mhupp Posted January 13, 2023 Posted January 13, 2023 You would have to set the value to test looks like it always returns nil. Quote nil is returned unless an error occurs when the property value is being updated. (if (setq x (getpropertyvalue obj "QUANTITY")) (setpropertyvalue obj "QUANTITY" (rtos (* di ht) 2 3)) ) Let us know. can't test in BricsCAD. side note I think entity names work as well doesn't have to be a vla-object name. 1 Quote
aridzv Posted January 14, 2023 Author Posted January 14, 2023 @mhupp First - your code worked, and I've read the article in the link you sheared. the strange thing is that both of the code examples I sheared before worked as well. what is the "true/clean" way to check if a property exist,and if true then set its value? Quote
mhupp Posted January 14, 2023 Posted January 14, 2023 (edited) oops sorry looked up the wrong thing. getpropertyvalue returns either "" if blank or the value if available, nil if not. So your if would be valid mine also stores the current value as x. so if you where looking for a specific value you could test for that. (if (and (setq x (getpropertyvalue obj "QUANTITY")) (> (atof x) 5)) ;if Quantity attribute is there and its greater than 5 Did you test to see what set does if the attribute isn't there? tho it is best to constrain lisp things could change in the future. And you can have prompts if it errors. Edited January 14, 2023 by mhupp Quote
aridzv Posted January 14, 2023 Author Posted January 14, 2023 my goal in this secnario is to check if a property named "QUANTITY" exist, and if yes then set it to a new value - (rtos (* di ht) 2 3) in this case. I want to avoid a situation that the lisp try to set a property name that dosen't exist - a sort of preventing an error from happening... that was my goal in this code: (if (getpropertyvalue obj "QUANTITY");; check if a property named "QUANTITY" exist (setpropertyvalue obj "QUANTITY" (rtos (* di ht) 2 3));; if yes then update it. ) is the code above is correct for that Purpose? Quote
Lee Mac Posted January 14, 2023 Posted January 14, 2023 Personally I don't tend to use the getpropertyvalue/setpropertyvalue functions (in favour of operating on the attribute reference entities/objects directly), but in testing, both functions will return an error if the supplied tag name does not correspond to an attribute reference held by the block. As such, issuing a call to getpropertyvalue prior to setpropertyvalue will offer no benefit in this case. Instead, you could account for errors using the vl-catch-all-apply function, e.g. the following will return T if successful, else nil: (not (vl-catch-all-error-p (vl-catch-all-apply 'setpropertyvalue (list obj "QUANTITY" (rtos (* di ht) 2 3))))) 2 Quote
aridzv Posted January 14, 2023 Author Posted January 14, 2023 (edited) @Lee Mac Thanks for the reply. here is what puzzel me: while the 2 code examples I used returned an error,they didn't caused the lisp to crash. I deleted the "QUATITY" attribute from a test block and run the lisp. the lisp done what is supposed to do (change the block length) and didn't crash or stoped. why is that? Edited January 14, 2023 by aridzv Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.