How to stop loop using return integer value?

How to stop loop using return integer value?

aliff_ahtenk
Enthusiast Enthusiast
420 Views
4 Replies
Message 1 of 5

How to stop loop using return integer value?

aliff_ahtenk
Enthusiast
Enthusiast

Hai, long time no see, i want to make a loop lisp and stop when that particular number is returning the same value for 3 to 5 time. how do i call it to end? below is part of my lisp routine.

 

(defun C:Test (/ sset)
  (setq brf (ssget "_X" '((0 . "INSERT"))))
  (while (/= count 0)
         (setq count (sslength brf))
         (command "move" brf "" "d" "0,0,0")
  )
(princ)
)

 

Supposedly, during while function, i want to do if function, so it is like this,

if count "value" is the same for 3 or 5 times, it will stop the loop. and it will continue to do next command. coz some block cannot be explode it will not stop.

0 Likes
421 Views
4 Replies
Replies (4)
Message 2 of 5

pbejse
Mentor
Mentor

@aliff_ahtenk wrote:

Hai, long time no see, i want to make a loop lisp and stop when that particular number is returning the same value for 3 to 5 time. how do i call it to end? below is part of my lisp routine.

You need the "count" to increment at every turn.

(defun C:Test (/ count)
  (setq count 5);<-- say for example the number of objects selected
  (while (/= count 0)
         (print (strcat "Current count is " (itoa count)))
    	 (getstring "\nPress any key to continue")
    	(setq count (1- count));<-- increment the count variable by 1 less than the original value 
    )
(princ)
)

If the number of the intended loop is absolute , use repeat instead of while

(defun C:Test2 (/ count)
  (setq count 5);<-- say for example the number of objects selected
  (setq index 1)
  (repeat count
         (print (strcat "Current number of index " (itoa index)))
    	 (setq index (1+ index));<-- increment the index variable by 1 more than the original value 
    )
(princ)
)

As for your routine, it could be as simple as this

(defun C:Test ()
  (if (ssget "_X" '((0 . "INSERT")))
         (command "move" "P" "" "d" "0,0,0")
  )
(princ)
)

No need for a loop or counter.

 


@aliff_ahtenk wrote:

..coz some block cannot be explode it will not stop.


That is true, it's either you convert the block to be explodable, or flag the name in the next loop for exclusion.

Let us know if you need help with that.

 

HTH

 

 

 

0 Likes
Message 3 of 5

Kent1Cooper
Consultant
Consultant

@aliff_ahtenk wrote:

...,

if count "value" is the same for 3 or 5 times, it will stop the loop. and it will continue to do next command. coz some block cannot be explode it will not stop.


Is your code what you really intended?  There's no Explode operation in it, so I don't see how it relates to the part of the Message quoted above.  And what is the point of Moving something at a Displacement of 0,0,0?  Since you say it's part of your routine, I think we need to see more of it.

Kent Cooper, AIA
0 Likes
Message 4 of 5

aliff_ahtenk
Enthusiast
Enthusiast

@pbejse i wish to use shorter like you said but, block is literally to many, and some times got block inside block.

 

@Kent1Cooper i hope you dont mind, coz i am using others program from autocad, but it is almost the same, in autocad, it will have command explode and burst. i use burst mostly before changing to current application to avoid the information lost. my target is only block. i found other lisp routine but it use explode.

 

in early routine i have this code:

(defun C:Test (/ sset)
  (setq brf (ssget "_X" '((0 . "INSERT"))))
  (while (/= count 0)
         (setq count (sslength brf))
         (command "move" brf "" "d" "0,0,0")
         (command "select" "p" "" "pburst" "p" "")
  )
(princ)
)

so this code function, but it do infinite bursting due to no stopping function.

 

i work on other, like @pbejse say, use repeat but i got other issue say no function definition.

i make the routine goes 3 time running. if it giving the same number block remaning, it will stop, if not, it will run again. here are the code. some times, block were set to unexplodable so that make my first routine do the infinite burst.

(defun C:test (/ sset)
(defun C:brfabc ()
  (setq brfa (ssget "_X" '((0 . "INSERT"))))
  (setq counta (sslength brfa))
  (command "pburst" brfa "")

  (setq brfb (ssget "_X" '((0 . "INSERT"))))
  (setq countb (sslength brfb))
  (command "pburst" brfb "")

  (setq brfc (ssget "_X" '((0 . "INSERT"))))
  (setq counta (sslength brfc))
  (command "pburst" brfc "")
(princ)
)

  (c:brfabc)
  (if (= counta countb countc)(prompt "\ndone")
        (t c:brfabc)
  )
(princ)
)

i just put done in the end for now. i wish to extend the routine in future. you can change the "pburst" to "explode" or "burst". for move function, i just to make sure that it selecting the visible block only, but it does not work. i thought if putting like that it will ignore block on the freeze and thawn layer.

 

anyway what am i missing on the routine?

{error : no function definition <T> ; expected FUNCTION at [eval]} i got this.

 

 

0 Likes
Message 5 of 5

pbejse
Mentor
Mentor

Explode command have this unusual behavior when used inside a lisp, you have to include the undocumented QAFLAGS and set it to 1

(defun c:explodeall ()
  (setvar "qaflags" 1)
  (while (setq ss (ssget "x" (list (cons 0 "insert"))))
    (command "explode" ss "")
  )
  (setvar "qaflags" 0)
  (princ)
)

If you need to use the Express tool burst

(defun c:burstall ()
  (while (setq ss (ssget "x" (list (cons 0 "insert"))))
    (sssetfirst nil ss)
    (c:burst)
  )
  (princ)
)

And if you are referring to this as the source for "pburst" , i would suggest use "nburst" to deal with nested blocks

 


@aliff_ahtenk wrote:

...block were set to unexplodable so that make my first routine do the infinite burst.


And then there's that problem. You may then include a routine to ensure all the blocks are Explodeable, something similar to this

  (setq BlockObjects (tblnext "BLOCK" T))
      (while BlockObjects
            (cond ((and
                  (< (logior 2 (cdr(assoc 70 BlockObjects))) 3)
	          (setq Ent (entget (cdr (assoc -2 BlockObjects)))
		        Ent (entget (cdr (assoc 330 Ent))))
		(entmod (subst (cons 280  1)
                               (assoc 280 Ent) Ent)))
                   )
                  )
            (setq BlockObjects (tblnext "BLOCK"))
      )

HTH

 

0 Likes