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

Lisp Routing Works Once or Twice and then Stops

10 REPLIES 10
Reply
Message 1 of 11
baberndt
828 Views, 10 Replies

Lisp Routing Works Once or Twice and then Stops

Hello!

 

I am very new to AutoLisp programming.  I am currently using Autocad 2013 and this is my first try on writing one.

 

Sometimes it works great, sometimes it will only work once.  I'm wondering if I am missing some basic component of a program that may cause this?

 

The program is suppose to insert sequential numbers, at a specified direction into a drawing.  Sometimes it gets to the ".text" command but does not execute this.

 

I have attached the file if anyone happens to have a chance to look at it.

 

Thanks!

 

- Barb

Tags (3)
10 REPLIES 10
Message 2 of 11
_Tharwat
in reply to: baberndt

Try it now and let me know .

 

(defun c:numbering (/ anno txht dir dirrad sp snum enum distpref dist
                    temploc tempnum
                   )
  (initget "Yes No")
  (setq anno (cond ((getkword "\n Is text annotative? (Y/N) <N> "))
                   ("No")
             )
  )
  (if (null anno)
    (setq txht (getreal "\nEnter Text Height: "))
  )
  (if (= anno "No")
    (setq txht (getreal "\nEnter Text Height: "))
  )

  (initget "0 90 180 270")
  (setq dir
         (getkword
           "\nEnter dirction of text from first entry in degrees (0,90,180,270) "
         )
  )
  (princ "Direction chosen is: ")
  (princ dir)
  (princ)

  (cond
    ((= dir "0") (setq dirrad 0))
    ((= dir "90") (setq dirrad (/ pi 2.)))
    ((= dir "180") (setq dirrad pi))
    ((= dir "270") (setq dirrad (* 1.5 pi)))
  )
  (princ "\nDirection chosen in radians is: ")
  (princ dirrad)
  (princ)
  (initget 1)
  (setq sp (getpoint "\nPick middle point of first text entry: "))
  (setq snum (getint "\nEnter start number: "))
  (setq enum (getint "\nEnter end number: "))
  (initget "P E")
  (setq distpref
         (getkword
           "\nWould you like to give distance by picking 2 points (P) or entering a number (E)? <P or E> "
         )
  )
  (cond
    ((= distpref "P")
     (setq dist (getdist "\nPick 2 points on screen: "))
    )
    ((= distpref "E")
     (setq
       dist (getreal
              "\nEnter distance between middle of text positions: "
            )
     )
    )
  )
  (setq temploc sp)
  (setq tempnum snum)
  (repeat (+ 1 (- enum snum))
    (cond
      ((= anno "No")
       (command "_.text" "m" "_none" temploc txht 0 tempnum)
      )
      ((= anno "Yes")
       (command "_.text" "m" "_none" temploc 0 tempnum)
      )
    )
    (setq temploc (polar temploc dirrad dist))
    (setq tempnum (1+ tempnum))
  )
)


 

Message 3 of 11
baberndt
in reply to: _Tharwat

That works fabulously! 🙂 I'm going with it.

 

I noticed you changed the following

- How I got if the text was annotative

- The command ".text" to "_.text" and added "_none"

 

Do you have feel if which one of these may have cause the problem?  (Meaning caused to program to act goofy after an inital run.)

 

Thanks for your help!

 

 

 

Message 4 of 11
bgingerich
in reply to: baberndt

Just a couple of suggestions, baberndt:

(if (null anno) (setq txht (getreal "\nEnter Text Height: ")))
(if (= anno "No") (setq txht (getreal "\nEnter Text Height: ")))

 could be replaced with:

(if (/= anno "Yes") (setq txht (getreal "\nEnter Text Height: ")))

 and:

(cond 
  ((= distpref "P") (setq dist (getdist "\nPick 2 points on screen: ")))
  ((= distpref "E") (setq dist (getreal "\nEnter distance between middle of text positions: ")))
)

 could be replaced with:

(setq dist 
  (if (= distpref "P")
    (getdist "\nPick 2 points on screen: ")
    (getreal "\nEnter distance between middle of text positions: ")
  ); ends if
);ends setq

 

Also the last "cond" statement test whether anno = "yes" or "no" but it could be nil.  Might want to replace (= anno "No") with (/= anno "Yes")

 

 (if ("mysolution"=answer) then (click "Accept As Solution"))

 

─────────────────────────────────────────────────────────────────────────────────────────────
Brandon Gingerich
Message 5 of 11
_Tharwat
in reply to: baberndt


@baberndt wrote:

Do you have feel if which one of these may have cause the problem?  (Meaning caused to program to act goofy after an inital run.)


 

 


No , actually there is something very important in the begining of the routine that you did not notice , read the code

once again .

Hint "anno" variable . Smiley Happy

