Code issues

Code issues

Isanders2
Explorer Explorer
434 Views
6 Replies
Message 1 of 7

Code issues

Isanders2
Explorer
Explorer

Hello I found this old code and I’m running into an issue where it resets all my snap values essentially only allowing nearest snap to be selected was wondering if someone can figure out the issue

 

;****************************************************************************
; -----TWST----- DVIEW TWIST & SNAP ANG
;****************************************************************************

;;;  LISP  uses two points or an input angle to
;;;    twist the current view using the the dview command.
;;;    the snapangle variable is reset to make the cursor
;;;    appear horizontal.

;;;;The dtr funtion converts degrees to radians
;;;;The rtd funtion converts radians to degrees

(defun RTD (/ANG) (/ (* ANG 180.0) pi))

(defun DTR (/ANG) (* pi (/ ANG 180.0)))

(defun C:TW (/ P1 P2 ANG)
;;;  store the cmdecho, osnapcoord, osmode and orhtomode system varibles
  (setq CE-SAV (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (setq OSCOORD-SAV (getvar "osnapcoord"))
  (setvar "osnapcoord" 1)
  (setq OSMODE-SAV (getvar "osmode"))
  (setvar "osmode" 512)
  (setq ORTHOMODE-SAV (getvar "orthomode"))
  (setvar "orthomode" 0)
  (princ
    "\nTwist view and cursor angle by picking two point or entering an angle
"
  )					; _ end ofPRINC
  (if (setq P2 (getpoint
		 "\nFirst point(left) <Press Enter to input an angle>: "
	       )			; _ end of GETPOINT
      ) ;_ end of SETQ
    (progn (initget 1)
	   (setq P1  (getpoint "\nSecond point(right): " P2)
		 ANG (angle P2 P1)
	   ) ;_ end of SETQ
	   (command "DVIEW" "L" "" "TW" (- 360 (RTD ANG)) "")
	   (setvar "SNAPANG" ANG)
    ) ;_  end of PROGN
    (progn (setq ANG (getangle "\nAngle: "))
	   (command "DVIEW" "L" "" "TW" (- 360 (RTD ANG)) "")
	   (setvar "SNAPANG" ANG)
    ) ;_ end of PROGN
  ) ;_  end of IF
  (setvar "cmdecho" CE-SAV)
  (setvar "osnapcoord" OSCOORD-SAV)
  (setvar "OSMODE" OSMODE-SAV)
  (setvar "orthomode" ORTHOMODE-SAV)
;;; print the rotation angle to the screen:  converts string to local units
  and
  precision
  (princ
    (strcat "\nTwist angle: "
	    (angtos ANG (getvar "aunits") (getvar "auprec"))
    ) ;_ end of strcat
  ) ;_ end of princ
  (princ)
) ;_  end of DEFUN

;****************************************************************************
; -----SNAG----- SET SNAP ANG TO DVIEW TWIST
;****************************************************************************

(defun c:snag (/ VANG SANG)
  (setq VANG (getvar "viewtwist"))
  (setq SANG (- (* 2 pi) VANG))
  (setvar "snapang" SANG)
  !
)
0 Likes
435 Views
6 Replies
Replies (6)
Message 2 of 7

paullimapa
Mentor
Mentor

These lines of code at the beginning is what's removing all your running object snap settings and setting it to near:

  (setq OSCOORD-SAV (getvar "osnapcoord"))
  (setvar "osnapcoord" 1)
  (setq OSMODE-SAV (getvar "osmode"))
  (setvar "osmode" 512) ; set object snap

Now for the code to work properly you really don't want to have any running object snaps set

otherwise the results will be wrong. But what should be done is saving the current running osnap setting first before changing it so that by the end of the code everything can go back to the way it was. So what I would do is first save the current running object snap settings but not set it to nearest instead set it to none so the code will look like this:

  (setq OSCOORD-SAV (getvar "osnapcoord"))
  (setvar "osnapcoord" 1)
  (setq OSMODE-SAV (getvar "osmode"))
  (setq osmode (getvar "osmode")) ; save current object snap settings
  (setvar "osmode" 0) ; set object snap to none

 Then towards the end of the code where these lines of code is shown:

  (setvar "cmdecho" CE-SAV)
  (setvar "osnapcoord" OSCOORD-SAV)
  (setvar "OSMODE" OSMODE-SAV)
  (setvar "orthomode" ORTHOMODE-SAV)

add an additional line of code to restore the original running object snaps:

(setvar "osmode" osmode) ; restore original running object snap settings

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 7

EnM4st3r
Advocate
Advocate
So what I would do is first save the current running object snap settings..

isnt that already done using the Variable  OSMODE-SAV?

0 Likes
Message 4 of 7

EnM4st3r
Advocate
Advocate

maybe try adding a Error function for restoring the variables

(defun C:TW (/ *error* CE-SAV OSCOORD-SAV OSMODE-SAV ORTHOMODE-SAV P1 P2 ANG)
  (defun *error* (msg)
    (if CE-SAV (setvar "cmdecho" CE-SAV))
    (if OSCOORD-SAV (setvar "osnapcoord" OSCOORD-SAV))
    (if OSMODE-SAV (setvar "OSMODE" OSMODE-SAV))
    (if ORTHOMODE-SAV (setvar "orthomode" ORTHOMODE-SAV))
    (princ (strcat "Error: " msg))
    (princ)
  )
0 Likes
Message 5 of 7

EnM4st3r
Advocate
Advocate

whats with "and precision"?

;;; print the rotation angle to the screen:  converts string to local units
  and
  precision
  (princ

should it be like that?

;;; print the rotation angle to the screen:  converts string to local units and precision
  (princ

 

0 Likes
Message 6 of 7

paullimapa
Mentor
Mentor

Oops you are correct l don’t know how I missed that. So if the function runs its course properly the running osnap setting should be restored back to the original settings. Is that not happening for you?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 7

Isanders2
Explorer
Explorer

I ended up find8ng a work around be deleting two lines instead so it didn’t end up reading the osnap settings

0 Likes