Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Determining if ssname will give an error

6 REPLIES 6
Reply
Message 1 of 7
AR12
328 Views, 6 Replies

Determining if ssname will give an error

Hi All,

Maybe I'm just not using the correct search words, but what I'm trying to do is determine if a block is in a drawing at a specific point. However, in the example I'm using, I only have 1 block with that point which is not the block I am looking for, so if I do (ssname poletopss 1) I will get an error, with poletopss being my selection set. How can I determine that it will give me an error and have the program continue on without ending?

Thanks,
Adam Richardson

If needed: AutoCAD 2017 User using Visual LISP for editing LISP and DCL files
6 REPLIES 6
Message 2 of 7
AR12
in reply to: AR12

Turns out the error I was getting was not from the part I thought and I figured out a way to do what I wanted.

I did a (while) statement as I realized that it won't give an error if I use (ssname poletopss 1) it would instead return nil, so I said that (while (/= (ssname poletopss COUNT) nil) and then did my function and inside of the while I had a (setq COUNT (+ COUNT 1)) and before the while statement I had a (setq COUNT 0)

Thanks,
Adam Richardson

If needed: AutoCAD 2017 User using Visual LISP for editing LISP and DCL files
Message 3 of 7
Kent1Cooper
in reply to: AR12


@AR12 wrote:

.... what I'm trying to do is determine if a block is in a drawing at a specific point. However, in the example I'm using, I only have 1 block with that point which is not the block I am looking for, so if I do (ssname poletopss 1) I will get an error, with poletopss being my selection set. How can I determine that it will give me an error and have the program continue on without ending?


You don't include enough code to be sure, so I hope I'm understanding this correctly.

 

If there is such a Block at that location, you want (ssname poletopss 0) rather than 1.  If there isn't, poletopss will simply be nil.  So rather than using a counter variable, you can just test whether the variable exists at all, i.e. do something like:

 

(if poletopss ; that is, if it's not nil

  (... do whatever you want to (ssname poletopss 0), which will exist ...)

)

 

It gets only slightly more complicated if there might be more than one such Block at that location.  You might then need to step through the resulting selection set to do what you want with them [depending on what that is -- for certain things, you could do it to all of them together without stepping through].  But you could still use

 

(if poletopss

 

as the test of whether to do so.

Kent Cooper, AIA
Message 4 of 7
AR12
in reply to: Kent1Cooper

Hi Kent,

I'm putting my code down below for what I ended up with, but what I was trying to do was determine if a specific block was in a location, as our Detail Blocks and Blocks that includes parts of the Detail itself(Detail Block being a border around the individual detail) are at the same point. The drawing I was using to test it had the block exploded so the block didn't exist at that point and thus (ssname poletopss 1) actually gave me nil, but with the rest of the code I was getting a entitity type error.

 

;;;determine if Galv or Weathering Pole Cap
  (setq poletopss (ssget "C" '(0.0 0.0) '(6.25 6.1) '((0 . "INSERT"))))
  (setq COUNT 0
    poletopblockname 0)
  (while (and (/= (ssname poletopss COUNT) nil) (and (/= poletopblockname "WeldedPoleCap_12") (/= poletopblockname "BoltedPoleCap_12")))
    (setq poletopblockname (cdr (assoc 2 (entget (ssname poletopss COUNT)))))
    (setq COUNT (+ COUNT 1))
    )

 

 

And Yes I am aware of the fact I used and twice in the while I forgot to delete the extra and, as I originally had an or at the first one and wasn't getting the result I was expecting.

Thanks,
Adam Richardson

If needed: AutoCAD 2017 User using Visual LISP for editing LISP and DCL files
Message 5 of 7
Kent1Cooper
in reply to: AR12


@AR12 wrote:

....what I was trying to do was determine if a specific block was in a location, as our Detail Blocks and Blocks that includes parts of the Detail itself(Detail Block being a border around the individual detail) are at the same point. ....

 

 

;;;determine if Galv or Weathering Pole Cap
  (setq poletopss (ssget "C" '(0.0 0.0) '(6.25 6.1) '((0 . "INSERT"))))
  (setq COUNT 0
    poletopblockname 0)
  (while (and (/= (ssname poletopss COUNT) nil) (and (/= poletopblockname "WeldedPoleCap_12") (/= poletopblockname "BoltedPoleCap_12")))
    (setq poletopblockname (cdr (assoc 2 (entget (ssname poletopss COUNT)))))
    (setq COUNT (+ COUNT 1))
    )

....


You can do that more directly:

 

(setq poletopss

  (ssget "C" '(0.0 0.0) '(6.25 6.1)

    '((0 . "INSERT") (2 . "WeldedPoleCap_12,BoltedPoleCap_12")); filter for those Block names only

  )

)

 

Then, assuming that if there are any there, it's going to be one or the other but not both, so it should have only one item in it, and you can get its Block name with:

 

(if poletopss

  (setq poletopblockname (cdr (assoc 2 (entget (ssname poletopss 0)))))

)

Kent Cooper, AIA
Message 6 of 7
AR12
in reply to: Kent1Cooper

Kent,

If I do it that way, and say there is no block existing with those names, will the program have an error or will it still continue in a way that I'd be able to have the program display an alert message and then exit out of the program? I didn't copy what you're suggesting to try yet as I'm involved in a different program at the moment but will test it once I have a chance to.

Thanks,
Adam Richardson

If needed: AutoCAD 2017 User using Visual LISP for editing LISP and DCL files
Message 7 of 7
Kent1Cooper
in reply to: AR12


@AR12 wrote:

Kent,

If I do it that way, and say there is no block existing with those names, will the program have an error or will it still continue in a way that I'd be able to have the program display an alert message and then exit out of the program? I didn't copy what you're suggesting to try yet as I'm involved in a different program at the moment but will test it once I have a chance to.


You can do this:

 

(if poletopss

  (setq poletopblockname (cdr (assoc 2 (entget (ssname poletopss 0))))); then

  (progn ; else

    (alert "No such Block(s) found.")

    (exit)

  ); progn

); if

(... then go on [if it hasn't exited] to do whatever, using or on the basis of the Block name...)

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost