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

struck with problem while.

14 REPLIES 14
SOLVED
Reply
Message 1 of 15
syedarshad
592 Views, 14 Replies

struck with problem while.

Dear all,

 

I am just a begineer for auto lisp.  I wrote my first program thanks to afra lisp, but got struck,  I am unable to get out from while loop if i Press "enter".  Please find the program below.

(defun c:arsh ()
(while
(setq p1 (getpoint "\n pick point :"))
(setq l 1500)
(setvar "osmode" 0)
(initget 7)
(setq p2 (polar p1 (dtr 0) l))
(initget 7)
(setq p3 (polar p2 (dtr 270) (/ l 2)))
(initget 7)
(setq p4 (polar p3 (dtr 180) l))
(command "line" p1 p2 p3 p4 "c")
(setvar "osmode" 111)
); end of while
(princ)
)

if any one could help me

14 REPLIES 14
Message 2 of 15
syedarshad
in reply to: syedarshad

sorry I did not included the dtr function.

 

(defun c:arsh ()
(while
(setq p1 (getpoint "\n pick point :"))
(setq l 1500)
(setvar "osmode" 0)
(initget 7)
(setq p2 (polar p1 (dtr 0) l))
(initget 7)
(setq p3 (polar p2 (dtr 270) (/ l 2)))
(initget 7)
(setq p4 (polar p3 (dtr 180) l))
(command "line" p1 p2 p3 p4 "c")
(setvar "osmode" 111)
); end of while
(princ)

)
(defun dtr (x)
(* pi (/ x 180.0))
)

Message 3 of 15
pbejse
in reply to: syedarshad

 

