Element Collector inaccurate results when using OpenAndActivateDocument

Element Collector inaccurate results when using OpenAndActivateDocument

Anonymous
Not applicable
2,559 Views
11 Replies
Message 1 of 12

Element Collector inaccurate results when using OpenAndActivateDocument

Anonymous
Not applicable

Note: This is duplicate post, I'm now posting under ADN account. I've redirected the other post's solution to this one.

 

I'm fairly confident I've found a bug in Revit API. When using OpenAndActivateDocument to open Revit doc, my view specific FilteredElementCollector is returning inaccurate result. The collector is picking up extra walls just above/below visible range of plan view. Using exact same code, but opening doc with OpenDocumentFile will return the proper result.

 

This code returns result of 4 walls in Level 1 view (this is NOT correct)

Dim doc = uiapp.OpenAndActivateDocument("C:\Users\Jon\Desktop\API test\Project1.rvt").Document

Dim collectorPlanViews As New FilteredElementCollector(doc)
collectorPlanViews.OfClass(GetType(ViewPlan))

Dim Level1View As ViewPlan = collectorPlanViews(0)

Dim collectorWalls As New FilteredElementCollector(doc, Level1View.Id)
collectorWalls.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType()

MessageBox.Show(collectorWalls.Count & " walls in " & Level1View.Name, "API Test 2")

Note: If I put an random message box after opening the doc, the collector returns proper result.

Note 2: If the walls above/below are moved farther away from Level 1 view range, they are not collected.

 

 

This identical code (other than how doc is opened) returns correct result of 2 walls)

Dim doc = app.OpenDocumentFile("C:\Users\Jon\Desktop\API test\Project1.rvt")

Dim collectorPlanViews As New FilteredElementCollector(doc)
collectorPlanViews.OfClass(GetType(ViewPlan))

Dim Level1View As ViewPlan = collectorPlanViews(0)

Dim collectorWalls As New FilteredElementCollector(doc, Level1View.Id)
collectorWalls.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType()

MessageBox.Show(collectorWalls.Count & " walls in " & Level1View.Name, "API Test 3")

doc.Close()

 

This is causing unexpected results through out my application when filtering objects per view.

 

Any thoughts on this?

 

Thanks,

Jon Albert

 

 

0 Likes
2,560 Views
11 Replies
Replies (11)
Message 2 of 12

jeremytammik
Autodesk
Autodesk

Dear Jon Albert,

  

This thread has not yet been escalated to an ADN case. I'll keep watching it and pick it up as soon as it is. If nothing happens soon, we should explore why it is not being escalated.

 

Meanwhile, we can already prepare for reporting this to the development team. They will require a minimal complete reproducible case, preferably a minimal model in which to test, a macro to execute, and detailed step by step instructions to reproduce the problematic behaviour:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

 

In this case, two macros might be in order, using the two different opening methods you describe.

 

Thank you!

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 12

jeremytammik
Autodesk
Autodesk

Now I noticed your other discussion with more details.

 

For the sake of completeness, here is the link to it:

 

https://forums.autodesk.com/t5/revit-api-forum/element-collector-inaccurate-results-when-using/m-p/8...

  



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 12

Anonymous
Not applicable

Thanks Jeremy,

 

Per your suggestion on "Reproducible Test Case", I'm providing 2 Revit projects for testing. One has Macros, the other has sample view with 2 walls showing.

 

Link to sample projects

 

Steps to reproduce.

  1. Download sample project files from link above.
  2. Place the "Project1.rvt" at "C:\Project1.rvt" (this is hard coded in Macros).
  3. Open "Macro Test Project.rvt" file.
  4. Enable Macros
  5. From Macro Manager, run "OpenAndActivateDocument".
    1. This macro has the bug and shows there are 4 walls in Level 1 view.
    2. Note: On few occasions this macro returned proper result of 2 walls, but most often returns 4 walls.
  6. From Macro Manager, run "OpenDocumentFile".
    1. This macro returns 2 walls every time.

 

For quick review, below is complete code of macros.

 

ThisDocument.vb

