|
| 1 | +''' |
| 2 | +CATEGORIES TO M SCRIPT - CREATE CONDITIONAL STATEMENT CODE FOR POWER BI |
| 3 | +- |
| 4 | +a dynamoPython script, visit the website for more details |
| 5 | +https://github.com/Amoursol/dynamoPython |
| 6 | +''' |
| 7 | +__author__ = 'Adam Bear - adam@ukbear.com' |
| 8 | +__twitter__ = '@adambear82' |
| 9 | +__github__ = '@adambear82' |
| 10 | +__version__ = '1.0.0' |
| 11 | + |
| 12 | +''' |
| 13 | +for large projects with lots of clashes it is useful to analyse in |
| 14 | +a business inteligence or data visualisation tool such as ms power bi. |
| 15 | +creating the conditonal statement in power bi can take a long time if |
| 16 | +there are a lot of categories to include |
| 17 | +''' |
| 18 | + |
| 19 | +# ------------------------ |
| 20 | +# import modules |
| 21 | +# ------------------------ |
| 22 | + |
| 23 | +# refer to the clipboard |
| 24 | +import clr |
| 25 | +clr.AddReference('System.Windows.Forms') |
| 26 | +from System.Windows.Forms import Clipboard |
| 27 | + |
| 28 | +# refer to the document manager |
| 29 | +clr.AddReference('RevitServices') |
| 30 | +import RevitServices |
| 31 | +from RevitServices.Persistence import DocumentManager |
| 32 | +doc = DocumentManager.Instance.CurrentDBDocument |
| 33 | + |
| 34 | +# refer to the revit API |
| 35 | +clr.AddReference('RevitAPI') |
| 36 | +import Autodesk |
| 37 | +from Autodesk.Revit.DB import * |
| 38 | + |
| 39 | +# ------------------------ |
| 40 | +# inputs & variables |
| 41 | +# ------------------------ |
| 42 | + |
| 43 | +# some categoreies exported from navisworks are not included as |
| 44 | +# categories in visibility graphics, for examplevv |
| 45 | +# Handrails, Landings, Pads, Runs, Slab Edges, Top Rails, Wall Sweeps |
| 46 | + |
| 47 | +# remove single and double spaces after commas and split into list |
| 48 | +catsInput = IN[0] |
| 49 | +catsReplace1 = catsInput.replace(', ', ',') |
| 50 | +catsReplace2 = catsReplace1.replace(', ', ',') |
| 51 | +catsManual = catsReplace2.split(',') |
| 52 | +catsManual.sort() |
| 53 | + |
| 54 | +# provide reference strings |
| 55 | +hashtag = 'Renamed Columns1' |
| 56 | +pathlink = 'pathlink' |
| 57 | +filterIn = 'filter_in' |
| 58 | +filterOut = 'filter_out' |
| 59 | + |
| 60 | +# ------------------------ |
| 61 | +# get categories |
| 62 | +# ------------------------ |
| 63 | + |
| 64 | +# get categories that can add sub categories |
| 65 | +# ie the categories which appear in vis graphics |
| 66 | +# annotated from forum post with kudos to René Picazo |
| 67 | +# https://forum.dynamobim.com/t/get-all-elements-in-model-categories/9447/7 |
| 68 | +modelCats = [] |
| 69 | +for cat in doc.Settings.Categories : |
| 70 | + if cat.CategoryType == CategoryType.Model and cat.CanAddSubcategory: |
| 71 | + modelCats.append(cat.Name) |
| 72 | + |
| 73 | +# only append extra categories if they have been defined in input |
| 74 | +if catsInput : |
| 75 | + for cat in catsManual : |
| 76 | + modelCats.append(cat) |
| 77 | + |
| 78 | +# sort alphabetically so its easier to read |
| 79 | +cats = sorted(modelCats) |
| 80 | + |
| 81 | +# ------------------------ |
| 82 | +# strings |
| 83 | +# ------------------------ |
| 84 | + |
| 85 | +# the 1st line adds a column to the table based on a filter on the hash |
| 86 | +table = ''.join(('= Table.AddColumn(#"', hashtag, '", "filter",')) |
| 87 | + |
| 88 | +# define strings to be used in M code |
| 89 | +each = 'each if [' |
| 90 | +elif0 = 'else if [' |
| 91 | +elif1 = '] = "' |
| 92 | +elif2 = '" then "' |
| 93 | +elif3 = '"' |
| 94 | + |
| 95 | +# the 2nd line is a special case |
| 96 | +# where cats[0] requires 'each' instead of 'else if' |
| 97 | +catJoin = each, pathlink, elif1, cats[0], elif2, filterIn, elif3 |
| 98 | +temp = ''.join(catJoin) |
| 99 | +listLines = [] |
| 100 | +listLines.append(temp) |
| 101 | + |
| 102 | +# the 3rd line and onwards starts with else if |
| 103 | +# each row is checked if it is equall to one of the remaining cats |
| 104 | +# cats is sliced by [1:] to return items from index 1 to the last index |
| 105 | +for c in cats[1:] : |
| 106 | + catJoin = elif0, pathlink, elif1, c, elif2, filterIn, elif3 |
| 107 | + temp = ''.join(catJoin) |
| 108 | + listLines.append(temp) |
| 109 | +lines = '\r\n'.join(listLines) |
| 110 | + |
| 111 | +# the final line starts with else |
| 112 | +# rows not in cats are given the filterOut value |
| 113 | +strElse = ''.join(('else "', filterOut, '")')) |
| 114 | + |
| 115 | +# the code is brought together with new lines between each line |
| 116 | +code = '\r\n'.join((table, lines, strElse)) |
| 117 | + |
| 118 | +# ------------------------ |
| 119 | +# send to clipboard |
| 120 | +# ------------------------ |
| 121 | + |
| 122 | +# annotated with kudos to bakery 'by send to clipboard from revit' (sic) |
| 123 | +# https://github.com/LukeyJohnson/BakeryForDynamo/blob/97e5622db7ba14cd42caac9b8bd4fdba6b66871e/nodes/bv%20Send%20to%20Clipboard%20from%20Revit.dyf#L5-L12 |
| 124 | +# try to copy the code, provide a message if it fails |
| 125 | +try: |
| 126 | + Clipboard.SetText(code) |
| 127 | + copyMsg = code |
| 128 | +except: |
| 129 | + copyMsg = 'Data could not be copied to clipboard' |
| 130 | + |
| 131 | +# ------------------------ |
| 132 | +# output |
| 133 | +# ------------------------ |
| 134 | + |
| 135 | +OUT = copyMsg |
0 commit comments