Command-exec hook

Is it possible to use an if statement to set whether a command is replaced or not by a command-exec hook?

if doc is a family document:
post default command
print message and don’t post command

sure, this is an example on the doc-opened hook:

    import sys

    from pyrevit import HOST_APP, framework
    from pyrevit import DB, revit
    from pyrevit import forms

    # from pyrevit import script
    # from os import path

    doc = __eventargs__.Document
    if not doc.IsFamilyDocument:
        do something      

Thanks but unfortunately that results in the same issue (looping of the replaced command being posted by itself) which I think is because command-exec hooks replace the PostableCommand at all references so the default command can’t be posted.

This is a hook which disables Import CAD in .rvt files. Import CAD is necessary in some rare cases (mostly .rfa files) and I can’t get that bit to work.

If you want to try it this is the code from a command-exec[ID_FILE_IMPORT] hook:

from pyrevit import script, revit
from Autodesk.Revit.UI import UIApplication, RevitCommandId, PostableCommand

doc = revit.doc
uiapp = UIApplication(doc.Application)
count = []
if not doc.IsFamilyDocument:
    output = script.get_output()
    output.resize(100, 300)
    output.print_md('NEVER import CAD to Revit projects. Use Link CAD.')
    found_id = RevitCommandId.LookupPostableCommandId(PostableCommand.ImportCAD)

I did not try it quite yet,
but you could try mine for inplace components:
This one works fine, you will get the structure from there:

    # -*- coding: UTF-8 -*-\n"
    from pyrevit import EXEC_PARAMS
    from pyrevit import forms, script

    doc = __revit__.ActiveUIDocument.Document

    res = forms.alert("ATTENTION!\n\n"
                      "Les familles insitu sont globalement une mauvaise pratique, "
                      "contrairement aux familles chargeables, elles ont des inconvénients: \n"
                      " - Problèmes d'affichage,\n"
                      " - Pas possible d'identifier son niveau adéquatement,\n"
                      " - La copie va créer un double de l'original,\n"
                      " - Impossible de modéliser de manière paramétrique\n\n"
                      "Voulez-vous vraiment créer une famille in situ?",
                               "Plus d'information sur les familles in situ"],
                      title="Familles In Situ",
                      footer="BIMONE Hooks")
    if res  == "Valider":
       EXEC_PARAMS.event_args.Cancel = False
       # logging to server
       from hooksScripts import hooksLogger
       hooksLogger("Inplace Component", doc)

    elif res  == "Annuler":
       EXEC_PARAMS.event_args.Cancel = True
    elif res  == "Plus d'information sur les familles in situ":
       EXEC_PARAMS.event_args.Cancel = True
       url = ''
       EXEC_PARAMS.event_args.Cancel = True
1 Like

Clever solution, but having tried both a copy/paste and modified version it’s still not working for me!

EXEC_PARAMS.event_args.Cancel = True

results in

Exception: Event cannot be cancelled!

Do you know what might be stopping it?

No idea.
and I cannot try right now.
you could take a look @[ID_INPLACE_COMPONENT].py
and all the hooks there


I was using command-exec incorrectly… Using command-before-exec worked perfectly as it allows you set rules to the default command, whereas command-exec replaces the default command with another. Thanks for your help, again