Archive | VBA RSS for this section

Match Icon Check Boxes

Many years ago I created a MicroStation basic macro that would check the boxes of the match element command. At the time, MicroStation J did not save these settings, so every time you used the command you would have to check the boxes for the element attributes you wanted to change. In MicroStation V8i these settings are saved, so you only have to check the boxes one time. The basic macro (before VBA) was really simple and you could record it using the record macro dialog. VBA of course is just as easy – as we will see in a moment.

match01

Do we still need a macro like this one? If you want all options on, or all off then there may still be room in your toolbox for this macro.  It’s easy enough to record the action of checking these boxes (and unchecking the boxes). You can then add these new commands to your MicroStation menus. I like the idea of adding a couple of extra commands in my tasks menu, but adding new icons to the toolbox or pull down menu can work too.

To record the macro, open the VBA Project Manager dialog – Utilities | Macro | Project Manager.

match02

The project manager dialog will open.

match03

Before we record the check boxes we need to start the Match command. You can select the command from the Task menu, or type the keyin “match icon”.

The settings dialog changes to show the Match Element Attribute controls.

match04

To record, highlight the Default VBA and then click the red button in the Project Manager dialog. Check the boxes.

match05

Now, click the stop button (white square) in the Project Manager dialog.

match06

Open the Visual Basic Editor (icon that looks like a dialog box – to the right of the triangle).

match07

The visual basic editor will contain the macro you just recorded. It will look similar to the following.

match08

You can remove all of the Dim statements because these are not used. Remove the second last line too, CommandStatement.StartDefaultCommand. This command will stop the match command and select your default command (usually smart selector). Finally, remove all of the extra lines. The finished macro will look like this.

match09

Repeat these steps but uncheck the boxes. Or, copy and paste Macro1 (change the new sub name to Macro2). Replace the value -1 with 0. This new macro will uncheck all of the boxes.

match10

If you want a different name for a macro, just change the sub name. Instead of Macro1() you could replace that with MatchChkAll() and for Macro2() maybe this could be MatchChkNone().

To run the vba you can use the keyin, vba run Macro1. But, we also want to make sure tha the Match Element Attributes command has been started. So, use the following keyin,

match icon;vba run Macro1

Run this keyin from the keyin browser, add an icon to the Change Attributes toolbox and maybe create a new task. With just a little bit of effort you can optimize your workflows and create a more productive CAD environment.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached directly at mstefanchuk@cadmanage.com.

photoDGN Beta

Advertisements

Transparent Dialogs

by Mark Stefanchuk, cadmanage.com

I was playing around with MicroStation preferences the other day and something was bugging me. Turning on/off dialog transparency was clumsy. I know it’s a user preference and that it’s normally on or normally off, but opening the preferences dialog, finding the transparency preference seemed to be more cumbersome than it should be. It was definitely not quick. In this case I just want to switch on/off the dialog transparency without having to go through the steps of opening checking and clicking ok – just to turn transparency on. Call me lazy.

So, to shorten the steps, I used the VBA macro recorder to capture the actions needed to turn on transparency.

To record the macro, open the VBA Project Manager dialog – Utilities | Macro | Project Manager. The project manager dialog will open. Select the Default project in the VBA Project Manager. Click the red circle to start recording.

td01

Go to Workspace | Preferences | Look and Feel. Check the box, “All modeless dialogs use the same transparency”.

td02

Click OK. Click Stop in the VBA Project Manager (the square white button).

td03

I then cleaned up (deleted) all of the extra lines of code that the recorder adds. That is, the lines we don’t need.

This records the steps in your VBA. It creates a new Macro and a Macro Handler (to control the dialog box). The recording process adds a lot of extra code that we don’t need. You can remove the extra code. Your code will look similar to the following when you are done.

Sub Macro1() 'turn on dialog transparency
  Dim modalHandler As New Macro1ModalHandler
  AddModalDialogEventsHandler modalHandler
  CadInputQueue.SendCommand "MDL SILENTLOAD USERPREF"
  RemoveModalDialogEventsHandler modalHandler
End Sub

And the handler will look similar to the following.

Implements IModalDialogEvents
Private Sub IModalDialogEvents_OnDialogClosed _
  (ByVal DialogBoxName As String, _
  ByVal DialogResult As MsdDialogBoxResult)
