Help with vl-catch-all-apply

msarqui
Collaborator
Collaborator

Help with vl-catch-all-apply

msarqui
Collaborator
Collaborator

Hello guys,


I have this routine thanks to the great work of BeekeeCZ 

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/erase-all-but/m-p/5926425

 

It will delete all objects outside specific rectangles. I made small modifications to fit my needs and everything works great 99% of the time.

 

(defun c:EOUT (/ spol sall sint i n e)
(if	(and
	(setq spol (ssget "_X" '((0 . "LWPOLYLINE") (410 . "Model") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>"))))							
	(setq sall (ssget "_X" '((0 . "~IMAGE") (410 . "Model") (-4 . "<AND") (-4 . "<NOT") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>")  (-4 . "NOT>") (-4 . "AND>"))))	
	);and
	(progn
		(setq i -1)
		(repeat (setq i (sslength spol))
		(setq sint (ssget "_CP" (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (ssname spol (setq i (1- i))))))))
			(repeat (setq n (sslength sint))											
				(if	(ssmemb (setq e (ssname sint (setq n (1- n)))) sall)								
					(ssdel e sall)												
				);if
			);repeat
		);repeat
	);progn
);if
(if	sall
	(command "_.ERASE" sall "")
);if
)


From time to time, something inexplicable happens and the routine crashes.
In my tests, the SSGET with CP gets an error in the LAMBDA that I do not understand. Maybe the file is corrupt or something. I attached two files, one that works and one that does not.
So my goal is to capture this error with a "vl-catch-all-apply" to avoid erase if the error occurs. But obviously I'm not having success. Could someone please help me?

This is what I get so far:

 

(defun c:EOUT (/ spol sall sint i n e errobj)
(if	(and
	(setq spol (ssget "_X" '((0 . "LWPOLYLINE") (410 . "Model") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>"))))					
	(setq sall (ssget "_X" '((0 . "~IMAGE") (410 . "Model") (-4 . "<AND") (-4 . "<NOT") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>")  (-4 . "NOT>") (-4 . "AND>"))))
	);and
	(progn
		(setq i -1)
		(repeat (setq i (sslength spol))
		(setq sint (ssget "_CP" (mapcar 'cdr (vl-remove-if-not (setq errobj (vl-catch-all-apply '(lambda (x) (= (car x) 10)) (entget (ssname spol (setq i (1- i))))))))))
		(if (vl-catch-all-error-p errobj)
			(print (strcat "An error occurred: " (vl-catch-all-error-message errobj)))
			(repeat (setq n (sslength sint))
				(if	(ssmemb (setq e (ssname sint (setq n (1- n)))) sall)
					(ssdel e sall)
				);if
			);repeat
		);if
		);repeat
	);progn
);if
(if	(and sall (null errobj))
	(command "_.ERASE" sall "")
);if
);defun

Thanks,

Marcelo

0 Likes
Reply
Accepted solutions (1)
1,237 Views
7 Replies
Replies (7)

dbroad
Mentor
Mentor

You shouldn't try to nest something that will result in an error object inside an ssget statement or mapcar statement. Move the step that generates a point list to before the ssget, test the list to see if it is a list and then run the ssget. 

Architect, Registered NC, VA, SC, & GA.
0 Likes

ВeekeeCZ
Consultant
Consultant

Can't answer your question, even I don't know why you're receive the error, but let's see if (foreach) can handle your 1%. Your bad-sample did. - edit: Probably just lucky coincidence. 😞

 

(defun c:EOUT (/ spol sall sint i n e e10)
  (if	(and
          (setq spol (ssget "_X" '((0 . "LWPOLYLINE") (410 . "Model") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>"))))
          (setq sall (ssget "_X" '((0 . "~IMAGE") (410 . "Model") (-4 . "<AND") (-4 . "<NOT") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>")  (-4 . "NOT>") (-4 . "AND>"))))
          );and
    (progn
      ;(setq i -1) why??
      (repeat (setq i (sslength spol))
        (setq sint (ssget "_CP" (mapcar 'cdr
                                        (progn
                                          (foreach e (entget (ssname spol (setq i (1- i))))
                                            (if (= (car e) 10)
                                              (setq e10 (cons e e10))))
                                          e10))))
        (repeat (setq n (sslength sint))
          (if	(ssmemb (setq e (ssname sint (setq n (1- n)))) sall)
            (ssdel e sall)
            );if
          );repeat
        );repeat
      );progn
    );if
  (if	sall
    (command "_.ERASE" sall "")
    );if
  )  

 

0 Likes

msarqui
Collaborator
Collaborator

Hi Bee,

 

I really appreciate your help.

 

;(setq i -1) why??

Sorry for that. My knowledge is limited and sometimes I mess things.

 

Your new solution still not works for me. Maybe because I have AutoCAD 2016? I do not know.

The routine stops here:

(repeat (setq n (sslength sint))

Because "sint" is nil. AutoCAD gives me: ; error: bad argument type: lselsetp nil

 

0 Likes

ВeekeeCZ
Consultant
Consultant

Just wondering, how did you make those files? How did you fix the bad one?

 

I've found the one way how to fix it - using SUPERFLATTEN on the block inside. I see no obvious reason why is that. The both files looks the same to me.

 

BTW the issue is that (ssget "_CP"...) return nil even the list supplied with looks good.

 

So you should test that...

(if (setq sint ...)

 (repeat ...)

 (princ "The routine failed for some reason."))

 

(written before your last post)

0 Likes

msarqui
Collaborator
Collaborator

It is really weird.

 

To make the GOOD, I took one file that I knew it was working and I erased everything (just to improve speed in tests), except what you see.
To make the BAD, I took a problematic file, I erased everything and copy/paste the objects from the GOOD.

So, both files have the same objects. That's why I thing this is a file problem (corrupted).

 

I am doing some tests with your suggestion, (if (setq sint ...) and I will come back later.

0 Likes

ВeekeeCZ
Consultant
Consultant
Accepted solution

Sorry, bad day. Gimme your first idea what could be a reason - that simple it is. 😞

 

That does not changes the fact, that routine is not good enough...

 

(defun c:EOUT (/ spol sall sint srem i n e)

  (if (and (setq spol (ssget "_X" '((0 . "LWPOLYLINE") (410 . "Model") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>"))))
           (setq sall (ssget "_X" '((0 . "~IMAGE") (410 . "Model") (-4 . "<AND") (-4 . "<NOT") (-4 . "<OR") (8 . "Tx-Viewport") (8 . "Tx-Contour") (-4 . "OR>")  (-4 . "NOT>") (-4 . "AND>"))))
           (setq srem (ssadd))
           )
    (repeat (setq i (sslength spol))
      (if (setq sint (ssget "_CP" (mapcar 
                                    '(lambda (x) (trans x 0 1))
                                    (mapcar
                                      'cdr
                                      (vl-remove-if-not
                                        '(lambda (x) (= (car x) 10))
                                        (entget (ssname spol (setq i (1- i)))))))))
        (repeat (setq n (sslength sint))
          (if (ssmemb (setq e (ssname sint (setq n (1- n)))) sall)
            (ssadd e srem))))))
  
  (if (and srem
           (< 0 (sslength srem))
    (command "_.ERASE" sall "_R" srem ""))
  (princ)
)

Giving you a chance to guess. The reason: GURCYNARVFERIREFRQWHFGGLCRHPFJBEYQ

Go HERE if you give up.

 

msarqui
Collaborator
Collaborator

You are kidding me? "Just" that?

Wow!

 

Just for the record, I think you missed a parenthesis here:

(< 0 (sslength srem)) )

I am really glad that you found the answer. I could not imagine that a UCS can do such "damage" in a routine.
Thanks for your help, it is priceless.

 

Marcelo