I had to cut the code down here.
I had more than 10000 characters for the post.
In this version I included the ability to use a csv text file to modify system variables.
You can use different text files for different routines.
The csv file looks like:
ATTDIA,0
DIMFIT,5
APERTURE,8
See uploaded files for full code.
P=
Code:
;___________________________________________________________________________________________________________|
;
; Written By: Peter Jamtgaard copyright 2024 All Rights Reserved
;___________________________________________________________________________________________________________|
;
; Abstract: This is a library of functions that store and restore system variable (to previous value)
; Example to store: (SystemVariablePut "DIMFIT" 3)
; Example to restore: (SystemVariableRestore "DIMFIT")
; Example to store: (SystemVariablesPut (list (list "ATTDIA" 0)(list "DIMFIT" 3)) nil)
; Example to read csv (SystemVariablesPutFromFile "SystemVariablesPut.txt" T)
; Example to restore all (SystemVariablesRestore)
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Command Line Function Header List
;___________________________________________________________________________________________________________|
...
;* C:SVT2
;* Command Line Function to test SystemVariablesPutFromFile
;* C:SystemVariableTest2
;* Command Line Function to test SystemVariablesPutFromFile
;___________________________________________________________________________________________________________|
;
; General Function Header List
;___________________________________________________________________________________________________________|
;
; Function List Argument1 Argument2 Arguement3
;* (CSVFiletoList strFilename strDelimiter)
;* Import a CSV File to a list of Sublists
;* (CSVStringToList strText strDelimiter)
;* Convert CSV String to List
;* (ErrorTrap symFunction)
;* Function to trap errors in expressions
... Ommitted
;* (SystemVariablesPutFromFile strTextFile blnClear)
;* Function to change the values of system variables from a csv file.
;* (SystemVariableTextRead strTextFile strDelimiter )
;* Function to read a text file with a specified delimiter
;* (SystemVariableTextToValue lstSublist)
;* Function to transform a system variable string value to value in a sublist
... Ommitted
;$ End Header
;___________________________________________________________________________________________________________|
;
; Command Line Function to test SystemVariablesPutFromFile
;___________________________________________________________________________________________________________|
(defun C:SVT2 ()(C:SystemVariableTest2))
(defun C:SystemVariableTest2 (/ )
(SystemVariablesPutFromFile "SystemVariablesPut.txt" T); <- T means do not store system variables
)
;___________________________________________________________________________________________________________
;
; Import a CSV File to a list of Sublists
;___________________________________________________________________________________________________________
(defun CSVFiletoList (strFileName strDelimiter / filCSVFile lstOfSublists strFullName strText )
(if (and (setq strFullName (findfile strFileName))
(setq filCSVFile (open strFilename "r"))
)
(progn
(while (setq strText (read-line filCSVFile))
(setq lstOfSublists (cons (CSVStringToList strText strDelimiter) lstOfSublists))
)
(close filCSVFile)
(reverse lstOfSublists)
)
)
)
;___________________________________________________________________________________________________________
;
; Convert CSV String to List **
;___________________________________________________________________________________________________________
(defun CSVStringToList (strText strDelimiter / intPosition lstStrings)
(while (setq intPosition (vl-string-search strDelimiter strText 0))
(setq lstStrings (cons (substr strText 1 intPosition) lstStrings)
strText (substr strText (+ intPosition 1 (strlen strDelimiter)))
)
)
(if lstStrings
(reverse (cons strText lstStrings))
(list strText)
)
)
;___________________________________________________________________________________________________________|
;
; Function to trap errors in expressions
;___________________________________________________________________________________________________________|
(defun ErrorTrap (symFunction / objError result)
(if (not
(vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X)(set X (eval symFunction)))
(list 'result)))))
(if result result 'T)
)
)
... Ommitted
;___________________________________________________________________________________________________________|
;
; Function to change the values of system variables from a csv file.
;___________________________________________________________________________________________________________|
(defun SystemVariablesPutFromFile (strTextFile blnClear / lstOfSystemVariables strFullName )
(if (and (setq strFullName (findfile strTextFile))
(setq lstOfSystemVariables (SystemVariableTextRead strFullName "," ))
)
(SystemVariablesPut lstOfSystemVariables blnClear)
)
)
... Ommitted
;___________________________________________________________________________________________________________|
;
; Function to read a text file with a specified delimiter
;___________________________________________________________________________________________________________|
(defun SystemVariableTextRead (strTextFile strDelimiter / lstOfSystemVariables strFullName)
(if (and (setq strFullName (findfile strTextFile))
(setq lstOfSystemVariables (CSVFileToList strFullName strDelimiter))
(setq lstOfSystemVariables (mapcar 'SystemVariableTextToValue lstOfSystemVariables))
(setq lstOfSystemVariables (vl-remove 'nil lstOfSystemVariables))
(/= (length lstOfSystemVariables) 0)
)
lstOfSystemVariables
)
)
;___________________________________________________________________________________________________________|
;
; Function to transform a system variable string value to value in a sublist
;___________________________________________________________________________________________________________|
(defun SystemVariableTextToValue (lstSublist / strSystemVariable strValue symType Value1 Value2)
(if (and (setq strSystemVariable (car lstSublist))
(setq Value1 (getvar strSystemVariable))
(errortrap '(setvar strSystemVariable Value1));<- Is not read only
(setq strValue (cadr lstSublist))
(setq symType (type Value1))
)
(progn
(cond ((= symType 'INT) (setq Value2 (atoi strValue)) )
((= symType 'LIST)(setq Value2 (eval (read strValue))) )
((= symType 'REAL)(setq Value2 (atof strValue)) )
((= symType 'STR) (setq Value2 strValue) )
)
(list strSystemVariable Value2)
)
)
)
;___________________________________________________________________________________________________________|
;
; Global variable of modelspace object of active document
;___________________________________________________________________________________________________________|
(setq objGlobalModelSpace (vla-get-modelspace
(vla-get-activedocument
(vlax-get-acad-object))))
(princ "!")
(vl-load-com)