Setting a timer to limit Dynamo script runtime

Hi, When I attempt to Solid.ByUnion thousands of simple solids (cuboids) into a single solid in order to export it to an SAT file, Dynamo freezes and doesn’t complete the operation. I think I’ll have to create the solids in Revit from Dynamo and export them from there.

However, if I try to add a timer it doesn’t work for me. It runs until completion and does not respect the time.

Method #1

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

import time

# Set the maximum number of seconds the script should run
max_seconds = 10

# Function to perform the union operation on a list of solids
def union_solids(solids):
    result = solids[0]
    for i in range(1, len(solids)):
        result = result.Union(solids[i])
    return result

# Get all solids from the current Dynamo document
solids = IN[0]

# Start the timer
start_time = time.time()

# Perform the union operation within the time limit
result = None
while True:
    result = union_solids(solids)
    elapsed_time = time.time() - start_time
    if elapsed_time >= max_seconds:
        break

# Output the resulting solid or None if the time limit was reached
OUT = result

Method #2

1 Like

@theshysnail ,

i use ranges f.e. i have 10.000 elements. i do

x[0..1000];
x[1001..2000];
...

KR
Andreas

1 Like

Although there have been some improvements with the below code to prevent the session from freezing, the script still does not adhere to the specified timer. It doesn’t matter if you enter 2 seconds or 10 seconds. The script may complete running in anywhere between 20 seconds to a couple of minutes.

TuneUp is also causing confusion up by showing about 10x more runtime. Instead of 68 seconds it shows 686 seconds (686084 ms).

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import Solid
import time

# Set the maximum number of seconds the script should run
max_seconds = 10

# Function to perform the union operation on a list of solids within the specified time limit
def union_solids(solids, max_time):
    start_time = time.time()
    result = solids[0]
    index_unioned = 0  # Initialize the counter
    for i in range(1, len(solids)):
        if time.time() - start_time >= max_time:
            break
        result = result.Union(solids[i])
        index_unioned += 1  # Increment the counter
    return result, index_unioned

# Get all solids from the current Dynamo document
solids = IN[0]

# Perform the union operation within the time limit
result, index_unioned = union_solids(solids, max_seconds)

# Output the resulting solid, the number of solids unioned, or None if the time limit was reached
OUT = result, index_unioned
1 Like