<?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 Re: MEPSystem Id From Element C# in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/11528633#M27405</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5263562"&gt;@Kevin.Lawson.PE&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Can we somehow access the first element of connector set?&lt;BR /&gt;Similiar to python by adding [0] at the end?&amp;nbsp;&lt;BR /&gt;the only way i found was to rewrite this to List&amp;lt;connector&amp;gt; and than access but maybe there is a simpler way ?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Nov 2022 16:05:49 GMT</pubDate>
    <dc:creator>lukasz_koc1</dc:creator>
    <dc:date>2022-11-03T16:05:49Z</dc:date>
    <item>
      <title>MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10164381#M27399</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In python this works:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;picked = uidoc.Selection.PickObject(ObjectType.Element)
element = doc.GetElement(picked)
element_systemId = element.MEPSystem.GetTypeId() &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I am trying to replicate this in C#:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;Reference selection1 = uidoc.Selection.PickObject(ObjectType.Element,p); 
Element element1 = doc.GetElement(selection1) as Element;
MEPSystem element_sys = element1 as MEPSystem; 
ElementId system_id = element_sys.GetTypeId();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And am getting an error of "object reference not set to an instance of an object"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 15:31:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10164381#M27399</guid>
      <dc:creator>smarente1</dc:creator>
      <dc:date>2021-03-17T15:31:33Z</dc:date>
    </item>
    <item>
      <title>MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10164615#M27400</link>
      <description>&lt;P&gt;Step through this in the debugger and the error will become obvious.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the code you have implemented, the C# variable element_sys is guaranteed to be null.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also navigate through your model using RevitLookup and the error will also become obvious.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, what is the error?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In Python, you are selecting a duct or pipe element or fitting or something and querying it for its MEP system element using its MEPSystem property.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your C# code is attempting to &lt;U&gt;&lt;STRONG&gt;cast&lt;/STRONG&gt;&lt;/U&gt; the same element to an MEPSystem object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is doomed to fail, I'm afraid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Simply&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;reference its property&lt;/STRONG&gt;&lt;/U&gt;, just like you did in Python:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="general"&gt;  MEPSystem element_sys = element1.MEPSystem;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 16:32:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10164615#M27400</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2021-03-17T16:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10164631#M27401</link>
      <description>&lt;P&gt;Thank you Jeremey for your detailed response!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This still gives me an error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"MEPSystem does not contain a definition for 'MEPSystem' "&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="smarente1_0-1616000194524.png" style="width: 848px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/894623iC3FD67AD6FE173BA/image-dimensions/848x98?v=v2" width="848" height="98" role="button" title="smarente1_0-1616000194524.png" alt="smarente1_0-1616000194524.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 16:59:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10164631#M27401</guid>
      <dc:creator>smarente1</dc:creator>
      <dc:date>2021-03-17T16:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10165094#M27402</link>
      <description>&lt;P&gt;Figured out a solution. Casted the element to a MEP Curve before referencing its property MEPSystem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;MEPCurve mepcurve = element1 as MEPCurve;
MEPSystem e1_sys = mepcurve.MEPSystem;
ElementId system_Id = e1_sys.GetTypeId();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone has a simpler way please let me know.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 19:14:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10165094#M27402</guid>
      <dc:creator>smarente1</dc:creator>
      <dc:date>2021-03-17T19:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10172786#M27403</link>
      <description>&lt;P&gt;Hi Steve,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that probably works in Python because python will do some implicit type conversions (automatically convert the Element into a MEPCurve if possible).&amp;nbsp; I’m not a huge fan of that because I like to know what I am working with, it makes the code easier to read.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/jeremytammik/RevitLookup" target="_blank" rel="noopener"&gt;https://github.com/jeremytammik/RevitLookup&lt;/A&gt; and &lt;A href="https://www.revitapidocs.com/" target="_blank" rel="noopener"&gt;https://www.revitapidocs.com/&lt;/A&gt; will be helpful as you convert from python to C#.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you look at &lt;A href="https://www.revitapidocs.com/" target="_blank" rel="noopener"&gt;https://www.revitapidocs.com/&lt;/A&gt; and type MEPSystem into the search bar you’ll see that only the MEPCurve and Connector class have the property MEPsystem.&amp;nbsp;&amp;nbsp; That means you need to convert your element into a MEPCurve or get its Connector before calling the MEPSystem property.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So your solution will work as long as the selection can be converted to an MEPCurve, if the user selects a fitting, or a piece of mechanical equipment, your plugin will throw an error. &amp;nbsp;&amp;nbsp;So first you can check if it’s an MEPcurve and if it is, convert it and get the system.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you us RevitLookup to “Snoop” a fitting or a piece of mechanical equipment, you can see that fittings are FamilyInstances and if you click around the properties of a fitting you’ll be able to find how to get to the connectors through the MEPModel and ConnectionManager.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a lot of code to put inline with your main method, so you’ll probably want to make a second method that returns an MEPSystem given an Element. &amp;nbsp;This way you get the MEPSystem from and element anywhere in your code you need to without having to copy and paste.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even after all that work, the user might make a selection that doesn’t have an MEPSystem, so you’ll still want to check for null.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;public static void MainFunction(Document doc, ElementId selection1)
{
    Element element1 = doc.GetElement(selection1);
    MEPSystem mepSystem = FindFirstMEPSystem(element1);
    if(mepSystem == null)
    {
        MessageBox.Show("Select a pipe!", "Warning");
        return;
    }
    else
    {
        //complete your transaction. 
    }
}

