Darth Vader
Darth Vader

Reputation: 161

Solidworks macro implementation not working with Python

I've got a macro (see below) that will load in a curve using xyz points from a .txt file into Solidworks. To be clear this gives the desired output.

Dim swApp As Object
Dim Part As Object

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
Part.InsertCurveFile("Generic Filepath\Points.txt")

End Sub

I'm trying to get the same macro to run from Python using this implementation:

import win32com.client

sldw = win32com.client.Dispatch('SldWorks.Application')
sldw.NewDocument("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\lang\english\Tutorial\part.prdot", 0, 0, 0)  

Part = sldw.ActiveDoc
Part.InsertCurveFile("Generic Filepath\Points.txt")  

#Cleanup the com reference. 
del sldw

I know I can run Solidworks macros from Python as I have got some tests to work. When I run the macro Python does not output any kind of error message and nor does it generate anything in Solidworks.

Running the Python code line by line in the editor line Part.InsertCurveFile("Generic Filepath\Points.txt") returns False.

With Solidworks open and a part document open the following works:

import win32com.client
import pythoncom
pythoncom.CoInitialize ()

sldw = win32com.client.GetObject (Class='SldWorks.Application')
Part = sldw.ActiveDoc
Part.InsertCurveFileBegin()
Part.InsertCurveFilePoint(0, 0, 0)
Part.InsertCurveFilePoint(0, 1, 1)
Part.InsertCurveFilePoint(1, 1, 1)
Part.InsertCurveFileEnd()

How do I make the Python implementation work?

Upvotes: 2

Views: 1183

Answers (1)

Vladyslav Litunovsky
Vladyslav Litunovsky

Reputation: 546

It looks the problem with \ (backslash). Replace it on / or declare string as raw (with and r in front of it) r"Generic Filepath\Points.txt"

Upvotes: 5

Related Questions