Jump to content

Problem with foreach (please an explanation)


Isaac26a

Recommended Posts

Hi everyone, maybe someone can help me understand what I was doing wrong, I was using this point file but could be any and I was doing this loop with foreach and it seemed to work fine but it always had this error at the end of the loop

(defun c:cme (/ a b c ct d e j l r s)
  (setvar "cmdecho" 0)
  (vl-cmdf "_.undo" "_begin")
  (cond	((setq s (ssget ":L" '((0 . "*TEXT,POINT") )))
	 (setq d 1.1)
     (foreach e (mapcar 'cadr (ssnamex s))
	   (if (wcmatch (cdr (assoc 0 (setq el (entget e))))"TEXT,MTEXT")
	     (setq a (cons (list (cdr (if (/= (assoc 10 el) '(0. 0. 0.)) (assoc 10 el) (assoc 11 el))) (cdr (assoc 1 el))) a))
	     (setq b (cons (list (cdr (assoc 10 el)) e) b))
	   )
	 )

the error I got was the following and it didn't matter if I changed the file the error was the same

error: bad argument type: lentityp (0 (12740.2 3185.31 0.0))

At the end I solved it with a while loop

(defun c:cme (/ a b c ct d e j l r s)
  (setvar "cmdecho" 0)
  (vl-cmdf "_.undo" "_begin")
  (cond	((setq s (ssget ":L" '((0 . "*TEXT,POINT") )))
	 (setq d 1.1)
     (setq ct 0
           l (sslength s))
     (while (< ct l)
           (if (wcmatch (cdr (assoc 0 (setq e (entget (ssname s ct))))) "TEXT,MTEXT")
               (setq a (cons (list (cdr (if (/= (assoc 10 e) '(0. 0. 0.)) (assoc 10 e) (assoc 11 e))) (cdr (assoc 1 e))) a))
               (setq b (cons (list (cdr (assoc 10 e)) (cdar e)) b))
           )
           (setq ct (1+ ct))
     )

But I just want to know what I was doing wrong or how could I have solved it with a foreach loop?

Thanks in advance for your kind help

Points.dwg

Link to comment
Share on other sites

Where you using this as your example?  ronjonp is using ssget with "X" option that select everything with out using mouse clicks. Your using ssget with the ":L" option that requires you to select things with your mouse. meaning the selection has all the entity's + the last point picked with the mouse and is shown like this when processed thought only (mapcar 'cadr

 

Quote

(<Entity name: 384ff500> <Entity name: 384ff180> <Entity name: 384ffb40> (0 (12762.6821816407 3114.44834460322 0.0)))

 

This means it will try enget on everything in that list.

<Entity name: 384ff500>  - ok

<Entity name: 384ff180>  - ok

<Entity name: 384ffb40>  - ok

(0 (12762.6821816407 3114.44834460322 0.0)) - errors here

 

You have to wrap the mapcar to remove any lists (aka point data) in the list so your only left with entity names.

(foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))

 

Quote

(<Entity name: 384ff500> <Entity name: 384ff180> <Entity name: 384ffb40>)

 

TLDR;

(foreach e (mapcar 'cadr (ssnamex s)) - use only with SSGET "X"
(foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))) - use with all other ssget

 

 

--edit

 

using (command deselects things so if you had stuff selected before running the command it won't be anymore and will have to re select everything. use the following to avoid (command allowing you to have things selected before you use the command.

 

(vl-load-com)
(vla-startundomark (setq Drawing (vla-get-activedocument (vlax-get-acad-object))) 
... code
(vla-endundomark Drawing) 

 

Edited by mhupp
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

42 minutes ago, mhupp said:

Yes indeed that was the example I was using, thank you for clarifying the error and the solution.

51 minutes ago, mhupp said:
(vl-load-com)
(vla-startundomark (setq Drawing (vla-get-activedocument (vlax-get-acad-object))) 
... code
(vla-endundomark Drawing) 

Is this way better than (vl-cmdf "._undo" "_begin")?, at first glance I think so.

Link to comment
Share on other sites

It's visual lisp. Won't work with older autocad I think 2000 and older. And like I said it allows you to keep things selected before the command is issued.

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