Message 6 of 11
baberndt
in reply to: _Tharwat

Ahhhhh, got it! 

 

Thank you both for the advice.  I really appreciate it!

 

Message 7 of 11
_Tharwat
in reply to: baberndt


@baberndt wrote:

Ahhhhh, got it! 

 

Thank you both for the advice.  I really appreciate it!

 


Good for you . Smiley Happy

Message 8 of 11
pbejse
in reply to: baberndt

So you know.

 

All this:

 

(initget "P E")
  (setq distpref (getkword "\nWould you like to give distance by picking 2 points (P) or entering a number (E)? <P or E> "))
  (cond
    ((= distpref "P") (setq dist (getdist "\nPick 2 points on screen: ")))
    ((= distpref "E") (setq dist (getreal "\nEnter distance between middle of text positions: ")))
    ) ; closes cond

 to

 

(setq dist (getdist sp "\nPick 2 points or Enter distance between middle of text positions on screen: "))

 

Message 9 of 11
pbejse
in reply to: pbejse

Another way to write the code

 

(defun c:numbering (/ anno  txht dirrad sp  snum  enum  dist
                    temploc tempnum )

  (if (zerop (cdr (assoc 40 (tblsearch "STYLE" (getvar 'textstyle)))))
    (setq txht (initget 1) txht  (getreal "\nEnter Text Height: "))
  )
  (initget 1)
  (setq sp (getpoint "\nPick middle point of first text entry: "))
  (initget 1)
  (setq dirrad (getangle sp "\nPick point for direction"))
  (princ (Strcat "Direction chosen is: " (angtos dirrad 0)))
  (initget 1)
  (setq snum (getint "\nEnter start number: "))
  (initget 1)
  (setq enum (getint "\nEnter end number: "))
  (initget 1)
  (setq dist
         (getdist
           sp
           "\nPick 2 points or Enter distance between middle of text positions on screen: "
         )
  )
  (setq temploc sp)
  (setq tempnum snum)
  (repeat (+ 1 (- enum snum))
    (command "_.text" "m" "_non" temploc)
    (if txht
      (command txht)
    )
    (command 0 tempnum)
    (setq temploc (polar temploc dirrad dist))
    (setq tempnum (1+ tempnum))
  )
  (princ)
)

 HTH

Message 10 of 11
_Tharwat
in reply to: baberndt

Was bored today and wanted to offer something new Smiley Very Happy

 

Try this ...

(defun c:Test (/ no st nd sp p pt rpt ang hop gap gr lst)
  (defun *error* (x)
    (if lst
      (foreach x lst (entdel x))
    )
    (princ "*Cancel*")
  )
  (defun _T (st nd pt sp ang / no e lst)
    (setq no st)
    (repeat (1+ (- nd st))
      (setq e (entmakex (list '(0 . "TEXT")
                              (cons 10 pt)
                              (cons 11 pt)
                              (cons 1 (itoa no))
                              (cons 40 (getvar 'textsize))
                              (cons 7 (getvar 'textstyle))
                              '(50 . 0.)
                        )
              )
      )
      (setq lst (cons e lst))
      (setq pt (polar pt ang sp)
            no (1+ no)
      )
    )
    lst
  )
  (if (and (if (not (not (eq 4 (logand 4 (cdr (assoc 70 (entget (tblobjname "LAYER" (getvar 'CLAYER)))))))))
             (progn (alert "Current layer is LOCKED <!> ") nil)
             t
           )
           (setq st (getint "\n Speficy START number :"))
           (setq nd (getint "\n Speficy END number :"))
           (if (> st nd)
             (progn (alert "START number must be smaller than the END number <!>") nil)
             t
           )
           (setq sp (getdist "\n Specify gap distance :"))
           (setq pt (getpoint "\n Specify base point  :"))
      )
    (progn (setq rpt (1+ (- nd st))
                 ang 0.
                 hop (* pi 0.25)
           )
           (setq lst (_T st nd pt sp 0.0))
           (while (or (eq (car (setq gr (grread t 15 0))) 5) (eq (cadr gr) 9))
             (redraw)
             (princ "\r *** Press TAB to have new prompt ***")
             (if (eq (cadr gr) 9)
               (progn (setq gap sp
                            p   pt
                            no  st
                      )
                      (if lst
                        (foreach x lst (entdel x))
                      )
                      (if (equal ang (+ pi pi))
                        (setq ang (* pi 0.25))
                        (setq ang (+ ang hop))
                      )
                      (setq lst (_T st nd pt sp ang))
               )
             )
           )
    )
  )
  (princ "\n Written by Tharwat Al Shoufi")
  (princ)
)

 Tharwat

Message 11 of 11
baberndt
in reply to: _Tharwat

Great to see the different approaches!  Best training around - try it yourself and then see how the pros do it 🙂

 

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost