Conditional nested in with loop not working like I need it to

Conditional nested in with loop not working like I need it to

Anonymous
Not applicable
1,077 Views
9 Replies
Message 1 of 10

Conditional nested in with loop not working like I need it to

Anonymous
Not applicable

Hi,

 

I've been tasked with working on an AutoLISP routine to get it to write out <Prefix> & <sequential number> as text on each click of the mouse while the insertion point is active - unless some key is depressed....great, except I haven't really done much in AutoLISP for ~25 years 😞  

 

All is not lost, that I've got works great if I take out my condition that the shift key is not down and I could use some help:

 

[CODE}

Content-Type: text; name="seq.lsp"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline; filename="seq.lsp"

(defun C:SEQ (/ base numb pt)
(setq base (getstring "\nPrefix: ")
numb (getint "\nBeginning Number: ")
ht (getdist "\nText height: "))
(while (setq pt (getpoint "\nInsertion point: "))
(entmake
(list
'(0 . "TEXT")
(cons 1 (strcat base (itoa numb)))
(cons 7 (getvar "textstyle"))
(cons 10 pt)
(cons 40 ht)
'(50 . 0)
) ;;list
) ;;entmake
;;;If the shift key is held down, don't iterate the number (numb), but keep it the same. Else, iterate numb += 1
(if (acet-sys-shift-down)
(setq numb (numb))
(setq numb (1+ numb))
);; if
) ;;while
(princ)
)

[/CODE]

 

I really appreciate any help and have a feeling I may have to call the function as a (normal programming) function would be called, pass in the arguments from the (would-be Sub) to it and return the "numb" parameter(?)

 

Thanks!

0 Likes
Accepted solutions (2)
1,078 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
Accepted solution

For what it's worth, I got it. Actually, it works out better for them if it doesn't index UNLESS they hold the shift down so it looks like this for any future folks' needs:

 

[CODE]

Content-Type: text; name="seq.lsp"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline; filename="seq.lsp"

(defun C:SEQ (/ base numb pt)
(setq base (getstring "\nPrefix: ")
numb (getint "\nBeginning Number: ")
ht (getdist "\nText height: "))
(while (setq pt (getpoint "\nInsertion point: "))
(entmake
(list
'(0 . "TEXT")
(cons 1 (strcat base (itoa numb)))
(cons 7 (getvar "textstyle"))
(cons 10 pt)
(cons 40 ht)
'(50 . 0)
) ;;list
) ;;entmake
;;;If the shift key is held down, iterate the number (numb). Else, keep it the same.
(if (acet-sys-shift-down)
(setq numb (1+ numb))
);; If
) ;;while
(princ)
)

[/CODE]

0 Likes
Message 3 of 10

Kent1Cooper
Consultant
Consultant

For comparison, or if you like anything about what's there better, there's an essentially identical topic >here<.   I posted a TagIncrement routine at Message 11 that isn't marked as a Solution, and uses a different method to keep the latest numerical part [an R option for Repeat], but has some features you may find useful:

::  You can choose the incrementing value  [it doesn't always have to be 1], which can also be negative  if you want to count downward;

::  It can specify a Prefix and a Suffix or both ;

::  It remembers your choices [of incrementing value, Prefix & Suffix] and also what number you got to, and offers them all as defaults on subsequent use, so you don't have to enter them again;

::  It shows you [at the command prompt area] what the content of the next Tag you place is going to be.

 

See more commentary at the top of the file, and also look into the other offerings on that thread.

Kent Cooper, AIA
0 Likes
Message 4 of 10

Anonymous
Not applicable

Very nice! If I need it, I've downloaded it. Thank you very much.

0 Likes
Message 5 of 10

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... it works out better for them if it doesn't index UNLESS they hold the shift down ....


 

The way that part works doesn't seem right to me.  If the numerical part of the last one I placed is 3, and I release the Shift key, the next one still increments up to 4, and only the one after that remains the same [at 4].  And after I've done some 4's, then I press down Shift again, the next one doesn't  increment up to 5, but that doesn't happen until the one after  that.  Is that really the way you want it to work?  You seem to need to know when you're going to want to change between incrementing and not one step in advance  of when you really want the change to occur.

Kent Cooper, AIA
0 Likes
Message 6 of 10

Anonymous
Not applicable

I noticed it lags by one too, when testing and showed the users what it was and, they do so many i.e. H4's that it was acceptable...but I do see what you mean. 

 

It's almost like the difference between a Do Until/Loop versus a Do/Loop Until where it's going to go through it that first time regardless on the Loop Until...

 

 

0 Likes
Message 7 of 10

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

I noticed it lags by one too....


 

Well, it occurred to me that this could be overcome by moving the check on the Shift key to a different place.  This adjustment works as I would prefer, BUT with one caveat:  if you want the numerical part to increment right from the start [no repeat of the first number], don't press down Shift until after you've placed the first one.  If you press it down before  you pick the first location, it will start with the number one higher than you asked for.  [It's likely that with an additional variable it could remove that restriction by checking whether or not you're placing the first one, but I didn't work that through.]

 

(defun C:SEQ (/ base numb pt)
  (setq
    base (getstring "\nPrefix: ")
    numb (getint "\nBeginning Number: ")
    ht (getdist "\nText height: ")
  ); setq
  (while (setq pt (getpoint "\nInsertion point: "))
    ;; if shift key is down, iterate number; else, keep the same.
    (if (acet-sys-shift-down) ;; <-- MOVED from after (entmake) to before
      (setq numb (1+ numb))
    );; If
    (entmake
      (list
        '(0 . "TEXT")
        (cons 1 (strcat base (itoa numb)))
        (cons 7 (getvar "textstyle"))
        (cons 10 pt)
        (cons 40 ht)
        '(50 . 0)
      ) ;;list
    ) ;;entmake
  ) ;;while
  (princ)
); defun
Kent Cooper, AIA
0 Likes
Message 8 of 10

Anonymous
Not applicable

Kent,

 

I'm not going to drive my 1968 Beetle after taking your 2019 Corvette Z06 for a spin, but thank you for pointing that out to me. Yours is just lightyears better and I appreciate you allowing me to (inspirationally borrow 🙂 ) it. 

 

Thanks and take good care!

 

-Daren

0 Likes
Message 9 of 10

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:
....  [It's likely that with an additional variable it could remove that restriction by checking whether or not you're placing the first one, but I didn't work that through.] ....

 

It was simpler than I expected to figure out a way to do that, so you can press Shift either before or after  the first pick, and you'll get the first number you asked for either way, and the next one will increment:

(defun C:SEQ (/ base numb pt next)
  (setq
    base (getstring "\nPrefix: ")
    numb (getint "\nBeginning Number: ")
    ht (getdist "\nText height: ")
  ); setq
  (while (setq pt (getpoint "\nInsertion point: "))
    ;; if shift key is down, iterate number; else, keep the same.
    (if
      (and
        next ; [will be nil at first pick]
        (acet-sys-shift-down)
      ); and
      (setq numb (1+ numb))
    );; If
    (entmake
      (list
        '(0 . "TEXT")
        (cons 1 (strcat base (itoa numb)))
        (cons 7 (getvar "textstyle"))
        (cons 10 pt)
        (cons 40 ht)
        '(50 . 0)
      ) ;;list
    ) ;;entmake
    (setq next T); i.e. past the first one
  ) ;;while
  (princ)
); defun
Kent Cooper, AIA
0 Likes
Message 10 of 10

Anonymous
Not applicable

Hi Kent,

 

One thing they did ask for, and I added, was the ability to change the angle of the text such that it can go to 90° and back to 0°. 

 

The only caveat to this is to get back to 0, you have to go back around to 360°...

 

But I thought I'd share with you and anyone else that needs it.

 

[CODE]


(defun C:TAGGING (/ Tag_GenString Tag_PrintVals oecho OldOSM ds th txt InsPt NumbStr)

(defun Tag_GenString ()
(setq NumbStr (itoa Tag_Numb))
(if Tag_PadTo
(while (< (strlen NumbStr) Tag_PadTo)
(setq NumbStr (strcat "0" NumbStr))
);While
);if PadTo
;build our string
(setq NumbStr
(cond
((and Tag_Pref Tag_Suff) (strcat Tag_Pref NumbStr Tag_Suff))
(Tag_Pref (strcat Tag_Pref NumbStr))
(Tag_Suff (strcat NumbStr Tag_Suff))
(T NumbStr)
);cond
);setq
NumbStr
);Tag_GenString

(defun Tag_PrintVals ()
(princ "\nCurrent Value: ") (princ (strcat "\"" (Tag_GenString) "\""))
(princ "\nPrefix = ") (princ Tag_Pref)
(princ "\tNumber = ") (princ Tag_Numb)
(princ "\tIncrement = ") (princ TAGGINGr)
(princ "\tPadding = ") (princ Tag_PadTo)
(princ "\tSuffix = ") (princ Tag_Suff)
(princ "\n")
);Tag_PrintVals

;set Acad variables
(setq oecho (getvar "cmdecho")
OldOSM (getvar "osmode")
)
(if (= 0 (getvar "dimscale"))(setq ds 1.0)(setq ds (getvar "dimscale")))
(setq th (getvar "dimtxt"))
(setq txt (* th ds))
(setvar "cmdecho" 0)
(setvar "osmode" 0)

;set defaults and/or ensure existence of required variables in the drawing session
;since these will be global variables help ensure they are specific to this function
;by adding the function name to them to help ensure that some other function
;doesn't use the same variables and set unusable values to them
(if (not Tag_Numb) (setq Tag_Numb 1))
(if (not TAGGINGr) (setq TAGGINGr 1))

(Tag_PrintVals)
(while (not (setq InsPt (getpoint (strcat
"\nPick Insertion point or \<Enter\> to change Current value: \("
(Tag_GenString) "\) : "
))))
(setq ModVal T)
(while ModVal
(Tag_PrintVals)
(initget "Prefix Number Increment Angle padTo Suffix eXit")
(setq ModVal (getkword "Value to Change: \[Prefix\/Number\/Increment\/Angle\/padTo\/Suffix\/eXit\] \<eXit\>: "))
(cond
((or (= ModVal "") (= ModVal "eXit")) (setq ModVal nil))
((= ModVal "Prefix") (setq Tag_Pref (getstring T "Enter New Prefix Value: \<Enter\> for none: ")))
((= ModVal "Number")
(progn
(initget 1)
(setq Tag_Numb (getint (strcat "Enter Starting Number: ")))
));Number cond
((= ModVal "Increment")
(progn
(initget 3)
(setq TAGGINGr (getint (strcat "Enter Increment Value: ")))
))
((= ModVal "Angle")
(progn
(initget 50)
(setq Text_Angle (getint (strcat "Enter Tagging Angle: ")))
));Increment Cond
((= ModVal "padTo")
(progn
(setq Tag_PadTo (getint (strcat "Enter desired length of number string: ")))
(if Tag_PadTo (if (< Tag_PadTo 2) (setq Tag_PadTo nil)))
));PadTo cond
((= ModVal "Suffix") (setq Tag_Suff (getstring T "Enter New Suffix Value: \<Enter\> for none: ")))
);cond stmt
);while ModVal option
);while not InsPt

(while InsPt
(Tag_GenString)
;create the text entity
(entmake (list (cons 0 "TEXT")
(cons 10 InsPt)
(cons 11 InsPt)
(cons 1 NumbStr) ; actual text
(cons 50 (rtd Text_Angle))
(cons 7 (getvar "TEXTSTYLE"))
(cons 40 txt)
(cons 72 4)
)
);entmake
(setq InsPt (getpoint
(strcat
"\nNext number location \[use shift key to repeat " "\'"
NumbStr "\"\]")))
(if (not (acet-sys-shift-down))
(setq Tag_Numb (+ TAGGINGr Tag_Numb))
);if
);while

;reset our variables
(setvar "cmdecho" oecho)
(setvar "osmode" OldOSM)
(princ)
);defun C:Tag

(princ "\n Type > TAGGING < to insert text with Incrementing numbers.")

(defun rtd (n)
(* pi (/ n 180.0))
)

[/CODE]

0 Likes