(while
(and (not (initget 128))
(setq p1 (getpoint "\n pick point :")))
 ....

 

Message 4 of 15
syedarshad
in reply to: pbejse

dear 

Thanks for your reply but still not working the following error occured,  plz see the command window msg.

 

Command: arsh
pick point :nil

Message 5 of 15
pbejse
in reply to: syedarshad

Really?

 

(defun c:arsh  ()
      (while
            (And
                  (not (initget 128))
                  (setq p1 (getpoint "\n pick point :")))
                 (setq l 1500)
                 (setvar "osmode" 0)
                 (initget 7)
                 (setq p2 (polar p1 (dtr 0) l))
                 (initget 7)
                 (setq p3 (polar p2 (dtr 270) (/ l 2)))
                 (initget 7)
                 (setq p4 (polar p3 (dtr 180) l))
                 (command "line" p1 p2 p3 p4 "c")
                 (setvar "osmode" 111)
                 )                      ; end of while
      (princ)
      )

 

Message 6 of 15
syedarshad
in reply to: pbejse

yes

I copied your lisp now I got this error.

 


Command: arsh
pick point :error :bad argument type: numberp:
Command:

Message 7 of 15
syedarshad
in reply to: syedarshad

thanks very much!

I got it.

 

I dont know how i got it but i just reopened cad and ran the program worked fine.

 

thanks very much

 

hoping for help in future also:smiley happy:

Message 8 of 15
pbejse
in reply to: syedarshad


@syedarshad wrote:

yes

I copied your lisp now I got this error.

 


Command: arsh
pick point :error :bad argument type: numberp:
Command:


Odd.....

 

EDIT:

<< I dont know how i got it but i just reopened cad and ran the program worked fine.>>

okay then.. you had me worried there for a while syedarshad

 

Cheers

 

Message 9 of 15
Kent1Cooper
in reply to: syedarshad

Could the problem have something to do with those (initget 7) lines in there?  I couldn't figure out what they could be accomplishing, so I commented them out, it works fine -- either Enter or space ends it.

 

Some other suggestions:  Those orthogonal directions are easy enough to define in relation to pi radians that it hardly seems worth bothering with a (dtr) conversion.  And I usually prefer not to bother with setting a variable that is used only once, instead just figuring that value at the place where it's needed.  The following also sets the l variable only once instead of setting it to the same value over again every time through the (while) loop.

 

(defun c:arsh (/ l p1)
  (setq l 1500)
  (while (setq p1 (getpoint "\n pick point :"))
    (setvar "osmode" 0)
    (command "_.line"

      p1

      (polar p1 0 l)

      (polar (getvar 'lastpoint) (* pi 1.5) (/ l 2))

      (polar p1 (* pi 1.5) (/ l 2))

      "c"

    ); command
    (setvar "osmode" 111)
  ); end of while
  (princ)
)

 

The following also works, if you don't mind the result being a closed Polyline rather than four independent Lines [which is preferable for several reasons, unless you have a reason to need them as Line entities].  It doesn't require turning Osnap off [it does it for each point designation], and sets the Osnap mode ahead of time since it doesn't need to turn it off and on inside the (while) loop.

 

(defun C:ARSH2 (/ l p1)
  (setvar 'osmode 111)
  (setq l 1500)
  (while
    (setq p1 (getpoint "\nPick point: "))
    (command "_.rectangle" "_none" p1 "_none" (mapcar '+ p1 (list l (/ l -2.0) 0)))
  ); while

  (princ)
); defun

 

The one that draws Lines could similarly not bother turning Osnap off and back on, if you preface each point with "_none" -- you just need to do that more times than with a Rectangle.

 

[Of course, if l is always going to be 1500, you could omit it as a variable entirely, and build the values in:

 

(defun C:ARSH3 (/ p1)
  (setvar 'osmode 111)
  (while
    (setq p1 (getpoint "\nPick point: "))
    (command "_.rectangle" "_none" p1 "_none" "@1500,-750")
  ); while

  (princ)
); defun

 

but I assume you have it as a variable either to more easily build different versions for different sizes, or to ask the User for a value in the final form of the routine, or something.]

Kent Cooper, AIA
Message 10 of 15
syedarshad
in reply to: Kent1Cooper

Thanks very much for your views, i appreciate your concern,
I will take into consideration. But to be clear with you That was my first program, i have just started and hope fully I want to master all apis related to CAD so I started with Lisp.

Any ways good people like you around will help people like me to get through.

Once again I am really thankful to you from bottom of heart.

Sent from my iPhone
Message 11 of 15
pbejse
in reply to: Kent1Cooper


@Kent1Cooper wrote:

Could the problem have something to do with those (initget 7) lines in there?  I couldn't figure out what they could be accomplishing, so I commented them out, it works fine -- either Enter or space ends it.

 


Whoa. i did'nt notice that. Smiley Happy True enough it does matter.

I was so too pre-occupied with  why the while function fails from the start and did not bother to check the rest.

 

Good catch Kent.

 

 

 

Message 12 of 15
Kent1Cooper
in reply to: pbejse


@pbejse wrote:

@Kent1Cooper wrote:

Could the problem have something to do with those (initget 7) lines in there?  I couldn't figure out what they could be accomplishing....


Whoa. i did'nt notice that. ... True enough it does matter.

.... 

Good catch Kent.


Thank you.  At first I didn't think about why it would matter, but simply noticed (initget) being used without a (get[something]...) function that typically follows it, for which it would be imposing limitations on the User's response.  So since I merely didn't know why they were there, I just tried eliminating them, and it worked.

 

Thinking about it some more, I realized why it matters.  Any (initget) "waits around" until some kind of (get...) function comes along that it can apply itself to, apparently however long that takes.  In this case, after the first point is picked, the (getpoint) function in every pass through that (while) loop is being affected by the last (initget) in the previous pass through the loop, because that hasn't had a chance to do its thing yet.  [I assume, though I'm not positive, that the last one in each pass overrides those before it that also haven't had a chance to operate on anything yet.]  Since (initget 7) means "No Enter, no zero, no negative for the next (get...) function," therefore the (getpoint) functions won't allow Enter.  It's the 1 bit in the binary makeup of the 7 that forbids Enter, so my guess [without trying it] is that if it were (initget 6) instead, or using any even number, the problem would go away.  The (initget) functions would still not be doing anything, as far as I can tell, but at least they wouldn't be preventing Enter in response to that (getpoint) function.

Kent Cooper, AIA
Message 13 of 15
scot-65
in reply to: syedarshad

Welcome to the wonderful life of LISP programming.

 

Another method of escaping the while loop is to study this:

 

(setq p1 (getpoint "Specify first point: "))

(while p1

 

 [do your thing here]

 

 (setq p1 (getpoint "Specify next point: "))

);while

 

scot-65 🙂

 


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.


Message 14 of 15
syedarshad
in reply to: scot-65

Thank you all.

Thats really a good idea for asking for a point and not passing.

 

best regards,

 

Syed Arshad

 

 

Message 15 of 15
alanjt_
in reply to: scot-65


@scot-65 wrote:

Welcome to the wonderful life of LISP programming.

 

Another method of escaping the while loop is to study this:

 

(setq p1 (getpoint "Specify first point: "))

(while p1

 

 [do your thing here]

 

 (setq p1 (getpoint "Specify next point: "))

);while

 

scot-65 🙂

 


Another similar method (example for collecting points and returning a list):

 

((lambda (/ lst pnt)
   (if (car (setq lst (list (getpoint "\nSpecify first point: "))))
     (while (setq pnt (getpoint (car lst) "\nSpecify next point: "))
       (setq lst (cons pnt lst))
     )
   )
   (reverse lst)
 )
)

 

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

Post to forums  

Autodesk Design & Make Report

”Boost