Reputation: 13
I'm trying to grab a file property (like "Name, Date modified", "Type", "Size", etc...) from a file in python. The property is called "SW Last saved with" (click for example). This property tells you what version of Solidworks a model was saved with.
After some research, it appears that this "SW last saved with" property was added to Windows Explorer via registering a .DLL file (sldwinshellextu.dll).
Is there a way to grab this specific file property using some python function like (file.getProperty("SW last saved with"))?
Upvotes: 0
Views: 954
Reputation: 13
So I figured out a way to do this with the help of another post I found:
import subprocess
newCOMObjectTxt = ("$path = 'PATH_TO_SLDPRT_FILE';"
"$shell = New-Object -COMObject Shell.Application;"
"$folder = Split-Path $path;"
"$file = Split-Path $path -Leaf;"
"$shellfolder= $shell.Namespace($folder);"
"$shellfile = $shellfolder.ParseName($file);")
swLastSavedWithIdx = None
swFindLastSavedWithProp = subprocess.Popen (["powershell.exe", newCOMObjectTxt + \
"0..500 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_)}"],
stdout = subprocess.PIPE)
while True:
line = swFindLastSavedWithProp.stdout.readline()
if b"SW Last saved with" in line:
swLastSavedWithIdx = int(line.split()[0])
break
if not line:
break
swLastSaveWithVersion = subprocess.Popen (["powershell.exe", newCOMObjectTxt + \
"$shellfolder.GetDetailsOf($shellfile, %i)" %swLastSavedWithIdx], stdout = subprocess.PIPE)
ver = str(swLastSaveWithVersion.stdout.readline(),'utf-8').strip()
Basically I found that you can get all file properties via a few Windows Powershell commands. We write a few quick commands in powershell using subprocess.Popen(), then PIPE from STDOUT.
Upvotes: 1
Reputation: 395
To access the file properties or some other information from a SOLIDWORKS file without opening the file directly in SW you can use the "SwDocumentManager.dll". With this API you can use "GetDocument()" to access the specific file and get an ISwDMDocument Object. Use this to read the property "LastSavedDate" to get your Information.
Object Information for the ISwDMDocument Object: https://help.solidworks.com/2020/English/api/swdocmgrapi/SolidWorks.Interop.swdocumentmgr~SolidWorks.Interop.swdocumentmgr.ISwDMDocument_members.html
General Information about how to use the SwDocumentManager API: https://help.solidworks.com/2020/English/api/swdocmgrapi/HelpViewerDS.aspx?version=2020&prod=api&lang=English&path=swdocmgrapi%2fGettingStarted-swdocmgrapi.html&id=74236490007f4b5eb5ba233479f1e707
But I dont know anything about python and how to use it in this language. But maybe I could point you in some direction.
Upvotes: 2