Ok I know environment variables are not as popular as they once use to be, but they can be very powerful in setting up unique values on the machine for company settings. As you might have noticed in a few of the past postings I have been releasing sample code that uses the FSO (File System Object) of the Shell object.
This sample follows that same trend and uses the Shell object to manipulate environment variables that are maintained by the OS. Below are three, yes I said three different examples of using the Shell object. The first shows how to expand environment variables in a string. This can be great for building file paths or getting information about the OS or PC.
;; Shows how to use expanding environment strings
;; Usage: (ExpEnvStr "%TEMP%\\MYDATA")
;; Results of sample: "C:\\DOCUME~1\\Lee\\LOCALS~1\\Temp\\MYDATA"
(defun ExpEnvStr ( strVal / wshShell)
(vl-load-com)
(setq wshShell (vlax-create-object "WScript.Shell"))
(vlax-invoke-method wshShell 'ExpandEnvironmentStrings strVal)
)
;; Retreive the value of the environment variable
;; Usage: (GetEnvStr "SYSTEM" "USERID")
(defun GetEnvStr ( strVarType strVarName / wshShell envVars)
(vl-load-com)
(setq wshShell (vlax-create-object "WScript.Shell"))
(setq envVars (vlax-get-property wshShell 'Environment strVarType))
(vlax-get-property envVars 'Item strVarName)
)
;; Set the value to an environment variable
;; Usage: (SetEnvStr "SYSTEM" "USERID" "L123")
(defun SetEnvStr ( strVarType strVarName strVarVal / wshShell envVars)
(vl-load-com)
(setq wshShell (vlax-create-object "WScript.Shell"))
(setq envVars (vlax-get-property wshShell 'Environment strVarType))
(vlax-put-property envVars 'Item strVarName strVarVal)
)
Got an idea for some sample code? If you do, I would like to hear from you. Your idea might be selected and the results posted on the blog or website.
Sincerely,
Lee
Lee, although you shell object is obviously more powerful, I have found that you can use (getenv) to return environment variables (at least the ones you can access through the DOS [SET] command).
For example:
(strcat (getenv "temp") "\\MYDATA")
So for simply retreiving the %temp% directory, the ComputerName, the UserDomain, etc. - (getenv) should work.
Posted by: R.K. McSwain | Friday, December 09, 2005 at 07:42 AM
GETENV does work for getting the variables, but the problem is that even though there is a SETENV function; it can't be used to update OS environment variables. It instead is wired to specific locations under the AutoCAD registry keys.
So if I have a variable called ALLDOCS that is defined in as a System Variable for the OS and use SETENV to update it, SETENV writes to the Fixed Profile >> General location under HKEY_CURRENT_USER >> Software >> Autodesk >> AutoCAD >> (Release) >> (ProdID).
So it is for this reason that I like to use FSO for everything. From what I have seen, there is no slow down in performance getting a variable value this way. Now setting one is a slightly different story.
Posted by: Lee Ambrosius | Friday, December 09, 2005 at 08:39 AM
hello sir
my name is asd khalil i have small question about autocad blocks
i need to bind specific value(block price) with each blocks in my library
when i drag this block some code must be execute and view the total sum for all blocks
Posted by: asd khalil | Thursday, January 05, 2006 at 04:11 AM
if i can do that please tell
thank you for your help
asd khalil
Posted by: | Thursday, January 05, 2006 at 04:12 AM
Hallo
I am trying to pass information from vba to autocad using the acad.cfg file.
I use the following syntax:
Dim zzGetVarzz as String
ThisDrawing.SendCommand "(setvar ""USERS1"" (getcfg ""AppData/SDTOPO/StringForTitle""))" & vbCr
zzGetVarzz = AutoCAD.Application.ActiveDocument.GetVariable("USERS1")
This works OK only if i use it one or two times within the Sub.
I need to call this syntax over 80 times within the same Sub and when i run it it seems command line gets confused (asynchronising issues) with so many commands and does not work rigth.
If anyone could give an example of how to use "Autocad.Application.Prefernces.property property"
which is the comparision of getcfg in vba (whithout using sendcommand).
Or could i use setenv and then getenv in vba ?
Thank you in advance.
Please help
Posted by: sdtopo | Sunday, January 17, 2010 at 09:20 AM