End Sub
Private Sub IModalDialogEvents_OnDialogOpened _
(ByVal DialogBoxName As String, _
DialogResult As MsdDialogBoxResult)
  If DialogBoxName = "Preferences [untitled]" Then
    '   Set a variable associated with a dialog box
    SetCExpressionValue "savePrefs.addFlags.dialogsUseSameTranparency", 1, "USERPREF"
    '   Remove the following line to let the user close the dialog box.
    DialogResult = msdDialogBoxResultOK
  End If  ' Preferences [untitled]
End Sub

The Macro is saved in Module1 and the handler is saved in the Class Module folder of the VBA. The macro opens the preferences dialog. It also starts and stops the event handler. The handler does the work. It turns on the transparency. All of this is built for you when you record the macro. All you have to do is remove the code that you don’t need.

Next, we want a command to turn off the transparency. Just copy Macro1 and rename it as Macro2. Change Macro1ModalHandler to Macro2ModalHandler.

Sub Macro2() 'turn on dialog transparency
  Dim modalHandler As New Macro2ModalHandler
  AddModalDialogEventsHandler modalHandler
  CadInputQueue.SendCommand "MDL SILENTLOAD USERPREF"
  RemoveModalDialogEventsHandler modalHandler
End Sub

Now, create a new class module called Macro2ModalHandler. Copy the contents of Macro1ModalHandler to this new class module. Finally, in the line that starts with SetCExpressionValue, change the 1 to a 0 (i.e. on to off). Like this.

Private Sub IModalDialogEvents_OnDialogOpened _
(ByVal DialogBoxName As String, _
DialogResult As MsdDialogBoxResult)
  If DialogBoxName = "Preferences [untitled]" Then
    '   Set a variable associated with a dialog box
    SetCExpressionValue "savePrefs.addFlags.dialogsUseSameTranparency", 0, "USERPREF"
    '   Remove the following line to let the user close the dialog box.
    DialogResult = msdDialogBoxResultOK
  End If  ' Preferences [untitled]
End Sub

The last thing we want to do is add these commands to the UI so that we have and easy way to run the commands.To do this I added a new dgnlib to my workspace. I’m using the default untitled workspace, so I added the dgnlib to $(MS)\Workspace\projects\untitled\DGNLIB\. While in the dgnlib, open the Customize dialog box – Workspace | Customize. Add a new toolbox and two new tools. Change the names as shown in the image below.

td04

On the first tool change the Key-in to vba run macro1. This will run the macro that turns off dialog transparency. On the second tool change the Key-in to “vba run macro2”. I’ve also changed the icon. The default tool icon is the wrench. You can select any icon you want. I just took a couple of icons from ustation.rsc (tools I don’t use very often).

Now, right click on the new toolbox name and select “Open Tool Box”.This is what mine looks like.

td05

You can dock this on your MicroStation window. I dock mine at the top so that I can quickly access these buttons. So, now I can control dialog transparency without multiple clicks.

td06

td07

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached directly at mstefanchuk@cadmanage.com.

photogeoDWG

VBA Custom Configs

by Mark Stefanchuk, cadmanage.com

I’m not really a big fan of increasing the number of MicroStation config variables. Where possible, try to use built in variables. For example, you can use CreateCellElement3

with just the name of the cell as long as the cell library is defined in MS_CELLLIST. You might have something that looks like this in your VB code.

oCell = MainForm.oMSApp.CreateCellElement3(MainForm.uc_o.txtCameraName.Text, Point, True)

This is from a VB.Net example, that’s why the MicroStation Application object is included.

Here’s how I might define MS_CELLLIST for use with photogeoDGN.

