Move Distance Based On User Input

Move Distance Based On User Input

Anonymous
Not applicable
1,481 Views
11 Replies
Message 1 of 12

Move Distance Based On User Input

Anonymous
Not applicable

Good afternoon everyone.

 

I am curious as to how I would create a LISP to take a user input and based on that input move and object a predefined distance.  

 

(defun C:MoveObj ()

   "Please input Floor:" 

   if input = 01
   (command "_.move" "_all" "" "_non" "500,500,500" "_non" "0,0,0")

   if input = 02
   (command "_.move" "_all" "" "_non" "250,250,250" "_non" "0,0,0") 

   if input = 1
   (command "_.move" "_all" "" "_non" "-500,-250,0" "_non" "0,0,0")

   Else

   (\n Not a valid input)
(princ))

 

A rough outline for sure. Any help would be very much appreciated. 

0 Likes
Accepted solutions (1)
1,482 Views
11 Replies
Replies (11)
Message 2 of 12

Ranjit_Singh
Advisor
Advisor
Accepted solution

Many ways to do this, I typically use cond. Initget with getkword might be another way. see autoCAD documentation here

 

(defun c:somefunc  (/ a)
 (setq a (getstring "\nSelect Move Choice [01/02/1]:"))
 (cond ((= a "01") (command "_.move" "_all" "" "_non" "500,500,500" "_non" "0,0,0"))
       ((= a "02") (command "_.move" "_all" "" "_non" "250,250,250" "_non" "0,0,0"))
       ((= a "1") (command "_.move" "_all" "" "_non" "-500,-250,0" "_non" "0,0,0"))
       (T (print "Not a valid choice!")))
(princ))
Message 3 of 12

john.uhden
Mentor
Mentor

I think this is what you are trying to do.

Please study and learn.

Your "move" commands are perfect.

 

(defun C:MoveObj ( / input)
  (setq input (getstring "\nPlease input Floor: "))
  (cond
    ((= input "01")
      (command "_.move" "_all" "" "_non" "500,500,500" "_non" "0,0,0")
    )
    ((= input "02")
      (command "_.move" "_all" "" "_non" "250,250,250" "_non" "0,0,0")
    )
    ((= input "1")
      (command "_.move" "_all" "" "_non" "-500,-250,0" "_non" "0,0,0")
    )
    ((princ "\n Not a valid input"))
  )
  (princ)
)

To make it more professional, we should add error and undo controls plus command echoing turned off.

Also, as it is it will error if there's nothing to move either because there's nothing there or all things are on locked layers.

John F. Uhden

Message 4 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

   "Please input Floor:" 

   if input = 01
   (command "_.move" "_all" "" "_non" "500,500,500" "_non" "0,0,0")

   if input = 02
   (command "_.move" "_all" "" "_non" "250,250,250" "_non" "0,0,0") 

   if input = 1
   (command "_.move" "_all" "" "_non" "-500,-250,0" "_non" "0,0,0")

   Else

   (\n Not a valid input)
.... 


If you want, you can eliminate that "Else" not-valid possibility by forcing the User to give a valid input:

 

(initget "01 02 1")

(setq input (getkword "\nPlease input Floor [01/02/1]: "))

 

If they type in anything else, it will scold them and ask again:

 

Please input Floor [01/02/1]: 8
Invalid option keyword.
Please input Floor [01/02/1]:

 

