Results 1 to 8 of 8

Thread: I can use a little bit of help with the WHILE function in my lisp

  1. #1
    I could stop if I wanted to
    Join Date
    2015-12
    Posts
    385
    Login to Give a bone
    0

    Default I can use a little bit of help with the WHILE function in my lisp

    Hello all,
    I am new to using WHILE in lsp Can someone please help me fix the posted lisp?
    I am under the impression that when I hit enter or right click, the function should end.

    Thanks all in advance

    Code:
    (defun c:crn (/ ocircno ocirccount ocircmulti circaddtext bname sp1 ocircadd)
      (cvo)
      
      (if (not circno)(setq circno 1))
      (if (not circmulti)(setq circmulti 1))
      (if (not circadd) (Setq circadd 1))
      (if (not circcount)(setq circcount 1))
      
      (setq circno (getreal (strcat "\nenter circuit number: ")))
      (setq circcount (getreal (strcat "\nenter number of outlets on circuit: ")))
      (setq circmulti (getreal (strcat "\nenter circuit number increments: ")))
    
      (while
        (progn
          (setq ocircno circno)
          (setq ocircmulti circmulti)
          (setq ocircadd circadd)
          (setq ocirccount circcount)
          
          (if (> circadd circcount)
            (progn
              (setq circno (+ circno circmulti))
              (setq ocircno circno)
              (setq circadd (+ circmulti 0))
              (setq ocircadd circadd)
              )
            )
    
          (command "-layer" "make" "E-POWR-TEXT" "" "")
          (command "-layer" "unlock" cclayer "" "")
    
          (setq sp1 (getpoint "\nSpecify point for text insertion: "))
    
          (setq bname "T:\\AEI CAD\\Electrical\\Symbols\\circuit number.dwg")
          (command "-insert" bname "S" cdimscale "r" 0 sp1)
          (command (strcat (rtos circno 2 0)))
    
          (cvr)
          
          (setq circadd (+ ocircadd 1))
          (princ (strcat "\nnumber = " (rtos circno 2 0) "     count = " (rtos circcount 2 0) "     increments = " (rtos circmulti 2 0)))
          (princ (strcat "\nCirc Add = " (rtos circadd 2 0)))
          )
        )
      (princ)
      )

  2. #2
    Member
    Join Date
    2009-06
    Posts
    16
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    Is your aim to keep asking for the circuit number, outlets & increments until one of these is nil (ie the user presses enter)?
    If it is you would put those questions as the condition for looping with an AND function and then place the remainder of the line inside the loop.
    Last edited by Steve.K; 2009-10-27 at 10:57 PM. Reason: Removed code

  3. #3
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    Yep, the 1st item in a while loop is the check. If it returns nil the loop is stopped. In the OP while there's a progn as the first item. Progn returns whatever it's last item returns, which in the OP case is a princ call. Thus the while will never end since princ never returns nil.

    BTW, you don't need the strcat's inside the getreal. You only use that when you need to combine the message with a variable.

    And another thing: sk.221493 seems to understand this a bit differently from me. I'd leave the 3 getreals outside the while and move the getpoint line to the check of the while.

  4. #4
    I could stop if I wanted to
    Join Date
    2015-12
    Posts
    385
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    Quote Originally Posted by irneb View Post
    BTW, you don't need the strcat's inside the getreal. You only use that when you need to combine the message with a variable.
    This I know, I was previously using a variable in the prompt and thought if I decide to add it again later, it will be easier to do.

    Quote Originally Posted by sk.221493 View Post
    Is your aim to keep asking for the circuit number, outlets & increments until one of these is nil (ie the user presses enter)?
    Actually my aim is to keep repeating the select point until the select point is nil.
    I am wanting the function to automatically provide the next number according to the increments and count number.

  5. #5
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    Quote Originally Posted by ReachAndre View Post
    Actually my aim is to keep repeating the select point until the select point is nil.
    I am wanting the function to automatically provide the next number according to the increments and count number.
    In which case follow my suggestion on the last paragraph in my previous post.

  6. #6
    Member
    Join Date
    2009-06
    Posts
    16
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    Quote Originally Posted by ReachAndre View Post
    Actually my aim is to keep repeating the select point until the select point is nil.
    I am wanting the function to automatically provide the next number according to the increments and count number.
    Quote Originally Posted by irneb View Post
    In which case follow my suggestion on the last paragraph in my previous post.
    So ReachAndre, try just moving spl to after while:
    Code:
    (while (setq sp1 (getpoint "\nSpecify point for text insertion: "))
             <stuff you want done in the loop>
             ...
           )

  7. #7
    Member
    Join Date
    2009-06
    Posts
    16
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    How'd you go with this?

  8. #8
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: I can use a little bit of help with the WHILE function in my lisp

    OK here's the code I had in mind:
    Code:
    (defun c:crn (/ ocircno ocirccount ocircmulti circaddtext bname sp1 ocircadd)
      (cvo)
    
      (if (not circno)
        (setq circno 1)
      ) ;_ end of if
      (if (not circmulti)
        (setq circmulti 1)
      ) ;_ end of if
      (if (not circadd)
        (Setq circadd 1)
      ) ;_ end of if
      (if (not circcount)
        (setq circcount 1)
      ) ;_ end of if
    
      (setq circno (getreal (strcat "\nenter circuit number: ")))
      (setq circcount (getreal (strcat "\nenter number of outlets on circuit: ")))
      (setq circmulti (getreal (strcat "\nenter circuit number increments: ")))
    
      ;; Only continue the loop if the user picked a point
      (while (setq sp1 (getpoint "\nSpecify point for text insertion: ")) ;Moved from below
        ;; (progn not needed
        (setq ocircno circno)
        (setq ocircmulti circmulti)
        (setq ocircadd circadd)
        (setq ocirccount circcount)
    
        (if (> circadd circcount)
          (progn
            (setq circno (+ circno circmulti))
            (setq ocircno circno)
            (setq circadd (+ circmulti 0))
            (setq ocircadd circadd)
          ) ;_ end of progn
        ) ;_ end of if
    
        (command "-layer" "make" "E-POWR-TEXT" "" "")
        (command "-layer" "unlock" cclayer "" "")
    
        ;; Remove the setq from here
        ;; (setq sp1 (getpoint "\nSpecify point for text insertion: "))
    
        (setq bname "T:\\AEI CAD\\Electrical\\Symbols\\circuit number.dwg")
        (command "-insert" bname "S" cdimscale "r" 0 sp1)
        (command (strcat (rtos circno 2 0)))
    
        (cvr)
    
        (setq circadd (+ ocircadd 1))
        (princ (strcat "\nnumber = "
                       (rtos circno 2 0)
                       "     count = "
                       (rtos circcount 2 0)
                       "     increments = "
                       (rtos circmulti 2 0)
               ) ;_ end of strcat
        ) ;_ end of princ
        (princ (strcat "\nCirc Add = " (rtos circadd 2 0)))
        ;; ) Progn not needed
      ) ;_ end of while
      (princ)
    ) ;_ end of defun
    My modifications marked in red. You also don't need the progn grouping inside a while loop. It wouldn't hurt to place it there, but it's not necessary.

Similar Threads

  1. New function in ObjectARX for LISP
    By erick_19_hk266024 in forum ARX
    Replies: 1
    Last Post: 2013-08-26, 12:46 PM
  2. My first lisp function...
    By JLHConsulting in forum AutoLISP
    Replies: 19
    Last Post: 2010-06-11, 06:06 PM
  3. changevpsettings .Net to LISP function
    By peter in forum Dot Net API
    Replies: 3
    Last Post: 2009-12-11, 03:44 PM
  4. Arrow OR Tab Key Function In Lisp?
    By omorah in forum AutoCAD Customization
    Replies: 2
    Last Post: 2008-11-20, 02:59 AM
  5. Using the inters function in Lisp
    By Lions60 in forum AutoLISP
    Replies: 5
    Last Post: 2007-07-09, 12:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •