Get Electrical Connector 'ElectricalSystemType' using Python

Get Electrical Connector 'ElectricalSystemType' using Python

dburwellVA
Participant Participant
4,026 Views
9 Replies
Message 1 of 10

Get Electrical Connector 'ElectricalSystemType' using Python

dburwellVA
Participant
Participant

Yes, I've searched the forums!

Yes, I've searched Jeremy's amazing resources!

Yes, I use Revit Lookup!

Yes, to probably everything else, so I admit I need help after a three day frustration journey!

 

Okay, being funny, but I wanted that out of the way.

 

I have a Python Script that gets All families in a model, adds the Family Name, Type Name, and a Shared Parameter value to separate lists.

 

The ONE thing that I cannot seem to resolve is how to get this information from Revit Lookup (when I look at a typical family in the model):

 

Current Selection/MEPmodel/ConnectorManager/Connectors/ElectricalSystemType

 

In my code I tried bringing over some C# that I gathered from Jeremy and another post.  So I left it in there even though it doesn't work and maybe someone can point me in the right direction.

 

Here's my entire Python Code:

 

 

 

import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import *
from  Autodesk.Revit.UI import *

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
app = doc.Application

import sys
import clr
clr.AddReference("RevitAPIUI")
import time
    
if doc.IsFamilyDocument == True:
    __window__.Close()
    TaskDialog.Show("Wrong Document Type", "This script is for getting information on Load Classifications in a project, it cannot be run in a family.")
    sys.exit()

start = time.time()

allFamilyTypes = []
containsLoadClass = []
LoadClassList = []

def runit():
    Elements = FilteredElementCollector(doc).OfClass(Family)
    for m in Elements:
        famCnt = 1
        try:
            symbols = list(m.GetFamilySymbolIds())
            allFamilyTypes.append("\n" + m.Name)
            for i in symbols:
                #Get symbols from family
                famsymbol = doc.GetElement(i)
                symbolName = famsymbol.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString()
                allFamilyTypes.append("    Type = " + symbolName)
                try:
                    #Retrieve Shared Parameter Load Classification if it exists
                    loadclass = famsymbol.LookupParameter("Load Classification")
                    loadClassStr = loadclass.AsValueString()
                    if famCnt == 1:
                        containsLoadClass.append("\n" + m.Name)
                        famCnt += 1
                    containsLoadClass.append("    Type = " + symbolName)
                    containsLoadClass.append("        Load Classification = " + loadClassStr)
                    LoadClassList.append(loadClassStr)
                except:
                    pass
                try:  
                    #Here I want to get ElectricalSystemType from a connector if it exists
                    #Revit Lookup - Current Selection/MEPModel/ConnectorManager/Connectors/ElectricalSystemType
                    it = famsymbol.MEPModel.ConnectorManager.Connectors.GetEnumerator()
                    it.MoveNext()
                    conn = it.Current
                    famConnInfo = conn.GetMEPConnectorInfo()
                    param = famConnInfo.GetConnectorParameterValue(ElectricalSystemType)
                    print param
                except:
                    pass
        except:
            pass

print "Gathering family information."
runit()

print "\nEnter 1 for ALL Families and Types"
print "Enter 2 for Families and Types containing Load Classification"
print "Enter 3 to list Load Classifications used in Project"
userinput = raw_input("\nEnter Number: ")

if int(userinput) == 1:
    #List all Families and Types
    for m in allFamilyTypes:
        print m
elif int(userinput) == 2:
    #List all Families and Types with Load Classification
    print "\nList of Families and Types with Load Classification defined:\n"
    for m in containsLoadClass:
        print m
elif int(userinput) == 3:
    #List ONLY the Load Classifications being used in a Project
    LoadClassNames = list(dict.fromkeys(LoadClassList))
    totalLC = 0
    totalLC = len(LoadClassNames)
    print "\nThere are " + str(totalLC) + " Load Classifications in Project:\n"
    cnt = 1
    for m in LoadClassNames:
        print "    " + str(cnt) +") " + m
        cnt += 1
else:
    print "That was not a choice!"

end = time.time()

hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
TotalTime = ("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))

print "\nScript run in: " + TotalTime

 

0 Likes
Accepted solutions (1)
4,027 Views
9 Replies
Replies (9)
Message 2 of 10

Kevin.Lawson.PE
Advocate
Advocate

Isn't the ElectricalSystemType a property of the connector?  Can you just call conn.ElectricalSystemType?  That's how it works in C#.

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
0 Likes
Message 3 of 10

dburwellVA
Participant
Participant

Thanks for the response.  I believe you're correct - but I can't seem to figure out how to get the connector info itself from within the family symbol.  This is my first go at trying to get connector info for one of our engineers, and I must not be fully understanding the syntax to get it!  I'll admit, I'm lost on this one...

0 Likes
Message 4 of 10

Kevin.Lawson.PE
Advocate
Advocate

It looks like you are trying to get the MEPModel from the FamilySymbol, the MEPModel is a property of the FamilyInstance: https://www.revitapidocs.com/2020/34173003-db39-bfa9-fa59-f7b5ac8da794.htm

 

So you need to get an instance of the FamilySymbol.  After that your code should work.  

 

                    it = FAMILYINSTANCE.MEPModel.ConnectorManager.Connectors.GetEnumerator()
                    it.MoveNext()
                    conn = it.Current
                    famConnInfo = conn.GetMEPConnectorInfo()
                    param = famConnInfo.GetConnectorParameterValue(ElectricalSystemType)

 

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
0 Likes
Message 5 of 10

dburwellVA
Participant
Participant

Klawson, thanks for pointing me towards an answer.  I'll dig into what you sent and let you know as soon as I put out a fire.  I greatly appreciate it!

0 Likes
Message 6 of 10

dburwellVA
Participant
Participant

Klawson, I'm trying to simplify to the lowest common denominator to figure this out.  I made a small python script that allows you to select a family with a connector in it (there are no checks right now, so I need to select exactly what I want).  Here's the code:

 

import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import *
from  Autodesk.Revit.UI import *

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
app = doc.Application

import sys
import clr
clr.AddReference("RevitAPIUI")

sel = uidoc.Selection.PickObject(Selection.ObjectType.Element, 'Choose element')
el = doc.GetElement(sel)

it = el.MEPModel.ConnectorManager.Connectors.GetEnumerator()
print it
it.MoveNext()
conn = it.Current
print conn
famConnInfo = conn.GetMEPConnectorInfo()
print famConnInfo
param = famConnInfo.GetConnectorParameterValue(ElectricalSystemType)
print param

 

As you can see I added "print" lines so I could see if I'm getting the correct info.  It looks like everything works fine but the last "param" value.

 

Here's what prints out in the Output window:

 

<Autodesk.Revit.DB.ConnectorSetIteratorForward object at 0x00000000000000A2 [Autodesk.Revit.DB.ConnectorSetIteratorForward]>
<Autodesk.Revit.DB.Connector object at 0x00000000000000A3 [Autodesk.Revit.DB.Connector]>
<Autodesk.Revit.DB.MEPFamilyConnectorInfo object at 0x00000000000000A4 [Autodesk.Revit.DB.MEPFamilyConnectorInfo]>

 

At the "param" value I get this error:

 

IronPython.Runtime.UnboundNameException: name 'ElectricalSystemType' is not defined
at IronPython.Runtime.Operations.PythonOps.GetVariable(CodeContext context, String name, Boolean isGlobal, Boolean lightThrow)
at IronPython.Compiler.LookupGlobalInstruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at RevitPythonShell.RpsRuntime.ScriptExecutor.ExecuteScript(String source, String sourcePath)

 

Still trying to wrap my head around this, but what am I missing (other than a brain)?

 

Thanks again!

 

0 Likes
Message 7 of 10

Kevin.Lawson.PE
Advocate
Advocate
Accepted solution

Is the electrical system type a parameter?  It looks like it's a property of the connector.  try just calling conn.ElectricalSystemType

 

it = el.MEPModel.ConnectorManager.Connectors.GetEnumerator()
print it
it.MoveNext()
conn = it.Current
---------------------
elecSys = conn.ElectricalSystemType

 

 

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
Message 8 of 10

dburwellVA
Participant
Participant

Well then, Bring the Funky Back and Amen!!!  You are a genius...

 

Here's my updated code:

import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import *
from  Autodesk.Revit.UI import *

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
app = doc.Application

import sys
import clr
clr.AddReference("RevitAPIUI")

sel = uidoc.Selection.PickObject(Selection.ObjectType.Element, 'Choose element')
el = doc.GetElement(sel)

it = el.MEPModel.ConnectorManager.Connectors.GetEnumerator()
it.MoveNext()
conn = it.Current
print conn.ElectricalSystemType

 

I've never used the MoveNext Method.  According to API Docs it moves the iterator forward one item.  What if it gets the wrong item (since I'm still trying to understand)?  Also, the docs say "Resource only available for 2015".  Has it been deprecated and if so, is there another method I should be using?

 

Last question - if you know, where can I get the Load Classification for the connector?  I've been looking through the family with Revit Lookup and can't seem to find it.  If I edit the family, it's defined in there.

 

Anyway, thanks again for all your help!  I can now move on...

 

0 Likes
Message 9 of 10

Kevin.Lawson.PE
Advocate
Advocate

I typically just use a for loop instead of the GetEnumerator(). The connector set will not be ordered, so you will probably get the wrong one, you need to filter out what you're looking for somehow.  Something like only get the info if the Domain == DomainElectrical (see below).  But this will give you the first electrical connector, so you may need to add more if statements. 

 

I'm not sure what you mean about "Resource only available for 2015".  In RevitAPIDocs.com?

 

As far as the load classification, I'm a mechanical engineer, so I'm not 100% sure how that's used, but it might be something like below. 

 

BuiltInParameter = DB.ElementId(DB.BuiltInParameter.RBS_ELEC_LOAD_CLASSIFICATION)

for ii, ElectricalElement in enumerate(ElectricalElementInputList):
        ConnectorsList = ElectricalElement.MEPModel.ConnectorManager.Connectors
        for jj, Connector in enumerate(ConnectorsList):
            if Connector.Domain == DB.Domain.DomainElectrical:
                ConnectorInfo = Connector.GetMEPConnectorInfo()
                ElecSys = Connector.ElectricalSystemType
                LoadClass = ConnectorInfo.GetConnectorParameterValue(BuiltInParameter).Value

 

 

-Kevin Lawson, PE
www.rippleengineeringsoftware.com
Revit heating and cooling load calculations in one click!
0 Likes
Message 10 of 10

dburwellVA
Participant
Participant

klawson - thanks one last time!  You've given me more than enough to keep me moving forward.  Greatly appreciated...

0 Likes