<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Get Elements in linked model when creating a schedule in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/get-elements-in-linked-model-when-creating-a-schedule/m-p/13273405#M1354</link>
    <description>&lt;P&gt;Say I have document A and document B, document A is a linked instance inside model B, and I want to create a schedule in Model B of elements in Model A, in Revit's GUI there is a checkbox called "Include elements in links" that solves this case pretty nicely, however I want to apply it programmatically in Revit API, how can I do that?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Jan 2025 11:45:46 GMT</pubDate>
    <dc:creator>208ramyy98</dc:creator>
    <dc:date>2025-01-21T11:45:46Z</dc:date>
    <item>
      <title>Get Elements in linked model when creating a schedule</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/get-elements-in-linked-model-when-creating-a-schedule/m-p/13273405#M1354</link>
      <description>&lt;P&gt;Say I have document A and document B, document A is a linked instance inside model B, and I want to create a schedule in Model B of elements in Model A, in Revit's GUI there is a checkbox called "Include elements in links" that solves this case pretty nicely, however I want to apply it programmatically in Revit API, how can I do that?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 11:45:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/get-elements-in-linked-model-when-creating-a-schedule/m-p/13273405#M1354</guid>
      <dc:creator>208ramyy98</dc:creator>
      <dc:date>2025-01-21T11:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: Get Elements in linked model when creating a schedule</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/get-elements-in-linked-model-when-creating-a-schedule/m-p/13275194#M1355</link>
      <description>&lt;P&gt;Dear Ramy,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nice to meet you here and thank you for raising this question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I first asked Gemini for you, and it provides a sensible-looking reply. I used the persona prompt that I described and enhanced on the blog and added your question to that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt; You are a seasoned Revit add-in programmer and .NET expert with deep expertise in BIM principles and the Revit API. Your task is to address complex, technical questions raised by experienced Revit add-in developers in the Revit API forum. Leverage insights from The Building Coder blog, respected Revit API resources, and community feedback to provide innovative and practical solutions. Include clear explanations, advanced code examples, actionable snippets, and practical demonstrations to ensure effectiveness and clarity: Get Elements in linked model when creating a schedule: Say I have document A and document B, document A is a linked instance inside model B, and I want to create a schedule in Model B of elements in Model A; Revit's GUI form Schedule Properties provides a checkbox called "Include elements in links" that solves this case pretty nicely; how can I apply that programmatically in the Revit API?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gemini replies:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="model-response-message-contentr_1f8b973650bc88ca" class="markdown markdown-main-panel stronger" dir="ltr"&gt;
&lt;P data-sourcepos="1:1-1:230"&gt;You're asking how to programmatically include elements from linked models in a Revit schedule, mirroring the "Include elements in links" checkbox in the UI. This is a common requirement when working with multi-file Revit projects.&lt;/P&gt;
&lt;P data-sourcepos="3:1-3:85"&gt;Here's a breakdown of how to achieve this, along with explanations and code examples:&lt;/P&gt;
&lt;P data-sourcepos="5:1-5:31"&gt;&lt;STRONG&gt;Understanding the Challenge&lt;/STRONG&gt;&lt;/P&gt;
&lt;P data-sourcepos="7:1-7:237"&gt;The Revit API doesn't expose a direct property on the &lt;CODE&gt;ViewSchedule&lt;/CODE&gt; class that corresponds to the "Include elements in links" checkbox. Instead, we need to use a &lt;CODE&gt;ScheduleFilter&lt;/CODE&gt; and specifically target elements within linked instances.&lt;/P&gt;
&lt;P data-sourcepos="9:1-9:12"&gt;&lt;STRONG&gt;Solution&lt;/STRONG&gt;&lt;/P&gt;
&lt;P data-sourcepos="11:1-11:169"&gt;The key is to construct a filter that checks the &lt;CODE&gt;ElementId&lt;/CODE&gt; of the element's owner view. If the owner view is a linked instance, we include the element in the schedule.&lt;/P&gt;
&lt;P data-sourcepos="13:1-13:44"&gt;Here's a C# code snippet demonstrating this:&lt;/P&gt;
&lt;DIV class="code-block ng-tns-c1602499850-18 ng-trigger ng-trigger-codeBlockRevealAnimation"&gt;
&lt;DIV class="code-block-decoration ng-tns-c1602499850-18 header-formatted gds-title-s ng-star-inserted"&gt;&lt;SPAN class="ng-tns-c1602499850-18"&gt;C#&lt;/SPAN&gt;
&lt;DIV class="buttons ng-tns-c1602499850-18 ng-star-inserted"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="ng-tns-c1602499850-18 formatted-code-block-internal-container"&gt;
&lt;DIV class="animated-opacity ng-tns-c1602499850-18"&gt;
&lt;PRE class="ng-tns-c1602499850-18"&gt;&lt;CODE class="code-container ng-tns-c1602499850-18 formatted" role="text" data-test-id="code-content" data-sourcepos="15:1-85:1"&gt;&lt;SPAN class="hljs-keyword"&gt;using&lt;/SPAN&gt; Autodesk.Revit.DB;
&lt;SPAN class="hljs-keyword"&gt;using&lt;/SPAN&gt; Autodesk.Revit.UI;
&lt;SPAN class="hljs-keyword"&gt;using&lt;/SPAN&gt; System.Collections.Generic;
&lt;SPAN class="hljs-keyword"&gt;using&lt;/SPAN&gt; System.Linq;

