Tag Archives: python

[Revit] Add sizes to a pipe segment using python + Dynamo + Excel

I didn’t want to use Dynamo as I really prefer line code programming to visual programming. But in fact Dynamo :

  • has a very active community
  • is open source too
  • use python as main scripting language
  • is great for debugging python script as you don’t need to include every single line in a try/except block
  • is much user friendly than as it is now included with Revit 2017.1 and by the way this new Dynamo Player makes it usable by anyone
  • scare many people less than line code so they can easily modify to adapt it to their objectives

Here is the Dynamo image. It uses Clockwork package :

ajoutertaillessegmentstuyaux

File is available here

Here is the python code block :

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

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
dataEnteringNode = IN

elem = doc.GetElement(IN[0])

TransactionManager.Instance.EnsureInTransaction(doc)

for dn, di, de in zip(IN[1], IN[2], IN[3]):
	DN = UnitUtils.ConvertToInternalUnits(dn , DisplayUnitType.DUT_MILLIMETERS)
	Di = UnitUtils.ConvertToInternalUnits(di , DisplayUnitType.DUT_MILLIMETERS)
	De = UnitUtils.ConvertToInternalUnits(de , DisplayUnitType.DUT_MILLIMETERS)
	try:
		elem.AddSize(MEPSize(DN,Di,De,True,True))
	except:
		elem.RemoveSize(DN)
		elem.AddSize(MEPSize(DN,Di,De,True,True))
		
#Affectez la sortie à la variable OUT.
OUT = IN

Enjoy

[Revit] Delete parameters including hidden ones

In some case you need add or delete many parameters. Spiderinnet did many great articles about parameters, here are 2 of them :

Create shared parameter

Create project parameter

Some Revit addins and extensions are adding many parameters to you project. Some are not even visible to the user. It means that you have to use API to remove it.  In most common case you’ll never see it unless you use Revit Lookup. But when you export your model or do a duct pressure loss report it appears on it. So I made a script to get quickly rid of unwanted parameters (hidden or not).

from Autodesk.Revit.DB import *
from System import Guid

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

#Retrieve all parameters in the document
params = FilteredElementCollector(doc).OfClass(ParameterElement)
filteredparams = []

#Store parameters which has a name starting with "magi" or "MC"
for param in params:
    if param.Name.startswith(("magi", "MC")): #startswith method accept tuple
        filteredparams.append(param)
        print param.Name #To check if a parameter in the list is not supposed to be deleted

#Delete all parameters in the list
t = Transaction(doc, "Delete parameters")
t.Start()
for param in filteredparams:
    doc.Delete(param.Id)
t.Commit()

Cheers !

Working with fluid properties 1/2 : Using CoolProp in a standard python environment

When you work in HVAC, you always need fluid properties in your calculations. The most common property you need for pipe work is heat capacity Cp,  mass density ρ and dynamic viscosity µ. We will use CoolProp which is an awesome open source cross-platform library developed in C++ with wrappers available for most common languages and environment (including python, Libre Office, Excel, shared library dll). You can thank developers and their supporters for this great library.

You may install CoolProp for python by typing “pip install CoolProp” in your command line (cmd.exe on windows) or with Anaconda.

You can then use it in a python script. Here is an example for Pure Fluid :

#Add CoolProp to your pythonpath
import sys
sys.path.append(r"PathToTheCoolPropFolder")
#Example, for me it is : sys.path.append(r"C:\Anaconda\Lib\site-packages\CoolProp-5.1.1-py2.7-win-amd64.egg")

#Import PropsSI function which will be used from CoolProp
from CoolProp.CoolProp import PropsSI

#Ask CoolProp water's heat capacity at 275.15 K (2°C) and common Earth pressure 101325 Pa
PropsSI('C','T',275.15,'P',101325,'INCOMP::Water') #return 4182.587592215201

#Ask CoolProp water's mass density and viscosity with same conditions
PropsSI('D','T',275.15,'P',101325,'INCOMP::Water') #return 1003.076063639064
PropsSI('C','T',275.15,'P',101325,'INCOMP::Water') #return 0.0016507819947969692

INCOMP stands for incompressible fluids, you can also work with Humid Air Properties for example with another function from CoolProp. Click to see list of available fluids.

We often need mixture properties (ethylen or propylen glycol, ethanol etc…). Here is an example :

#Ask CoolProp the heat capacity of mixture Water/ASHRAE, Ethylene Glycol 25% (volume based)  at 275.15 K (2°C) and common Earth pressure 101325 Pa
PropsSI('C','T',275.15,'P',101325,'INCOMP::AEG[0.25]') #return 3684.7400408010444
#Ask CoolProp ASHRAE, Ethylene Glycol's mass density, heat capacity and freezing temperature with same conditions
PropsSI('D','T',275.15,'P',101325,'INCOMP::AEG[0.25]') #return 1043.3307802810543
PropsSI('V','T',275.15,'P',101325,'INCOMP::AEG[0.25]') #return 0.0032736727033664615
PropsSI('T_FREEZE','T',275.15,'P',101325,'INCOMP::AEG[0.25]') #return 261.0307763027968 (~ -12°C)

Click here to see inputs available for PropsSI function and units used (SI as said in the function name).

Enjoy !