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

While loop issue

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
565 Views, 5 Replies

While loop issue

Hello all,

 

I'm creating a code for work and the only hold up I have now is this while loop below:

 

(while (< cnttotal occurances)
  (setq e1 (nth cnttotal order))
  (setq e2 (if (= (+ 1 cnttotal) occurances)
    (progn
      (setq pieces (append pieces (list e1)))
      (setq cnttotal (+ 1 cnttotal))
      )
    (nth (+ 1 cnttotal) order)
    )
  )
  (setq pt1 (cadr (cadr e1)))
  (setq pt2 (cadr (car e2)))
  (setq dif (abs (- pt1 pt2)))
  (if (equal dif jt 0.001)
  (progn
    (setq addendum (list (car e1) (cadr e2)))
    (if (= cnttotal 0)
      (setq pieces (append (list pieces) (list addendum)))
      (setq pieces (append pieces (list addendum)))
      )
    (setq cnttotal (+ 2 cnttotal))
    )
  (progn
    (if (= cnttotal 0)
      (setq pieces (append (list pieces) (list e1)))
      (setq pieces (append pieces (list e1)))
      )
    (setq cnttotal (+ 1 cnttotal))
    )
    )
  )

 

Summary of overall code:

Creating a command that allows a user to window a "column" of lines, all at unique angles, and it will draw a stainless steel trim piece (using lines/polylines).  This part of the code here is trying to analyze if there is a 1/4" vertical joint (Y-direction) between two adjacent endpoints.  Because, if it is 1/4", I'm sure you can see that it is to ignore those two points and grab the last point of the following line.  Then, append the list to store for calculation purposes that follow this while loop.  Calculations are obviously for the stainless steel trim, which I will not go into detail here because I have a prototype code that works perfectly already.

 

Issue/Question:

I have another while loop right before this one with the same test-expression.  Why is this one spitting out the "error: bad argument type: consp 5"?  Am I getting tunnel vision and missing something?  

 

In terms of processing the lines of code, this while loop works perfectly fine (completes work required obviously).  However, it generates the error message above because the test-expression is nil.  The cnttotal= 5 and occurances=5... Which should stop the while loop.  However, this one is spitting out the message.

 

I hope someone can find what little, dumb mistake I might be making.  Or point out the flaw in my logic.

 

 

Thanks for the help in advance everyone!

Frost

5 REPLIES 5
Message 2 of 6
Kent1Cooper
in reply to: Anonymous

It's a little hard to say without knowing what kind(s) of things are contained in the list in the 'order' variable.  But I'm guessing it's in this portion:

 

  (setq e2 (if (= (+ 1 cnttotal) occurances)
    (progn; thenexpr
      (setq pieces (append pieces (list e1)))
      (setq cnttotal (+ 1 cnttotal)); <-- this is what will be returned and stored in 'e2' when (if) test above is satisfied
    ); end progn
    (nth (+ 1 cnttotal) order); elseexpr <-- this is what will be returned when (if) test above is NOT satisfied
    ); if
  ); setq

 

When 'cnttotal' is 4 [1 less than 'occurances' if that's 5], the (if) test at the top will be satisfied, and 'e2' will be set to an integer [5] by the last expression in the (progn) function.  When 'cnttotal" is any other value than one less than 'occurances', 'e2' will be set to hold an item from the 'order' list, whatever kind of thing that is, but I assume from later appearances of 'e2' that it is not an integer.  I expect that the error message is from when e2 is an integer, to which it can't apply (car), (cadr), etc.

Kent Cooper, AIA
Message 3 of 6
Anonymous
in reply to: Kent1Cooper

Thank you Kent!

 

I don't know what I was trying to do with this portion of the code.  I know what I was trying to do, but yes it definitely that entire batch of code.  Can't thank you enough for looking over this for me real quick Kent.  I'll revise this portion of the code real fast and let you know the outcome.

 

Next time it's going on midnight after looking at a code for several hours, I'll definitely call it a night before I go off and try to make revisions!

 

 

Will follow-up to confirm this is the issue,

Frost

Message 4 of 6
Anonymous
in reply to: Anonymous

That was it!  Revised my code to the following and works as should:

 

(while (< cnttotal occurances) ;; Spits out "error: bad argument type: consp 5" at end of loop... Not sure why
(setq e1 (nth cnttotal order))
(setq e2 (nth (+ 1 cnttotal) order))
(if (/= e2 nil)
(progn
(setq pt1 (cadr (cadr e1)))
(setq pt2 (cadr (car e2)))
(setq dif (abs (- pt1 pt2)))
(if (equal dif jt 0.001)
(progn
(setq addendum (list (car e1) (cadr e2)))
(if (= cnttotal 0)
(setq pieces (append (list pieces) (list addendum)))
(setq pieces (append pieces (list addendum)))
)
(setq cnttotal (+ 2 cnttotal))
)
(progn
(if (= cnttotal 0)
(setq pieces (append (list pieces) (list e1)))
(setq pieces (append pieces (list e1)))
)
(setq cnttotal (+ 1 cnttotal))
)
)
)
(progn
(setq pieces (append (list pieces) (list e1)))
(setq pieces (append pieces (list e1)))
(setq cnttotal (+ 1 cnttotal))
)
)
)

 

What I was trying to do with e2 was analyze it.  However, in "heat" of the moment, I tried compiling it all within e2 as you pointed out.  Had to break it up and get what I posted above.  Thanks again Kent!  Now, just troubleshooting a couple issues elsewhere in my code and I'll be good to go.

 

 

Thanks again Kent.  Much appreciated.

Message 5 of 6
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

That was it!  Revised my code to the following and works as should....  Thanks again Kent!  ....


Good!  You're welcome.  There are some other little things you can refine....

 

Rather than check whether a variable is not nil, you can just check whether it exists at all -- any non-nil return will satisfy an (if) test.  You can replace:
  (if (/= e2 nil)

with simply:

  (if e2

 

The various (c...r) functions can be combined/embedded [within certain limits beyond the needs of this routine].  You can take the r off the end of the containing one and the c off the beginning of the one nested inside it, and concatenate the function names:

  (cadr (car e2))
can be replaced with:

  (cadar e2)
and:

  (cadr (cadr e2))
can be replaced with:

  (cadadr e2)

 

You can set more than one variable in one (setq) function.  This:

  (setq pt1 (cadr (cadr e1)))
  (setq pt2 (cadr (car e2)))
  (setq dif (abs (- pt1 pt2)))

can be replaced with this:

  (setq

    pt1 (cadr (cadr e1))
    pt2 (cadr (car e2))
    dif (abs (- pt1 pt2))

  ); setq

[just remember to take the right parentheses that closed the separate (setq) functions off the ends of the separate variable lines when within one such function].

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

I've seen that in other codes across the web, but never tried to implement them myself.  Thanks for giving me the pointers!  Right now I think most things (like the setq functions) are line by line because it's easiest for me to just read like a book.  I'm a newbie to AutoLISP.

 

Been teaching myself AutoLISP the past few weeks and loving it.  Unfortunately, it seems like there is no literature that is "go-to" on the AutoLISP.  More of a teach yourself and "trial and error" approach from what I'm gathering on the web.  Found a few really good pieces on the web that explain functions like mapcar and lambda really well, but everything else is learned from Lee-Mac or Afra-LISP.

 

Anyways, thanks again Kent.

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

Post to forums  

Autodesk Design & Make Report

”Boost