public static MEPSystem FindFirstMEPSystem(Element element)
{
    MEPSystem mepSystem = null; //initialize an MEPSystem variable to return.
    if(element is MEPCurve) //check if the element is an MEPCurve
    {
        MEPCurve mepCurve = element as MEPCurve; //convert element to MEP curve
        mepSystem = mepCurve.MEPSystem; //get MEPSystem
        return mepSystem; //return MEPSystem. 
    }
    else if(element is FamilyInstance) //else check if the element is a family instance
    {
        FamilyInstance familyInstance = element as FamilyInstance;
        //a family instance may not have and MEPModel or a connector manager, so check those
        if(familyInstance.MEPModel != null &amp;amp;&amp;amp; familyInstance.MEPModel.ConnectorManager!= null)
        {
            ConnectorSet connectorSet = familyInstance.MEPModel.ConnectorManager.Connectors;
            //cycle through each connector and return the first MEPSystem
            foreach (Connector connector in connectorSet)
            {
                mepSystem = connector.MEPSystem;
                return mepSystem;
                //if you're looking for a specific type of connector, you can check here.
                //for example if you want the first connector that is DomesticColdWater do:
                //if(connector.PipeSystemType == Autodesk.Revit.DB.Plumbing.PipeSystemType.DomesticColdWater)
                //{
                //    mepSystem = connector.MEPSystem;
                //    return mepSystem;
                //}
            }
        }
    }
    //If the element wasn't an MEPCurve or a FamilyInstance, or the family Instance didn't have connectors
    //we will return mepSystem which is still null. 
    return mepSystem;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully this helps makes up for the fact that you can’t debug your software while our software is installed &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt; We’re still working on that by the way!&lt;/P&gt;&lt;DIV class="UserSignature lia-message-signature"&gt;&lt;BR /&gt;&lt;STRONG&gt;Faster Heating and Cooling Load Calculations with Fewer Errors:&lt;/STRONG&gt;&lt;BR /&gt;&lt;A href="https://RippleEngineeringSoftware.com" target="_blank" rel="noopener"&gt;https://RippleEngineeringSoftware.com&lt;/A&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 20 Mar 2021 18:20:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10172786#M27403</guid>
      <dc:creator>Kevin.Lawson.PE</dc:creator>
      <dc:date>2021-03-20T18:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10614563#M27404</link>
      <description>amazing thanks!</description>
      <pubDate>Fri, 10 Sep 2021 20:25:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/10614563#M27404</guid>
      <dc:creator>Lee_Hammarback_x3001</dc:creator>
      <dc:date>2021-09-10T20:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: MEPSystem Id From Element C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/11528633#M27405</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5263562"&gt;@Kevin.Lawson.PE&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Can we somehow access the first element of connector set?&lt;BR /&gt;Similiar to python by adding [0] at the end?&amp;nbsp;&lt;BR /&gt;the only way i found was to rewrite this to List&amp;lt;connector&amp;gt; and than access but maybe there is a simpler way ?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 16:05:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/mepsystem-id-from-element-c/m-p/11528633#M27405</guid>
      <dc:creator>lukasz_koc1</dc:creator>
      <dc:date>2022-11-03T16:05:49Z</dc:date>
    </item>
  </channel>
</rss>

