Search code examples
pythonvbasolidworks

Running a SolidWorks macro from Python


I write a txt file on Python which contains the coordinates of the control points, which is then read by SolidWorks.

I have written a macro on SolidWorks to save the new modified STL file.

import numpy as np 
import os
def GenerateCoordinates(low,high,size):
    X=np.random.randint(low,high,size)
    Y=np.random.randint(low,high,size)
    #Z=np.random.randint(low,high,size)
    return X,Y
X=GenerateCoordinates(0,6,7)[0]
Y=GenerateCoordinates(0,4,7)[1]
k=0
sketch_number=1 #Generate coordinates
g=open('Cdd.txt','w')
for i in range(1,len(X)):
    g.write('CoordinatesX'+str(i)+'='+str(X[i])+'\n')
    g.write('"D'+str(k)+'@Sketch'+str(sketch_number)+'"'+'=CoordinatesX'+str(i)+'\n')
    k+=1
    g.write('CoordinatesY'+str(i)+'='+str(Y[i])+'\n')
    g.write('"D'+str(k)+'@Sketch'+str(sketch_number)+'"'+'=CoordinatesY'+str(i)+'\n')
    k+=1
    #g.write('CoordinatesZ'+str(k)+'='+str(Z[i])+'\n')
    #g.write('D'+str(k)+'@Sketch'+str(sketch_number)+'=CoordinatesZ'+str(k)+'\n')
g.close() #writes coordinates in a txt file then saves the txt file

os.popen('"C:/Users/Public/Desktop/Program Files/SOLIDWORKS Corp/SOLIDWORKS/.exe"') #I want to call the macro that rebuilds the solidworks part with the modified coordinates.

How do I run the macro from Python, to import the new file into Python?


Solution

  • Update in case someone bumps into the same problem: the \m should not be included in the brackets. So for my example, it would be: os.popen('"C:/Users/Public/Desktop/Program Files/SOLIDWORKS Corp/SOLIDWORKS/.exe" \m "Path to the macro"'). However, this just opens the macro without unfortunately running it.