What is the error of this logic?

What is the error of this logic?

adaptacad
Advocate Advocate
1,079 Views
7 Replies
Message 1 of 8

What is the error of this logic?

adaptacad
Advocate
Advocate

What is the error of this logic?
Why does not the loop work?
How to make it work?

(defun c:test (/ v1)
  (initget "A B ")
  (while
    (setq v1 (getkword "\n [A/B]: "))
    (if (= v1 "A")
      (Command "_.Insert" "c:\\Test\\Test.Dwg" "_Scale" 1 pause 0 "_.explode" "_last")
      (Command "_.Insert" "c:\\Test\\Test1.Dwg" "_Scale" 1 pause 0 "_.explode" "_last")
      )
    )
  )
0 Likes
Accepted solutions (1)
1,080 Views
7 Replies
Replies (7)
Message 2 of 8

J-Rocks
Collaborator
Collaborator
Accepted solution

 

(while (progn (initget 6 "A B")
              (setq v1 (getkword "\n [A/B]: "))
       )
  (if (= v1 "A")
   ( ;;;;;;
                       

 

Message 3 of 8

john.uhden
Mentor
Mentor

In addition to what @adaptacad wisely told you, you might consider inserting the block (DWG) exploded...

(Command "_.Insert" "*c:\\Test\\Test.Dwg" "_Scale" 1 pause 0)

The main difference is that inserting and then exploding creates a block definition in your drawing that might be either unwanted overhead or blocking the way for a new definition to be inserted later.

John F. Uhden

Message 4 of 8

ВeekeeCZ
Consultant
Consultant

@adaptacad wrote:

What is the error of this logic?
Why does not the loop work?

The (initget) establishes keywords for use by the next user-input function call.

So the first run is Ok. But for the second and every other there is NO (initget). You need to add that into a loop:

(while 

  (initget) ; test-expression??

  (getkword)

  ....)

 

But the issue with a (while) loop is that the first expression is a test-expression. At the code above the (initget) became a test-expression. But we don't need that - we need somehow skip the (initget) because the (getkword) must be a test-expression. So one way is to wrap both functions using (progn).

(while (progn 

  (initget) (getkword)

  ) ; end of progn

  ...)

 

But we can also use the (or) function. Try to figure out why this works as good as progn.

(while (or

              (initget)

              (getkword)

            ) ; end of or

...)

 

Message 5 of 8

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

... you might consider inserting the block (DWG) exploded...

(Command "_.Insert" "*c:\\Test\\Test.Dwg" "_Scale" 1 pause 0)

....


The one thing that you might consider a disadvantage of doing it that way is that it won't "drag" as you move the cursor to place it -- you won't see it to position it.  But if you know where it's supposed to go without seeing it drag, it shouldn't matter.

Kent Cooper, AIA
Message 6 of 8

roland.r71
Collaborator
Collaborator

Wouldn't it be easier to just test v1 for a value?

 

(defun c:getAB (/ v1)
   (while (= v1 nil)
      (initget 6 "A B")
      (setq v1 (getkword "\n [A/B]: "))
      (cond
         ((= v1 "A")
            (Command "_.Insert" "c:\\Test\\Test.Dwg" "_Scale" 1 pause 0 "_.explode" "_last")
         )
         ((= v1 "B")
            (Command "_.Insert" "c:\\Test\\Test1.Dwg" "_Scale" 1 pause 0 "_.explode" "_last")
         )
      )
   )
)

 

note: I turned the if into a cond since it's way better for this kind of thing, as it's easy to expand to more then just 2 choices. (C/D/E etc.) - BUT - an if should work just fine too.

 

edit:

Note 2: I also changed the function name to "getAB" as i'm allergic to functions called "test" Smiley Tongue

Message 7 of 8

ВeekeeCZ
Consultant
Consultant

@roland.r71 wrote:

Wouldn't it be easier to just test v1 for a value?

 

(defun c:getAB (/ v1)
   (while (= v1 nil)
      (initget 6 "A B")
      (setq v1 (getkword "\n [A/B]: "))
...

I guess that's a matter of personal preference and/or complexity of the case. In general, why not to solve the zero case right within a test expression? You need to be sure, that no line within while' body does not fail on zero case. Sure, the cond function can take care of it pretty easily... but its better to overcome this if you can.  

 

Anyway, the point was to explain the principle. You've shown other possible solution...

 

BTW for same reason I prefer (not v1) instead of (= v1 nil). I think its more obvious.

Message 8 of 8

adaptacad
Advocate
Advocate

Thank you, you guys are awesome.!!

0 Likes