Hi Fusion Forum Denizens,
I'm doing some toolpath costing analysis and would find it very useful to be able to automatically extract simulation data for each operation - specifically the volume of material remaining after each cut. In the verification section below, you can see this data is available but having looked into the API I don't see a way to get this out without manually clicking thorugh all the operations - an intern's worst nightmare.
Does anyone know how to get this data? I imagine either custom setup sheets or some sort of api script would do the job.
Many thanks,
Alex
Solved! Go to Solution.
Solved by kandennti. Go to Solution.
Hi @manufacturing4QWS7 -San.
With the dialog displayed, the following text command can be executed to obtain information about the dialog.
Toolkit.cmdDialog
We have created a sample that actually starts a simulation and retrieves information from the dialog.
Even if the dialog is displayed, the correct information cannot be obtained until the calculation is finished, so the information is obtained after the calculation is finished.
# Fusion360API Python script
import traceback
import adsk
import adsk.core as core
import adsk.cam as cam
import time
import re
def run(context):
ui: core.UserInterface = None
try:
app: core.Application = core.Application.get()
ui = app.userInterface
camObj: cam.CAM = cam.CAM.cast(app.activeProduct)
if not camObj: return
if camObj.setups.count < 1: return
# First “setup”
setup: cam.Setup = camObj.setups[0]
# A list of “Setup”, “Operations”, “NC Programs”, etc.
msg = show_simulation_info([setup])
ui.messageBox(
msg,
"Simulation Info"
)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def show_simulation_info(
entities: list) -> None:
ui: core.UserInterface = None
try:
app: core.Application = core.Application.get()
ui = app.userInterface
# Select entities
sels: core.Selections = ui.activeSelections
sels.clear()
for ent in entities:
sels.add(ent)
# Start Simulation
app.executeTextCommand(u"Commands.Start IronSimulation")
# Wait for calculation to finish
pattern = r"None(?: \(\d+\.\d+%\))?"
dialogInfo = ""
while True:
time.sleep(0.5)
adsk.doEvents()
dialogInfo = app.executeTextCommand(u"Toolkit.cmdDialog")
result = extract_data(dialogInfo, pattern)
print(result)
if result == "None": break
# Stop Simulation
app.executeTextCommand(u"Commands.Start SimulationStop")
# Volume
msgLst = []
pattern = r"Volume, (\d+\.\d+) cm\^3 \(\d+\.\d+%\)"
msgLst.append(extract_data(dialogInfo, pattern))
# Start volume
pattern = r"Start volume, (\d+\.\d+) cm\^3"
msgLst.append(extract_data(dialogInfo, pattern))
# Result
return "\n".join(msgLst)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def extract_data(
text: str,
pattern: str
) -> str:
match = re.search(pattern, text)
if match:
return match.group()
else:
return ""
Incredible, that's exactly what I was after! If you're ever in Bristol, UK, I'd definitely buy you a pint. For future users I've pasted the small modification I made to output the data automatically.
# Fusion360API Python script
import traceback
import adsk
import adsk.core as core
import adsk.cam as cam
import time
import re
import json
def run(context):
ui: core.UserInterface = None
try:
app: core.Application = core.Application.get()
ui = app.userInterface
camObj: cam.CAM = cam.CAM.cast(app.activeProduct)
if not camObj: return
if camObj.setups.count < 1: return
# First “setup”
volData = []
for setup in camObj.setups:
setupName=setup.name
for i, op in enumerate(setup.operations):
msg = show_simulation_info([op])
volData.append(
{
"Setup": setupName,
"data": msg,
"op": op.name,
"i": i
}
)
with open(r"C:\output.txt", "w+") as f:
f.write(json.dumps(volData))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def show_simulation_info(
entities: list) -> None:
ui: core.UserInterface = None
try:
app: core.Application = core.Application.get()
ui = app.userInterface
# Select entities
sels: core.Selections = ui.activeSelections
sels.clear()
for ent in entities:
sels.add(ent)
# Start Simulation
app.executeTextCommand(u"Commands.Start IronSimulation")
# Wait for calculation to finish
pattern = r"None(?: \(\d+\.\d+%\))?"
dialogInfo = ""
while True:
time.sleep(0.5)
adsk.doEvents()
dialogInfo = app.executeTextCommand(u"Toolkit.cmdDialog")
result = extract_data(dialogInfo, pattern)
print(result)
if result == "None": break
# Stop Simulation
app.executeTextCommand(u"Commands.Start SimulationStop")
# Volume
msgLst = []
pattern = r"Volume, (\d+\.\d+) cm\^3 \(\d+\.\d+%\)"
msgLst.append(extract_data(dialogInfo, pattern))
# Start volume
pattern = r"Start volume, (\d+\.\d+) cm\^3"
msgLst.append(extract_data(dialogInfo, pattern))
# Result
return "\n".join(msgLst)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def extract_data(
text: str,
pattern: str
) -> str:
match = re.search(pattern, text)
if match:
return match.group()
else:
return ""
@manufacturing4QWS7 -San.
The function name was inappropriate because the process was rewritten just before.
# def show_simulation_info(
# entities: list) -> None:
def get_volume_of_simulation(
entities: list) -> str:
Can't find what you're looking for? Ask the community or share your knowledge.