def main():
    """Main Function."""
    
    print("🐍 Running {fname} version {ver}...".format(fname=__name, ver=__version))

    # STEP 0: Setup
    doc = __revit__.ActiveUIDocument.Document
    uidoc = __revit__.ActiveUIDocument

    # STEP 1: Get Selection
    selection = uidoc.Selection
    element_ids = selection.GetElementIds()
    
    # STEP 2: Filter Selection
    ducts, flex_ducts = [], []
    pipes, flex_pipes = [], []
    for element_id in element_ids:
        element = doc.GetElement(element_id)
        if isinstance(element, db.Mechanical.Duct):
            ducts.append(element)
        if isinstance(element, db.Mechanical.FlexDuct):
            flex_ducts.append(element)
        if isinstance(element, db.Plumbing.Pipe):
            pipes.append(element)
        if isinstance(element, db.Plumbing.FlexPipe):
            flex_pipes.append(element)
    
    # STEP 3: Aggregate data
    duct_length = total_length(ducts)
    flex_duct_length = total_length(flex_ducts)
    pipe_length = total_length(pipes)
    flex_pipe_length = total_length(flex_pipes)

    # STEP 4: Ouput Result
    summary = "Duct = {0} m / Flex Duct = {1}m\n".format(duct_length, flex_duct_length)
    summary += "Ducting = {0} m\n\n".format(duct_length + flex_duct_length)
    summary += "Pipe = {0} m / Flex Pipe = {1}m\n".format(pipe_length, flex_pipe_length)
    summary += "Piping = {0} m".format(pipe_length + flex_pipe_length)

    dialog = ui.TaskDialog(title="Duct Length")
    dialog.MainInstruction = "Selection Lengths Summary"
    dialog.MainContent = summary
    dialog.CommonButtons = ui.TaskDialogCommonButtons.Close
    dialog.DefaultButton = ui.TaskDialogResult.Close
    dialog.Show()

    print("✔\nDone. 😊")
    return ui.Result.Succeeded
def main():
    """Main Function."""

    print("Running {fname} version {ver}...".format(fname=__name,
                                                    ver=__version))

    # STEP 0: Setup
    doc = __revit__.ActiveUIDocument.Document
    view = doc.ActiveView

    # STEP 1: Ask user for clash report html file adn parse it
    print("Opening interference check report file...")
    open_dialog = swf.OpenFileDialog()
    open_dialog.Title = "Open Interference Check Report"
    open_dialog.Filter = "HMTL files (*.html)|*.html"
    if open_dialog.ShowDialog() == swf.DialogResult.OK:  # file selected
        file_path = open_dialog.FileName
        # STEP 2: Parse clash report file and summarize findings
        print("Reading {fname}...".format(fname=file_path))
        with open(file_path, mode="rb") as html_file:
            html = html_file.read()  # just read the plain bytes
            uhtml = unicode(
                html, "utf-16")  # Revit exports html in UTF-16(-LE) encoding
        print("Parsing file contents...")
        parser = InterferenceReportParser()
        parser.feed(uhtml)
        parser.close()
        clashes = parser.clashes
        clashing_ids = []
        for pair in parser.clashes.values():
            for elem_id in pair:
                clashing_ids.append(elem_id)
        clashing_ids = set(clashing_ids)
        # Get all element ids of the elements in the view
        all_ids = db.FilteredElementCollector(doc, view.Id)\
                    .WhereElementIsNotElementType()\
                    .ToElementIds()
        all_ids = set([elem_id.IntegerValue for elem_id in all_ids])
        # Get all element ids of non-clashing elements in the view
        non_clashing_ids = all_ids - clashing_ids
        # Create summary text for user input dialog
        summary_text = "Checked report {path}\n".format(path=file_path)
        summary_text += "Found {num} clashes in the report.\n".format(
            num=len(clashes))
        summary_text += "Found {num} clashing elements involved in those clashes.\n".format(
            num=len(clashing_ids))
        summary_text += "The total number of elements in the current view is {num}\n".format(
            num=len(all_ids))
        summary_text += "Found {num} non-clashing elements in the current view.".format(
            num=len(non_clashing_ids))
        print(summary_text)

        # STEP 3: Ask user for display option
        dialog = ui.TaskDialog(title="Mark All Clashes")
        dialog.MainInstruction = "Interference Report Summary"
        dialog.MainContent = summary_text
        dialog.AddCommandLink(ui.TaskDialogCommandLinkId.CommandLink1,
                              "Mark clashing elements and fade the rest")
        dialog.AddCommandLink(ui.TaskDialogCommandLinkId.CommandLink2,
                              "Hide all non-clashing elements temporarily")
        dialog.CommonButtons = ui.TaskDialogCommonButtons.Close
        dialog.DefaultButton = ui.TaskDialogResult.Close
        result = dialog.Show()

        # Step 4: Emphasize the clashes based on the user selection
        transaction = db.Transaction(doc)
        transaction.Start("{name} - v{ver}".format(name=__name, ver=__version))
        try:
            if result == ui.TaskDialogResult.CommandLink1:  # Mark clashes and fade the rest
                print("Marking all clashing elements and fading the rest...")
                for elem_id in all_ids:  # fade all visible elements in the view
                    view.SetElementOverrides(db.ElementId(elem_id),
                                             faded_overrides)
                for elem_id in clashing_ids:  # emphasize the clashing elements
                    view.SetElementOverrides(db.ElementId(elem_id),
                                             clashing_overrides)
            elif result == ui.TaskDialogResult.CommandLink2:  # Hide all non-clashing elements
                print(
                    "Hiding all non-clashing elements in the view temporarily..."
                )
                for elem_id in non_clashing_ids:  # hide alll non-clashing elements
                    view.HideElementTemporary(db.ElementId(elem_id))
            else:
                print("Nothing to do.")
        except Exception as ex:
            print("Exception: {ex}".format(ex=ex))
            transaction.RollBack()
        else:
            transaction.Commit()
            print("Done.")
    else:  # no file to parse
        print("Nothing to do.")
