This script runs, the task exits, and the task manager says it executed successfully. But it doesn't save the open drawing as a pdf. It opens it, it does all the pomp and circumstance, but when it runs the Extension.SaveAs command nothing actually happens. I tried changing visibility from False to True, neither helps. Using ModelDoc2.SaveAs3 works but is antiquated and doesn't appear to be able to force silent saving (so the pdf's open after being saved). Any thoughts? Thanks!
<p>Option Explicit</p><p>Sub main()</p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swCustPrpMgr As SldWorks.CustomPropertyManager</p><p>Dim longstatus As Long</p><p>Dim longwarnings As Long</p><p>Dim sFileName As String</p><p>Dim Path As String</p><p>Dim FileName As String</p><p>Dim Value As String</p><p>Dim DwgRev As String</p><p>Dim Success As Object</p><p>Dim swExtension As SldWorks.ModelDocExtension</p><p>Dim Errors As Long</p><p>Dim Warnings As Long</p><p>Dim vSheetName As Variant</p><p>Dim swExportPDFData As SldWorks.ExportPdfData</p><p></p><p>On Error Resume Next</p><p></p><p>Set swApp = Application.SldWorks</p><p>swApp.Visible = True</p><p></p><p>sFileName = "<Filepath>"</p><p></p><p> Set swModel = swApp.OpenDoc6(sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings)</p><p> Set swModel = swApp.ActiveDoc</p><p> </p><p> swModel.ViewZoomtofit2</p><p> </p><p> DwgRev = swModel.GetCustomInfoValue("", "Revision")</p><p> </p><p> If DwgRev = "?" Or DwgRev = "" Or DwgRev = "-" Then</p><p> DwgRev = "--"</p><p> End If</p><p></p><p> vSheetName = swModel.GetSheetNames</p><p> Set swExportPDFData = swApp.GetExportFileData(1)</p><p></p><p> FileName = Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "_" & DwgRev & ".PDF"</p><p> 'swModel.SaveAs3 "" & FileName & "", 0, 0</p><p> swExtension = swModel.Extensiona</p><p> Success = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, vSheetName)</p><p> swExportPDFData.ViewPdfAfterSaving = True</p><p> 'Success = swExtension.SaveAs(FileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Errors, Warnings)</p><p> Success = swExtension.SaveAs(FileName, 0, 0, swExportPDFData, Errors, Warnings)</p><p> swApp.QuitDoc swModel.GetTitle</p><p> Set swModel = Nothing</p><p></p><p>End Sub</p>SolidworksApi/macros
Print :
FileName
Errors
Warnings
and let's see those values.
Best,
Amen Jlili
SOLIDWORKS API / PDM API Developer
www.cadsharp.com
Nice catch! I think I moved on to the default PDM macro but I'm sure that was the problem. We will never know but I'll mark this as solved because I'm sure that was it.
Thanks!
whats that?
Thanks Dmitry. I have done this, that's how I know what the error is and other variables. Anyway, we'll see. Thanks!
<pre __jive_macro_name="quote" class="jive_macro_quote jive_text_macro"><pre class="plain" style="display: none;">Option Explicit Sub main() Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swCustPrpMgr As SldWorks.CustomPropertyManager Dim longstatus As Long Dim longwarnings As Long Dim sFileName As String Dim Path As String Dim FileName As String Dim Value As String Dim DwgRev As String Dim Success As Object Dim swExtension As SldWorks.ModelDocExtension Dim Errors As Long Dim Warnings As Long Dim vSheetName As Variant Dim swExportPDFData As SldWorks.ExportPdfData On Error Resume Next Set swApp = Application.SldWorks swApp.Visible = True sFileName = "<Filepath>" Set swModel = swApp.OpenDoc6(sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings) Set swModel = swApp.ActiveDoc swModel.ViewZoomtofit2 DwgRev = swModel.GetCustomInfoValue("", "Revision") If DwgRev = "?" Or DwgRev = "" Or DwgRev = "-" Then DwgRev = "--" End If vSheetName = swModel.GetSheetNames Set swExportPDFData = swApp.GetExportFileData(1) FileName = Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "_" & DwgRev & ".PDF" 'swModel.SaveAs3 "" & FileName & "", 0, 0 swExtension = swModel.Extensiona Success = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, vSheetName) swExportPDFData.ViewPdfAfterSaving = True 'Success = swExtension.SaveAs(FileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Errors, Warnings) Success = swExtension.SaveAs(FileName, 0, 0, swExportPDFData, Errors, Warnings) swApp.QuitDoc swModel.GetTitle Set swModel = Nothing End SubDrew,
Place a "stop" at the beginning of the sub and run it step by step to see where the task craps out on you. Probably before "OnErrorResumeNext". You can highlight the variables and see if the tooltip shows any variable values (note that the task add-in debugger isn't very powerful, but you can at least see if your variables are getting populated).
apisupport@solidworks.com
Interesting. The link you provide talks of not being able to overwrite a file, but when I run the task it doesn't even generate a file! Alas.
I think this is a cache problem.
Check this out: S-053733 - How to update the convert task to delete output file.pdf - pCloud
I tried running the script in Solidworks and it works fine. It doesn't work in the PDM task. That's something!
I tried running the script in Solidworks and it works fine. It doesn't work in the PDM task. That's something!
We are on 2017 task add-in. Just checked.
Yeah. And when I use SaveAs3 or SaveAs4 it saves and shows up as a local file in the vault, then the vault auto-adds it as vaulted. But this newer method isn't working at all...
Interesting. Did you refresh the save folder after executing the task?
Thanks for the info on 4.
I have that info PDF and am willing to do that but this code is much simpler and works just fine, I just need to find a way to save the pdfs with swModel.SaveAs4 or something else that actually works but still doesn't open the pdf after saving!
4. It's recommended to upgrade AddIns after upgrade, let look for instruction how to do that: 2018 SOLIDWORKS PDM Help - Performing the SWTaskAddin Upgrade (For SOLIDWORKS PDM Professional Only)
Oh, so you just want to add revision in the PDF Output File Name, yes?
Then let's a look for S-054527 in Knowledge Base, you can find there instruction step by step how to modify default convert task to add variable (like revision) into output file name.
Let me know if it is what you want!
BR,
Kamil
Currently my settings are as below. I should be seeing the PDF. Other local files show up...
Ok. So it saved correctly. The problem I reckon that it is saved as a local file. Eventually you will need to add it to the vault. Can you make sure your vault user's settings are set to view local files?
Hey Kamil, thanks.
1. Script. The default doesn't consistently add the revision to the pdf file name.
2. More than one. Running the script as SW 2015.
3. 2017
4. Not sure. I'll have to look into this. You have to upgrade it separately?
5. No warnings pop up in SW.
6. I will look but it is saving the file to the same path as the drawing based on the FileName variable.
Thanks again!
Hi Drew!
A few questions to help find a clue what is wrong:
1. Are you just using default PDM convertion task or are you writing your script to convert drawings on PDFs? The script which you upload doesn't look like default convertion task so may let's try to use them?
2. Do you have only one SW installation or more? Like SW 2019 and SW 2017? If yes, just check if
3. What PDM version are you using?
4. If you were upgrading PDM vault, did you upgrade SWTaskAddin too?
5. If you are openning a drawing which you want to convert, the some warnings are poping up in SW? If yes, just try to remove them to won't show next time.
6. May PDF is created on local disc but it's not added to vault? Could you check if there is no PDF locally?
Let me know your observation!
Best Regards,
Kamil Wilkosz
Thanks Amen,
FileName = "C:\Vault\Data\P001\1001\MODELS\DWG\Z000.PDF"
Errors = 0
Warnings = 0