Is it possible to use an if statement to set whether a command is replaced or not by a command-exec hook?
i.e.
if doc is a family document:
post default command
else:
print message and don’t post command
Is it possible to use an if statement to set whether a command is replaced or not by a command-exec hook?
i.e.
if doc is a family document:
post default command
else:
print message and don’t post command
Hi,
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
else:
pass
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.set_title('Forbidden')
output.print_md('NEVER import CAD to Revit projects. Use Link CAD.')
else:
found_id = RevitCommandId.LookupPostableCommandId(PostableCommand.ImportCAD)
uiapp.PostCommand(found_id)
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?",
options=["Valider",
"Annuler",
"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 = 'https://knowledge.autodesk.com/support/revit-products/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/Revit-Model/files/GUID-B63B71A6-E8F2-40C9-9CAC-FFB738C431E4-htm.html'
script.open_url(url)
else:
EXEC_PARAMS.event_args.Cancel = True
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 @ https://github.com/eirannejad/pyRevit/blob/develop/extensions/pyRevitDevHooks.extension/hooks/command-exec[ID_INPLACE_COMPONENT].py
and all the hooks there
https://github.com/eirannejad/pyRevit/tree/develop/extensions/pyRevitDevHooks.extension/hooks
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
Hi Jean-Marc, sorry about to replying here, it is just that I am trying this solution and it seems to be working well but giving me this error, using Revit 2022.1 and pyRevit v4.8.13.
this is the code I am using:
# -*- coding: UTF-8 -*-\n"
from pyrevit import EXEC_PARAMS
from pyrevit import forms, script
doc = __revit__.ActiveUIDocument.Document
res = forms.alert("WARNING!\n\n"
"In-situ families are generally a bad practice,"
"unlike loadable families, they have drawbacks:\n"
" - Display issues,\n"
" - Couldn't identify its level properly,\n"
" - The copy will create a duplicate of the original,\n"
" - Unable to model parametrically\n\n"
"Do you really want to create a family in situ?",
options=["Validate",
"Cancel",
"More information about families in situ"],
title="Families In Situ",
footer="HDS Hooks")
if res == "Validate":
EXEC_PARAMS.event_args.Cancel = False
# logging to server
from hooksScripts import hooksLogger
hooksLogger("Inplace Component", doc)
elif res == "Cancel":
EXEC_PARAMS.event_args.Cancel = True
elif res == "More information about families in situ":
EXEC_PARAMS.event_args.Cancel = True
url = 'https://help.autodesk.com/view/RVT/2022/ENU/?guid=GUID-B63B71A6-E8F2-40C9-9CAC-FFB738C431E4'
script.open_url(url)
else:
EXEC_PARAMS.event_args.Cancel = True
Any idea of why it do not recognizes the hooksScripts module?, I have tried with the hooks_logger too and gives me the same error.
Thanks in advance!.