Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import subprocess
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
|
@@ -8,6 +9,18 @@ app = Flask(__name__)
|
|
| 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:
|
|
@@ -26,7 +39,7 @@ def generate_model():
|
|
| 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:
|
|
@@ -34,101 +47,35 @@ def generate_toolpath():
|
|
| 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
|
| 40 |
-
result = generate_cnc_toolpath(tool_diameter, feed_rate
|
| 41 |
|
| 42 |
-
# If
|
| 43 |
-
return jsonify({"status": "success", "message": result, "
|
| 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 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 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
|
| 94 |
try:
|
| 95 |
-
#
|
| 96 |
-
|
| 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 |
-
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import subprocess
|
| 3 |
+
import sys
|
| 4 |
import os
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
|
|
|
| 9 |
SCRIPT_PATH = 'path_to_your_fusion_360_scripts_folder' # Change this to your actual script path
|
| 10 |
OUTPUT_PATH = 'output'
|
| 11 |
|
| 12 |
+
# Function to install platform-specific dependencies
|
| 13 |
+
def install_platform_specific_dependencies():
|
| 14 |
+
if sys.platform == "win32": # Windows-specific dependencies
|
| 15 |
+
try:
|
| 16 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "pywin32==303"])
|
| 17 |
+
print("Successfully installed pywin32 on Windows.")
|
| 18 |
+
except subprocess.CalledProcessError as e:
|
| 19 |
+
print(f"Error installing pywin32: {e}")
|
| 20 |
+
else:
|
| 21 |
+
print("Skipping pywin32 installation for non-Windows platform.")
|
| 22 |
+
|
| 23 |
+
# Endpoint to generate Fusion 360 3D model
|
| 24 |
@app.route('/generate_model', methods=['POST'])
|
| 25 |
def generate_model():
|
| 26 |
try:
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return jsonify({"status": "error", "message": str(e)}), 500
|
| 41 |
|
| 42 |
+
# Endpoint to generate toolpath for CNC
|
| 43 |
@app.route('/generate_toolpath', methods=['POST'])
|
| 44 |
def generate_toolpath():
|
| 45 |
try:
|
|
|
|
| 47 |
data = request.json
|
| 48 |
tool_diameter = data.get('tool_diameter', 10.0)
|
| 49 |
feed_rate = data.get('feed_rate', 100.0)
|
|
|
|
| 50 |
|
| 51 |
+
# Generate toolpath G-code using Fusion 360 API
|
| 52 |
+
result = generate_cnc_toolpath(tool_diameter, feed_rate)
|
| 53 |
|
| 54 |
+
# If successful, return success message and path to toolpath
|
| 55 |
+
return jsonify({"status": "success", "message": result, "output_path": OUTPUT_PATH}), 200
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
return jsonify({"status": "error", "message": str(e)}), 500
|
| 59 |
|
| 60 |
+
# Function to generate Fusion 360 model (example)
|
| 61 |
def generate_fusion_360_model(length, width, height):
|
| 62 |
try:
|
| 63 |
+
# Fusion 360 API calls here to create model
|
| 64 |
+
# Assuming you will add your Fusion 360 Python script logic here
|
| 65 |
+
print(f"Creating model with dimensions: {length}x{width}x{height}")
|
| 66 |
+
return "Model created successfully"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
return f"Error creating model: {str(e)}"
|
| 69 |
|
| 70 |
+
# Function to generate CNC toolpath
|
| 71 |
+
def generate_cnc_toolpath(tool_diameter, feed_rate):
|
| 72 |
try:
|
| 73 |
+
# Example logic for generating CNC toolpath (this can be enhanced)
|
| 74 |
+
print(f"Generating CNC toolpath with tool diameter: {tool_diameter} and feed rate: {feed_rate}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return "Toolpath generated successfully"
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return f"Error generating toolpath: {str(e)}"
|
| 78 |
|
|
|
|
| 79 |
if __name__ == '__main__':
|
| 80 |
+
install_platform_specific_dependencies() # Call the function to handle platform-specific installs
|
| 81 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|