Hello Dynamo Friends
Going the first steps at creating a collection of python scripts for Dynamo and pyRevit, I´d like to know what are your needs when you are looking for code, classes, methods.
It will for sure get search and filter options, but for now it´s more of creating a good naming convention and finding out what code is really needed. In the following what I have come up with so far and I´m looking forward to your thoughts and hopefully contributions! The code is on Github.
Some rules for the code:
- Every script is a full working code including imports.
- Necessary imports only.
- No inputs from dynamo needed, necessary elements are created in the code.
- Dynamo code: tabs as indent only.
- pyRevit code: spaces as indent only.
- No comments, just clean code.
- snake_case only, for variables, functions, everything.
- No “for i in a:”, proper names for everything (except list comprehension).
Code is available in different versions, here are examples to explain the naming convention (which is far from perfect right now!):
Basic:
The most simple representation of a task/method:
import clr
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import Material
doc = DocumentManager.Instance.CurrentDBDocument
material_name = 'material_01'
TransactionManager.Instance.EnsureInTransaction(doc)
material_id = Material.Create(doc, material_name)
material = doc.GetElement(material_id)
TransactionManager.Instance.TransactionTaskDone()
OUT = material
Advanced:
Includes error handling.
import clr
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import Material
doc = DocumentManager.Instance.CurrentDBDocument
material_name = 'material_01'
try:
TransactionManager.Instance.EnsureInTransaction(doc)
material_id = Material.Create(doc, material_name)
material = doc.GetElement(material_id)
TransactionManager.Instance.TransactionTaskDone()
OUT = material
except Exception as e:
TransactionManager.Instance.ForceCloseTransaction()
OUT = str(e)
Example:
A usecase with a corresponding methods and all available API properties.
import clr
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import Material
doc = DocumentManager.Instance.CurrentDBDocument
def get_fill_pattern(doc, pattern_name):
fill_patterns = FilteredElementCollector(doc).OfClass(FillPatternElement)
fill_pattern = next((fp for fp in fill_patterns if fp.Name == pattern_name), None)
return fill_pattern
def get_appearance_asset(doc, asset_name):
collector = FilteredElementCollector(doc).OfClass(AppearanceAssetElement)
for asset in collector:
if asset.Name == asset_name:
return asset
return None
def set_properties(material):
material.Color = color
material.Shininess = 0.5
material.Smoothness = 0.5
material.Transparency = 10
material.MaterialClass = 'Concrete'
material.MaterialCategory = 'Structural'
material.UseRenderAppearanceForShading = True
material.AppearanceAssetId = asset.Id
material.SurfaceForegroundPatternId = fill_pattern.Id
material.SurfaceForegroundPatternColor = color
material.SurfaceBackgroundPatternId = fill_pattern.Id
material.SurfaceBackgroundPatternColor = color
material.CutForegroundPatternId = fill_pattern.Id
material.CutForegroundPatternColor = color
material.CutBackgroundPatternId = fill_pattern.Id
material.CutBackgroundPatternColor = color
material_name = 'material_01'
pattern_name = '<Solid fill>'
asset_name = 'Cyan'
color = Color(128, 128, 128)
try:
TransactionManager.Instance.EnsureInTransaction(doc)
fill_pattern = get_fill_pattern(doc, pattern_name)
if fill_pattern is None:
raise Exception('Solid fill pattern not found.')
asset = get_appearance_asset(doc, asset_name)
if asset is None:
raise Exception('Appearance asset not found.')
material_id = Material.Create(doc, material_name)
material = doc.GetElement(material_id)
set_properties(material)
TransactionManager.Instance.TransactionTaskDone()
result = material
except Exception as e:
TransactionManager.Instance.ForceCloseTransaction()
result = str(e)
OUT = result
Function:
The desired task/method as a function, for me the most important code for copy paste purposes.
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import FilteredElementCollector, View, ViewType
import Autodesk
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
def get_view_templates():
views = FilteredElementCollector(doc).OfClass(View)
templates = []
for view in views:
if view.ViewType != ViewType.ThreeD and view.ViewType != ViewType.Schedule and view.ViewType != ViewType.Section and view.IsTemplate == True:
templates.append(view)
return templates
templates = get_view_templates()
OUT = templates
designscript:
For dynamo geometry objects:
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import Solid, Cuboid
def dynamo_solid():
width = 10.0
length = 20.0
height = 15.0
solid = Cuboid.ByLengths(width, length, height)
return solid
def faces_from_solid(solid):
if isinstance(solid, Solid):
faces = solid.Faces
return faces
else:
return 'Input is not a Dynamo Solid.'
solid_dynamo = dynamo_solid()
faces = faces_from_solid(solid_dynamo)
OUT = faces
Revit:
For Revit geometry objects:
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import XYZ, Line, CurveLoop, Solid, GeometryCreationUtilities
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
def revit_solid():
width = 10.0
length = 20.0
p0 = XYZ(0, 0, 0)
p1 = XYZ(width, 0, 0)
p2 = XYZ(width, length, 0)
p3 = XYZ(0, length, 0)
line1 = Line.CreateBound(p0, p1)
line2 = Line.CreateBound(p1, p2)
line3 = Line.CreateBound(p2, p3)
line4 = Line.CreateBound(p3, p0)
curveLoop = CurveLoop()
curveLoop.Append(line1)
curveLoop.Append(line2)
curveLoop.Append(line3)
curveLoop.Append(line4)
height = 15.0
solid = GeometryCreationUtilities.CreateExtrusionGeometry([curveLoop], XYZ.BasisZ, height)
return solid
def faces_from_solid(solid):
if isinstance(solid, Solid):
faces = solid.Faces
return faces
else:
return 'Input is not a Revit Solid.'
solid_revit = revit_solid()
faces = faces_from_solid(solid_revit)
OUT = faces
So far, this are my ideas, how about yours?