automationtools / app.py
karthikmn's picture
Create app.py
201f8be verified
import win32com.client # For SolidWorks API
import pyansys # For ANSYS APDL integration
import os
import numpy as np
import pandas as pd
# SolidWorks Automation
def create_solidworks_model(material, thickness, length, width):
swApp = win32com.client.Dispatch("SldWorks.Application")
swApp.Visible = True
model = swApp.NewDocument("C:\\ProgramData\\SOLIDWORKS\\SOLIDWORKS 2020\\templates\\part.prtdot", 0, 0, 0)
swModel = swApp.ActiveDoc
swPart = swModel.Extension.SelectByID2('Face1', 'FACE', 0, 0, 0, False, 0, None, 0)
# Create a rectangle for the part (simple example)
swModel.CreateRectangle2(length, width, thickness)
# Save the model
model.SaveAs(f"C:\\Users\\OM\\Desktop\\press_tool_{material}.SLDPRT")
# ANSYS APDL Automation
def run_ansys_simulation(model_file):
# Initialize ANSYS API using pyansys
solution = pyansys.Mapdl()
# Define material properties (for example: steel)
solution.prep7()
solution.mp('EX', 1, 2.1e11) # Young's modulus in Pa
solution.mp('NUXY', 1, 0.3) # Poisson's ratio
# Import the model created by SolidWorks
solution.import_model(model_file)
# Set boundary conditions, loads, and run the simulation
solution.solve()
# Extract results (e.g., stress, displacement)
stress_results = solution.post_processing.stress()
# Return results as a dataframe for easy analysis
result_data = pd.DataFrame(stress_results)
return result_data
# Main execution
def main():
# Example parameters for a tool
material = 'steel'
thickness = 5 # mm
length = 100 # mm
width = 50 # mm
# Create model in SolidWorks
create_solidworks_model(material, thickness, length, width)
# Path to the model file
model_file = f"C:\\Users\\OM\\Desktop\\press_tool_{material}.SLDPRT"
# Run simulation in ANSYS
results = run_ansys_simulation(model_file)
# Output results
results.to_excel(f"C:\\Users\\OM\\Desktop\\simulation_results.xlsx")
print("Simulation completed. Results saved.")
if __name__ == "__main__":
main()