Jump to content

Complex looping


Delta-Sword

Recommended Posts

I have a problem with looping a function to a previous point. :x

 

i have written a small program to

1. choose a layer name

2. check to see if the layer all ready exists

if it does

3a. tell user it exists

3b. ask if they want to add to the existing layer.

3c. if the want to add to it set it as current.

-if they dont want to add to it loop back to choosing the layer name

 

4. if the layer doesnt exist make it.

 

(DEFUN C:SETLAYER ()

(SETQ PREVLAYER  (GETVAR "CLAYER"))
**********
(SETQ LAYERNAME  (GETSTRING "\n ENTER NAME FOR NEW LAYER : "))
(SETQ CHECKLAYER (TBLSEARCH "LAYER" LAYERNAME ))

( IF CHECKLAYER 
   (PROGN 
       (PROMPT "\n LAYER ALREADY EXISTS : ")
       (SETQ CONTINUE (GETSTRING "\n ADD TO CURRENT LAYER? [Y/N] : "))

           (IF ( = CONTINUE Y )
               (COMMAND "-LAYER" "M" LAYERNAME "")
               (-------)
           )
   )
   (COMMAND "-LAYER" "M" LAYERNAME "")
)

(PRINC)
)
(PRINC)

 

In the Code i want to loop from the (------)

back up to the ***** but only if they select N.

 

i hope someone can help. cos im pulling my hair out. what ever is left of it :x:x:x:x:x

Link to comment
Share on other sites

I have a problem with looping a function to a previous point. :x

 

i have written a small program to

1. choose a layer name

2. check to see if the layer all ready exists

if it does

3a. tell user it exists

3b. ask if they want to add to the existing layer.

3c. if the want to add to it set it as current.

-if they dont want to add to it loop back to choosing the layer name

 

4. if the layer doesnt exist make it.

 

(DEFUN C:SETLAYER ()

(SETQ PREVLAYER  (GETVAR "CLAYER"))
**********
(SETQ LAYERNAME  (GETSTRING "\n ENTER NAME FOR NEW LAYER : "))
(SETQ CHECKLAYER (TBLSEARCH "LAYER" LAYERNAME ))

( IF CHECKLAYER 
   (PROGN 
       (PROMPT "\n LAYER ALREADY EXISTS : ")
       (SETQ CONTINUE (GETSTRING "\n ADD TO CURRENT LAYER? [Y/N] : "))

           (IF ( = CONTINUE Y )
               (COMMAND "-LAYER" "M" LAYERNAME "")
               (-------)
           )
   )
   (COMMAND "-LAYER" "M" LAYERNAME "")
)

(PRINC)
)
(PRINC)

 

In the Code i want to loop from the (------)

back up to the ***** but only if they select N.

 

i hope someone can help. cos im pulling my hair out. what ever is left of it :x:x:x:x:x

 

 

 

Try this: You can change the function name to your own choice.

 

(DEFUN C:SETLAYER ()
(SETQ PREVLAYER  (GETVAR "CLAYER"))
(INPUT)
)
(DEFUN INPUT ()
(SETQ LAYERNAME  (GETSTRING "\n ENTER NAME FOR NEW LAYER : "))
(SETQ CHECKLAYER (TBLSEARCH "LAYER" LAYERNAME ))
( IF CHECKLAYER 
   (PROGN 
       (PROMPT "\n LAYER ALREADY EXISTS : ")
       (SETQ CONTINUE (GETSTRING "\n ADD TO CURRENT LAYER? [Y/N] : "))
           (IF ( = CONTINUE Y )
               (COMMAND "-LAYER" "M" LAYERNAME "")
               (INPUT)
           )
   )
   (COMMAND "-LAYER" "M" LAYERNAME "")
)
(PRINC)
)
(PRINC)

Link to comment
Share on other sites

THANK YOU SOOOOO MUCH

 

have been looking through all manner of places for a special looping function.

 

:\ didnt find one tho

 

Thanks again :)

Link to comment
Share on other sites

This will probably give you the most control over the loop behaviour:

 

(defun c:setlayer (/ lay)
 (while
   (progn
     (setq lay (getstring t "\nSpecify Layer Name: "))
     (cond ((eq "" lay) nil) ; Exit loop
           ((not (snvalid lay))
            (princ "\n** Invalid Layer Name **"))
           ((tblsearch "LAYER" lay)
            (princ "\n** Layer Already Exists **")
            (initget "Yes No")
            (if (eq "No" (getkword "\nAdd to Current Layer? [Y/N] <Yes> : "))
              nil ; Exit loop
              (not (setvar "CLAYER" lay)))) ; Exit loop
           (t (command "_.-layer" "_M" lay "") nil)))) ; Exit Loop
 (princ))

Link to comment
Share on other sites

This will probably give you the most control over the loop behaviour:

 

(defun c:setlayer (/ lay)
 (while
   (progn
     (setq lay (getstring t "\nSpecify Layer Name: "))
     (cond ((eq "" lay) nil) ; Exit loop
           ((not (snvalid lay))
            (princ "\n** Invalid Layer Name **"))
           ((tblsearch "LAYER" lay)
            (princ "\n** Layer Already Exists **")
            (initget "Yes No")
            (if (eq "No" (getkword "\nAdd to Current Layer? [Y/N] <Yes> : "))
              nil ; Exit loop
              (not (setvar "CLAYER" lay)))) ; Exit loop
           (t (command "_.-layer" "_M" lay "") nil)))) ; Exit Loop
 (princ))

 

The way to go Lee!

Link to comment
Share on other sites

hrmmm interesting :)

 

i see what it does basically but could you explain the

((not (snvalid lay))

 

and

 

((eq "" lay) nil)

and just the how each of the parts works.

 

i have only started using lisps in last few weeks.

 

many thanks

Link to comment
Share on other sites

hrmmm interesting :)

 

i see what it does basically but could you explain the

((not (snvalid lay))

 

and

 

((eq "" lay) nil)

and just the how each of the parts works.

 

i have only started using lisps in last few weeks.

 

many thanks

 

snvalid checks the symbol table name for valid characters

 

((eq "" lay) nil) says if enter or blank information is supplied then exit the loop.

 

All this is better error trapping.

Link to comment
Share on other sites

i see what it does basically but could you explain the

((not (snvalid lay))

 

and

 

((eq "" lay) nil)

and just the how each of the parts works.

 

Pretty much echoeing what Buzzard said,

 

snvalid will check for dodgy characters, like * for example in the layer name.

 

If the user hits enter on the layer name prompt, an empty string is returned (""), hence the loop will exit. Tbh, its just one long wrapped test expression, no functions are executed within the main "loop". :)

 

I would be inclined to use either vla-add or entmake to create the layer though, as it is much faster and much more reliable, but if you are just starting, (command "_.-layer"..) is quick and easy :)

Link to comment
Share on other sites

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