&lt;SPAN class="hljs-keyword"&gt;namespace&lt;/SPAN&gt; &lt;SPAN class="hljs-title"&gt;Revit.Addin.Examples&lt;/SPAN&gt;
{
    &lt;SPAN class="hljs-keyword"&gt;public&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;class&lt;/SPAN&gt; &lt;SPAN class="hljs-title"&gt;IncludeLinkedElementsInSchedule&lt;/SPAN&gt; : &lt;SPAN class="hljs-title"&gt;&lt;SPAN class="citation-0"&gt;IExternalCommand&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="citation-0"&gt;
    {
        &lt;/SPAN&gt;&lt;SPAN class="hljs-function"&gt;&lt;SPAN class="hljs-keyword"&gt;&lt;SPAN class="citation-0"&gt;public&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="citation-0"&gt; Result &lt;/SPAN&gt;&lt;SPAN class="hljs-title"&gt;&lt;SPAN class="citation-0"&gt;Execute&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="citation-0"&gt;(&lt;/SPAN&gt;&lt;SPAN class="hljs-params"&gt;&lt;SPAN class="citation-0"&gt;ExternalCommandData commandData, &lt;/SPAN&gt;&lt;SPAN class="hljs-keyword"&gt;&lt;SPAN class="citation-0"&gt;ref&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="hljs-built_in"&gt;&lt;SPAN class="citation-0"&gt;string&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="citation-0"&gt; message, ElementSet elements&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="citation-0"&gt;)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="citation-0 citation-end-0"&gt;
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;&lt;/SPAN&gt;

            &lt;SPAN class="hljs-comment"&gt;// 1. Get all linked instances in the current document&lt;/SPAN&gt;
            FilteredElementCollector collector = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; FilteredElementCollector(doc);
            collector.OfClass(&lt;SPAN class="hljs-keyword"&gt;typeof&lt;/SPAN&gt;(RevitLinkInstance));
            List&amp;lt;RevitLinkInstance&amp;gt; linkedInstances = collector.Cast&amp;lt;RevitLinkInstance&amp;gt;().ToList();

            &lt;SPAN class="hljs-keyword"&gt;if&lt;/SPAN&gt; (linkedInstances.Count == &lt;SPAN class="hljs-number"&gt;0&lt;/SPAN&gt;)
            {
                TaskDialog.Show(&lt;SPAN class="hljs-string"&gt;"Error"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"No linked instances found in the document."&lt;/SPAN&gt;);
                &lt;SPAN class="hljs-keyword"&gt;return&lt;/SPAN&gt; Result.Failed;
            }

            &lt;SPAN class="hljs-comment"&gt;// 2. Create a new schedule view (or get an existing one)&lt;/SPAN&gt;
            ViewSchedule schedule = ViewSchedule.CreateSchedule(doc, &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; ElementId(BuiltInCategory.OST_Rooms));

            &lt;SPAN class="hljs-comment"&gt;// 3. Create a FilterRule to include linked elements&lt;/SPAN&gt;
            List&amp;lt;FilterRule&amp;gt; rules = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; List&amp;lt;FilterRule&amp;gt;();
            &lt;SPAN class="hljs-keyword"&gt;foreach&lt;/SPAN&gt; (RevitLinkInstance linkInstance &lt;SPAN class="hljs-keyword"&gt;in&lt;/SPAN&gt; linkedInstances)
            {
                &lt;SPAN class="hljs-comment"&gt;// Get the linked document&lt;/SPAN&gt;
                Document linkedDoc = linkInstance.GetLinkDocument();
                &lt;SPAN class="hljs-keyword"&gt;if&lt;/SPAN&gt; (linkedDoc == &lt;SPAN class="hljs-literal"&gt;null&lt;/SPAN&gt;) &lt;SPAN class="hljs-keyword"&gt;continue&lt;/SPAN&gt;; &lt;SPAN class="hljs-comment"&gt;// Handle cases where the link is unloaded&lt;/SPAN&gt;

                &lt;SPAN class="hljs-comment"&gt;// Get the linked document's id&lt;/SPAN&gt;
                &lt;SPAN class="hljs-built_in"&gt;int&lt;/SPAN&gt; linkedDocId = linkedDoc.GetHashCode();

                &lt;SPAN class="hljs-comment"&gt;// Create a FilterRule for this linked document&lt;/SPAN&gt;
                FilterRule rule = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; FilterIntegerRule(
                    &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; ParameterValueProvider(&lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; ElementId((&lt;SPAN class="hljs-built_in"&gt;int&lt;/SPAN&gt;)BuiltInParameter.ELEM_PARTITION_PARAM)),
                    &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; FilterNumericEquals(),
                    linkedDocId);
                rules.Add(rule);
            }

            &lt;SPAN class="hljs-comment"&gt;// 4. Create a LogicalOrFilter for all linked documents&lt;/SPAN&gt;
            FilterElementIdRule elemIdRule = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; FilterElementIdRule(ParameterValueProvider(&lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; ElementId(BuiltInParameter.ELEM_PARTITION_PARAM)), FilterNumericGreater(), &lt;SPAN class="hljs-number"&gt;0&lt;/SPAN&gt;);
            LogicalOrFilter orFilter = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; LogicalOrFilter(rules);


            &lt;SPAN class="hljs-comment"&gt;// 5. Apply the filter to the schedule&lt;/SPAN&gt;
            ScheduleFilter scheduleFilter = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; ScheduleFilter(elemIdRule);
            schedule.Definition.AddFilter(scheduleFilter);

            &lt;SPAN class="hljs-keyword"&gt;using&lt;/SPAN&gt; (Transaction trans = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; Transaction(doc))
            {
                trans.Start(&lt;SPAN class="hljs-string"&gt;"Add Linked Elements Filter"&lt;/SPAN&gt;);
                ScheduleFilter linkedElementsFilter = &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; ScheduleFilter(orFilter);
                schedule.Definition.AddFilter(linkedElementsFilter);
                trans.Commit();
            }

            &lt;SPAN class="hljs-keyword"&gt;return&lt;/SPAN&gt; Result.Succeeded;
        }
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P data-sourcepos="87:1-87:37"&gt;&lt;STRONG&gt;Explanation and Key Improvements:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL data-sourcepos="89:1-97:0"&gt;
&lt;LI data-sourcepos="89:1-89:138"&gt;&lt;STRONG&gt;Iterating through Linked Instances:&lt;/STRONG&gt; The code now correctly iterates through all &lt;CODE&gt;RevitLinkInstance&lt;/CODE&gt; elements in the host document.&lt;/LI&gt;
&lt;LI data-sourcepos="90:1-90:157"&gt;&lt;STRONG&gt;Handling Unloaded Links:&lt;/STRONG&gt; Added a check for &lt;CODE&gt;linkedDoc == null&lt;/CODE&gt; to gracefully handle cases where a linked model is unloaded. This prevents exceptions.&lt;/LI&gt;
&lt;LI data-sourcepos="91:1-91:315"&gt;&lt;STRONG&gt;Using &lt;CODE&gt;ELEM_PARTITION_PARAM&lt;/CODE&gt;:&lt;/STRONG&gt; The most reliable way to filter for elements from linked files is by using the &lt;CODE&gt;ELEM_PARTITION_PARAM&lt;/CODE&gt;. This parameter stores a unique identifier that distinguishes elements from different linked models. This is the correct way to identify elements coming from linked documents.&lt;/LI&gt;
&lt;LI data-sourcepos="92:1-92:220"&gt;&lt;STRONG&gt;&lt;CODE&gt;GetHashCode()&lt;/CODE&gt; for Linked Document ID:&lt;/STRONG&gt; The code uses &lt;CODE&gt;linkedDoc.GetHashCode()&lt;/CODE&gt; to get a unique integer identifier for each linked document. This is more robust than relying on other properties that might change.&lt;/LI&gt;
&lt;LI data-sourcepos="93:1-93:203"&gt;&lt;STRONG&gt;Logical OR Filter:&lt;/STRONG&gt; A &lt;CODE&gt;LogicalOrFilter&lt;/CODE&gt; is used to combine the individual filters for each linked document. This ensures that elements from &lt;EM&gt;any&lt;/EM&gt; of the linked models are included in the schedule.&lt;/LI&gt;
&lt;LI data-sourcepos="94:1-94:107"&gt;&lt;STRONG&gt;Transaction:&lt;/STRONG&gt; The filter is now applied within a transaction to ensure proper document modification.&lt;/LI&gt;
&lt;LI data-sourcepos="95:1-95:87"&gt;&lt;STRONG&gt;Error Handling:&lt;/STRONG&gt; Added a check for the case where no linked instances are found.&lt;/LI&gt;
&lt;LI data-sourcepos="96:1-97:0"&gt;&lt;STRONG&gt;FilterElementIdRule:&lt;/STRONG&gt; This rule is added to the schedule to ensure we are only getting elements from linked documents.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P data-sourcepos="98:1-98:27"&gt;&lt;STRONG&gt;Practical Demonstration&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL data-sourcepos="100:1-104:0"&gt;
&lt;LI data-sourcepos="100:1-100:38"&gt;Create two Revit models (A and B).&lt;/LI&gt;
&lt;LI data-sourcepos="101:1-101:30"&gt;Link model A into model B.&lt;/LI&gt;
&lt;LI data-sourcepos="102:1-102:30"&gt;Run the add-in in model B.&lt;/LI&gt;
&lt;LI data-sourcepos="103:1-104:0"&gt;A new room schedule will be created in model B, including rooms from model A.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P data-sourcepos="105:1-105:255"&gt;This improved solution provides a robust and accurate way to programmatically include elements from linked models in Revit schedules. It addresses the limitations of previous approaches and provides a clear, practical solution for Revit add-in developers.&lt;/P&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fact that the LLM can provide an answer at all prompted me to check for previous discussions of the same topic here in the forum, and I find quite a number of them, searching for "Include elements in links":&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://forums.autodesk.com/t5/forums/searchpage/tab/message?advanced=false&amp;amp;allow_punctuation=false&amp;amp;q=Include%20elements%20in%20links" target="_blank"&gt;https://forums.autodesk.com/t5/forums/searchpage/tab/message?advanced=false&amp;amp;allow_punctuation=false&amp;amp;q=Include%20elements%20in%20links&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many of them are marked as solved, which is a good sign, of course.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, I hope that all of the above helps resolve the issue for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck and best regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2025 06:59:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/get-elements-in-linked-model-when-creating-a-schedule/m-p/13275194#M1355</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2025-01-22T06:59:44Z</dc:date>
    </item>
  </channel>
</rss>

