Jump to content

Please help me with this "while loop"


MarcoW

Recommended Posts

Hello,

 

Just for the learning process, I want to make a selection set of blocks, then mirror each block 'one by one". Therefore I need to count and subtract 1 with eacht loop...

 

How do I get the block one by one?

 

My code below, the red part is what is wrong!

Thanks for the help.

 

ps.: the routine is not special, it's just to learn / know.

 

 

(defun c:test (/)
 (setq oldfiledia (getvar "filedia"))
 (setvar "filedia" 0)
 (prompt
   "\t« « You should now select one ore more blocks » »"
 )
 (if (setq ss (ssget '((0 . "INSERT"))))
   (progn (setq i (sslength ss))
          (while (not (minusp (setq i (1- i))))
            [color=red](setq blst (entget (ssname ss i))[/color]
                  mpt1 (getpoint "Mirrorline point 1")
                  mpt2 (getpoint "Mirrorline point 2")
            )
            (command "_.mirror" [color=red]blst[/color] mpt1 mpt2)
          )
   )
   (princ "Nothing selected!!")
 )
 (setvar "filedia" oldfiledia)
 (princ)
)
(princ)
;Code partial comes from "a lesson" LeeMac once taught me :-)
; I need to localize and make some error trap, I know...;-)

Link to comment
Share on other sites

Hey MarcoW

 

[color=red](setq blst (entget (ssname ss i))[/color]

 

Retuns an elist but mirror will only accept an ename or a selection set

 

(setq blst (ssname ss i))

 

(command "_.mirror" blst "" mpt1 mpt2 ....

Link to comment
Share on other sites

just a couple of typical newbie errors but a good attempt(keep it up)

there are several ways to use a while loop this correction i made is just one

 

 
(defun c:test (/)
 (setq oldfiledia (getvar "filedia"))
 (setvar "filedia" 0)
 (prompt
   "\t« « You should now select one ore more blocks » »"
 )
 (if (setq ss (ssget '((0 . "INSERT"))))
   (progn (setq i (sslength ss))
      (setq cnt 0);_counter
          (while (< cnt i)     
            (setq blst (ssname ss cnt);_did not need entget this gets the entname
                  mpt1 (getpoint "Mirrorline point 1")
                  mpt2 (getpoint "Mirrorline point 2")
            )
     ;_added "" after blst to tell it no more abjects will be selected
     ;_added "no" to the question to delete source object
            (command "_.mirror" blst "" mpt1 mpt2 "no" "")
     (setq cnt (1+ cnt));_add 1 to the counter
          );_while
   )
   (princ "Nothing selected!!")
 )
 (setvar "filedia" oldfiledia)`
 (princ)
)
(princ)

Link to comment
Share on other sites

Just a little editing...

 

(defun c:Test (/ i ss mp1 mp2)
 (if (setq i  -1
           ss (ssget "_:L" '((0 . "INSERT")))
     )
   (while (setq e (ssname ss (setq i (1+ i))))
     (and (setq mp1 (getpoint "\nSpecify first mirror point: "))
          (setq mp2 (getpoint mp1 "\nSpecify next mirror point: "))
          (command "_.mirror" e "" "_non" mp1 "_non" mp2)
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

Everyone thanks for helping me out! Great learning. Allthoug I will keep some questions, but of course!

 

First this: my routine still failed... "Points must be distinct" :x

I had no clue, but figured it had something to do with "osnap". Turning it of was not working. Just before I posted the routine my eye catched this (red part below):

 

Just a little editing...

 

(defun c:Test (/ i ss mp1 mp2)
 (if (setq i  -1
           ss (ssget "_:L" '((0 . "INSERT")))
     )
   (while (setq e (ssname ss (setq i (1+ i))))
     (and (setq mp1 (getpoint "\nSpecify first mirror point: "))
          (setq mp2 (getpoint mp1 "\nSpecify next mirror point: "))
          (command "_.mirror" e "" [color=red][b]"_non"[/b][/color] mp1 [color=red][b]"_non"[/b][/color] mp2)
     )
   )
 )
 (princ)
)

 

That such thing can cause that kind of trouble :huh:

Well, Alanjt, thanks for that one.

 

What I did is create a routine to mirror my blocks and then mirror them again. If you give this a thought, you will find it is not like nothing happened!!

 

Here is my routine, any critics are very welcome! I have needed some time for this to create and I know it is messy and not the way it should be. Like the variablenames, what about them? Are the too long?

 

I use a lot of "old school" lisp like (command "_.mirror"......).

Is this a way to avoid? Is it slow...?

 

As said: I am ready for some serious comment :oops:.

 

Thanks in advance!!

 

 
; double mirror function
; 21-10-2010 MarcoW
; great help ~ CADTutor ~
(defun c:test (/ mirrorlinepoint1 mirrorlinepoint2
              selectionset numberblocks counter
              blocklist ent entdata newinspt
              newrot newinspt2 rotdgr)

 (setq mirrorlinepoint1 (getpoint "Mirrorline point 1")
       mirrorlinepoint2 (getpoint mirrorlinepoint1 "Mirrorline point 2")
 )
 (prompt
   "          « « You really should select one ore more blocks now...» »"
 )
 (if (setq selectionset (ssget '((0 . "INSERT"))))
   (progn (setq numberblocks (sslength selectionset))
          (setq counter 0)
          (while (< counter numberblocks)
            (setq blocklist (ssname selectionset counter))
            (command
              "_.mirror"
              blocklist
              ""
              mirrorlinepoint1
              mirrorlinepoint2
              "no"
              ""
              )
            (setq ent (entlast))
            (setq entdata (entget ent))
            (setq newinspt  (cdr (assoc 10 entdata))
                  newrot    (cdr (assoc 50 entdata))
                  newinspt2 (polar newinspt (+ newrot pi) 50)
            )
            (setq rotdgr (/ (* newrot 180) pi))
            (command "_.mirror"
                     (entlast)
                     ""
                     "_NON"
                     newinspt
                     "_NON"
                     newinspt2
                     "Yes"
                     ""
            )
            (setq counter (1+ counter))
          )
   )
   (princ "Nothing selected!!")
 )
 (princ)
)
(princ)

 

Edit: I was wondering, now I filter for "INSERTS". Because other stuff like lines etc. will not work. HOw ould I need to approach it so my routine (test) does it's job for all selected blocks, and all that is not selected is "normally mirrored".

Link to comment
Share on other sites

Yes Alan, you did, the code is much shorter too.

But I was happy with what I had achieved allready :-)

 

There's nothing wrong with the way you did it. I was just showing you another route. I hope you didn't take it as me saying your code was garbage by comparison, because that's EXACTLY what I was getting at.:wink:

 

just kidding.

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