Skip to content

Enhancement 1739 - Improve Copy Legend Functionality #2745

Merged
jmcouffin merged 13 commits intopyrevitlabs:developfrom
MohamedAsli:dev/1739
Aug 4, 2025
Merged

Enhancement 1739 - Improve Copy Legend Functionality #2745
jmcouffin merged 13 commits intopyrevitlabs:developfrom
MohamedAsli:dev/1739

Conversation

@MohamedAsli
Copy link
Copy Markdown
Contributor

Improve Copy Legend Functionality

Description

This PR enhances the functionality for copying legend views between documents in Revit by adding :

  • Progress Bar
  • Transaction Group
  • Clear success/failure messages
  • Improved error handling and detailed logging of errors and skipped elements

Checklist

Before submitting your pull request, ensure the following requirements are met:

  • Code follows the PEP 8 style guide.
  • Code has been formatted with Black
  • Changes are tested and verified to work as expected.

Related Issues

#1739


Additional Notes

For now I just skipped ReferencePlanes, because they get copied in the model space (unwanted) (see #2319)

@devloai
Copy link
Copy Markdown
Contributor

devloai bot commented Aug 4, 2025

I've hit an internal error while addressing the comment. Please try again later.

@jmcouffin jmcouffin requested a review from Copilot August 4, 2025 10:25
@jmcouffin
Copy link
Copy Markdown
Contributor

@devloai please review this PR

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the Copy Legends functionality by adding a progress bar, transaction groups, improved error handling, and clearer user feedback messages. The refactoring transforms a simple copy function into a more robust operation with detailed logging and user experience improvements.

  • Added progress bar for better user feedback during copy operations
  • Implemented transaction groups and enhanced error handling with detailed logging
  • Improved user messaging with success/failure notifications and detailed operation summaries

Comment on lines +138 to +141
logger_messages.log_message_with_doc_and_legend(
dest_doc.Title,
legend_name,
"Legend name already exists. Renaming to: {}".format(new_name))
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log message is logged for every legend regardless of whether it was actually renamed. This should only be logged when counter > 0 (i.e., when a rename actually occurred).

Suggested change
logger_messages.log_message_with_doc_and_legend(
dest_doc.Title,
legend_name,
"Legend name already exists. Renaming to: {}".format(new_name))
if counter > 0:
logger_messages.log_message_with_doc_and_legend(
dest_doc.Title,
legend_name,
"Legend name already exists. Renaming to: {}".format(new_name))

Copilot uses AI. Check for mistakes.

options = DB.CopyPasteOptions()
options.SetDuplicateTypeNamesHandler(CopyUseDestination())
copied_elements = DB.ElementTransformUtils.CopyElements(
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation of this line is inconsistent with the surrounding code block. It should align with the other statements in the transaction.

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +83
# skip reference plane for now, they are copied in the model space
if isinstance(el, DB.Element) and el.Category and not isinstance(el, DB.ReferencePlane):
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The condition logic could be more readable by extracting the ReferencePlane check into a separate variable or adding a comment explaining why ReferencePlanes are excluded.

Suggested change
# skip reference plane for now, they are copied in the model space
if isinstance(el, DB.Element) and el.Category and not isinstance(el, DB.ReferencePlane):
# ReferencePlanes are skipped because they are copied in the model space, not in the legend view.
is_reference_plane = isinstance(el, DB.ReferencePlane)
if isinstance(el, DB.Element) and el.Category and not is_reference_plane:

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@devloai devloai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary:

This PR enhances the Copy Legend functionality by adding:
• Progress bar with cancellation support
• TransactionGroup for better transaction management
• Clear success/failure messages with detailed reporting
• Improved error handling and logging
• Reference plane exclusion to avoid model space copying (addresses #2319)

Review Summary:

Overall solid enhancement that addresses the original issue #1739. The code adds meaningful user feedback and better error handling. I identified 3 logic bugs that should be fixed: incorrect boolean condition for logging, misleading log messages, and missing progress bar cancellation handling. The changes follow pyRevit patterns well and the Reference plane exclusion properly addresses a known issue.

Follow-up suggestions:

@devloai fix the identified logic issues

content = "\n".join(details)
forms.alert(msg=main_instruction, sub_msg=content)

if logger_messages:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition here is incorrect. logger_messages is an instance of LogMessage class and will always evaluate to True in a boolean context. You need to check if there are actually messages to log.

if logger_messages.get_messages().strip():
    logger.warning(logger_messages.get_messages())
actions

Feedback: Rate this comment to help me improve future code reviews:

  • 👍 Good - Helpful and accurate
  • 👎 Poor - Wrong, unclear, or unhelpful
  • Skip if you don't have any strong opinions either way.

while new_name in all_legend_names:
counter += 1
new_name = src_name + " (Duplicate %s)" % counter
logger_messages.log_message_with_doc_and_legend(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logging message is misleading. It logs "Legend name already exists" for every legend, even when no duplication occurs. The message should only be logged when counter > 0.

if counter > 0:
    logger_messages.log_message_with_doc_and_legend(
        dest_doc.Title,
        legend_name,  
        "Legend name already exists. Renaming to: {}".format(new_name))
actions

Feedback: Rate this comment to help me improve future code reviews:

  • 👍 Good - Helpful and accurate
  • 👎 Poor - Wrong, unclear, or unhelpful
  • Skip if you don't have any strong opinions either way.

current_operation = 0
skipped_docs = []

with forms.ProgressBar(cancellable=True) as pb:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Progress bar should handle cancellation. Currently, if user cancels the operation, the script will continue processing. Check pb.cancelled and break/exit if needed.

with forms.ProgressBar(cancellable=True) as pb:
    for dest_doc in open_docs:
        if pb.cancelled:
            break
        # ... rest of the loop
actions

Feedback: Rate this comment to help me improve future code reviews:

  • 👍 Good - Helpful and accurate
  • 👎 Poor - Wrong, unclear, or unhelpful
  • Skip if you don't have any strong opinions either way.

@jmcouffin
Copy link
Copy Markdown
Contributor

It seems fine overall, please review and test the comments from our AI friends,
Nice contribution again @MohamedAsli Keep them coming!

@MohamedAsli
Copy link
Copy Markdown
Contributor Author

@jmcouffin I think we're good now

@jmcouffin
Copy link
Copy Markdown
Contributor

@MohamedAsli at line 61, Using variable 'tg' before assignment

@MohamedAsli
Copy link
Copy Markdown
Contributor Author

@jmcouffin Good catch!
How did you manage to catch it, since you can't physically hit "cancel" before the script moves forward?
Anyway, I simply removed the first 'if pb.cancelled' check - it won't change the execution time. I'll just catch it after beginning to loop through the legends.

@jmcouffin jmcouffin added Enhancement Enhancement request [class->Improved #{number}: {title}] Tools Issues related to pyRevit commands [subsystem] labels Aug 4, 2025
@jmcouffin jmcouffin merged commit 3dbf2d3 into pyrevitlabs:develop Aug 4, 2025
@jmcouffin
Copy link
Copy Markdown
Contributor

How did you manage to catch it, since you can't physically hit "cancel" before the script moves forward?

It was listed in my code editor errors list :)
The code itself could be further improved for sure, but this is a good enhancement already

Thanks for all of the above @MohamedAsli

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 4, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25216+1429-wip

@MohamedAsli
Copy link
Copy Markdown
Contributor Author

Thanks!
Absolutely agree on the improvements. I've started but cannot squeeze much time into it. I'll invest it in major commands for now :)

It was listed in my code editor errors list

My pycharm didn't, surely missing some settings

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 7, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25219+0909-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 8, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25251+0805-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 8, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25251+0824-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 9, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25252+1659-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25255+0644-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25255+0911-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25256+0727-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25258+1448-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25265+1047-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25266+0803-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25268+1757-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25269+1336-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25269+1431-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25269+1436-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25269+1822-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25271+1719-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25272+2003-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25272+2012-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25272+2017-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25272+2149-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 1, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25274+1734-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 4, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25277+1425-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 4, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25277+1427-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 6, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25279+2157-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 7, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25280+0218-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 7, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25280+1054-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 7, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25280+1057-wip

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 9, 2025

📦 New work-in-progress (wip) builds are available for 5.2.0.25282+1656-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25283+0140-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25286+1022-wip

@github-actions
Copy link
Copy Markdown
Contributor

📦 New work-in-progress (wip) builds are available for 5.2.0.25302+0949-wip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Enhancement request [class->Improved #{number}: {title}] Tools Issues related to pyRevit commands [subsystem]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants