Author Topic: Help writing a simple lisp!  (Read 636 times)

0 Members and 1 Guest are viewing this topic.

Vikram

  • Newt
  • Posts: 42
Help writing a simple lisp!
« on: December 31, 2019, 02:44:35 AM »
I'm new at lisp programming and I need a lisp which will write the coordinates of the points which I select.However  the lisp I wrote only writes one coordinate at a time in text file. i want it to write in the file as I select points. Right now it only writes the last point I selected.

Code: [Select]
(defun c:test (/ p x y)
 (while
   (setq p (getpoint "\nPick Point: "))
    (setq fileName "C:\\Users\\Developer\\Desktop\\cir.txt")
    (progn (setq filename (open filename "w"))
                (write-line (strcat (rtos (car p) 2) "\t" (rtos (cadr p) 2) "\t") fileName)
                ;;;(write-line (strcat (rtos (car pt2) 2) "\t" (rtos (cadr pt2) 2) "\t" (rtos (caddr pt2) 2)) fileName)
           
           (close fileName)
    )
    (princ)
 )
)

Dlanor

  • Bull Frog
  • Posts: 238
Re: Help writing a simple lisp!
« Reply #1 on: December 31, 2019, 04:22:52 AM »

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ fp p)
  2.   (setq fp (open "C:\\Users\\Developer\\Desktop\\cir.txt" "w"))
  3.   (while (setq p (getpoint "\nPick Point: "))
  4.     (write-line (strcat (rtos (car p) 2) "\t" (rtos (cadr p) 2) "\t") fp)
  5.   )
  6.   (close fp)
  7.   (princ)
  8. )
  9.  

Vikram

  • Newt
  • Posts: 42
Re: Help writing a simple lisp!
« Reply #2 on: December 31, 2019, 05:14:47 AM »
Ah! It was so easy!
Thanks for the help and your time! :smitten:

Vikram

  • Newt
  • Posts: 42
Re: Help writing a simple lisp!
« Reply #3 on: January 22, 2020, 05:06:46 AM »
Dlanor I need your little more help! Its simple actually. I have edited the last code according to my needs but I dont know how to put If condition. I'm new to this programming

Code: [Select]
(defun c:rollp (/ fp p)
  (setq fp (open (strcat (getvar "dwgprefix")(vl-filename-base (getvar "dwgname")) ".roll") "w"))
  (setq nbs 0)
  (setq gbs 6)
  (while (setq p (getpoint(strcat "\nPick Roller S" (itoa (setq nbs (+ 1 nbs))) " position: ")))
    (write-line (strcat "Roller S" (itoa (setq nbs (+ 0 nbs))) "  X "(rtos (car p) 2) "\t" "Y "(rtos (cadr p) 2) "\t") fp)
  )
  (close fp)
  (princ)
)

I want to pick exactly 12 points. So when my variable nbs == 12 I want the Pick points to stop.
One more thing I wanted to add,

if(nbs<6)
(write-line (strcat "Roller S" (itoa (setq nbs (+ 0 nbs))) "  X "(rtos (car p) 2) "\t" "Y "(rtos (cadr p) 2) "\t") fp)
else
(write-line (strcat "Roller Tangents " (itoa (setq gbs (+ 0 gbs))) "  X "(rtos (car p) 2) "\t" "Y "(rtos (cadr p) 2) "\t") fp)
(+ 0 nbs)
---------------------------------------------------------
What Exactly I want is
while(nbs>12)
{
  int cnt=6;

if(nbs<6)
"Pick Roller S (nbs)";
writeline();
else
{" Pick Roller Tangent (cnt)";
writeline();
cnt++;
}
nbs++;

}

How to do this is Autolisp?
« Last Edit: January 22, 2020, 05:14:39 AM by Vikram »

Dlanor

  • Bull Frog
  • Posts: 238
Re: Help writing a simple lisp!
« Reply #4 on: January 22, 2020, 01:28:42 PM »
Two versions, as I wasn't sure exactly what you wanted. The top one asks for all the Roller S positions then all the Roller Tangents
The second asks for Roller S the Roller Tangent

