jonathann3891 Posted April 24, 2019 Share Posted April 24, 2019 I'm learning how to have a lisp read & write information to a text file. I am a little stuck and looking for some help. Everything seems to be working up to the point where I try to set the OSMODE to USNAPS. All it says is "Insufficient Data". Why is this different from saving the osmode setting then recalling it later? Please critique my code as well. Is there a better way to write this? (defun c:ccc (/ Usnaps Uconfig) (setq Usnaps (rtos (getvar 'osmode) 2 0)) (if (setq Uconfig (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "w")) (progn (write-line (strcat Usnaps) Uconfig) (close Uconfig) ) ) (princ) ) (defun c:ttt (/) (setq fname (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "r")) (if (not fname) (prompt "File not found! ") ) (progn (setq Usnaps (read-line fname))(close fname)) (setvar 'osmode Usnaps) ) Quote Link to comment Share on other sites More sharing options...
Tharwat Posted April 24, 2019 Share Posted April 24, 2019 Hi, Based on your way of coding I did modify the codes a bit but untested: (if (not (setq fname (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "r"))) (prompt "File not found! ") (progn (setq Usnaps (read-line fname)) (close fname) (setvar 'osmode (atoi Usnaps)) ) ) Quote Link to comment Share on other sites More sharing options...
jonathann3891 Posted April 24, 2019 Author Share Posted April 24, 2019 Thanks @Tharwat! This is something I pieced together by looking at other examples. If there is a better way to code it, I would like to know. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted April 24, 2019 Share Posted April 24, 2019 (edited) Quickly written and untested: (defun c:wrt (/ find file) (if (setq file (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "w")) (progn (write-line (itoa (getvar 'osmode)) file) (close file) ) ) (princ) ) ;; ;; (defun c:red (/ find file val) (if (setq find (findfile "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt")) (progn (setq file (open find "r")) (setq val (read-line file)) (close file) (setvar 'osmode (atoi val)) ) (alert "\nFile not found !") ) (princ) ) Edited April 27, 2019 by Tharwat Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 27, 2019 Share Posted April 27, 2019 The reason that your original code fails is because the content read from your file will be a string, and the OSMODE system variable requires an integer value. Since you are looking for suggestions, I would recommend the following code: (setq snap:file "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt") (defun c:writesnaps ( / des ) (if (setq des (open snap:file "w")) (progn (princ (getvar 'osmode) des) (close des) (princ "\nObject Snap Mode written to file.") ) (princ (strcat "\nUnable to create or write to file: \"" snap:file "\".")) ) (princ) ) (defun c:readsnaps ( / des ) (if (setq des (open snap:file "r")) (progn (setvar 'osmode (atoi (read-line des))) (close des) (princ "\nObject Snap Mode restored from file.") ) (princ (strcat "\nThe file \"" snap:file "\" was not found or could not be read.")) ) (princ) ) Here, the location of the file is defined as a global variable which is referenced in both functions - this approach has the benefit that any change to the location or name of the file can be made in a single place in the code and will be automatically reflected in both functions. Also note the use of the atoi function to convert the string value to an integer (converting to 0 in the worst case) before updating the OSMODE system variable value. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 27, 2019 Share Posted April 27, 2019 On 4/24/2019 at 2:36 PM, Tharwat said: Quickly written and untested Note that you are writing the file in both functions; also, the test for the existence of the file in the first function is unnecessary, since the write mode of the open function will create the file if it doesn't exist, else it will overwrite the file if it does. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted April 27, 2019 Share Posted April 27, 2019 Thank you Lee for your notes. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted April 29, 2019 Share Posted April 29, 2019 Maybe something like this also can be used in lisp code just needs to be loaded on startup. To use (c:47) is all required likewise (c:0) to set no snaps. (defun C:15 ()(setvar "osmode" 15359)) ; sets all snaps on (defun C:47 ()(setvar "osmode" 47)) (defun C:99 ()(setvar "osmode" 99)) (defun C:8 ()(setvar "osmode" 8)) (defun C:512 ()(setvar "osmode" 512)) (defun C:9 ()(setvar "osmode" 9)) (defun C:0 ()(setvar "osmode" 0)) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 29, 2019 Share Posted April 29, 2019 @BIGAL This may present a less destructive way to disable snaps: (defun c:0 nil (setvar 'osmode (logior 16384 (getvar 'osmode))) (princ)) Quote Link to comment Share on other sites More sharing options...
BIGAL Posted April 30, 2019 Share Posted April 30, 2019 Thank you lee. Its more in a lisp that I set 0 but reset at end. F3 is easier when manual. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.