Ensure WPF Add-in Remains in Foreground

Autumn arrived and passed in a flash.
The woods are beautifully coloured, and, unusually, the first snow is already lying on the surrounding hills.

Here are some items of interest from cases we looked at last week:

  1. Ensure WPF add-in remains in foreground
  2. Retrieve all model lines
  3. Set linked file visibility
  4. Access linked file elements and data
  5. Visual Studio 2012 Model Editor Supports OBJ and FBX
  6. Sie Mögen Sich

Ensure WPF Add-in Remains in Foreground

Here is an issue concerning an interaction between a WPF add-in and the Revit TaskDialog, raised and solved by Simon Hooper of
Bestech Systems Ltd:

Question: I have a modal WPF Revit add-in application.

If I show a TaskDialog, e.g. call TaskDialog.Show("Global", "Hello World"), my WPF add-in is sent to ‘the back’ behind Revit, in spite of being modal!

If I use MessageBox.Show(""), the problem does not occur.

How can this be resolved, please?

Answer: I ensured that the WPF form properly parented, i.e. is it a child form of the Revit main application window.

Googling for “JtWindowHandle wrapper class” provides lots of examples of how to parent a normal .NET modeless form.

The procedure is applicable to WPF forms as well.

It seems like the problem is solved by using the following lines:


  exportForm = new ExportForm();
 
  System.Windows.Interop.IWin32Window revit_window
    = new JtWindowHandle( Autodesk.Windows
      .ComponentManager.ApplicationWindow );
 
  System.Windows.Interop.WindowInteropHelper helper
    = new System.Windows.Interop.WindowInteropHelper(
      exportForm );
 
  helper.Owner = revit_window.Handle;

The helper line was the key.

Retrieve all Model Lines

Question: I have a very simple question.

I want to scan my Revit model for all Model Line instances.

RevitLookup tells me I should use the built-in category OST_Lines in my filter, but it does not find the lines.

Here is my logic:


  FilteredElementCollector collector
    = new FilteredElementCollector( doc );
 
  ElementClassFilter familyInstanceFilter
    = new ElementClassFilter(
      typeof( FamilyInstance ) );
 
  ElementCategoryFilter oCategoryFilter
    = new ElementCategoryFilter(
      BuiltInCategory.OST_Lines );
 
  LogicalAndFilter andFilter
    = new LogicalAndFilter(
      familyInstanceFilter,
      oCategoryFilter );
 
  ICollection<Element> oElements
    = collector.WherePasses( andFilter )
      .ToElements();

However, this returns nothing at all!

What is wrong?

Answer:
Yes, this is indeed a rather simple question.

Model lines are model curves.
Model curves are not family instances.

Your collector includes a family instance filter.
The family instance filter will eliminate all model curves, preventing any model lines from being found.

Simply remove the family instance class filter, or replace it by a model curve class filter.

By the way, the most common quick filters are all available using shortcut methods on the collector class itself.

Using the shortcut methods saves you from having to instantiate a separate filter instance, and ensures that all the filters you apply are quick filers.

All the following three variants should work, and presumably produce identical results:


  FilteredElementCollector collector
    = new FilteredElementCollector( doc )
      .OfClass( typeof( ModelCurve ) )
      .OfCategory( BuiltInCategory.OST_Lines );
 
  FilteredElementCollector collector
    = new FilteredElementCollector( doc )
      .OfCategory( BuiltInCategory.OST_Lines );
 
  FilteredElementCollector collector
    = new FilteredElementCollector( doc )
      .OfClass( typeof( ModelCurve ) );

Set Linked File Visibility

Saikat Bhattacharya demonstrates the use of the View class HideElements and UnhideElements methods to

control the visibility of linked files
,
and points out that the SetVisibility method cannot be used for this purpose.

Access Linked File Elements and Data

On a still more exciting and ever recurring linked file topic, Saikat shows how easy it is to access the elements and data contained in a linked file, e.g. to

read the level information of all walls in a linked file
.

Visual Studio 2012 Model Editor Supports OBJ and FBX

Visual Studio 2012 includes a

Model Editor
that
enables you to inspect the 3-D model formats OBJ, Autodesk FBX and COLLADA.
You can also use built-in 3-D primitive generation and materials to create placeholder art for 3-D games and applications.

That sounds handy, doesn’t it?

I recently had a look at the OBJ format to implement

Revit model export to OBJ
,
and obviously Revit already provides built-in support for
FBX export
(LT,
Vasari).

If you wanted to work in depth with FBX yourself, you would presumably use the

FBX SDK
,
currently at version 2013.3, supporting C++ development in Visual Studio 2005, 2008, and 2010.

Sie Mögen Sich

For something completely unrelated to computers and technology, here is a seven minute video, Sie mögen sich, a quite deep philosophical relationship analysis by Shaban & Käptn Peng that my son Christopher pointed out and that I enjoyed a lot:

It is in German, however, and probably makes no sense at all unless you understand the language.


Comments

8 responses to “Ensure WPF Add-in Remains in Foreground”

  1. Hi, Jeremy.
    I had already encountered with the WPF Modal Window bug. I described it in the case #06929715 and the problem still didn’t resolve.
    As I can notice sometimes WPF modal window become non-modal in some cases even if Owner is specified. I can’t determine when it happens and to reproduce this behavior but the problem is really exists.
    Regards, Victor.

  2. Dear Victor,
    Thank you for the important pointer!
    I looked at the case you mention. From its description, it sounds as if it were mainly concerned with extensible storage.
    Since this WPF issue seems to be a totally separate topic, it might help if you could submit a separate case for that.
    I am sorry to hear that it is not reproducible. A reproducible case would make if much easier and more convincing to describe and provide an invaluable basis for working towards a resolution.
    Cheers, Jeremy.

  3. Hi, Jeremy.
    As I remember a WPF modal window became non-modal if any Revit message dialog appears.
    If I don’t forget, I’ll try to find dependence in WPF modal dialog behavior, when I encountered next time with the issue.
    Regards, Victor.

  4. Dear Victor,
    Thank you. Actually, I now found your resulting SPR #218817 as well and added some comments to it.
    I hope the workaround described above solves the issue in all your cases as well.
    Cheers, Jeremy.

  5. I love the song “Sie mögen sich”!

  6. Dear Daren,
    Wow, cool, seriously? Thanks! Do you understand it? For me the text and story and philosophy behind it is a lot more important than the music.
    Cheers, Jeremy.

  7. Ning Zhou Avatar
    Ning Zhou

    hi Jeremy,
    i have more general questions, if we’re going to buy new VS, is Microsoft Visual Studio Pro -VsPro2012 OLP the correct one to buy?
    currently we’re using both Revit 2012 and Revit 2013, for API development, most likely we’ll still use Revit 2012 for now due to obvious reason, is VS2012 OK for both Revit 2013 and 2012 API?
    if we buy 2010 version first then upgrade to 2012 later, are there any issues like price-wise?
    many thanks.
    Joe

  8. Dear Joe,
    Price-wise, I cannot say anything at all. I have no idea about prices, since I have an MSDN subscription and can download any MS development software I need.
    I think you can use both VS 2010 and 2012 for both Revit 2012 and 2013. The .NET framework shields you well from differences. All you need to ensure is that you support .NET 4.0, I think. Or was it 3.5 for Revit 2012? Look in the developer guide system requirements for 2012, and in the wikihelp for 2013:
    http://wikihelp.autodesk.com/Revit/enu/2013/Help/00006-API_Developer%27s_Guide/0001-Introduc1/0002-Welcome_2/0008-Supporte8
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading