Using IronPython and Interop
import clr
import sys
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
import os
from System import Array
import time
from pyrevit import forms
# t = System.Type.GetTypeFromProgID("Excel.Application")
# excel = System.Activator.CreateInstance(t)
excelTypeLibGuid = System.Guid("00020813-0000-0000-C000-000000000046")
clr.AddReferenceToTypeLibrary(excelTypeLibGuid)
from Excel import Application
from Excel import *
excel = Application()
clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' )
from Microsoft.Office.Interop import Excel
from Microsoft.Office.Interop.Excel import ApplicationClass
from System.Runtime.InteropServices import Marshal
xlDirecDown = System.Enum.Parse(Excel.XlDirection, "xlDown")
xlDirecRight = System.Enum.Parse(Excel.XlDirection, "xlToRight")
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
uidoc = __revit__.ActiveUIDocument
title = doc.Title
#excel = ApplicationClass()
excel.DisplayAlerts = False
myexcelfile = os.path.expanduser('~\Desktop\\Revit Object Styles.xlsx')
fileexists = os.path.isfile(myexcelfile)
if fileexists:
workbook = excel.Workbooks.Open(myexcelfile)
worksheets = workbook.WorkSheets
exists = False
for s in worksheets:
if s.Name == title:
exists = True
break
if exists:
ws = worksheets(title)
ws.Activate
worksheets(title).Cells.Clear
else:
ws = worksheets.Add()
ws.Name = title
worksheets(title).Activate
else:
workbook = excel.Workbooks.Add()
ws = workbook.ActiveSheet
ws.Name = title
cell = ws.Range["A1"]
workbook.SaveAs(myexcelfile)
def WriteRange(sheet, data):
rows = len(data) - 1
ts = time.time()
for i in range(rows):
cellrow = i + 1
xlrange = sheet.Range[sheet.Cells(cellrow,1), sheet.Cells(cellrow,11)]
r = data[i]
myarray = Array[str](r)
xlrange.Value = myarray
te = time.time()
t = te - ts
def getLinePattern(graphicstyleid):
if graphicstyleid.ToString() == str(-3000010):
linepattern = "Solid"
else:
linepattern = doc.GetElement(graphicstyleid)
if linepattern:
linepattern = linepattern.Name
else: linepattern = None
return linepattern
def getCategoryDataTranspose(cat):
cattype = cat.CategoryType.ToString()
id = cat.Id
bic = cat.BuiltInCategory
if bic == BuiltInCategory.INVALID:
isbic = "False"
else:
isbic = "True"
name = cat.Name
lineprojection = None
linecut = None
projectionId = cat.GetLinePatternId(GraphicsStyleType.Projection)
lineprojection = getLinePattern(projectionId)
projectionpen = cat.GetLineWeight(GraphicsStyleType.Projection)
cutId = cat.GetLinePatternId(GraphicsStyleType.Cut)
linecut = getLinePattern(cutId)
cutpen= cat.GetLineWeight(GraphicsStyleType.Cut)
linecolor = cat.LineColor
r = linecolor.Red
g = linecolor.Green
b = linecolor.Blue
lineRGB = "(" + str(r) + "," + str(g) + "," +str(b) + ")"
material = cat.Material
if material:
materialname = material.Name
else:
materialname = "None"
if cat.IsCuttable == 0:
cuatable = "False"
else:
cutable = "True"
if cat.CanAddSubcategory == 0:
canaddsubcategory = "False"
else:
canaddsubcategory = "True"
parent = cat.Parent
if parent:
parent = parent.Name
else:
parent = "None"
data = [cattype, str(id.IntegerValue), isbic, name, lineRGB, str(lineprojection), str(linecut), materialname, parent]
dataB = tuple(zip(*data))
return(dataB)
def getCategoryData(cat):
cattype = cat.CategoryType.ToString()
id = cat.Id
bic = cat.BuiltInCategory
if bic == BuiltInCategory.INVALID:
isbic = "False"
else:
isbic = "True"
name = cat.Name
#builtincategoeyname = LabelUtils(bic)
lineprojection = None
linecut = None
projectionId = cat.GetLinePatternId(GraphicsStyleType.Projection)
lineprojection = getLinePattern(projectionId)
projectionpen = cat.GetLineWeight(GraphicsStyleType.Projection)
cutId = cat.GetLinePatternId(GraphicsStyleType.Cut)
linecut = getLinePattern(cutId)
cutpen= cat.GetLineWeight(GraphicsStyleType.Cut)
linecolor = cat.LineColor
r = linecolor.Red
g = linecolor.Green
b = linecolor.Blue
lineRGB = "(" + str(r) + "," + str(g) + "," +str(b) + ")"
material = cat.Material
if material:
materialname = material.Name
else:
materialname = "None"
if cat.IsCuttable == 0:
cuatable = "False"
else:
cutable = "True"
if cat.CanAddSubcategory == 0:
canaddsubcategory = "False"
else:
canaddsubcategory = "True"
parent = cat.Parent
if parent:
parent = parent.Name
else:
parent = "None"
data = [cattype, str(id.IntegerValue), isbic, name, lineRGB, str(lineprojection), str(projectionpen), str(linecut), str(cutpen), materialname, parent]
return(data)
def getCategories():
header = ["Category Type",
"Category Id",
"Is BuiltInCategory",
"Category Name",
"Category Color",
"Porjection Linetype",
"Projection Pen",
"Cut Linetype",
"Cut Pen",
"Material",
"Parent Category"]
dataout = []
categories = doc.Settings.Categories
for cat in categories:
catdata = getCategoryData(cat)
dataout.append(catdata)
try:
subcats = cat.SubCategories
for sc in subcats:
subcatdata = getCategoryData(sc)
dataout.append(subcatdata)
except:
pass
#sorted(dataout)
dataout.sort(key = lambda dataout: dataout[0])
dataout.insert(0, header)
return dataout
def getCategoriesTranspose():
header = ["Category Type", "Category Id", "Is BuiltInCategory", "Category Name", "Category Color", "Porjection Linetype", "Cut Linetype", "Material", "Parent Category"]
dataout = []
categories = doc.Settings.Categories
for cat in categories:
catdata = getCategoryData(cat)
dataout.append(catdata)
try:
subcats = cat.SubCategories
for sc in subcats:
subcatdata = getCategoryData(sc)
dataout.append(subcatdata)
except:
pass
#sorted(dataout)
dataout.sort(key = lambda dataout: dataout[0])
dataout.insert(0, header)
d = tuple(zip(*data))
return d
def getObjectStyles():
objectstyles = uidoc.ObjectStyles
return objectstylestyles
def rgbint(rgb):
r = rgb[0]
g = rgb[1]
b = rgb[2]
rgbint = 65536 * r +256 * g + b
return rgbint
def FormatExcel(ws):
count = 1
with forms.ProgressBar(title = 'Well hold on. This will take a second...', steps=10) as pb:
ws.Range("A1:K1").Font.FontStyle = "Bold"
ws.Range("A1:K1").Columns.AutoFit
excel.ActiveSheet.Columns.AutoFit()
r = 192
g = 192
b = 192
ws.Range("A1:K1").Interior.Color = 65536 * r +256 * g + b
ws.Columns.HorizontalAlignment = XlHAlign.xlHAlignCenter
ws.Columns("B:D").HorizontalAlignment = XlHAlign.xlHAlignRight
ws.Columns("I").HorizontalAlignment = XlHAlign.xlHAlignLeft
colorcount = ws.Range("E1").End(xlDirecDown).Row
colorrange = ws.Range("E1:" + "E" + str(colorcount))
for c in colorrange:
rgb = c.Text
rgb = rgb[1:]
rgb = rgb[:-1]
rgb = rgb.split(",")
try:
rgb = list(map(int, rgb))
rgb = rgbint(rgb)
except:
rgb = None
if rgb is not None:
c.Interior.Color = rgb
if rgb == int(0):
c.Font.Color = 16777215
elif rgb < 2210752:
c.Font.Color = 16777215
# else:
# c.Font.Color = 0
isbiccount = ws.Range("C1").End(xlDirecDown).Row
isbicrange = ws.Range("C1:" + "C" + str(isbiccount))
for c in isbicrange:
if c.Value() == "False":
myrow = ws.Range("A" + str(c.Row) + ":" + "D" + str(c.Row))
myrow.Interior.Color = 12632256
myrow = ws.Range("F" + str(c.Row) + ":" + "K" + str(c.Row))
myrow.Interior.Color = 12632256
pb.update_progress(count, isbiccount)
count = count + 1
categorydata = getCategories()
WriteRange(ws, categorydata)
#excel.Visible = True
FormatExcel(ws)
forms.toast(
"We're all done here. Have a Nice day. Click me for your Excel.",
title = "Otto Form 1.0",
appid ="Otto Pilot",
click = "https://www.yaaeger.com/",
actions={
"Open Excel":myexcelfile
})
#workbook.Save()
workbook.SaveAs(myexcelfile)
workbook.Close()
excel.Quit()