Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Path to Fusion 360 script directory
|
| 8 |
+
SCRIPT_PATH = 'path_to_your_fusion_360_scripts_folder' # Change this to your actual script path
|
| 9 |
+
OUTPUT_PATH = 'output'
|
| 10 |
+
|
| 11 |
+
@app.route('/generate_model', methods=['POST'])
|
| 12 |
+
def generate_model():
|
| 13 |
+
try:
|
| 14 |
+
# Get the parameters from the request
|
| 15 |
+
data = request.json
|
| 16 |
+
length = data.get('length', 100.0)
|
| 17 |
+
width = data.get('width', 50.0)
|
| 18 |
+
height = data.get('height', 10.0)
|
| 19 |
+
|
| 20 |
+
# Generate the Fusion 360 model using the script
|
| 21 |
+
result = generate_fusion_360_model(length, width, height)
|
| 22 |
+
|
| 23 |
+
# If model generation is successful, return success
|
| 24 |
+
return jsonify({"status": "success", "message": result, "output_path": OUTPUT_PATH}), 200
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return jsonify({"status": "error", "message": str(e)}), 500
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.route('/generate_toolpath', methods=['POST'])
|
| 31 |
+
def generate_toolpath():
|
| 32 |
+
try:
|
| 33 |
+
# Get the parameters from the request (toolpath generation settings, etc.)
|
| 34 |
+
data = request.json
|
| 35 |
+
tool_diameter = data.get('tool_diameter', 10.0)
|
| 36 |
+
feed_rate = data.get('feed_rate', 100.0)
|
| 37 |
+
spindle_speed = data.get('spindle_speed', 1500)
|
| 38 |
+
|
| 39 |
+
# Generate CNC toolpath using Fusion 360
|
| 40 |
+
result = generate_cnc_toolpath(tool_diameter, feed_rate, spindle_speed)
|
| 41 |
+
|
| 42 |
+
# If toolpath generation is successful, return success
|
| 43 |
+
return jsonify({"status": "success", "message": result, "output_file": f"{OUTPUT_PATH}/generated_toolpaths.tap"}), 200
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return jsonify({"status": "error", "message": str(e)}), 500
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def generate_fusion_360_model(length, width, height):
|
| 50 |
+
try:
|
| 51 |
+
# Construct the Fusion 360 script for generating the model (3D rectangle)
|
| 52 |
+
script_content = f"""
|
| 53 |
+
import adsk.core, adsk.fusion, adsk.cam, adsk.utility
|
| 54 |
+
|
| 55 |
+
def create_3d_model():
|
| 56 |
+
app = adsk.core.Application.get()
|
| 57 |
+
doc = app.activeDocument
|
| 58 |
+
design = doc.design
|
| 59 |
+
|
| 60 |
+
rootComp = design.rootComponent
|
| 61 |
+
|
| 62 |
+
# Define the model parameters
|
| 63 |
+
length = {length}
|
| 64 |
+
width = {width}
|
| 65 |
+
height = {height}
|
| 66 |
+
|
| 67 |
+
# Create a 2D rectangle for the base
|
| 68 |
+
sketches = rootComp.sketches
|
| 69 |
+
xy_plane = rootComp.xYConstructionPlane
|
| 70 |
+
sketch = sketches.add(xy_plane)
|
| 71 |
+
sketch.sketchCurves.sketchLines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(length, width, 0))
|
| 72 |
+
|
| 73 |
+
# Extrude the rectangle to create a 3D model
|
| 74 |
+
profile = sketch.profiles.item(0)
|
| 75 |
+
extrude_feat = rootComp.features.extrudeFeatures.addSimple(profile, adsk.core.ValueInput.createByString(str(height)), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
|
| 76 |
+
|
| 77 |
+
# Call the function to create the model
|
| 78 |
+
create_3d_model()
|
| 79 |
+
"""
|
| 80 |
+
script_path = os.path.join(SCRIPT_PATH, "create_3d_model.py")
|
| 81 |
+
with open(script_path, 'w') as script_file:
|
| 82 |
+
script_file.write(script_content)
|
| 83 |
+
|
| 84 |
+
# Execute the script in Fusion 360 (via subprocess)
|
| 85 |
+
subprocess.run(["fusion360", "-script", script_path]) # Adjust with how you run scripts in Fusion 360
|
| 86 |
+
|
| 87 |
+
return "3D model created successfully"
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Error creating model: {str(e)}"
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def generate_cnc_toolpath(tool_diameter, feed_rate, spindle_speed):
|
| 94 |
+
try:
|
| 95 |
+
# Construct the Fusion 360 script for generating the CNC toolpath
|
| 96 |
+
toolpath_script = f"""
|
| 97 |
+
import adsk.core, adsk.fusion, adsk.cam, adsk.utility
|
| 98 |
+
|
| 99 |
+
def create_toolpath():
|
| 100 |
+
app = adsk.core.Application.get()
|
| 101 |
+
doc = app.activeDocument
|
| 102 |
+
design = doc.design
|
| 103 |
+
rootComp = design.rootComponent
|
| 104 |
+
|
| 105 |
+
# Define toolpath parameters
|
| 106 |
+
tool_diameter = {tool_diameter}
|
| 107 |
+
feed_rate = {feed_rate}
|
| 108 |
+
spindle_speed = {spindle_speed}
|
| 109 |
+
|
| 110 |
+
# Select the tool (for simplicity, assuming a predefined tool)
|
| 111 |
+
tool = rootComp.machines.toolCatalog.tools.item(0) # Get the first tool from the tool catalog
|
| 112 |
+
|
| 113 |
+
# Create a simple pocketing operation for the toolpath
|
| 114 |
+
setup = rootComp.machining.setups.addSimple(rootComp.features.sketches[0])
|
| 115 |
+
operation = setup.machiningOperations.addPocketingOperation(tool, feed_rate, spindle_speed)
|
| 116 |
+
|
| 117 |
+
# Call the function to create the toolpath
|
| 118 |
+
create_toolpath()
|
| 119 |
+
"""
|
| 120 |
+
toolpath_script_path = os.path.join(SCRIPT_PATH, "generate_toolpath.py")
|
| 121 |
+
with open(toolpath_script_path, 'w') as toolpath_file:
|
| 122 |
+
toolpath_file.write(toolpath_script)
|
| 123 |
+
|
| 124 |
+
# Run the Fusion 360 toolpath generation script
|
| 125 |
+
subprocess.run(["fusion360", "-script", toolpath_script_path]) # Adjust with how you run scripts in Fusion 360
|
| 126 |
+
|
| 127 |
+
return "Toolpath generated successfully"
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
return f"Error generating toolpath: {str(e)}"
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
if __name__ == '__main__':
|
| 134 |
+
app.run(debug=True)
|