Author Topic: Set a default and ask the user for input  (Read 4902 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Set a default and ask the user for input
« Reply #15 on: December 08, 2018, 03:48:12 PM »
Here are some suggestions for your code:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:zigzag ( / *error* lay ltp scl val var )
  2.     ;; Define function and declare local variables
  3.  
  4.     ;; Define a local error handler to reset system variables in the event of an error
  5.     (defun *error* ( msg )
  6.         (mapcar 'setvar var val) ;; Reset system variables
  7.         (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")) ;; Suppress cancellation messages
  8.             (princ (strcat "\nError: " msg)) ;; Print critical errors
  9.         ) ;; end IF
  10.         (princ) ;; Suppress the value returned by the last evaluated expression
  11.     ) ;; end DEFUN
  12.  
  13.     (setq var '(cmdecho clayer celtype celtscale osmode) ;; List of system variables whose values will be modified
  14.           val  (mapcar 'getvar var) ;; Retrieve current values
  15.           ltp  "ZIGZAG" ;; Linetype to be set
  16.           lay  "CLOUD"  ;; Layer for polyline
  17.     ) ;; end SETQ
  18.    
  19.     (while ;; While the following expression returns a non-nil value
  20.         (progn ;; Evaluate the following expressions and return the value returned by the last expression
  21.             (initget 6) ;; Disallow zero or negative values
  22.             ;; Whilst the supplied value is not a member of this list:
  23.             (not (member (setq scl (cond ((getreal "\nSet LTSCALE [.25 Text/.5 Lines] <.5>: ")) (0.5))) '(0.25 0.5)))
  24.         ) ;; end PROGN
  25.         (princ "\nYou must choose 0.25 or 0.5.") ;; Notify the user to provide another value
  26.     ) ;; end WHILE
  27.    
  28.     (setvar 'cmdecho 0) ;; Suppress command-line echo
  29.     (if (not (tblsearch "ltype" ltp)) ;; If the linetype isn't defined in this drawing
  30.         (command "_.-linetype" "_l" ltp "acad.lin" "") ;; Load it
  31.     ) ;; end IF
  32.    
  33.     ;; Create/modify the target layer and make it current
  34.     (command "_.-layer" "_M" lay "_C" "Red" lay "")
  35.     (setvar 'celtype ltp) ;; Make the linetype current
  36.     (setvar 'celtscale (* scl (getvar 'dimscale))) ;; Set the linetype scale appropriately
  37.     (setvar 'osmode  1) ;; Set Object Snap to Endpoint
  38.     (setvar 'cmdecho 1) ;; Now we want command-line echo
  39.     (command "_.pline") ;; Initiate the PLINE command
  40.    
  41.     (while (< 0 (getvar 'cmdactive)) ;; While the command is active, pause
  42.         (command "\\") ;; Pause for user input
  43.     ) ;; end WHILE
  44.    
  45.     (mapcar 'setvar var val) ;; Reset the system variable values
  46.     (princ) ;; Suppress the value returned by the last evaluated expression
  47. ) ;; end DEFUN

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Set a default and ask the user for input
« Reply #16 on: December 08, 2018, 05:27:58 PM »
A more universal selector for the LineTypeFile may be something like :


Code - Auto/Visual Lisp: [Select]
  1.  
  2.  (if (zerop (getvar "measurement") )
  3.    (setq LineTypeFile (findfile "acad.lin"))
  4.    (setq LineTypeFile (findfile "acadiso.lin")) ;; for metric system
  5.  )
  6.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Set a default and ask the user for input
« Reply #17 on: December 10, 2018, 04:15:15 AM »
A more universal selector for the LineTypeFile may be something like :


Code - Auto/Visual Lisp: [Select]
  1.  
  2.  (if (zerop (getvar "measurement") )
  3.    (setq LineTypeFile (findfile "acad.lin"))
  4.    (setq LineTypeFile (findfile "acadiso.lin")) ;; for metric system
  5.  )
  6.  
My version:

Code: [Select]
; Function: ALE_Layer_LoadLinetype
;
; Version 1.00 - 16/11/2006
;
; Arguments:
;   VlaDoc: IAcadDocument: An AutoCAD drawing or IAxDbDocument: Interface
;   LTpCol: LineType Collection
;   LtpNam: Linetype name [STR]
;
; Return Values: [VLA-OBJECT] or nil if vla-load fails
;
; Example:
;   (or *AcadApp* (setq *AcadApp* (vlax-get-Acad-Object)            ))
;   (or *AcAcDwg* (setq *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)))
;   (or *AcLnTps* (setq *AcLnTps* (vla-get-Linetypes      *AcAcDwg*)))
;
;   (ALE_Layer_LoadLinetype *AcAcDwg* *AcLnTps* "Divide")
;
(defun ALE_Layer_LoadLinetype (VlaDoc LTpCol LtpNam / VrtVal)
  (cond
    ( (ALE_Utl_GetItem LTpCol LtpNam) )
    ( (vl-catch-all-error-p
        (vl-catch-all-apply
          'vla-load
          (list
            LTpCol
            LtpNam
            (cond
              ( (vl-catch-all-error-p (setq VrtVal (vl-catch-all-apply 'vla-getvariable (list VlaDoc "MEASUREINIT"))))
                "acadiso.lin"  ; load linetype from metric  linetype file if VlaDoc is a IAxDbDocument: Interface
              )                ; because getvariable it is not a property of a ODbx Document
              ( (zerop (vlax-variant-value VrtVal));English or metric
                 "acad.lin"    ; load linetype from english linetype file
              )
              ( "acadiso.lin" ); load linetype from metric  linetype file
            )
          )
        )
      )
;_i__ (alert (strcat "Asso message:\nTipolinea '" LtpNam "' non trovato in Acadxxx.lin"))
;|e|; (alert (strcat "Asso message:\nLinetype '"  LtpNam "' not found in Acadxxx.lin"  ))
    )
    ( (ALE_Utl_GetItem LTpCol LtpNam) )
  )
)

; Function: ALE_Utl_GetItem
;
; Arguments:
;   VlaCol = Collection Object > ex. (vla-get-Layers *AcAcDwg*)
;   KeyNam = String            > "0"
;
; Return Values:
;   VLA-OBJECT or nil if (vla-item) fails
;
; Note:
;   the Item method is case-sensitive when used with the SelectionSets
;   collection it is not case-sensitive for other collections.
;
; Example:
;  (ALE_Utl_GetItem
;    (vla-get-Layers (vla-get-ActiveDocument (vlax-get-Acad-Object)))
;    "0"
;  )
;
(defun ALE_Utl_GetItem (VlaCol KeyNam / VlaObj)
  (vl-catch-all-apply
   '(lambda ( )
      (setq VlaObj (vla-item VlaCol KeyNam))
    )
  )
  VlaObj
)
 

jlogan02

  • Bull Frog
  • Posts: 327
Re: Set a default and ask the user for input
« Reply #18 on: December 10, 2018, 05:12:25 PM »
Thanks guys for all the suggestions.

Lee, I like...

Code - Auto/Visual Lisp: [Select]
  1. (setq var '(cmdecho clayer celtype celtscale osmode) ;; List of system variables whose values will be modified
  2.           val  (mapcar 'getvar var) ;; Retrieve current values

With my limited exposure I hadn't really considered this. Makes sense though. Thanks for the line by line comments to. Big help.

kdub,

Although I didn't use your suggestion, I filed it away in my "starter code" folder.

Marc' Antonio,

Sadly I'm not familiar with this approach. Baby steps, I say. Baby steps.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Set a default and ask the user for input
« Reply #19 on: December 10, 2018, 05:55:15 PM »
You're most welcome - feel free to ask if any of the comments are unclear  :-)