Save PDF to PDM

Library for macros
arild80
Posts: 15
Joined: Tue Jan 16, 2024 3:48 am
Answers: 1
x 2
x 2

Save PDF to PDM

Unread post by arild80 »

Hi
I am realy bad at macro since i dont know VB.
I have a code that save PDF into folder in PDM (if folder is created)
I want to tweak this code a bit. To get the 8 first letters\numbers of the file name it looks into.

If fil name is named AB123456_01.assy i want to get 8 first didigts AB123456 as the folder name to look for, and the PDF name to be AB123455_01
The code we use is like this

Annyone who can edit the code for me ? :-)


Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc


Dim swModel As SldWorks.ModelDoc2
Dim sFileName As String

Set swModel = swApp.ActiveDoc
' Zoom To Fit
Part.ViewZoomtofit2

' Save As
sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)
'sFileName = swModel.GetTitle
Debug.Print sFileName
longstatus = Part.SaveAs3("C:\Nordic Steel PDM\Varenummer\" + sFileName & "\" + sFileName + ".pdf", 0, 0)
End Sub
by gupta9665 » Tue Jan 30, 2024 2:41 am
arild80 wrote: Tue Jan 30, 2024 2:21 am Would it be easy to do the same with STEP file?
Yes.

Change

Code: Select all

sFileName = PDMPath + sFolderName & "\" + sFileName + ".PDF"
to

Code: Select all

sFileName = PDMPath + sFolderName & "\" + sFileName + ".STEP"
and change

Code: Select all

swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Nothing, nErrors, nWarnings
to

Code: Select all

swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, Nothing, nErrors, nWarnings
Go to full post
User avatar
gupta9665
Posts: 420
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 438
x 459

Re: Save PDF to PDM

Unread post by gupta9665 »

Try these codes.

Code: Select all

Option Explicit

Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim sFolderName     As String
Dim sFileName       As String
Dim nErrors         As Long
Dim nWarnings       As Long
Const PDMPath       As String = "C:\Nordic Steel PDM\Varenummer\"

Sub Main()

  Set swApp = Application.SldWorks
  Set swModel = swApp.ActiveDoc

  If swModel Is Nothing Then
    MsgBox "No active document.", vbExclamation
    Exit Sub
  End If
  
  
  sFolderName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
  sFolderName = Left(sFolderName, InStrRev(sFolderName, ".") - 1)
  sFolderName = Left(sFolderName, 8)
  
  sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
  sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)
  sFileName = PDMPath + sFolderName & "\" + sFileName + ".PDF"
  
  swModel.ViewZoomtofit2
  Set swExportPDFData = swApp.GetExportFileData(1)
  
  swExportPDFData.SetSheets swExportData_ExportAllSheets, ""
  swExportPDFData.ViewPdfAfterSaving = False

  
  swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Nothing, nErrors, nWarnings
  
End Sub
Deepak Gupta
SOLIDWORKS Consultant/Blogger
arild80
Posts: 15
Joined: Tue Jan 16, 2024 3:48 am
Answers: 1
x 2
x 2

Re: Save PDF to PDM

Unread post by arild80 »

Love it Gupta. Thank you so much :-D
User avatar
SPerman
Posts: 2080
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2256
x 1902
Contact:

Re: Save PDF to PDM

Unread post by SPerman »

As another option, here is how I parse the filename and filepath.

Code: Select all

'this is the full name and path
sModelPath = swModel.GetPathName

