Archive | VBA RSS for this section

Is Anyone Using that Command?

by Mark Stefanchuk, CADmanage.com

As a CAD Manager I often wonder if I’m focusing on the right things. I don’t know if anyone else ever has this issue, but I often think, as I’m updating a program or some documentation, if anyone is even using the thing.

If the number of CAD users is less than twenty, you can just ask everyone if they are using the command. It’s tedious but manageable. When you have hundreds of users however, asking everyone is not practical. So, what do you do? Just go ahead and update it anyway? Or, like some of our colleagues might do, just leave it out of the mix next time you upgrade the CAD engine and wait for someone to scream, “Hey, what happened to <insert favorite command here>”. It’s a dangerous approach, but sometimes you just have a feeling that the add-on, plug-in, or whatever is obsolete.

I think we can be a little more pro-active. If it’s your code and you still have the source you can insert a bit of code to write a log file each time the command is run. This is a good approach, especially for multi-platform environments. I’ll describe how to do this, but first let’s look at a quick way to capture command use on your AutoCAD platform without writing code.

Before we begin, here is a shameless self promotion:

AutoCAD – The Log File:

Yes, that’s right. Turn on the log file option. There’s going to be lots of files and lots of junk that you don’t need, but you’re only looking for one instance of the command to validate that the thing is being used. You turn on the log file option by opening the options dialog (command options). Click on the open and save tab and check the “Maintain a Log File” option.

Set the location of the log files, Options > Files > Log File Location,

I can’t really predict what the lasting ramifications are for doing this. The files aren’t really that big, but if you have a large number of users all writing log files to the same location then you might not want to be using up disk space for this. So, after you’re done collecting data turn off the use log file options.

The log files contain the commands your users have recently run. Now you need a quick way of searching the files. Grep for Windows is a good tool to use. Download the open source gnu version from here,

http://gnuwin32.sourceforge.net/packages/grep.htm

On my 64bit Windows 7 computer Grep.exe is installed to C:\Program Files (x86)\GnuWin32\bin\. Add the location of this program to your system path.

The basic usage is

grep <pattern> <files to search>

When I searched on the command “_qsave” grep found 2 instances – not a big surprise. It will also work when you want to find out if that older command has been used recently. If grep does not return a result then you may be able to conclude that the command is no longer needed.

To be more certain that a program is no longer in use, run this process over the course of several weeks. There are probably several old programs that you can eliminate from the maintenance cycle. So, log files and grep create a quick and fairly easy way to run an audit without having to write a bunch of code.

Write Some Code:

You have many choices when you commit to writing some code, but I’m going to keep it simple and stick with VBA. It’s a common platform for both MicroStation and AutoCAD and will give us an opportunity to re-use some code. I’m also just going to output to a text file. You can then grep or edit the file a few times during your audit period to find out if the command you’re interested in is being used. We can get much more sophisticated, but for a quick test this will work. Insert the following function in your VBA.

Public Function reportCommand(sCmd As String, sFile As String) As Boolean
    Dim fnum As Integer
    reportCommand = False
    On Error GoTo ERRORHANDLER
    fnum = FreeFile ' get next available file number
    ' report command usage
    Open sFile For Append As #fnum
        Print #1, sCmd
    Close #fnum
    reportCommand = True
    Exit Function
ERRORHANDLER:
    'Debug.Print "reportCommand: " + err.Description
End Function

And in the macro sub you are interested in, add a call to this function.

Public Sub ACADCmd()
    Call reportCommand("tempcmd", "c:\temp\test.txt")
    ' . . .
    ' run/initialize something
    ' . . .
End Sub

All we’re doing here is writing a string, sCmd to a text file specified by SFile (example: C:\temp\test.txt).

What I like most about this approach is that you can use the code in any CAD system that supports VBA. For instance, I developed the code in MicroStation, but cut and pasted it into my AutoCAD VBA without making any changes. The call to the function looks the same in both environments too. You can take this a step further by building a re-usable library, but we’ll save this topic for a future post.

Cell Rotation – MicroStation VBA

Want the rotation of a cell about the Z-axis? This is how I get it using the Bentley object model. 

Public Function getCellRotation(oCell As CellElement) As Double
    Dim rAngle As Double
    getCellRotation = 0
    If (Matrix3dIsXYRotation(oCell.Rotation, rAngle)) Then
        getCellRotation = Degrees(rAngle)
        Debug.Print Trim(Str(Format(Degrees(rAngle), "#0.0")))
    End If
End Function

Matrix3dIsXYRotation passes the rotation matrix to the method and then sends back the angle (in radians) back to rAngle. The method returns true when there is a rotation about Z. If the cell is skewed or mirrored the method will return false. Be careful not to assume that if the function returns false that there is 0 rotation. You can make this assumption if your application controls the orientation of the cell and you can then predict that cells won’t be skewed or mirrored.

VBA Shortcut Methods

Bentley Systems does a great job of making MicroStation VBA easy. With each new release of the object model there are new objects, methods, and properties.Most support new functionality, but in some cases these additions help improve readability. For example,

GetLastValidGraphicalElement is a shortcut for GraphicalElementCache.GetLastValidElement

I recommend checking the Remarks section of the help documentation and the version for the methods you use. Especially if you are writting VBAs for older V8 releases. You may have to use the long form instead of the shortcut if you are writting a macro for an older version of MicroStation. I’ve also noticed that the documents don’t provide the precise release versions. So even though the new method was implemented in V8 2004, it may not have been added until a later release of V8 2004. In this case replace the shortcut with the long form of the method.