Author Topic: Having brain freeze..help !  (Read 1995 times)

0 Members and 1 Guest are viewing this topic.

RAIN CODE

  • Guest
Having brain freeze..help !
« on: April 16, 2013, 05:28:28 AM »
Hi guys,

I think I am having a brain freeze (Can't even write  a simple lisp).

I am trying to write a lisp that draws line continuously like the line command with a undo options but mine doesn't work as expected.

I want it to have gridsnap etc. below is my lisp and i spent few hours and still not working.
can someone look at it or see what you can come up with.

Thanks



   



;-----------------------------------------------------------------------
;draw path line
;-------------------------------------------------------
(defun c:DP ( / Spt)

  ;setting
  (setvar "orthomode" 1)
  (setvar "snapmode" 1)
  (setvar "gridmode" 1)
  ;why use setvar won't work ?? command only works !
  (command "GRIDUNIT" "450,450")
  (command "snapunit" "450,450")
  (command "snapbase" "30000,30000")

  ;refresh grid
  (command "_zoom" "e")
  (setq olay (getvar "clayer"))
  (command "_layer" "s" "path" "")
  (setq Npt t)
  (while Npt
    ;pick start pt if start pt is not picked
    (if (not Spt)
        (setq Spt (getpoint "\nPick start point for path line "))
        ;(setvar "lastpoint" Spt)
    )
   
    ;pick next pt
    (initget "u")
    (setq Npt (getpoint Spt "\nPick next point for path line "))
    (cond
      ((= Npt "u")
       (command "_u")       
       (setq Spt (getvar "lastpoint"))
      )
      (Npt
       (command "_line" Spt Npt "")       
       ;(setvar "lastpoint" Spt)
       (command "_line" "lastpoint" "@")
       (setq Spt Npt)
      )
    );cond
  );while
   
  (princ "\nPathLine Done.")
  (setvar "clayer" olay)
  (setvar "snapmode" 0)
  (setvar "gridmode" 0)
  (princ)
);defun c:DP

(princ "\ntemp draw PathLine-1.LSP loaded...type DP to start ")




 

   

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Having brain freeze..help !
« Reply #1 on: April 16, 2013, 05:49:51 AM »
Two issues:
1.
You are drawing a zero length line at the endpoint of each segment:
Code: [Select]
(Npt
  (command "_line" Spt Npt "")       
  ;(setvar "lastpoint" Spt)
  (command "_line" "lastpoint" "@") ; <<< zero length line.
  (setq Spt Npt)
)
2.
For the undo option to work properly you will have to store the old value of Spt.

But why 'rewrite' the line command?

RAIN CODE

  • Guest
Re: Having brain freeze..help !
« Reply #2 on: April 17, 2013, 02:52:36 AM »
sorry, I was still doing trial and error on that lisp. It was not the lisp I intended to post becos I was running out of cybercafe internet time and I quickly posted the wrong Lisp.

Initially I tried to use the Line command inside the lisp as shown below becos the line command offers a good undo feature.
But when the program run until the right bracket of the (command "_line") it goes straight down and completes the defun c:DP then continue the still on-running line command at the prompt. Without the grid snap etc becos it set back setvar variables.

Which is why I was trying to add in the line with undo feature into the lisp since I could not use the original line command. If adding a line command inside the lisp and could not get it to work...I am wondering how people write the Lisp with setting at the beginning of the lisp then the line command and lastly set back the setting as original, I am sure people like in my former company people have been using Line Lisp with setting revert back.

Initially I was thinking of writing this lisp, like
1) set up the grids and snap mode at 450,450 c/c then
2) run the original line command then
3) after line drawn and exit the line command then set back the setting (gridmode etc) to original.

But it is harder then it looks, I did not expect the program to complete the lisp then continue the line command which is still running.

;-----------------------------------------------------------------------
;draw path line that snap at every 450 grids
;-------------------------------------------------------
(defun c:DP ( / Spt)

  ;setting
  (setvar "orthomode" 1)
  (setvar "snapmode" 1)
  (setvar "gridmode" 1)
  ;why use setvar won't work ?? command only works !
  (command "GRIDUNIT" "450,450")
  (command "snapunit" "450,450")
  (command "snapbase" "30000,30000")

  ;refresh grid
  (command "_zoom" "e")
  (setq olay (getvar "clayer"))
  (command "_layer" "s" "path" "")
 
   (command "_line")

  (princ "\nPathLine Done.")
  (setvar "clayer" olay)
  (setvar "snapmode" 0)
  (setvar "gridmode" 0)
  (princ)
);defun c:DP

(princ "\ntemp draw PathLine-2.LSP loaded...type DP to start ")

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Having brain freeze..help !
« Reply #3 on: April 17, 2013, 03:27:16 AM »
Try using this:
Code: [Select]
(command "_line")
(while (=/ (getvar 'cmdactive) 0)
  (command pause)
)

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Having brain freeze..help !
« Reply #4 on: April 17, 2013, 03:28:03 AM »
Code: [Select]
(defun C:DP ( / *error* var_list var_value)
 
  (setq var_list  '(orthomode snapmode gridmode gridunit snapunit snapbase clayer)
        var_value (mapcar 'getvar var_list))

  (defun *error* (msg)
    (and msg (princ msg))
    (mapcar 'setvar var_list var_value)
    (princ)
    )
 
  (mapcar 'setvar var_list '(1 1 1 (450 450) (450 450) (30000 30000) "path"))
 
  (command "_line")
  (while (= 1 (getvar 'cmdactive))
    (command "\\")
    )

  (*error* nil)
  (princ)
  )

RAIN CODE

  • Guest
Re: Having brain freeze..help !
« Reply #5 on: April 18, 2013, 03:59:52 AM »
Thanks Roy and Stefan

Your Lisp works like a charm and with the undo feature too. (It took me few hours to write mine and even that it didn't work to my expectation).

I learned few things from your lisp, Stefan. especially the mapcar.
Just not so use to the mapcar thing and I have always wanted to use the mapcar after learning about it when CAB posted his lisp some time back.
Guess I missed the chance to use it in this 'draw path' lisp until I read your post. But I will make more effort on it to see if the mapcar and lambda can be used in any lisp that I am doing now.

You know something, I have still a lot to learn about Lisp after I see the short-cut that some of you guys wrote (like the Russian master, Cab, Lee Mac, Roy, irneb and you and some others too, in this forum. You guys are outstanding. I wish I could write as good as you guys)

Even though this 'Pacman' game I have written, it works but there are many areas that can be written in a better or shorter way. As I go back to read the whole programs and notice few area can be simplified and I did simplified and improve it (like the legendary Bill Gates once said he likes to rewrite his program so as to improve it).

There will be some areas that can be improved and I may have missed it as I myself could not see it (the short-cut) as it is beyond my knowledge, only the people who can write better Lisp will be able to see it and give advice. I am thinking maybe later when I post the updated game, can someone be kind enough to at least let me know like which area (at least some pointer) can be written in a better way, I would be grateful to that.

Not the whole program maybe like when you read through my code and see...'ah, this area can be simplified..' then post part of that code.

Thanks in advance.








« Last Edit: April 18, 2013, 04:05:55 AM by RAIN CODE »