And it will keep doing so until they do give a valid input, before going on to the appropriate Move command in a (cond) function such as has already been suggested, but without that final possible condition, since one of the earlier ones will always be met.  So with the wrong input (even if it's only accidentally hitting a wrong key), they won't have to restart the routine, but will be able to redeem themselfes within it.

 

EDIT:  A further suggestion....

 

Any time I see a bunch of code repeated numerous times, I figure there's probably a way to eliminate the redundancy, with some rearrangement.  Put the (cond) function inside the Move command, since the starting point of the Move displacement is the only thing that differs depending on the input:

 

(command "_.move" "_all" "" "_non"
  (cond
    ((= a "01") "500,500,500")
    ((= a "02") "250,250,250")
    ((= a "1") "-500,-250,0")
  ); cond
  "_non" "0,0,0"
); command

 

Kent Cooper, AIA
Message 5 of 12

Anonymous
Not applicable

If one were to have some objects already in a drawing, is it possible to move just new objects that are loaded in with your code?  For example, if I had a drawing in progress, can I use your code to adjust the position of a XREF I load in?  If "Some Drawing.dwg" and "Some Other Drawing.dwg" are loaded in after I have work already in progress in my file, can your code be adjusted so that just those 2 files be moved and the rest of the objects remain where they currently live?  Something like the following

 

(defun c:somefunc  (/ a)
(command "-xref" "Overlay" "R:\\Some Drawing.dwg" "0,0,0" "" "" "")
(command "-xref" "Overlay" "R:\\Some Other Drawing.dwg" "0,0,0" "" "" "") (setq a (getstring "\nSelect Move Choice [01/02/1]:")) (cond ((= a "01") (command "_.move" "_all" "" "_non" "500,500,500" "_non" "0,0,0")) ((= a "02") (command "_.move" "2 Drawings" "" "_non" "250,250,250" "_non" "0,0,0")) ((= a "1") (command "_.move" "2 Drawings" "" "_non" "-500,-250,0" "_non" "0,0,0")) (T (print "Not a valid choice!"))) (princ))

 

0 Likes
Message 6 of 12

Anonymous
Not applicable

I love the edit.  Makes the code very clean. i will have to incorporate it. Thanks! 

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

If one were to have some objects already in a drawing, is it possible to move just new objects that are loaded in ...?  ... 


Try something like this [also incorporating my other suggestions]:

 

(defun c:somefunc  (/ ToBeMoved a)
  (setq ToBeMoved (ssadd)); initially empty selection set
  (command "-xref" "Overlay" "R:\\Some Drawing.dwg" "0,0,0" "" "" "")
  (ssadd (entlast) ToBeMoved); put it in the selection
  (command "-xref" "Overlay" "R:\\Some Other Drawing.dwg" "0,0,0" "" "" "")
  (ssadd (entlast) ToBeMoved); put it in the selection
  (initget "01 02 1")
  (setq a (getkword "\nSelect Move Choice [01/02/1]: "))
  (command "_.move" ToBeMoved "" "_non"
    (cond
      ((= a "01") "500,500,500")
      ((= a "02") "250,250,250")
      ((= a "1") "-500,-250,0")
    ); cond
    "_non" "0,0,0"
  ); command
  (princ)
); defun

 

Kent Cooper, AIA
Message 8 of 12

Anonymous
Not applicable

Brilliant! 

0 Likes
Message 9 of 12

john.uhden
Mentor
Mentor

Um, er, why doesn't he just put them where they belong in the first place?

It's sorta like drawing a bunch of polygons and then wondering how to convert them to circles.

Or, like installing a new window and then deciding you wanted it somewhere else.

Back in '98 my wife decided to relocate a number perennials.  One plant I moved 5 times, the last place was to where it was first located.

PLAN A

            h

              e

                a

                  d

John F. Uhden

0 Likes
Message 10 of 12

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

Um, er, why doesn't he just put them where they belong in the first place?

....


Certainly a good point.  They could ask the User for their selection first, and then set the appropriate insertion point based on a similar (cond) function, and use that in the Xref commands.  The Xrefing part was not an aspect of the original question, so things carried on from that beginning....

Kent Cooper, AIA
0 Likes
Message 11 of 12

john.uhden
Mentor
Mentor

That's the answer...

Things just get carried on.

John F. Uhden

0 Likes
Message 12 of 12

Ranjit_Singh
Advisor
Advisor

@Anonymous wrote:

If one were to have some objects already in a drawing, is it possible to move just new objects that are loaded in with your code?  For example, if I had a drawing in progress, can I use your code to adjust the position of a XREF I load in?  If "Some Drawing.dwg" and "Some Other Drawing.dwg" are loaded in after I have work already in progress in my file, can ..........

 


Defining "new objects" is kind of relative. One could say newer objects since last save? or maybe xrefs since last save? or any AutoCAD entity created since the last time somefunc was run (maybe even entities that were edited?) As you can see it can get pretty complicated. I would suggest just allow the user to make a selection. I would recommend provide an additional input parameter like "custom", then when the user says "custom" let them make the selection manually. You could get fancier and give an option for "XREF" and then force to pick only XREFS.

(defun c:somefunc (/ a)
(setq a (getstring "\nSelect Move Choice [01/02/1/custom]:"))
(cond ((= a "01") (command "_.move" "_all" "" "_non" "500,500,500" "_non" "0,0,0"))
((= a "02") (command "_.move" "_all" "" "_non" "250,250,250" "_non" "0,0,0"))
((= a "1") (command "_.move" "_all" "" "_non" "-500,-250,0" "_non" "0,0,0"))
((= a "custom") (command "_.move" (ssget) "" "_non" "-500,-250,0" "_non" "0,0,0"))
(T (print "Not a valid choice!")))
(princ))

 

0 Likes