Issues Defining Electrical Parameters When Inserting Outlets in Revit via API

Issues Defining Electrical Parameters When Inserting Outlets in Revit via API

PEDROVITOR.PAGLIARIN
Explorer Explorer
373 Views
3 Replies
Message 1 of 4

Issues Defining Electrical Parameters When Inserting Outlets in Revit via API

PEDROVITOR.PAGLIARIN
Explorer
Explorer

 

Hello everyone,

I am developing a Python script using the Revit API to insert multiple electrical outlets into selected walls and automatically assign them to electrical circuits. However, I am encountering a problem when defining the electrical parameters of the inserted outlets. Specifically, the voltage and number of phases parameters are being set to 0 after creating the circuit, indicating that the electrical data is not being assigned correctly.


Project Characteristics:

  • Outlet Height: 1.10 meters
  • Number of Outlets: Defined by the user (e.g., 1)
  • Interval Between Outlets: Defined by the user or uses the total length of the wall
  • Wall Face: Front or Rear
  • Electrical Parameters:
    • Apparent Power (VA): 1000 VA (example)
    • Power Factor (cos φ): 0.8 (example)
    • Voltage (V): 220 V (example)
    • Number of Phases: 1 (example)

Detailed Description of the Problem:

When executing the script, the outlets are correctly inserted at the desired positions on the selected wall. However, when attempting to set the electrical parameters (voltage and number of phases) on the family instance and the electrical circuit, these values are not being assigned correctly and appear as 0 in Revit. I believe the issue is related to how the string parameters are being defined in the code.


Error Message Received:

Erro ao inserir tomada: global name 'string' is not defined

Relevant Code Snippets:

Below are the sections of my script related to defining the electrical parameters and creating the electrical circuit. I believe the problem lies in these parts of the code.

# Defining the electrical parameters on the family instance
# Apparent Power (VA)
parametro_S = tomada_instancia.LookupParameter('Potência Aparente (VA)')
if parametro_S and parametro_S.StorageType == StorageType.Double:
    parametro_S.Set(potencia_aparente)

# Power Factor
parametro_cos_phi = tomada_instancia.LookupParameter('Fator de Potência')
if parametro_cos_phi and parametro_cos_phi.StorageType == StorageType.Double:
    parametro_cos_phi.Set(fator_potencia)

# Voltage (V)
parametro_tensao = tomada_instancia.LookupParameter('Tensão (V)')
if parametro_tensao and parametro_tensao.StorageType == StorageType.Double:
    parametro_tensao.Set(tensao)

# Number of Phases
parametro_fases = tomada_instancia.LookupParameter('N° de Fases')
if parametro_fases and parametro_fases.StorageType == StorageType.Integer:
    parametro_fases.Set(numero_fases)

# Correction Added: Correctly setting string parameters
# For example, setting the instance comment
parametro_comentario = tomada_instancia.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
if parametro_comentario and parametro_comentario.StorageType == StorageType.String:
    parametro_comentario.Set("")  # Uses an empty string instead of string.Empty
def criar_circuito_eletrico(tomadas_inseridas, tensao, numero_fases, parametros_elet):
    """Creates an electrical circuit with the inserted outlets and adjusts the parameters."""
    potencia_aparente, fator_potencia, _, _ = parametros_elet

    try:
        with revit.Transaction("Criar Circuito Elétrico"):
            # Get the ElementIds of the outlets
            tomadas_ids = [t.Id for t in tomadas_inseridas]
            # Create a list of ElementIds
            elementos_ids = List[ElementId](tomadas_ids)

            # Define the electrical system type (PowerCircuit)
            sistema_tipo = ElectricalSystemType.PowerCircuit
            # Create the electrical circuit
            circuito = ElectricalSystem.Create(doc, elementos_ids, sistema_tipo)

            if circuito:
                forms.alert("Circuito elétrico criado com sucesso.", exitscript=False)

            # Correction Added: Correctly setting string parameters on the circuit
            # For example, setting the circuit comment
            parametro_comentario = circuito.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
            if parametro_comentario and parametro_comentario.StorageType == StorageType.String:
                parametro_comentario.Set("")  # Uses an empty string instead of string.Empty

            # Set the voltage and number of phases of the circuit based on the provided values
            voltage_param = circuito.get_Parameter(BuiltInParameter.RBS_ELEC_VOLTAGE)
            if voltage_param and voltage_param.StorageType == StorageType.Double:
                voltage_param.Set(tensao)

            number_of_poles_param = circuito.get_Parameter(BuiltInParameter.RBS_ELEC_NUMBER_OF_POLES)
            if number_of_poles_param and number_of_poles_param.StorageType == StorageType.Integer:
                number_of_poles_param.Set(numero_fases)

            # Set the Apparent Power and Power Factor on the circuit
            apparent_load_param = circuito.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD)
            if apparent_load_param and apparent_load_param.StorageType == StorageType.Double:
                apparent_load_param.Set(potencia_aparente)

            power_factor_param = circuito.get_Parameter(BuiltInParameter.RBS_ELEC_POWER_FACTOR)
            if power_factor_param and power_factor_param.StorageType == StorageType.Double:
                power_factor_param.Set(fator_potencia)

            # Regenerate the document to update calculated parameters
            doc.Regenerate()

    except Exception as e:
        tb = traceback.format_exc()
        forms.alert("Erro ao criar circuito elétrico:\n{0}".format(tb))

