Jump to content

Recommended Posts

Posted

I am just learning, no specific thing though...

If I have a while loop like below, how to get it to work that pressing a single key (without) enter will do something like (alert "Hello")?

 

 
(defun c:Test (/ pt1 input pt2 MyLine)
 (setq pt1 (getpoint "\nSPecify first point: "))
 ; (setq input (grread T 4 4)); not shure how to begin with this
 (while
   (and
     (/= (setq pt2 (getpoint pt1 "\nSpecify next point: ")) nil)
     ;(/= (car input) 1); like this?
     ) ;and
   (setq MyLine (list
    (cons 0 "LINE")
    (cons 10 pt1)
    (cons 11 pt2)
    ) ;_list
  ) ;_setq
   (entmake MyLine)
   (setq pt1 pt2)
   ) ;_while
 (princ)
 ) ;_defun

 

Wich key does not matter.

My problem is the grread function, the hjelp section does not clarify.:oops:

 

Thanks in advance!

Posted

Simple example...

 

(defun c:TEst (/ pt gr)
 (if (setq pt (getpoint "\nSpecify base point: "))
   (while (progn (setq gr (grread T 4 4))
                 (princ "\rSpecify point:        ")
                 (cond ((eq 5 (car gr)) (redraw) (grdraw pt (cadr gr) 7 -1) T)
                       ((member gr '((2 65) (2 97))) (alert "You pressed \"A\"") T)
                       ((eq 3 (car gr))
                        (entmakex (list '(0 . "LINE")
                                        (cons 10 (trans pt 1 0))
                                        (cons 11 (trans (cadr gr) 1 0))
                                  )
                        )
                       )
                 )
          )
   )
 )
 (redraw)
 (princ)
)

 

Follows pointer unless user left-clicks (draws line) and will display prompt if user types "A".

Posted

Hi Alan,

 

Thank you for the help.

 

I took a bit of time to really understand the code you provided. Since I am not familiar with grread / grdraw I have some questions. Care to explain some issues I have? See the code below.

 

Thanks in advance.

 

 
(defun c:TEst (/ pt gr)
 (if
   ; if the next line returns anything but nil
   (setq pt (getpoint "\nSpecify base point: "))
   ; then jump in the while loop
   (while
     ; while the next "wrapped together functions" are executed continuously
     (progn

; first functions: the variable gr should return any coordinate like (5 (0.000 5.5000)
; where the first 5 means pointing device and the 2 other are coordinates of the cursor at that time
; the result is a list.
; While in a loop this is a continuous returned item I guess.
(setq gr (grread T 4 4)); I don't get the second 4 as argument here
; second function: comment out that the user need to pick a point
                 (princ "\rSpecify point:        ")
; why doesn't it "stop" to prompt for user input? I mean all the rest is looping
; even if no point is picked
; third function: a cond where things are compared
(cond
  ; if the first item in the list "gr" is equal to 5 then redraw first (clear previous grdraw lines)
  ; next grdraw a line (vector ??) from the first picked point pt the coordinate as picked second (second item in list gr)
  ; grdraw arguments are "from" "to" "color" "linetype" so 7 = white, -1 = dashed/hidden
  ((eq 5 (car gr)) (redraw) (grdraw pt (cadr gr) 7 -1) T); I do not understand the T in here?
  ; if the list ((2 65) (2 97)) happens to be a part the list gr then alert out. Again the T ...?
  ((member gr '((2 65) (2 97))) (alert "You pressed \"A\"") T)
  ; I know chr 65 = A and chr 97 is a but how does this match in the list gr?
  ; if the first item in the list "gr" is equal to 3 then entmake a line
  ; I cannot understand the trans function because the 0 ... cant find the explaination
  ((eq 3 (car gr))
   (entmakex (list '(0 . "LINE")
                                        (cons 10 (trans pt 1 0))
                                        (cons 11 (trans (cadr gr) 1 0))
                                  )
                        )
                       )
                 )
          );_progn
   )
 )
 ; make shure all drawn vectors are erased
 (redraw)
 (princ)
)

 

btw.: I did search the help section & google to find some answers but I didn't find or understand.

Posted

I have rewritten the code, and dissected it with comments. Hopefully this will help you understand things Marco, please read the comments carefully.

 

[color=RED]([/color][color=BLUE]defun[/color] c:test [color=RED]([/color] [color=BLUE]/[/color] pt gr code data [color=RED])[/color]

 [color=RED]([/color][color=BLUE]if[/color] [color=RED]([/color][color=BLUE]setq[/color] pt [color=RED]([/color][color=BLUE]getpoint[/color] [color=#a52a2a]"\nSpecify First Point: "[/color][color=RED]))[/color]
   [color=RED]([/color][color=BLUE]progn[/color]
     [color=RED]([/color][color=BLUE]princ[/color] [color=#a52a2a]"\nSpecify Next Point: "[/color][color=RED])[/color]

     [color=RED]([/color][color=BLUE]while[/color]
       [color=RED]([/color][color=BLUE]progn[/color]
         [color=RED]([/color][color=BLUE]setq[/color] gr [color=RED]([/color][color=BLUE]grread[/color] [color=BLUE]t[/color] [color=#009900]15[/color] [color=#009900]0[/color][color=RED])[/color] code [color=RED]([/color][color=BLUE]car[/color] gr[color=RED])[/color] data [color=RED]([/color][color=BLUE]cadr[/color] gr[color=RED]))[/color]

         [color=RED]([/color][color=BLUE]cond[/color]
           [color=RED]([/color]
             [color=RED]([/color][color=BLUE]=[/color] [color=#009900]5[/color] code[color=RED])[/color]

             [color=RED]([/color][color=BLUE]redraw[/color][color=RED])[/color]
             [color=RED]([/color][color=BLUE]grdraw[/color]  pt data [color=#009900]-1[/color][color=RED])[/color]

             [color=BLUE]t[/color]
           [color=RED])[/color]
           [color=RED]([/color]
             [color=RED]([/color][color=BLUE]=[/color] [color=#009900]2[/color] code[color=RED])[/color]

             [color=RED]([/color][color=BLUE]alert[/color] [color=RED]([/color][color=BLUE]strcat[/color] [color=#a52a2a]"You pressed: "[/color] [color=RED]([/color][color=BLUE]chr[/color] data[color=RED])))[/color]

             [color=BLUE]t[/color]
           [color=RED])[/color]
           [color=RED]([/color]
             [color=RED]([/color][color=BLUE]=[/color] [color=#009900]3[/color] code[color=RED])[/color]

             [color=RED]([/color][color=BLUE]entmakex[/color]
               [color=RED]([/color][color=BLUE]list[/color]
                 [color=RED]([/color][color=BLUE]cons[/color] [color=#009900]0[/color] [color=#a52a2a]"LINE"[/color][color=RED])[/color]
                 [color=RED]([/color][color=BLUE]cons[/color] [color=#009900]10[/color] [color=RED]([/color][color=BLUE]trans[/color] pt   [color=#009900]1[/color] [color=#009900]0[/color][color=RED]))[/color]
                 [color=RED]([/color][color=BLUE]cons[/color] [color=#009900]11[/color] [color=RED]([/color][color=BLUE]trans[/color] data [color=#009900]1[/color] [color=#009900]0[/color][color=RED]))[/color]
               [color=RED])[/color]
             [color=RED])[/color]
             [color=RED]([/color][color=BLUE]setq[/color] pt data[color=RED])[/color]
           [color=RED])[/color]
         [color=RED])[/color]
       [color=RED])[/color]
     [color=RED])[/color]
   [color=RED])[/color]
 [color=RED])[/color]

 [color=RED]([/color][color=BLUE]redraw[/color][color=RED])[/color] [color=RED]([/color][color=BLUE]princ[/color][color=RED])[/color]
[color=RED])[/color]

[color=RED]([/color][color=BLUE]defun[/color] c:test [color=RED]([/color] [color=BLUE]/[/color] pt gr code data [color=RED])[/color]

 [color=#990099];; Define function, localise variables.[/color]

 [color=RED]([/color][color=BLUE]if[/color]

   [color=#990099];; If the following returns non-nil:[/color]

   [color=RED]([/color][color=BLUE]setq[/color] pt [color=RED]([/color][color=BLUE]getpoint[/color] [color=#a52a2a]"\nSpecify First Point: "[/color][color=RED]))[/color]

   [color=#990099];; Prompt for a point (the base point)[/color]
   
   [color=RED]([/color][color=BLUE]progn[/color]

     [color=#990099];; Wrap the following statements as one expression[/color]
     [color=#990099];; to be supplied to the IF function as its 'then' argument.[/color]
     
     [color=RED]([/color][color=BLUE]princ[/color] [color=#a52a2a]"\nSpecify Next Point: "[/color][color=RED])[/color]

     [color=#990099];; Print the above message to the user[/color]

     [color=RED]([/color][color=BLUE]while[/color]

       [color=#990099];; While the following test expression returns true[/color]
       
       [color=RED]([/color][color=BLUE]progn[/color]

         [color=#990099];; Wrap the following expressions into one expression[/color]
         [color=#990099];; so that it may be passed to the While function as its[/color]
         [color=#990099];; test expression argument.[/color]

         [color=#990099];; Note that *not* all of the expressions within the progn[/color]
         [color=#990099];; statement need to return non-nil, the progn expression[/color]
         [color=#990099];; will only return the value of the last evaluated expression[/color]
         [color=#990099];; this value will then be used to control the While loop.[/color]
         
         [color=RED]([/color][color=BLUE]setq[/color] gr [color=RED]([/color][color=BLUE]grread[/color] [color=BLUE]t[/color] [color=#009900]15[/color] [color=#009900]0[/color][color=RED])[/color]

           [color=#990099];; Monitor user input for every pass of the While loop[/color]
           [color=#990099];; GrRead pauses until it detects user input (you can test[/color]
           [color=#990099];; this by executing a single instance of it at the command[/color]
           [color=#990099];; line), then, when user input is detected it returned a list[/color]
           [color=#990099];; encoding type of input and any data associated with that input.[/color]

              code [color=RED]([/color][color=BLUE]car[/color] gr[color=RED])[/color]

           [color=#990099];; Get the type of input (e.g. 3=left-click, 5=drag, 2=keyboard...)[/color]

              data [color=RED]([/color][color=BLUE]cadr[/color] gr[color=RED])[/color]

           [color=#990099];; Get the value associated with that type of input[/color]
         [color=RED])[/color]

         [color=RED]([/color][color=BLUE]cond[/color]

           [color=#990099];; Still inside the progn expression (nothing as yet has been passed[/color]
           [color=#990099];; to the While function.[/color]

           [color=#990099];; Here we check the user input detected:[/color]
           
           [color=RED]([/color]
             [color=RED]([/color][color=BLUE]=[/color] [color=#009900]5[/color] code[color=RED])[/color] [color=#990099];; Mouse has been dragged [/color]

             [color=RED]([/color][color=BLUE]redraw[/color][color=RED])[/color]

             [color=#990099];; Clear the screen[/color]
            
             [color=RED]([/color][color=BLUE]grdraw[/color]  pt data [color=#009900]-1[/color][color=RED])[/color]

             [color=#990099];; Draw a vector from the base point to the current mouse position.[/color]
             [color=#990099];; Vector is drawn in XOR ink (since colour is -1).[/color]

             [color=#990099];; Note that both pt and data are expressed relative to the current UCS[/color]
             [color=#990099];; and that grdraw takes points expressed in UCS.[/color]

             [color=BLUE]t[/color]

             [color=#990099];; At this point, a condition in the COND stament has been evaluated,[/color]
             [color=#990099];; so no other COND statements will be evaluated and whatever is returned[/color]
             [color=#990099];; by this COND expression will be passed to the While loop as a test[/color]
             [color=#990099];; condition.[/color]

             [color=#990099];; We wish to stay in the loop at this point, so we pass a 't' since[/color]
             [color=#990099];; grdraw returns nil.[/color]
           [color=RED])[/color]
           [color=RED]([/color]
             [color=RED]([/color][color=BLUE]=[/color] [color=#009900]2[/color] code[color=RED])[/color] [color=#990099];; Keyboard has been pressed[/color]

             [color=RED]([/color][color=BLUE]alert[/color] [color=RED]([/color][color=BLUE]strcat[/color] [color=#a52a2a]"You pressed: "[/color] [color=RED]([/color][color=BLUE]chr[/color] data[color=RED])))[/color]

             [color=#990099];; Provide an Alert detailing the pressed key.[/color]

             [color=BLUE]t[/color]

             [color=#990099];; Stay in the Loop.[/color]
           [color=RED])[/color]
           [color=RED]([/color]
             [color=RED]([/color][color=BLUE]=[/color] [color=#009900]3[/color] code[color=RED])[/color] [color=#990099];; Left-Click of the mouse[/color]

             [color=RED]([/color][color=BLUE]entmakex[/color]
               [color=RED]([/color][color=BLUE]list[/color]
                 [color=RED]([/color][color=BLUE]cons[/color] [color=#009900]0[/color] [color=#a52a2a]"LINE"[/color][color=RED])[/color]
                 [color=RED]([/color][color=BLUE]cons[/color] [color=#009900]10[/color] [color=RED]([/color][color=BLUE]trans[/color] pt   [color=#009900]1[/color] [color=#009900]0[/color][color=RED]))[/color]
                 [color=RED]([/color][color=BLUE]cons[/color] [color=#009900]11[/color] [color=RED]([/color][color=BLUE]trans[/color] data [color=#009900]1[/color] [color=#009900]0[/color][color=RED]))[/color]
               [color=RED])[/color]
             [color=RED])[/color]

             [color=#990099];; Create a line from the base point to the clicked point.[/color]
             [color=#990099];; As previously mentioned, both 'pt' and 'data' are expressed[/color]
             [color=#990099];; relative to the current UCS; however, the start and end points[/color]
             [color=#990099];; in the DXF data for a LINE are expressed relative to the WCS.[/color]
             [color=#990099];; Hence a transformation is required, which can be accomplished using[/color]
             [color=#990099];; the trans function.[/color]
            
             [color=RED]([/color][color=BLUE]setq[/color] pt data[color=RED])[/color]

             [color=#990099];; Set the new base point as the endpoint of the last line created.[/color]
             [color=#990099];; No need for an explicit 't' here, since the above will always[/color]
             [color=#990099];; return a non-nil value and hence keep us in the loop.[/color]
           [color=RED])[/color]
         [color=RED])[/color]
         
       [color=RED])[/color] [color=#990099];; End Progn[/color]

       [color=#990099];; Note that the While function only has a 'test' expression.[/color]
       [color=#990099];; No other statements are executed within the while loop.[/color]
     [color=RED])[/color]
   [color=RED])[/color]
 [color=RED])[/color]

 [color=RED]([/color][color=BLUE]redraw[/color][color=RED])[/color] [color=RED]([/color][color=BLUE]princ[/color][color=RED])[/color]

 [color=#990099];; Finally, clear the screen and exit cleanly.[/color]
[color=RED])[/color]

Posted

Dirty work done for me again. I like this.

Posted

Hi Lee,

 

Thank you very much for the reply and the provided help within the code.

It has taken some time before I could respond, I am sorry for that.

But I did study the code (and explain) and can understand it pretty well.

I notice that there is no better learning than practicing. I used to "read" code.

That alone does not do it.

 

The trans function however I found to be a litle odd. I did come across it sometimes. But never paid real attention to it. Now I see the difference between the UCS and WCS. Wich brought me to a good tutorial on how to use "user coordinate system". Where "world" (or WCS) is kinda the original UCS and all the rest us called UCS... ahum. Since I am doing some 3D lately too, that came in handy.

 

Anyway, I have gotten exactly the response I needed, it helps a lot.

 

Now I need to keep the info in my head :-)

 

Thanks again,

regards,

MarcoW

 

ps.: (= "Lucky" "AlanJT")

Posted
Thank you very much for the reply and the provided help within the code.

It has taken some time before I could respond, I am sorry for that.

But I did study the code (and explain) and can understand it pretty well.

I notice that there is no better learning than practicing. I used to "read" code.

That alone does not do it.

 

You're very welcome Marco, and I completely agree - it takes practice to get better at the code, you'll learn from reading the code, but the information will stick a lot better when you try to write it yourself.

 

The trans function however I found to be a litle odd. I did come across it sometimes. But never paid real attention to it. Now I see the difference between the UCS and WCS. Wich brought me to a good tutorial on how to use "user coordinate system". Where "world" (or WCS) is kinda the original UCS and all the rest us called UCS... ahum. Since I am doing some 3D lately too, that came in handy.

 

Many people are sometimes confused by the 'trans' function. I would say the way to think about it is that there is no absolute coordinate frame: we can position our three orthogonal coordinate vectors in any orientation in 3D space and define a coordinate system relative to those vectors since they 'span' the space (i.e. you can describe every point in the space in terms of multiples of those three vectors).

 

Notice that the WCS is just another coordinate frame with the orthogonal basis vectors being the standard (1,0,0), (0,1,0) and (0,0,1).

 

Now, since there is no absolute coordinate frame, points may be defined relative to different Coordinate frames, hence we need a way to transform these points between coordinate frames. That is where the 'trans' function comes in.

 

Hope this helps with the understanding.

Posted

Yes it does Lee, thanks.

 

I tried the code and left out the 'trans' part. It did work as normal.

Then I moved UCS to a different position and there you have it, it's all "out of key".

Whenever I come across a problem like this I will remember the 'trans' function.

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