I am exploring the routes module. It is fairly time consuming as it requires a full restart of Revit. I think I have hit a road block with Transactions.
The start up script.
# first import the routes module
#from update_element import update_element_parameter_value
print("Start Up Routes")
from pyrevit import routes, DB,forms
from pyrevit.routes import Request
from Autodesk.Revit.DB import Transaction, StorageType
import sys
#from update_element import update_element_parameter_value
api = routes.API('revit')
@api.route('/doors')
def get_doors(doc):
"""Find doors in active model"""
doors = DB.FilteredElementCollector(doc)\
.OfCategory(DB.BuiltInCategory.OST_Doors)\
.WhereElementIsNotElementType()\
.ToElements()
doors_data = ["Pass","this","data"]
forms.alert('Door Route Triggered')
# create response and return
return routes.make_response(data=doors_data)
def update_data(request: Request):
"""Update element parameter value from POST request"""
# Access the active document
doc = __revit__.ActiveUIDocument.Document
# Start a transaction to modify the document
try:
# Begin the transaction
with Transaction(doc, "Update Parameter Value") as transaction:
transaction.Start()
print("Inside Transaction")
data = request.data
transaction.Commit()
return routes.make_response(data=data)
except Exception as e:
# Roll back the transaction in case of an error
transaction.RollBack()
print(f"Error: {e}")
return routes.make_response(data={"error": str(e)}, status_code=500)
This is the script I am using to trigger the url
import requests
import json
# URL for the pyRevit route
url = 'http://<LOCAL_IP>:48884/revit/update'
data = {
"Interger": 3321,
"String": "Data is passed",
}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=data, headers=headers)
# Print response
print(response.status_code)
print(response.text)
I posted the errors to ChatGPT:
https://chatgpt.com/share/452e8adc-11f7-46d8-b58d-70b31fa1caca
On other attempts I got errors that ChatGPT outlined other transaction issue, to do with multithread transaction vs main thread.
Transaction timing is well outside my experience level. Can anyone inform if it is possible to use the routes to call functions that include transactions?
Many thanks