Zoom your BIM

by Guy Robinson 8. December 2008 12:28

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 (3) -

Eric Anastas
Eric Anastas United States
12/12/2008 8:55:57 AM #

I problem I have is the handle of the button (or the main Revit window for that matter) changes every time I run Revit. I am able to get the handle of the main Revit window by looking for a process named REVIT, but the mass floors button is nested 3 levels deep from the main window.

Here's a screenshot from SPY++:
i2.photobucket.com/albums/y23/aireq/RevitSpy.jpg

I know this will only open the mass floors dialog, but as far as I can see there is no other way to generate a MassFloor through the interface or the API. Mass objects have a MassFloor parameter, but this is always null regardless of whether there are mass floors attached to the mass.

Guy Robinson
Guy Robinson New Zealand
12/12/2008 2:02:38 PM #

Eric,

Haven't had to use Mass floors before from the API. Similar technique though. You need to get the handle of the button and then send the button a message. You'll probably need to send a WM_SETFOCUS first. And then sending a WM_COMMAND and an IDOK should work I think (usually gets interpreted as a keypress). If not use WM_KEYDOWN and then a WM_KEYUP.  Note this will just bring up the dialog for selecting the levels which may not be all that you're after.

Cheers,

Guy

Eric Anastas
Eric Anastas United States
12/13/2008 4:32:01 AM #

This helps a project I am working on a bunch.

Is there a way to click buttons on the Options bar? I'm using Spy++, which shows WM_COMMAND's for all the menus and toolbar buttons in Revit. However clicking a button on the options bar does not show a WM_COMMAND message. Is there some other kind of message I can send to do that?

Specifically I'm trying to generate a MassFloor from a level and a mass by clicking the "Mass Floors" button when a mass is selected.

Comments are closed

About the Author

A .NET software Developer providing custom applications and commands for architecture firms exclusively working with Autodesk Revit and integration with any associated applications. All from a little place north of Whitianga, New Zealand.

Page List

Disclaimer

I'm self employed so the opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway☺

© Copyright2008

Creative Commons License
Blog content is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

With the following exception. All code snippets, application and libraries are licensed under a a Apache License Version 2.0

Autodesk Revit®

Autodesk: Revit is a product that is wholly owned by Autodesk. Any reference to Revit,Revit API, Revit Architecture, Revit MEP or Revit Structure on this site is made acknowledging this ownership. Refer to Autodesk's own web site and product pages for specific trademark and copyright information. Autodesk represents a great many products and every attempt will be made to respect their ownership whenever one of these other products is mentioned on this site.