The Buzzard 发表于 2022-7-6 10:58:26

 
 
这似乎不是个坏主意,但在某些情况下,我可能希望在程序中的某个地方使用旧设置。我将程序设置为在需要时使用用户自己的对象捕捉。如果我把所有设置都放在一个变量下,那对我来说不够灵活。
 
见下文:
 

;//////////////////////////////////////////////////////////////////////////
; Start-Up Function
(defun C:GL ()                                                             ;Define function
(GL_MF)                                                                  ;Go to GL_MF ~ Main Function
(princ)                                                                  ;Exit quietly
)                                                                        ;End defun
(princ "\nGL.lsp Loaded....")                                              ;print message to command line
(princ "\nType GL to start program.")                                    ;print message to command line
;;;//////////////////////////////////////////////////////////////////////////
; Main Function
(defun GL_MF (/ SUCE SUOM SUSM SUAB SUAD MIDPT PLENPT01 PT02)            ;Define function, Declare local variables
(setq SUCE (getvar "cmdecho"))                                           ;Save User Command Echo
(setq SUOM (getvar "orthomode"))                                       ;Save User Orthomode
(setq SUSM (getvar "osmode"))                                          ;Save User Object Snapmode
(setq SUAB (getvar "angbase"))                                           ;Save User Angle Base
(setq SUAD (getvar "angdir"))                                          ;Save User Angle Direction
(setq SUCL (getvar "clayer"))                                          ;Save User Current Layer
(setq temperr *error*)                                                   ;Save Temporary Error
(setq *error* GL_ET)                                                   ;Go to GL_ET ~ Error Trap Function
(GL_LC "GAS LINE" "3" "GAS_LINE" "")                                     ;Go to GL_LC Layer Function, Layer Name, Color, Linetype & Lineweight
(setvar "cmdecho" 0)                                                   ;Set Command Echo to 0
(setvar "angbase" 0.0000)                                                ;Set Angle Base to 0
(setvar "angdir"0)                                                   ;Set Angle Direction to 0
(setvar "orthomode" 1)                                                   ;Turn on Orthomode
(setvar "osmode"    SUSM)                                                ;Turn on User Object Snaps
(setq PT01 (getpoint "\nEnter the line start point:"))                   ;Get the first point of line
(while                                                                   ;Evaluate expression till nill
   (/= nil                                                                ;If not =
   (setq PT02 (getpoint PT01 "\nEnter the line end point:"))            ;Set point 2
   )                                                                      ;End if not
   (setvar "osmode" 0)                                                    ;Turn off all Object Snaps
   (progn                                                               ;Then do the following
   (command "._pline" PT01 PT02 "")                                     ;Pline command
   (setq RADIANS (angle PT01 PT02))                                     ;Get line angle in radians
   (setq DEGREES (GL_RTD RADIANS))                                    ;Convert radians to degrees
   (setq PLEN (distance PT01 PT02))                                     ;Get the line distance
   (setq MIDPT (polar PT01 (GL_DTR DEGREES)(/ PLEN 2.0)))               ;Set the midpoint of the line
   (setq PT01 PT02)                                                   ;Set point 1 from point 2
   (setvar "osmode" SUSM)                                             ;Turn on User Object Snaps
   (if                                                                  ;If the following returns true
       (and                                                               ;Return the logical AND of the supplied arguments
         (>DEGREES 90.0)                                                ;Degrees is greater than 90
         (<= DEGREES 270.0)                                             ;And less than or equal to 270
       )                                                                  ;End and
       (command "._rotate" "_last" "" MIDPT "180.0")                      ;Rotate the line on it mid-point 180 degrees
   )                                                                  ;End if
   )                                                                      ;Otherwise...
)                                                                        ;End while
(setq *error* temperr)                                                   ;Restore error
(setvar "cmdecho"   SUCE)                                                ;Restore Saved User Command Echo
(setvar "orthomode" SUOM)                                                ;Restore Saved User Orthomode
(setvar "osmode"    SUSM)                                                ;Restore Saved User Object Snapmode
(setvar "angbase"   SUAB)                                                ;Restore Saved User Angle Base
(setvar "angdir"    SUAD)                                                ;Restore Saved User Angle Direction
(setvar "clayer"    SUCL)                                                ;Restore Saved User Current Layer
(princ)                                                                  ;Exit quietly
)                                                                        ;End defun
;//////////////////////////////////////////////////////////////////////////
; Degrees to Radians Function
(defun GL_DTR (DEGREES)                                                    ;Define function, DEGREES is the argument
(* pi (/ DEGREES 180.0))                                                ;Calculate Degress to radians
)                                                                        ;End defun
;//////////////////////////////////////////////////////////////////////////
; Radians to Degrees Function
(defun GL_RTD (RADIANS)                                                    ;Define function, RADIANS is the argument
(* 180.0 (/ RADIANS pi))                                                 ;Calculate Radians to degress
)                                                                        ;End defun
;//////////////////////////////////////////////////////////////////////////
; Layer Create Function
(defun GL_LC (NLAY LCLR LTYP LWGT / LAY FRZ)                               ;Define function, Declare local variables and arguments
(setq CLAY (getvar "clayer"))                                          ;Get the current layer
(setq LAY (tblsearch "layer" NLAY))                                    ;Search drawing to find layer, Note: (NOT USED)
(if                                                                      ;If the following returns true
   (not LAY)                                                            ;Layer not in drawing
   (command "._layer" "_m" NLAY "_c" LCLR "" "_lt" LTYP "" "_lw" LWGT "") ;Layer command
   (progn                                                               ;Then do the following
   (setq FRZ (cdr (assoc 70 LAY)))                                    ;Variable FRZ is frozen layer
   (if                                                                  ;If the following returns true
       (= FRZ 65)                                                         ;Layer frozen from last edit
       (progn                                                             ;Then do the following
         (command "._layer" "_t" NLAY "")                                 ;Thaw layer
         (command "._layer" "_s" NLAY "")                                 ;Set layer
       )                                                                  ;Otherwise...
       (command "._layer" "_s" NLAY "")                                 ;Set layer
   )                                                                  ;End if
   )                                                                      ;Otherwise...
)                                                                        ;End if
(princ)                                                                  ;Exit quietly
)                                                                        ;End defun
;//////////////////////////////////////////////////////////////////////////
; Error Trap Function
(defun GL_ET (errmsg)                                                      ;Define function, errmsg is the argument
(command nil nil nil)                                                    ;When escape selected
(if                                                                      ;If the following returns true
   (not                                                                   ;Verify that an item evaluates to nil
   (member errmsg '("console break" "Function Cancelled"))            ;Search list for an occurence of an expression
   )                                                                      ;End not
   (princ (strcat "\nError:" errmsg))                                     ;Print message to command line
)                                                                        ;End if
(setvar "cmdecho"   SUCE)                                                ;Restore Saved User Command Echo
(setvar "orthomode" SUOM)                                                ;Restore Saved User Orthomode
(setvar "osmode"    SUSM)                                                ;Restore Saved User Object Snapmode
(setvar "angbase"   SUAB)                                                ;Restore Saved User Angle Base
(setvar "angdir"    SUAD)                                                ;Restore Saved User Angle Direction
(setvar "clayer"    SUCL)                                                ;Restore Saved User Current Layer
(princ "\nError, Restoring Variables.")                                  ;Print message to command line
(terpri)                                                               ;Terminate print
(setq *error* temperr)                                                   ;Restore error
(princ)                                                                  ;Exit quietly
)                                                                        ;End defun
;//////////////////////////////////////////////////////////////////////////

 
然后重新打开捕捉:
 

; Layer Create Function
(defun GL_LC (NLAY LCLR LTYP LWGT / LAY FRZ)                               ;Define function, Declare local variables and arguments
(setq CLAY (getvar "clayer"))                                          ;Get the current layer
(setq LAY (tblsearch "layer" NLAY))                                    ;Search drawing to find layer, Note: (NOT USED)
(if                                                                      ;If the following returns true
   (not LAY)                                                            ;Layer not in drawing
   (command "._layer" "_m" NLAY "_c" LCLR "" "_lt" LTYP "" "_lw" LWGT "") ;Layer command
   (progn                                                               ;Then do the following
   (setq FRZ (cdr (assoc 70 LAY)))                                    ;Variable FRZ is frozen layer
   (if                                                                  ;If the following returns true
       (= FRZ 65)                                                         ;Layer frozen from last edit
       (progn                                                             ;Then do the following
         (command "._layer" "_t" NLAY "")                                 ;Thaw layer
         (command "._layer" "_s" NLAY "")                                 ;Set layer
       )                                                                  ;Otherwise...
       (command "._layer" "_s" NLAY "")                                 ;Set layer
   )                                                                  ;End if
   )                                                                      ;Otherwise...
)                                                                        ;End if
(princ)                                                                  ;Exit quietly
)                                                                        ;End defun
 
或者,如果它在列表中靠后,只需使用nth

The Buzzard 发表于 2022-7-6 11:01:16

 
 
我从没想过那么多,李很好。
我将在下一个代码中尽量记住这一点。
 
谢谢
秃鹰

MarcoW 发表于 2022-7-6 11:03:07

不客气,设置多个系统变量时更方便。
 
例如,相当多的程序只会改变CMDECHO OSMODE和CLAYER。
 
所以代码可以是这样的:
 

(setq SUCE (getvar "cmdecho"))                                           ;Save User Command Echo
(setq SUOM (getvar "orthomode"))                                       ;Save User Orthomode
(setq SUSM (getvar "osmode"))                                          ;Save User Object Snapmode
(setq SUAB (getvar "angbase"))                                           ;Save User Angle Base
(setq SUAD (getvar "angdir"))
然后在关闭CMDECHO时,OSMODE:
 
在错误处理程序中:
 
希望这有帮助,
 

The Buzzard 发表于 2022-7-6 11:08:45

The Buzzard 发表于 2022-7-6 11:09:46

Marcow,
 
I had to correct the line indicated in RED for the error trap. See below. I must have been in a hurry.
 
Sorry about that.
The Buzzard

;//////////////////////////////////////////////////////////////////////////; Start-Up Function(defun C:GL ()                                                             ;Define function (GL_MF)                                                                  ;Go to GL_MF ~ Main Function (princ)                                                                  ;Exit quietly)                                                                        ;End defun(princ "\nGL.lsp Loaded....")                                              ;print message to command line(princ "\nType GL to start program.")                                    ;print message to command line;;;//////////////////////////////////////////////////////////////////////////; Main Function(defun GL_MF (/ SUCE SUOM SUSM SUAB SUAD MIDPT PLENPT01 PT02)            ;Define function, Declare local variables (setq SUCE (getvar "cmdecho"))                                           ;Save User Command Echo (setq SUOM (getvar "orthomode"))                                       ;Save User Orthomode (setq SUSM (getvar "osmode"))                                          ;Save User Object Snapmode (setq SUAB (getvar "angbase"))                                           ;Save User Angle Base (setq SUAD (getvar "angdir"))                                          ;Save User Angle Direction (setq SUCL (getvar "clayer"))                                          ;Save User Current Layer (setq temperr *error*)                                                   ;Save Temporary Error (setq *error* GL_ET)                                                   ;Go to GL_ET ~ Error Trap Function (GL_LC "GAS LINE" "3" "GAS_LINE" "")                                     ;Go to GL_LC Layer Function, Layer Name, Color, Linetype & Lineweight (setvar "cmdecho" 0)                                                   ;Set Command Echo to 0 (setvar "angbase" 0.0000)                                                ;Set Angle Base to 0 (setvar "angdir"0)                                                   ;Set Angle Direction to 0 (setvar "orthomode" 1)                                                   ;Turn on Orthomode (setvar "osmode"    SUSM)                                                ;Turn on User Object Snaps (setq PT01 (getpoint "\nEnter the line start point:"))                   ;Get the first point of line (while                                                                   ;Evaluate expression till nill   (/= nil                                                                ;If not =   (setq PT02 (getpoint PT01 "\nEnter the line end point:"))            ;Set point 2   )                                                                      ;End if not   (setvar "osmode" 0)                                                    ;Turn off all Object Snaps   (progn                                                               ;Then do the following   (command "._pline" PT01 PT02 "")                                     ;Pline command   (setq RADIANS (angle PT01 PT02))                                     ;Get line angle in radians   (setq DEGREES (GL_RTD RADIANS))                                    ;Convert radians to degrees   (setq PLEN (distance PT01 PT02))                                     ;Get the line distance   (setq MIDPT (polar PT01 (GL_DTR DEGREES)(/ PLEN 2.0)))               ;Set the midpoint of the line   (setq PT01 PT02)                                                   ;Set point 1 from point 2   (setvar "osmode" SUSM)                                             ;Turn on User Object Snaps   (if                                                                  ;If the following returns true       (and                                                               ;Return the logical AND of the supplied arguments          (>DEGREES 90.0)                                                ;Degrees is greater than 90         (

Lee Mac 发表于 2022-7-6 11:15:03

Buzzard,
 
I notice that you use a lot of variables to store and reset your sys Vars:
 

(setq SUCE (getvar "cmdecho"))                                           ;Save User Command Echo (setq SUOM (getvar "orthomode"))                                       ;Save User Orthomode (setq SUSM (getvar "osmode"))                                          ;Save User Object Snapmode (setq SUAB (getvar "angbase"))                                           ;Save User Angle Base (setq SUAD (getvar "angdir"))
 
Why not just store them all at once:
 

(setq OldVars (mapcar 'getvar '("CMDECHO" "ORTHOMODE" "OSMODE" "ANGBASE" "ANGDIR")))
 
Lee

The Buzzard 发表于 2022-7-6 11:17:02

 
 
That does not seem like a bad idea, But in some instance I may want to use the old settings somewhere within the program. I set the program up to use the Users own object snaps when called for. Thats not flexible enough for me if I place all settings under one variable.
 
See below:
 

(while                                                                   ;Evaluate expression till nill   (/= nil                                                                ;If not =   (setq PT02 (getpoint PT01 "\nEnter the line end point:"))            ;Set point 2   )                                                                      ;End if not   (setvar "osmode" 0)                                                    ;Turn off all Object Snaps   (progn                                                               ;Then do the following   (command "_.pline" PT01 PT02 "")                                     ;Pline command   (setq RADIANS (angle PT01 PT02))                                     ;Get line angle in radians   (setq DEGREES (GL_RTD RADIANS))                                    ;Convert radians to degrees   (setq PLEN (distance PT01 PT02))                                     ;Get the line distance   (setq MIDPT (polar PT01 (GL_DTR DEGREES)(/ PLEN 2.0)))               ;Set the midpoint of the line   (setq PT01 PT02)                                                   ;Set point 1 from point 2   (setvar "osmode" SUSM)                                             ;Turn on User Object Snaps   (if                                                                  ;If the following returns true       (and                                                               ;Return the logical AND of the supplied arguments          (>DEGREES 90.0)                                                ;Degrees is greater than 90         (

Lee Mac 发表于 2022-7-6 11:21:38

No, its not too much more - you just need to plan things a little
 
For example, structure the list like something like this:
 

(setq OldVars (mapcar 'getvar '("OSMODE" "CMDECHO"...
 
Then to switch snaps back on:
 

(setvar "OSMODE" (car OldVars))
 
Or, if it is further down in the list, just use nth

The Buzzard 发表于 2022-7-6 11:22:33

 
 
I never gave that much thought, Very nice Lee.
I will try to keep that in mind on my next code.
 
Thanks,
The Buzzard

Lee Mac 发表于 2022-7-6 11:26:41

You're welcome, its much more convenient when setting multiple Sys vars.
 
For instance, quite a few programs will only alter CMDECHO OSMODE and CLAYER, for example.
 
So the code can be something like:
 

(setq VarList '("CLAYER" "CMDECHO" "OSMODE"))(setq OldVars (mapar 'getvar VarList))Then when switching off CMDECHO, OSMODE:
 

(mapcar 'setvar (cdr VarList) '(0 0))And in the Error Handler:
 

(defun *error* (msg) (if OldVars (mapcar 'setvar VarList OldVars)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")   (princ (strcat "\n** Error: " msg " **"))) (princ))Hope this helps,
 
Lee
页: 1 [2]
查看完整版本: 关于线型/旋转的问题