Code - Auto/Visual Lisp: [Select]
  1. (defun c:rollp (/ fp cnt nbs gbs pt)
  2.   (setq fp (open (strcat (getvar "dwgprefix")(vl-filename-base (getvar "dwgname")) ".roll") "w"))
  3.   (setq cnt 0 nbs 0 gbs 6)
  4.   (while (< cnt 12)
  5.     (if (< cnt 6)
  6.       (progn
  7.         (setq pt (getpoint (strcat "\nPick Roller S" (itoa (setq nbs (1+ nbs))) " position: ")))
  8.         (write-line (strcat "Roller S" (itoa nbs) "  X "(rtos (car pt) 2) "\t" "Y "(rtos (cadr pt) 2)) fp)
  9.       );end_progn
  10.       (progn
  11.         (setq pt (getpoint (strcat "\nPick Roller Tangent" (itoa (setq gbs (1+ gbs))) " position: ")))
  12.         (write-line (strcat "Roller Tangents " (itoa gbs) "  X "(rtos (car pt) 2) "\t" "Y "(rtos (cadr pt) 2)) fp)
  13.       );end_progn
  14.     );end_if
  15.     (setq cnt (1+ cnt))
  16.   );end_while
  17.   (close fp)
  18.   (princ)
  19. );end_defun
  20.  
  21. ;; This will ask for roller S # position (# is 1 - 6) followed by Roller Tangent # position
  22. (defun c:rollp (/ fp cnt nbs pt)
  23.   (setq fp (open (strcat (getvar "dwgprefix")(vl-filename-base (getvar "dwgname")) ".roll") "w"))
  24.   (setq cnt 0 nbs 0)
  25.   (while (< cnt 12)
  26.     (if (= (rem cnt 2.0) 0.0)
  27.       (progn
  28.         (setq pt (getpoint (strcat "\nPick Roller S" (itoa (setq nbs (1+ nbs))) " position: ")))
  29.         (write-line (strcat "Roller S" (itoa nbs) "  X "(rtos (car pt) 2) "\t" "Y "(rtos (cadr pt) 2)) fp)
  30.       );end_progn
  31.       (progn
  32.         (setq pt (getpoint (strcat "\nPick Roller Tangent" (itoa nbs) " position: ")))
  33.         (write-line (strcat "Roller Tangents " (itoa nbs) "  X "(rtos (car pt) 2) "\t" "Y "(rtos (cadr pt) 2)) fp)
  34.       );end_progn
  35.     );end_if
  36.     (setq cnt (1+ cnt))
  37.   );end_while
  38.   (close fp)
  39.   (princ)
  40. );end_defun
  41.  
  42.  

Vikram

  • Newt
  • Posts: 42
Re: Help writing a simple lisp!
« Reply #5 on: January 22, 2020, 11:55:38 PM »
Thanks for the reply! I somehow managed to do it by myself  :-D.

Code: [Select]
(defun c:rollp (/ fp p)
  (setq fp (open (strcat (getvar "dwgprefix")(vl-filename-base (getvar "dwgname")) ".rta") "w"))
  (setq nbs 0)
  (while (< nbs 6)
 
  (setq p (getpoint(strcat "\nPick Roller S" (itoa (setq nbs (+ 1 nbs))) " position: ")))
    (write-line (strcat "Roller Position S" (itoa (setq nbs (+ 0 nbs))) "  X "(rtos (car p) 2) "\t" "Y "(rtos (cadr p) 2) "\t") fp)
  )

(write-line (strcat "\n") fp)
  (setq nbs 0)
  (while (< nbs 6)
 
  (setq p (getpoint(strcat "\nPick Roller S" (itoa (setq nbs (+ 1 nbs))) " tangent position: ")))
    (write-line (strcat "Roller Tangent S" (itoa (setq nbs (+ 0 nbs))) "  X "(rtos (car p) 2) "\t" "Y "(rtos (cadr p) 2) "\t") fp)
  )
  (close fp)
  (princ)
)