Thursday, July 29, 2010
Home   |   Search   |   About AUGI   |   My AUGI   |   Join Now
<< < > >>

Controlled Growth - June 2004

(Discuss this Article! in AUGI's new Discussion Forums.)

“All progress is based upon a universal innate desire on the part of every organism to live beyond its income.” – Samuel Butler, 1612-1680, British Poet, Satirist.

“What,” you may ask, “does this have to do with AutoCAD?” It is very simple. I believe that our installation of AutoCAD has achieved some sort of critical mass. I believe, too, that this installation is constantly multiplying and growing. Ever so slowly, but still growing nonetheless, each week that it continues to feed on our network. I have no doubt that the longer it stays installed on the network, the larger its footprint grows. Now don’t take this the wrong way, I am not talking about files that are project related or even blocks or block libraries that you might use to be more productive. I am talking about that insidious growth that occurs at the very root level of support as you modify and tweak the necessary files required for daily productivity. That’s right; I am talking about Plot Style Tables or Pen Tables as they are commonly known.

You might have been lulled into complacency for a short while immediately after the 2004 products were first introduced. If you looked for these nefarious interlopers in the usual place, you would’ve happily found that they were no longer there. In their place, a mysterious help file titled appropriately “Where are my Plot Styles?” was all that could be found. Ever the curious one, you might have even double clicked on this help file, expecting to see a path pointing to the files. No such luck. This help document simply sent the curious on a cryptic search through the inner workings of the current AutoCAD profile.

This of course meant that your search was on and if you persevered to the end you might have found the plot styles buried so deep they might never see the light of day again. Perhaps these files had simply become the latest victim of “Mushroom Management” techniques. But in a manner closely akin to their pungent fungal cousins, these files hidden away in dark corners continued to grow and grow. Surely the powers that be didn’t think that a surreptitious foray into the depths of XP compliant obfuscation could hide them forever.

Of course, my concern didn’t become alarm until I realized that the practice of providing pen tables on demand for clients, consultants, and one-off projects had grown this modest collection into a “not so miniscule” miasma with a life of its own. No longer was I getting calls asking how to plot with lineweights. Now the first question being asked was about which file on “that un-ending list that dropped down from the plot dialogue box” was the correct one to plot with. “You might say that I am making mountains out of molehills”, and you’d probably be correct, but I am also ever the one to look for opportunities to streamline or simplify the user's experience.

It seemed to me that rather than continuing to add pen tables to a network location, we could minimize the standard collection and simply instruct AutoCAD to look in an additional folder to find these standard pen tables. To my delight, I was reminded that AutoCAD would automatically search through a shortcut into the folder pointed to by that shortcut to find additional files when needed. It does this automatically, so rather than point all the users at a “read-only” network location, I could point them to their local folder, allow them to save any files they wished in this location, and simply create a shortcut in this folder which would point AutoCAD to the “standards folder.” With this in mind I offer the following solution, which provides each user the ability to add as many pen tables as they can stand to their AutoCAD environment while maintaining standardization and providing the CAD Manager with the ability to also provide a standard collection of plot style tables in a protected folder that is available to every user. So if you are ready to start climbing my “molehill” mountain, I offer the following.

In order to provide these features, our journey this episode will take us into both the VBA environment and into Visual LISP. In order to reach the peak, we will have to hike into the registry to determine the current running application, shake off the crampons of XP compliant circumnavigation, and switchback across the shell dynamically creating shortcuts with the help of Windows Scripting Host. Got all that? Grab your gear and let’s review our goals.

When complete with this article, you will have learned:

  • How to detect which product and version is currently running.
  • How to use the File Preferences Object to locate the Plot Style Tables search path.
  • How to use the Windows Scripting Host to create valid “folder based” shortcuts.
  • How to ensure that your users are always working with your standard paths.

So without further ado, let’s get coding.

First things first, we need to add the following references to our VBA environment. We will make use of the Windows Scripting Host and the Microsoft Scripting Runtime. Both of these files should be available on either Windows 2000 or Windows XP. To add these to our project, you will navigate to the “Tools” -> “References” menu items as shown in the accompanying graphic. This action will display the References dialog box as shown in the next graphic. Make sure that your entries appear in the list as shown in this graphic. Don’t worry if you are still working in 2004 instead of 2005. This method will even work in older versions although the paths created by AutoCAD may differ.

Once you have added the correct references, we’ll get started by creating a public sub routine, mine is named “RedirectPlotStyles”. To this sub we will add various variables and representative objects to contain the paths, object types, and arrays that will be necessary to accomplish our goals.

Take a moment to look at the code shown below.

Note: I have removed all the comments from this code, but the code when downloaded from the AUGI.com forum will be fully documented. Some might say that this code is documented ad nauseaum, but I think it is helpful for those learning to code to see this type of step by step commentary.

Public Sub RedirectPlotStyles()
On Error GoTo ErrorHandler   
Dim strSpecialFolder As String  
Dim strFolderPath As String  
Dim strKeyBuilder As String  
Dim varTemp As Variant 
Dim oShell As New IWshRuntimeLibrary.WshShell  
Dim oShortCut As New IWshRuntimeLibrary.WshShortcut 
Dim oFile As New Scripting.FileSystemObject  
Dim oPref As AcadPreferencesFiles   
Set oPref = Application.Preferences.Files
strKeyBuilder = oShell.RegRead("HKCU\Software\Autodesk\
AutoCAD\R16.0\CurVer")
strFolderPath = oShell.RegRead("HKCU\Software\Autodesk\
AutoCAD\R16.0\" & _
CStr strKeyBuilder) & "\RoamableRootFolder") varTemp = Split(strFolderPath, "\", -1, vbTextCompare) varTemp(2) = "All Users" strFolderPath = "" strFolderPath = Join(varTemp, "\") & "Plot Styles" strSpecialFolder = oShell.SpecialFolders.Item("Favorites") Set oShortCut = oShell.CreateShortcut(strSpecialFolder
& "\" &
"Pen Tables Folder.lnk") With oShortCut .Description = "Pen Tables Link" .TargetPath = strFolderPath .WorkingDirectory = strFolderPath .WindowStyle = WshHide .Save End With With oFile If Not .FileExists((Trim$(strFolderPath & "\THCPlotStyles.
lnk"))) Then strSpecialFolder = oShell.SpecialFolders.Item
("AllUserstrSpecialFoldertop") Set oShortCut = oShell.CreateShortcut(strSpecialFolder &
"\" & "THCPlotStyles.lnk") With oShortCut If Not oPref.PrinterStyleSheetPath Like strFolderPath
Then .TargetPath = oPref.PrinterStyleSheetPath .WorkingDirectory = oPref.PrinterStyleSheetPath Else If Not oFile.FolderExists("W:\CAD2004\Plot Styles")
Then MsgBox "Don't forget to create this folder: " & _ vbCrLf & "W:\CAD2004\Plot Styles", vbInformation End If .TargetPath = "W:\CAD2004\Plot Styles" .WorkingDirectory = "W:\CAD2004\Plot Styles" End If .Description = "PlotStyles Link" .WindowStyle = WshHide .Save End With If .FileExists(Trim$(oShortCut.FullName)) Then .CopyFile oShortCut.FullName, (Trim$(strFolderPath &
"\THCPlotStyles.lnk")), True .DeleteFile oShortCut.FullName, True End If End If If .FolderExists(oPref.PrinterStyleSheetPath) Then oPref.PrinterStyleSheetPath = strFolderPath Else MsgBox strFolderPath & " Does not Exist,
please create this folder!",
vbInformation End If End With ErrorHandler: On Error GoTo 0 Set oShell = Nothing Set oShortCut = Nothing Set oFile = Nothing Set oPref = Nothing MsgBox "Done" & vbCrLf & "You are now able to save Project"
& _ vbCrLf & "Specific Pen Tables to your Local Drive" & vbCrLf
& _ "To find this folder, look for a new" & vbCrLf & _ "shortcut in your favorites!", vbInformation, "THC CADD" End Sub

Notice the areas in code shown that make use of the “.FolderExists” and “.FileExists” methods of the Scripting Runtime type library. These tools represent a relatively compact and useful way to verify, create, copy, etc files and folders without resorting to controls, classes, and complexities otherwise required in VBA.

Note also that AutoCAD will automatically search a shortcut found in its plot style table search path folder anytime the “options” dialog is called up in addition to any pagesetup/plot functions. In the code example, I use the Scripting runtime to validate this path prior to creation. This verification step is not necessary, but may prevent a message like the following from appearing the first time you attempt to plot after creating an invalid shortcut or path.

If you run this code successfully, it will swap out the current Plot Style Table Search Path with that of the out of the box location in case it has been altered. It will drop a dynamically created shortcut in to this default location pointing to either the “previous” path or the hard coded path shown in the example. (Don’t forget to change “W:\CAD2004\Plot Styles” to the path you need on your network.) The last step in this example drops a shortcut in the “Favorites” folder as shown in the example below. Who wants to remember that long path anyway? Now you don’t have to!

Did I say that we would also use Visual LISP to accomplish part of our task? Then I guess we need to unwrap this part of our discussion. In order to keep all our users on the same page and ensure that certain paths and locations are maintained and validated, I typically use Visual LISP and/or AutoLISP to redirect or standardize profiles every time AutoCAD is launched. For this purpose I added the following code to our version of the Acad.lsp file which is run the first time a user launches an AutoCAD session. Note: you will need a function that parses strings and returns the nodes as a list with the delimiter removed. In the third line below, the parse function is called on the plot styles table search path folder and is split into an array by separating folders on either side of every back slash.

;Get current Profile User Support root folder location
(setq THCPstylePath (strcat (getvar "roamablerootprefix")
"Plot Styles")) (setq tmpLine (parse THCPstylePath "\\")) ;Update Registry with New value (setq THCPstylePathNew (vl-string-subst "All Users"
(car (cddr tmpLine))THCPstylePath)) (vlax-put-property (vlax-get (vlax-get (vlax-get-acad-object)
"Preferences")"Files")
PrinterStyleSheetPath" THCPstylePathNew) (PRINC)

Please join us on the newly released AUGI Forums for more discussion of this code and to download the original source code with comments, the accompanying LISP file and an example of how to use the WMI and Windows Scripting Host to read the registry and modify paths using VBScript or Wscript prior to launching AutoCAD.

See you on the forums!

Submitted by Richard Binning, AUGI board member.

(Discuss this Article! in AUGI's new Discussion Forums.)


<< < > >>