Пример #3
0
def main():
    """Main Script."""

    print("🐍 Running {fname} version {ver}...".format(fname=__name,
                                                      ver=__version))

    # STEP 0: Setup
    doc = __revit__.ActiveUIDocument.Document

    # STEP 1: Inspect Model and summarize findings
    pipe_insulations = query_all_elements_of_category(
        doc=doc, cat=db.BuiltInCategory.OST_PipeInsulations)
    duct_insulations = query_all_elements_of_category(
        doc=doc, cat=db.BuiltInCategory.OST_DuctInsulations)
    rogue_pipe, unhosted_pipe = find_rogue_and_unhosted_elements(
        doc=doc, elems=pipe_insulations)
    rogue_duct, unhosted_duct = find_rogue_and_unhosted_elements(
        doc=doc, elems=duct_insulations)
    summary_list = write_summary(
        tpipe=pipe_insulations,
        tduct=duct_insulations,  # totals
        upipe=unhosted_pipe,
        uduct=unhosted_duct,  # unhosted
        rpipe=rogue_pipe,
        rduct=rogue_duct)  # rogue
    summary_text = "\n".join(summary_list)
    print(summary_text)

    # STEP 2: Receive User Input
    dialog = ui.TaskDialog(title="Insulation Cleanup")
    dialog.MainInstruction = "Insulation Cleanup Summary"
    dialog.MainContent = summary_text
    dialog.AddCommandLink(ui.TaskDialogCommandLinkId.CommandLink1,
                          "Write Report")
    dialog.AddCommandLink(ui.TaskDialogCommandLinkId.CommandLink2,
                          "Clean Insulation")
    dialog.CommonButtons = ui.TaskDialogCommonButtons.Close
    dialog.DefaultButton = ui.TaskDialogResult.Close
    result = dialog.Show()

    # STEP 3: Write report or clean up insulation
    if result == ui.TaskDialogResult.CommandLink1:  # Write report
        save_dialog = swf.SaveFileDialog()
        save_dialog.Title = "Save Insulation Cleanup Report"
        save_dialog.Filter = "Text files|*.txt"
        save_dialog.FileName = "report.txt"
        if save_dialog.ShowDialog() == swf.DialogResult.OK:  # Save report
            file_path = save_dialog.FileName
            print("Writing report to {0}".format(file_path))
            with open(file_path, mode="wb") as fh:
                report = write_report(doc, unhosted_pipe, rogue_pipe,
                                      unhosted_duct, rogue_duct)
                for line in report:
                    fh.write("{line}\r\n".format(line=line))
            print("✔\nDone. 😊")
            return ui.Result.Succeeded
        else:  # Don't save report
            print("🛈 File save dialog canceled.")
            return ui.Result.Cancelled
    elif result == ui.TaskDialogResult.CommandLink2:  # Clean Insulation
        transaction = db.Transaction(doc)
        transaction.Start("{name} - v{ver}".format(name=__name, ver=__version))
        try:
            print("Cleaning Insulation...")
            for pipe_element in unhosted_pipe:
                doc.Delete(pipe_element.Id)
            print("Deleted {num} unhosted pipe insulation elements".format(
                num=len(unhosted_pipe)))
            for pipe_pair in rogue_pipe:
                cleanup_insulation(pipe_pair)
            print("Moved {num} rogue pipe insulation elements.".format(
                num=len(rogue_pipe)))
            for duct_element in unhosted_duct:
                doc.Delete(duct_element.Id)
            print("Deleted {num} unhosted duct insulation elements.".format(
                num=len(unhosted_duct)))
            for duct_pair in rogue_duct:
                cleanup_insulation(duct_pair)
            print("Moved {num} rogue duct insulation elements.".format(
                num=len(rogue_duct)))
        except Exception as exception:
            print("Failed.\nException:\n{ex}".format(ex=exception))
            transaction.RollBack()
            return ui.Result.Failed
        else:
            print("✔\nDone. 😊")
            transaction.Commit()
            return ui.Result.Succeeded
    else:
        print("Nothing to do.")
        return ui.Result.Cancelled