Opening solidworks part using python and solidworks api

1.1k Views Asked by At

I am trying to open a solidworks file using python and the solidworks api. I dont seem to understand the solidworks OpenDoc6 documentation. The last line in the following does not work and I get an AttributeError: swDocPART

Does anyone happen to know what is wrong with my code?

import win32com.client

# part path and name
filePath = r'\Part1.SLDPRT'

# Connect to SolidWorks and open the document
swApp = win32com.client.Dispatch('SldWorks.Application')

if swApp is None:
    print("Error: Could not connect to SolidWorks")
else:
    print("Success: Connected to SolidWorks")

# Open the SolidWorks document using OpenDoc6
swDoc = swApp.OpenDoc6(filePath, win32com.client.constants.swDocPART, 0, "", 0, 0)
1

There are 1 best solutions below

0
On

I am not familiar enough with Python and how it leverages the SolidWorks API libraries, specifically Enums. However, with .NET we have to convert the enum to an int (ie. (int)swDocumentType.swDocPART). In the case of swDocPART, the enum value is 1.

The document type enums can be found here - https://help.solidworks.com/2019/english/api/swconst/SOLIDWORKS.Interop.swconst~SOLIDWORKS.Interop.swconst.swDocumentTypes_e.html