CADMANAGE = C:/ProgramData/Cadmanage/PhotogeoDGN/Contents/
MS_ADDINPATH < $(CADMANAGE)Windows/8.11/
MS_CELLLIST < $(CADMANAGE)Resources/*.cel

Sometimes however, it is necessary to identify a special folder or some other resource in your custom program. For example, your program might include a catalog file that provides some engineering data that your program will associate with a cell. You could decide to put this in a resources folder that is always in the install directory. But, a better solution might be to let the end user decide where she wants to save the resource. In this case the resource or file is one that MicroStation doesn’t know anything about, but your program has to be able to find. To fix that we could define the following,

CMRI_CATALOG = $(CADMANAGE)Resources/testcatalog.xml

Put the variable in one of MicroStation’s configuration files, like your UCF. Now, to access the variable from your custom VBA program you need to use ConfigurationVariableValue. Here’s how to do that in VBA.

Sub testConfig()
Debug.Print ActiveWorkspace.ConfigurationVariableValue("CMRI_CATALOG")
End Sub

The example will print the value of the configuration variable in the VBA IDE’s immediate window.

C:\ProgramData\Cadmanage\PhotogeoDGN\Contents\Resources\testcatalog.xml

Use this expansion to open the file, or in this case define your XML document object.

As long as the variable name is different than any other MicroStation config variable, you can use it, but I would recommend  including a prefix. In the previous example, I used CMRI (for CAD Management Resources, Inc). This just helps to avoid variable conflicts.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached directly at mstefanchuk@cadmanage.com.

photogeoDWG

photoDGN Beta

Reference Files and XRef Paths

by Mark Stefanchuk, CADmanage.com

I was looking through some old emails recently and found this questions from Adrian. “How do I get the reference file path of a reference file?”

Why would you need the path? Good question. You might want to audit projects to make sure users are using standard naming conventions. You may need to include reference file names and paths in the border for cross referencing. Or, maybe, as is the case with MicroStation you want to know if the file is located in the project path or if it’s coming from another folder – another project or a common files folder like you might have for standard borders.

So, how do we do it? Well, you could open the reference dialog and hover your mouse over the file name until the pop-up window shows you the file specification, or you can write a small app.

In MicroStation we get the reference file path and file name using the designfile object with fullname. In this example, I’m just sending the name to the immediate window.

Sub reftest()
    Dim att As Attachment
    For Each att In ActiveModelReference.Attachments
        Debug.Print att.DesignFile.FullName
    Next
End Sub

AutoCAD looks a little different, because we have to search the list of blocks in the file and filter out the ones that are XREFS.

Sub xreftest()
    Dim oBlk As AcadBlock
    For Each oBlk In ThisDrawing.Blocks
        If oBlk.IsXRef Then
            Debug.Print oBlk.Path
        End If
    Next
End Sub

Printing to the immediate window is a quick and easy solution for you, but if you want your users to be able to cut and paste give them a list box or dump the results to at text file.

A listbox approach might look like this.

And the code for this is essentially the same, except you put the for each into the userform_initialize event and add items to the listbox instead of printing it to the immediate window.

 Load frmRefList
 frmRefList.Show
 frmRefList.lstXRefs.Clear
 Dim att As Attachment
 For Each att In ActiveModelReference.Attachments
    frmRefList.lstXRefs.AddItem att.DesignFile.FullName
 Next

Finally, if you want your users to be able to copy the list to the clipboard then you will have to provide a copy button because CTRL-C does not work on the list contents. This example shows you how to use a DataObject to copy the list. Put the code into a button click event and you’ll be good to go.

 Dim s As String, i As Integer, oData As DataObject
 For i = 0 To Me.lstXRefs.ListCount - 1
    If Len(Trim(Me.lstXRefs.List(i))) > 0 Then
        s = s + Trim(Me.lstXRefs.List(i)) + vbCrLf
    End If
 Next i
 Set oData = New DataObject
 oData.Clear
 oData.SetText Trim(s)
 oData.PutInClipboard

Code download is available on our CADgurus page.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached by contacting us at info@cadmanage.com.

Experiences in CAD Management – System Performance

by Mark Stefanchuk, CADmanage.com

I was thinking about all of the things a CAD Manager does and decided to start a list. It was very long. At the highest level the list is typically support, training, and development – a very basic IT management framework. That is, get feedback from support and address systemic issues first with training and if necessary with development. Of course, the details of the day to day activities of a CAD Manager paint a much more complex picture. The CAD Manager doesn’t just have to know how to reset passwords. The CAD Manager also has to be able to peel back the layers of the CAD system one by one to  separate the relevant from the irrelevant. CAD support might appear to be as simple as demonstrating feature function, but more often it requires traversing multiple integrated systems.

A few years back I had a situation where several users were seeing really poor performance, but only when working in a CAD drawing. Basic drawing commands – lines, circles, boxes and text were really slow. Everyone had the same software, the same computer, the same OS, but only a handful of users were seeing performance degradation. What gives?

In large corporations there are large IT teams and on occasion they can be a challenge to work with. Their requests can seem ridiculous at times, but usually these requests or “push-backs” are opportunities to eliminate a suspected root cause. In this case, our IT partners were skeptical and told us that we were imagining the performance issue. Ok. A simple timing script resolved that. I created a macro to place objects into a drawing. Pretty simple really – time stamp before and after and take the difference between the two. In VBA use the timer function. The output will be in seconds.

Dim t1 as Single, t2 as Single
t1 = Timer()
‘ do lots of stuff
‘ ...
t2 = Timer()
Debug.Print Str(t2-t1)

Test some stuff, but be sure that your macro tests remote resources not just local desktop operations. Open a file, or place a block (or cell) that resides on a server. If you want to run a file open test, run it from a VBS.

' VBS - open and close a MicroStation Files
dim t1, t2, msApp
t1 = Timer()
Set msApp = CreateObject("MicroStation.Application")
msApp.visible = 1
msApp.mbeSendCommand ("rd=C:\tmp\test.dgn")
msApp.quit
t2=Timer()
'--- CREATE AN OUTPUT FILE SO THAT WE KNOW IT IS EMPTY
Set tfo = CreateObject("Scripting.FileSystemObject")
Set tf = tfo.CreateTextFile("c:\tmp\tout.txt", True)
tf.WriteLine(" TIME FOR FILE OPEN AND CLOSE: ")
tf.WriteLine(t2-t1)
tf.close

The test did the job. We were able to prove that performance was bad on these select machines. Still no root cause, but we proved the problem existed. The next “push-back” – well it must be the CAD software. Hmm. I thought I had done enough, but I went ahead and stepped through my checklist (working inside out) – drawings same problem with a new/different drawings so it can’t be drawing file specific, data connections not applicable, CAD application versions and settings are the same, CAD engine configurations the same. There weren’t any differences related to the CAD environment.

Volley back to IT. At this point I had the evidence to demonstrate that the problem wasn’t due to the CAD environment. This meant that our IT partners now had to take a closer look at the differences in the desktop programs and utilities that they managed. This included many things including network, terminal windows, OS system and patches. Network diagnostics checked out, OS system and patches ok, anti-virus software was the same, but… there was a difference. Turns out the AVS settings were not the same. On the computers with the problem the check box to scan remote computers was checked. Every time the CAD program grabbed a file or resource from the server it was being scanned.

A systematic analysis of each component of our design automation platform eventually found the root cause. And, in this case the resolution was relatively simple. That is, turn off remote detection. No need for redundant scans of internally managed servers.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached by contacting us at info@cadmanage.com.

Information to Help You Better Manage Your CAD Environment

by Mark Stefanchuk, CADmanage.com

Information is the key. Without it we can’t analyze and resolve issues, or win funding for new initiatives. As a CAD Manager, one of the toughest things I had to do was present and support the business case for moving projects forward. It required formulating data in a way that each stake holder could understand. Often that was a monetary presentation other times it was technical. We need information that can help us to understand overall operations, as well as detailed data about each workstation. With information readily available we will have much better control of the CAD ecosystem and a better argument when we have to reach out to our boss for funding. Here are five things, pieces of information, that can help you manage your CAD environment.

Computer Names

Capture computer names to support automated deployments. If you have a small footprint this is easy enough to manage and you probably aren’t doing automated deployments – but can offer you insights when combined with other data. In larger environments, especially those with self service request centers knowing who has what software isn’t always that easy. Even if you have a small pool of licenses your installed base could be much larger. Automating license capture is necessary and can help you with the next deployment.

User Names

In addition to providing very granular usage stats, user names can also provide data on asset usage – shared and individual. Tracking user names per computer name can tell you how effectively you are using your hardware assets. And you may find there are opportunities to consolidate resources or in some cases pro-actively address maintenance issues for shared resources. You can extend usage monitoring to just about any asset including software. In large environments software deployment, including CAD software is often managed by a centralized IT department. In this scenario CAD Managers will quickly lose track of who has and is using critical software assets.

How can we capture username and computer name? In a small environment (say fewer than 10) you can just survey the team and catalog the data manually. In larger environments you will want to use automation to capture this data. A quick and easy way to do this is to add some code to your start up script or program. In VBA you can use the following…

   Dim sComp as String, sUser as String
   sComp = $Environ(“computername”)
   sUser = $Environ(“username”)

And a quick way to save this data is to just append it to a log file using the open and print statements. Check http:\\en.wikibooks.org\wiki\Visual_Basic\Files for details on writing text files. Of course a more sophisticated approach would be to use a SQL database or some other data system that you can query and report.

Version Audit

Going a step further in your intelligence gathering you might also consider capturing version dates on first tier software. These include your business critical applications – that is MicroStation, AutoCAD, Civil3D, and so on.  Version audits can help to avert support issues. For example, incompatible plug-ins.

In both MicroStation and AutoCAD VBA this is easy – it’s just a simple call to the application object.

Application.Version

 

In addition to analyzing support issues, version audits can also help with funding – as is the case with Adobe applications where each version counts as a license instance. In larger environments with self service software requests I’ve seen incorrect versions deployed and version audits can help identify workstations that need to be upgraded. For those of you working for a consulting shop, it’s not unusual to be running multiple versions because in these situations we tend to upgrade as users finish one project and are just starting a new project. So in this case we can use version audits to avoid potential production losses.

In smaller environments versions and software deployments are more easily managed, but if you’re managing a reasonably sized CAD ecosystem computer names, user names, and versions can provide better feedback from your CAD environment. And there are many other things you might want to consider tracking – hardware form factors, plotter usage, and user profiles – each provides valuable information and combined they can inform upgrade schedules, refresh strategy, and software management policies. I’ll investigate these more in future posts, but for now experiment with user, computer, and versions, and let me know what else you might want to track.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached by contacting us at info@cadmanage.com.

MicroStation Cell Copy VBA

by Mark Stefanchuk, cadmanage.com

I’ve had several requests to update the cell copy MDL example I wrote for MicroStation SE. Thanks to Amos from Jacobs Engineering for the push earlier this year.

I had updated the MDL shortly after the first V8 release and it continued to work through V8 2004. More recent versions of MicroStation however, have introduced several changes that break this old command. Instead of just updating the MDL code, I decided to take a different approach.

The new version is written in VBA, which really simplifies the code and approach. Using the VBA IDE (integrated development environment), creating user forms (dialog boxes) is faster and easier than in MDL. And if you don’t know how to do something in VBA a quick web search will tell you how. So, for CAD managers who are pressed for time this is definitely the way to go.

The new program operates essentially like the old one. Load the source library into the left listbox. Load the destination library into the right listbox. Highlight the cells from the source library you want to add to the destination library and click the arrow button that points to the destination listbox. Click the Add button located at the bottom of the destination library listbox.

You can find a download link to the new version on our cadgurus page. The code is not locked – something I’m likely going to get grief for – “why are you giving away the store…” – “… because I like these cad manager types…”. And, as always these are examples that come with limited validation and error checking, so use at your own risk. Create backups of your libraries before modifying them with CellCopy, and be careful with the delete option. It will remove all cells from the library and you won’t be able to get them back if you haven’t first backed up the original.*

This is a MicroStation VBA. So you can load it using the “VBA Load (path + vba name)” key-in, for example “VBA LOAD C:\temp\cellcopy.mvba”. I usually use the VBA Project Manager so I can browse for the program and then set the Auto-Load option. (from the pull down menu, Utilities > Macro > Project Manager).

There are a couple of key coding points to make.

1) To copy the cells I use the information provided in the dialog box – the source and destination locations. The cell names are gathered by indexing the  destination listbox and grabbing the highlighted cell names. If the cell name is already in the library then the program will skip it.

2) Delete Cells (advanced) – I didn’t find a method in VBA to delete the cells, but I did find one in the MDL reference. This MDL function requires that you pass a pointer for the cell name string. How do you do that?!

This MicroStation V8i Bentley Community page refers to the undocumented VBA function VarPtr. It says that you should use this to pass a pointer of your char* or void*. The MDL function I used was mdlCell_deleteInLibrary and the MDL Funtion Reference says to pass a type of MSWChar const*. So, when I used VarPtr, the function didn’t work – it returned a cell not found error. Ok.

The same VarPtr discussion on the Bentley Community says to look at http://msdn.microsoft.com/library for more information. Here you will find several other undocumented pointer functions including StrPtr. This one works. Yay. The indexing logic for delete is the same as it is for copy and deleting cells requires these four lines.

One more thing about the example – the Windows Open File Dialog (browse for cell library) helper code (unmodified) can be found on la-solutions web site. Bookmark this site, especially if you are developing programs for MicroStation – there are plenty of great programming examples here.

* If you want to share cell copy with others on your team, but you don’t want them to be able to delete cells then just hide the delete button. You can turn off it’s visible property and/or you can just set the enabled property to false.

About Mark Stefanchuk: Mark is a senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached by contacting us at info@cadmanage.com.