My Photo

Search


  • Search 280+ postings

Site Awards



Syndications for this Site


« Creating a New Document in AutoCAD with AutoLISP | Main | AutoCAD ActiveX History Reference »

Working with Environment Variables through AutoLISP

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

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83420642b53ef00d8346c552f53ef

Listed below are links to weblogs that reference Working with Environment Variables through AutoLISP:

Comments

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.


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.

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

if i can do that please tell

thank you for your help


asd khalil


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.

Feeds


  • Between the Lines Blog

  • Will render for food... Digital Media Blog

  • HyperPics.com