jithenderchoudary's picture
Update app.py
09b2e7e verified
from flask import Flask, request, jsonify
import subprocess
import sys
import os
app = Flask(__name__)
# Path to Fusion 360 script directory
SCRIPT_PATH = 'path_to_your_fusion_360_scripts_folder' # Change this to your actual script path
OUTPUT_PATH = 'output'
# Function to install platform-specific dependencies
def install_platform_specific_dependencies():
if sys.platform == "win32": # Windows-specific dependencies
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "pywin32==303"])
print("Successfully installed pywin32 on Windows.")
except subprocess.CalledProcessError as e:
print(f"Error installing pywin32: {e}")
else:
print("Skipping pywin32 installation for non-Windows platform.")
# Endpoint to generate Fusion 360 3D model
@app.route('/generate_model', methods=['POST'])
def generate_model():
try:
# Get the parameters from the request
data = request.json
length = data.get('length', 100.0)
width = data.get('width', 50.0)
height = data.get('height', 10.0)
# Generate the Fusion 360 model using the script
result = generate_fusion_360_model(length, width, height)
# If model generation is successful, return success
return jsonify({"status": "success", "message": result, "output_path": OUTPUT_PATH}), 200
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
# Endpoint to generate toolpath for CNC
@app.route('/generate_toolpath', methods=['POST'])
def generate_toolpath():
try:
# Get the parameters from the request (toolpath generation settings, etc.)
data = request.json
tool_diameter = data.get('tool_diameter', 10.0)
feed_rate = data.get('feed_rate', 100.0)
# Generate toolpath G-code using Fusion 360 API
result = generate_cnc_toolpath(tool_diameter, feed_rate)
# If successful, return success message and path to toolpath
return jsonify({"status": "success", "message": result, "output_path": OUTPUT_PATH}), 200
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
# Function to generate Fusion 360 model (example)
def generate_fusion_360_model(length, width, height):
try:
# Fusion 360 API calls here to create model
# Assuming you will add your Fusion 360 Python script logic here
print(f"Creating model with dimensions: {length}x{width}x{height}")
return "Model created successfully"
except Exception as e:
return f"Error creating model: {str(e)}"
# Function to generate CNC toolpath
def generate_cnc_toolpath(tool_diameter, feed_rate):
try:
# Example logic for generating CNC toolpath (this can be enhanced)
print(f"Generating CNC toolpath with tool diameter: {tool_diameter} and feed rate: {feed_rate}")
return "Toolpath generated successfully"
except Exception as e:
return f"Error generating toolpath: {str(e)}"
if __name__ == '__main__':
install_platform_specific_dependencies() # Call the function to handle platform-specific installs
app.run(debug=True, host='0.0.0.0', port=5000)