Save PDF to PDM

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

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: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 11
Joined: Tue Jan 16, 2024 3:48 am
Answers: 0
x 2
x 1

Re: Save PDF to PDM

Unread post by arild80 »

Love it Gupta. Thank you so much :-D
User avatar
SPerman
Posts: 1834
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 13
x 2014
x 1688
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: 11
Joined: Tue Jan 16, 2024 3:48 am
Answers: 0
x 2
x 1

Re: Save PDF to PDM

Unread post by arild80 »

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

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: 24
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 5
x 17
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: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 24
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 5
x 17
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

BR
Markku
www.sloworks.fi
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 24
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 5
x 17
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

BR
Markku
www.sloworks.fi
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 133
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 313
x 112

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: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 24
Joined: Tue Mar 23, 2021 12:24 pm
Answers: 0
Location: Finland
x 5
x 17
Contact:

Re: Save PDF to PDM

Unread post by sloworks »

BR
Markku
www.sloworks.fi
Post Reply