'get filename from full path
CropPos = InStrRev(sModelPath, "\")
sNewName = Right(sModelPath, (Len(sModelPath) - CropPos))

'strip sldprt extension
sNewName = Left(sNewName, Len(sNewName) - 7)

'get path
sNewPath = Left(sModelPath, CropPos - 1)
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
arild80
Posts: 15
Joined: Tue Jan 16, 2024 3:48 am
Answers: 1
x 2
x 2

Re: Save PDF to PDM

Unread post by arild80 »

Would it be easy to do the same with STEP file?
User avatar
gupta9665
Posts: 420
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 438
x 459

Re: Save PDF to PDM

Unread post by gupta9665 »

arild80 wrote: Tue Jan 30, 2024 2:21 am Would it be easy to do the same with STEP file?
Yes.

Change

Code: Select all

sFileName = PDMPath + sFolderName & "\" + sFileName + ".PDF"
to

Code: Select all

sFileName = PDMPath + sFolderName & "\" + sFileName + ".STEP"
and change

Code: Select all

swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Nothing, nErrors, nWarnings
to

Code: Select all

swModel.Extension.SaveAs3 sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, Nothing, nErrors, nWarnings
Deepak Gupta
SOLIDWORKS Consultant/Blogger
sloworks
Posts: 31
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 6
x 19
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

Just an idea...you could also do a check-in for the exported files...PDM has quite nice support for VBA
BR
Markku
www.sloworks.fi
User avatar
gupta9665
Posts: 420
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 438
x 459

Re: Save PDF to PDM

Unread post by gupta9665 »

sloworks wrote: Thu Feb 22, 2024 11:18 am Just an idea...you could also do a check-in for the exported files...PDM has quite nice support for VBA
I'm trying to check-in an exported PDF file (which is saved locally into the vault) but it is not working. Can you please guide? Thank you.

Here are the codes I'm trying to use

Code: Select all

Dim bRetval As Boolean
bRetval = model.Extension.SaveAs(OutputfileName, 0, 0, swExportPDFData, 0, 0)

Dim vault As New EdmVault5
vault.LoginAuto("VaultName", 0)

If vault.IsLoggedIn Then
	vault.RefreshFolder(outputfolderPath)	
	Dim afile As IEdmFile5 
	afile = vault.GetFileFromPath(OutputfileName)
	afile.UnlockFile(0, "", 0)

End If
The OutputfileName is the PDF file full name inside the vault folder. Also the PDF does not show up unless vault is refreshed manually. Using RefreshFolder doesn't work either. The aFile is always nothing,.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
sloworks
Posts: 31
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 6
x 19
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

BR
Markku
www.sloworks.fi
User avatar
gupta9665
Posts: 420
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 438
x 459

Re: Save PDF to PDM

Unread post by gupta9665 »

Thank you for sharing the video, I have already checked and in the video the codes works OK because file is already there in the vault and visible. In my case, it show up only after refreshing manually. And I believe this is the reason that file is always nothing.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
sloworks
Posts: 31
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 6
x 19
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

BR
Markku
www.sloworks.fi
User avatar
gupta9665
Posts: 420
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 438
x 459

Re: Save PDF to PDM

Unread post by gupta9665 »

Thank you I'll check these out.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
CarrieIves
Posts: 169
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 388
x 144

Re: Save PDF to PDM

Unread post by CarrieIves »

When we save a PDF into a PDM folder it automatically adds to the vault based on our settings in PDM for adding file types. We only have PDM Standard so can't do PDM macros, but our save as PDF SolidWorks macro, puts the PDF in the right folder. The setting adds it. We then just have to check it in.
User avatar
gupta9665
Posts: 420
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 438
x 459

Re: Save PDF to PDM

Unread post by gupta9665 »

CarrieIves wrote: Mon Feb 26, 2024 2:52 pm When we save a PDF into a PDM folder it automatically adds to the vault based on our settings in PDM for adding file types. We only have PDM Standard so can't do PDM macros, but our save as PDF SolidWorks macro, puts the PDF in the right folder. The setting adds it. We then just have to check it in.
Even for PDM professional, the add-in is putting the file in right location but PDF does not show until vault folder is refreshed. And adding the file to vault via API is not working.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
sloworks
Posts: 31
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 6
x 19
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

BR
Markku
www.sloworks.fi
hugozanga2024
Posts: 2
Joined: Thu Aug 08, 2024 7:54 pm
Answers: 0
x 1

Re: Save PDF to PDM

Unread post by hugozanga2024 »

Thanks for all the insights shared here! I've been following the discussion closely, and I'm trying to implement a similar solution for saving PDFs and STEP files to the PDM.

Thanks to Gupta's code and suggestions, I've managed to adapt it to my needs, especially with the folder naming based on the first 8 characters of the filename.

However, I'm running into a small issue when trying to check-in the exported files into the PDM. Like sloworks mentioned, I'm struggling with making the files visible in the vault without having to manually refresh. Even after using the vault.RefreshFolder method, the files still don’t show up until I manually refresh the folder, and the file object (aFile) is always returning as nothing.

Has anyone experienced this issue before or found a workaround? I saw the links provided by sloworks, and I'm about to try those methods, but any additional tips would be greatly appreciated!

Thanks in advance for your help!
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1410

Re: Save PDF to PDM

Unread post by bnemec »

Welcome to CADForum.net @hugozanga2024

If you haven't already, please stop by the forum rules section to read through them, matt is our admin and as you can see in the "couple of rules added" matt is human.

Maybe stop by the welcome forum viewforum.php?f=28 and say hi, introduce yourself a just a little.

Onto my point. The thread you responded to is a few months cold and more importantly is checked as solved. It's probably best to start a new thread for your question. To help us out link back to the threads that you have searched and found helpful then maybe call out the exception of why that didn't quite solve your problem or make clear.
Post Reply