Jump to content

check if attribute property exist - is my code correct?


aridzv

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

  • Like 1
Link to comment
Share on other sites

@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?

 

Link to comment
Share on other sites

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 by mhupp
Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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)))))

 

  • Like 2
Link to comment
Share on other sites

@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 by aridzv
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...