Python; Create new circuit in panel and assign electrical elements to circuit

So far I am able to reassign electrical elements to an existing circuit.

I want to be able to create a new Electrical System (Circuit) connected to a provided Panel element.

Any clues how to do that?

Here is what I have so far:

Thanks to @Mostafa_El_Ayoubi for helping me figure out how to reassign elements to existing circuits.

1 Like

@andre.abotnes please copy pase your code.

Basically you need to create the ElectricalSystem defining all the arguments of the Constructor (you’re missing the document) :
image

Then add your ElementSet to this electrical system .

1 Like

Aha, I was working with the Create method for an unused connector. That makes sense.
Alot of my imports are probably not needed. I am just starting to learn python in Dynamo.

I still cannot make it though. I do not get what to input for “ElectricalSystemType” :blush:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('DSCoreNodes')
import DSCore
from DSCore.List import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

#The inputs to this node will be stored as a list in the IN variables.
input = UnwrapElement(IN[0])
addto = UnwrapElement(IN[1])

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
OUT = []

elemSet = ElementSet()

for _ in input:
	elemSet.Insert(_)

OUT = input, addto

TransactionManager.Instance.EnsureInTransaction(doc)

Autodesk.Revit.DB.Electrical.ElectricalSystem.Create(doc, elemSet)
	
TransactionManager.Instance.TransactionTaskDone()

Hei André! Just write ElectricalSystemType.x and replace the x with one of the following types.

image

They are called Enumerations and are constant variables (immutable/not changeable), if you want to use a string input to choose from you can use the Enum.Parse() method. See an example here.

3 Likes

Thank you so much for your help!

I am currently getting this error message:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Electrical import *
from Autodesk.Revit.DB import MEPSystem

clr.AddReference('DSCoreNodes')
import DSCore
from DSCore.List import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

# Import List ( ICollection(ElementId) = List[ElementId]() )
clr.AddReference("System")
from System.Collections.Generic import List 

#The inputs to this node will be stored as a list in the IN variables.
input = UnwrapElement(IN[0])

electComponents = List[ElementId]()


for i in input:
	electComponents.Add(i.Id)


doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

TransactionManager.Instance.EnsureInTransaction(doc)

ElectricalSystem.Create(doc, electComponents, ElectricalSystemType.PowerUnBalanced)

TransactionManager.Instance.TransactionTaskDone()

OUT = []

Does anyone know what I need to input that can create the specific circuit “ElectricalSystemType.PowerUnBalanced” ?

From what I can guess from the error message: the input elements don’t have a PowerUnbalanced connector assigned in their family. You could check this by editing the family.

1 Like

Thanks. I checked the family, and the electrical connector is of type Power Balanced, so I changed the code to “ElectricalSystemType.PowerBalanced”, but still got the same error message.

I am able to manually connect the lighting fixture to power.

Try setting it to PowerCircuit, that worked for me.

1 Like

Damn I still get the errormessage.

Can you send me the graph and the family you connect, which works for you?

Turns out that one of the elements I input was already connected to a circuit, which caused the error.
Setting the ElectricalSystemType to PowerCircuit did the trick.

Thanks to @Einar_Raknes, @T_Pover & @Mostafa_El_Ayoubi

circuitstest.dyn (22.8 KB)

7 Likes

HI,
I was trying to do the same, and I found your code but I get the same error.
The “.create” seems to be unsupported in the electrical system class. Any help on how to solve this will be much appreciated

This method was introduced in 2018, it will not work in Revit 2017

1 Like

For older version you can use this method: doc.Create.NewElectricalSystem() with all the same arguments as the newer version.

1 Like

Thanks alot
This solved the problem

1 Like

Great :slight_smile:
It also seems that the script does not run well if already powered elements are part of the selection.
I think that could be implemented in the script with a boolean input; Disconnect powered elements and connect the input elements all together, or to skip already powered elements and connect the remaining elements to a new circuit.

By the way I have published a few nodes in a package called “RIE”. Nodes for creating circuits, selecting panel, and drawing wires for circuit ++.
The code for the nodes is implemented for revit 2018, so some of them might not work on older versions.

Do you know of any good packages for electrical installations?
I have MEPover, which has been kind of useful, but I still haven’t found any great packages for creating stuff for electrical installations.

Thanks alot :smile:
MEP over is the best one.
But for electrical systems I use a combination of shared parameters and excel sheets to perform necessary calculations

2 Likes

Hey Editor_X

I’m new to Python and after several failed attempts; unable to adapt the code for 2017. Is there a chance you could provide your fix for the 2017 versions?

Thanks!!

Edit: Feel stupid, see below

change:

newcircuit = doc.Create.NewElectricalSystem.NewElectricalSystem(c,ElectricalSystemType.PowerCircuit)

to

newcircuit = ElectricalSystem.Create(doc, electComponents, ElectricalSystemType.PowerCircuit)
1 Like

Does anyone know if its possible to modify the above python code, so that it generates a circuit per fittings selected.

i’m looking into doing something with modular wiring. This could work well if it could create a new circuit per fitting and connect to a control module, That then connects to a DB.

@T_Pover @Joe.ashton @andre.abotnes

Sure, that’s possible, here is how:
image

import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

if isinstance(IN[0], list):
	connectors = IN[0]
else:
	connectors = [IN[0]]

listout = []

TransactionManager.Instance.EnsureInTransaction(doc)
for conn in connectors:
	ckt = conn.ElectricalSystemType
	
	#Revit 2017 and older:
	#newckt = doc.Create.NewElectricalSystem(conn,ckt)
	
	#Revit 2018 and newer:
	newckt = Electrical.ElectricalSystem.Create(conn,ckt)
	
	listout.append(newckt.ToDSType(False))
	
TransactionManager.Instance.TransactionTaskDone()


OUT = listout

Find the connectors on the elements that you want to use and use those as input for creating the circuits.

4 Likes