Attempts to Solve:

  1. Replacing string.Empty with "": Initially, I tried using string.Empty to set string parameters but received the error global name 'string' is not defined. Subsequently, I replaced it with "" (empty string) as suggested by ChatGPT, but the parameters still return 0.
  2. Checking Storage Type: I ensured that the parameters I am trying to set correspond to the expected StorageType (Double for voltage and power, Integer for the number of phases).

Questions:

  1. Why are the electrical parameters for voltage and number of phases being set to 0 in Revit after creating the circuit?
  2. I am using empty strings ("") to set string parameters. Is this correct, or is there another recommended way to avoid null values?
  3. Are there any specific details in the Revit API that I should consider when setting electrical parameters for families and circuits?
0 Likes
Accepted solutions (1)
374 Views
3 Replies
Replies (3)
Message 2 of 4

sragan
Collaborator
Collaborator

The first thing you need to do is use the family editor and open your family.   Select the connector, and make sure it is using the parameters you are editing.   

 

Second, I don't see where you actually specify the voltage or number of phases.  Those obviously have to be variables of the correct type.  Voltage is usually a "double".  A "string" can't be stored in a parameter that holds a double or integer value.

 

Third, revit uses its own internal system of units.   That means a voltage of 220 volts is actually stored as 2368.  To convert units from what the revit stores to what the user would see, use something like this:

 

 

string sAppLoad = "";
p = elemType.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD);
sAppLoad = UnitFormatUtils.Format(doc.GetUnits(), SpecTypeId.ApparentPower, p.AsDouble(), false);

 

 

 

Here is an example of storing a VA value.  (Here, TextBoxWatts.Text is the string value you want to store as a Wattage or VA):

 

 

//elemType.get_Parameter (new Guid ("b4c5299d-e6a7-4c3c-8c45-29807e7bc670")).Set(TextBoxWatts.Text);  this does not work with a parameter that holds a VA value
                        	        double dWatts= 0;
                        	        bool parsed = UnitFormatUtils.TryParse(doc.GetUnits(), SpecTypeId.ApparentPower, TextBoxWatts.Text, out dWatts);
                        	        if (parsed == true)
                        	        {
                        	        	elemType.get_Parameter (new Guid ("b4c5299d-e6a7-4c3c-8c45-29807e7bc670")).Set(dWatts);
                        	        	
                        					
                        	        		
                        	        }
                        	        
                        	        if (parsed == false)
                        	        {
                        	        	TaskDialog.Show("Error ", "Can't set Watts to that value.");
                        	        }

 

 

Those are just a couple of examples.  You will have to research a little and edit the code to work for what you want.

 

Message 3 of 4

ricaun
Advisor
Advisor
Accepted solution

What Revit version are you using? By the image looks like is version 2023 or less.

 

The zero voltage problem is a bug in the ElectricalSystem.Create method in the Revit API. Was fixed in Revit 2025, and Revit 2024.2, in older version if you are using a instance parameter in the voltage in the family the voltage goes to zero when using the ElectricalSystem.Create.

 

Here some references for the issue.

If you try in Revit 2025 should work fine.

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 4 of 4

PEDROVITOR.PAGLIARIN
Explorer
Explorer
As soon as possible, I going to try on Revit 2024.2, thanks for the response.
If i be sucessful, i'll acept this solution
0 Likes