Imports System
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI.Selection
Imports System.Collections.Generic
Imports System.Linq

<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
<Autodesk.Revit.DB.Macros.AddInId("72A9A007-C98B-4AE8-8491-E4D6FA1108D1")> _
Partial Public Class ThisDocument

    Private Sub Module_Startup(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Startup
    End Sub
    
    Private Sub Module_Shutdown(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Shutdown
    End Sub
    
    Public Sub OpenAndActivateDocument()
        
        Dim OrigDocPath As String = Me.Application.ActiveUIDocument.Document.PathName

        Dim doc = Me.Application.OpenAndActivateDocument("C:\Project1.rvt").Document

         Dim sample As New SearchForWalls(doc)
        sample.Run()

        Me.Application.OpenAndActivateDocument(OrigDocPath)
        doc.Close(False)
        
    End Sub
    
    Public Sub OpenDocumentFile()
        
        Dim doc = Me.Application.Application.OpenDocumentFile("C:\Project1.rvt")

         Dim sample As New SearchForWalls(doc)
         sample.Run()
         
    End Sub
    
End Class

SearchForWalls.vb

Imports System
Imports System.Collections.Generic

Imports Autodesk
Imports Autodesk.Revit
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI

Public Class SearchForWalls
    
    Private doc As Document = Nothing

    Public Sub New(ByVal _doc As Document)
        doc = _doc
    End Sub

   Public Sub Run()
       
       Try
          
          Dim Level1View As ViewPlan = Nothing
          
          Dim collector As New Autodesk.Revit.DB.FilteredElementCollector(doc)
          collector.OfClass(GetType(ViewPlan))
          
          For Each elem As Element In collector.ToElements()
              
              If elem.Name = "Level 1" Then
                  
                  Level1View = elem
                  Exit For
                  
              End If
                            
          Next

        Dim collectorWalls As New FilteredElementCollector(doc, Level1View.Id)
        collectorWalls.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType()
        
        Dim walls As List(Of Element) = collectorWalls.ToElements()
        
        TaskDialog.Show("API Test", walls.Count)

      Catch ee As Exception
          
          TaskDialog.Show("API Test", ee.Message)
          
      End Try
      
   End Sub
   
End Class
0 Likes
Message 5 of 12

Anonymous
Not applicable
Edit: I've added a 3rd macro called "OpenAndActivateWithMessage" (identical to "OpenAndActivate" but with message box prior to wall search). It returns proper result of 2 walls. Additional tests steps: 7. From Macro Manager, run "OpenAndActivateDocumentWithMessage". 1. This macro returns 2 walls every time which is correct. 8. From Macro Manager, run "OpenDocumentFile", then immediately run "OpenAndAtivateDocument". 1. For some reason, "OpenAndAtivateDocument" returns the proper result when ran immediately after "OpenDocumentFile". Hopefully I have not made this to confusing 🙂
0 Likes
Message 6 of 12

jeremytammik
Autodesk
Autodesk

Dear Jon Albert,

 

Thank you for your update and sample macros.

 

I logged the issue REVIT-146394 [filtered element collector with view returns wrong result after OpenAndActivateDocument] with our development team for this on your behalf as it requires further exploration and possibly a modification to our software. Please make a note of this number for future reference.

 

You are welcome to request an update on the status of this issue or to provide additional information on it at any time quoting this change request number.

 

This issue is important to me. What can I do to help?

 

This issue needs to be assessed by our engineering team and prioritised against all other outstanding change requests. Any information that you can provide to influence this assessment will help. Please provide the following where possible:

 

  • Impact on your application and/or your development.
  • The number of users affected.
  • The potential revenue impact to you.
  • The potential revenue impact to Autodesk.
  • Realistic timescale over which a fix would help you.
  • In the case of a request for a new feature or a feature enhancement, please also provide detailed Use cases for the workflows that this change would address.

 

This information is extremely important. Our engineering team have limited resources, and so must focus their efforts on the highest impact items. We do understand that this will cause you delays and affect your development planning, and we appreciate your cooperation and patience.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 7 of 12

Anonymous
Not applicable

Thanks Jeremy!

 

I'll be sure to provide the additional info via ADN site.

 

Jon

0 Likes
Message 8 of 12

jeremytammik
Autodesk
Autodesk

Dear Jon Albert,

 

Thank you for your patience.

 

The development team praised the sample macros that you supplied for the issue REVIT-146394 [filtered element collector with view returns wrong result after OpenAndActivateDocument] and say:

 

First, thank you for the excellent demo macros!

 

From the behaviour, it is clear that there is a problem with the OpenAndActivateDocument API workflow. If you open macro_test_project.rvt, edit the macros so that they have the correct path to project1.rvt, then run the OpenAndActivateDocument macro, you'll get a count of 4 walls. If you run OpenDocumentFile macro, you get count of 2 walls, which is the correct count.

 

One interesting thing is that if you run the OpenDocumentFile macro before using the OpenAndActivateDocument macro, sometimes you'll get OpenAndActivateDocument to report 2 walls instead of the normal 4 walls.

 

I did a little debugging in Revit...

 

More work is required to fix this in a future version.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 9 of 12

Anonymous
Not applicable

Great, thanks for the update!

 

Jon

0 Likes
Message 10 of 12

Anonymous
Not applicable

When reading through comments on the FilteredElementCollector constructor, I found this:

 

For elements which are outside of a crop region, they may
still be passed by the collector because Revit relies on later processing to
eliminate the elements hidden by the crop. This effect may more easily occur
for non-rectangular crop regions, but may also happen even for rectangular crops.
You can compare the boundary of the region with the element's boundary if more
precise results are required.

0 Likes
Message 11 of 12

Anonymous
Not applicable

Hi,

 

Is there any update on change # REVIT-146394 for the issue reported in this thread?

 

I just tested with the latest Revit 2022 Preview and this is still an issue with API.

 

Thanks,

 

Jon

0 Likes
Message 12 of 12

jeremy_tammik
Alumni
Alumni

Dear Jon Albert,

 

Sorry, bad news for you, but also some explanation and workaround suggestions:

 

The development team verified the problem and created a new development issue for deeper investigation:

 

REVIT-146655 [filtered element collector with view returns wrong result after OpenAndActivateDocument]

 

That led to the following report:

 

I am closing this issue as 'will not fix'. The issue is much broader than the description indicates and the problem is with OpenAndActivateDocument() being an asynchronous call. The call returns even before the document has been fully initialised. This is a known issue, cf. another reference:

 

https://forums.autodesk.com/t5/revit-api-forum/modifying-an-openandactivate-document/td-p/6781646

 

The following simple test works. If you insert a modal task dialogue soon after the call to OpenAndActivateDocument() and then call sample.Run();  it ends up finding the right number of walls (i.e. 2 walls). See the sample below:

 

  Dim doc = Me.Application.OpenAndActivateDocument("C:\Path\To\Project1.rvt").Document
  TaskDialog.Show("Waiting", "Waiting")
  Dim sample As New SearchForWalls(doc)
  sample.Run()

  

The above is not an acceptable workaround, but a better one exists. For developers needing to update or query the document and activate it too, I'd recommend using OpenDocumentFile() first. The problem description states that this works well. Once the document is opened and fully initialised, you can query/update it and then call OpenAndActivateDocument(). Since the document is already opened, this call will merely activate the document. For example, the following code should work:

 

  Dim doc = Me.Application.Application. OpenDocumentFile("C:\Path\To\Project1.rvt")
  Dim sample As New SearchForWalls(doc)
  sample.Run()
  Me.Application.OpenAndActivateDocument("C:\Path\To\Project1.rvt")

  

That said, i would be better if the `DocumentOpened` event was called after the document is fully initialised. I see the same behaviour in the DocumentOpened event handler when opened from OpenAndActivateDocument; everything works as expected when opened from OpenDocumentFile(). Fixing these issues would be more than just a fix. I am closing REVIT-146655.

 

This explains what is happening and describes how to easily handle it in a reliable manner.

 

I hope this helps.

 

Best regards,

 

Jeremy

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes