2

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?

5
  • 1
    try to add sldw.NewDocument(r'C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\lang\english\Tutorial\part.prtdot', 0, 0, 0) after line with Dispatch Commented Aug 6, 2019 at 16:39
  • @VladyslavLitunovsky I tried that, I should have mentioned that I had Solidworks open with a new part document open in it. Interestingly running the Python code line by line in the editor, it is fine up till Part = sldw.ActiveDoc but Part.InsertCurveFile("Generic Filepath\Points.txt") returns False. I'll update my question. Commented Aug 6, 2019 at 17:19
  • Try Part.InsertCurveFile(r"Generic Filepath\Points.txt") or Part.InsertCurveFile("Generic Filepath/Points.txt") Commented Aug 7, 2019 at 3:24
  • Neither of those made a difference. I'm wondering if its a permissions issue with the file? Commented Aug 7, 2019 at 6:10
  • I think I've got it to work using Part.InsertCurveFile("Generic Filepath/Points.txt"). It was an issue with the filename. I've got it working now. Commented Aug 7, 2019 at 6:24

1 Answer 1

5

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.