Posted on 8. December 2008

Zoom your BIM

Jeremy has started to cover how you can use Win32API methods to enhance your commands. I’ve been doing a lot of this recently and I found a simple way to establish the handle for the active Revit session the command was run from.  This is important because Users can have multiple sessions running at once , so enumerating windows to find the handle needs additional techniques to ensure you get the correct handle.

Although it’s slower than enumerating windows, you only need to do it once and it’s guaranteed to get the active Revit session:

   1: IntPtr windowHandleByProcess = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

It’s not the perfect solution but it’s reliable and easy. So what to do with this handle?

You’ll notice if you use ShowElements() with one of the overloads that it tends to be a little too zoomed. Wouldn’t be great if you could use some of the other standard zoom commands in your own code to zoom out a little or zoom to fit? Something like this:

   1: private void BtnZoom2X_Click(object sender, EventArgs e)
   2: {
   3:     Zoom.Out2X();
   4: }
   5:  
   6: private void ZoomFit_Click(object sender, EventArgs e)
   7: {
   8:     Zoom.ToFit();
   9: }
  10:  
  11: private void ZoomAllFit_Click(object sender, EventArgs e)
  12: {
  13:     Zoom.AllToFit();
  14: }
  15:  
  16: private void ZoomSSize_Click(object sender, EventArgs e)
  17: {
  18:     Zoom.SheetSize();
  19: }

Well now you can. Source and binaries at the usual place. Here’s a screen shot of the demo command:

Zoom

What’s happening is I’m sending Revit a Windows Message which is the same thing that happens when a User selects a command from a menu. The only additional information you need is the command ID which you can get using WinSpy++ or other similar tools.

Comments

Comments are closed