create PDF to external hard drive

create PDF to external hard drive

MBJ1975
Contributor Contributor
352 Views
6 Replies
Message 1 of 7

create PDF to external hard drive

MBJ1975
Contributor
Contributor

Hi everybody.

 

I would like to have a little bit of help with my rule.

I have my working folder (WF) on local drive C:\WF\9001-9500\9205 Vessel. And I have a server G:\SAGS-BIBLIOTEK\9205 Vessel. I'm looking for a rule to indificate 9205 Vessel onto the G:\SAGS-BIBLIOTEK

The two folders on both drives has the same name. Is it possible to make a rule that looks for the same name, by searching into Inventor "Project" "Part number (first 4-5 numbers) and look into the G: drive and find the same numbers and place the PDF into that folder.

So when I start a new project with a new case number (Ex. 11410) it will place a PDF drawing both to my working folder and into Ex. 11410 on the G: drive.

Does my quistions make any sence?


'------start of iLogic-------
'save a PDF to 2 different folders
Workspace = ThisDoc.WorkspacePath()
WorkspcePathLenght = Len(WorkspacePath)
oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
DirectoryPath = DirectoryPath
PDFPath = DirectoryPath
'Folder 1
oFolder1 = oPath

'Folder 2. Need a rule to indified oFolder2 with the same numbers as "Project" "Part number" (first 4-5 numbers)
'so oFolder 2 will be saved into "G:\SAGS-BIBLIOTEK\...Same number as "Part number" (first 4-5 numbers)
oFolder2 = "G:\SAGS-BIBLIOTEK\11336 Desmet Ballestra PT 130x1450 x2450 x 0.8\Tegninger"

'get the filename of the drawing
oFileName = ThisDoc.FileName(False) 'without extension

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium


'Check for the PDF folder 1 and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder1) Then
System.IO.Directory.CreateDirectory(oFolder1)
End If

'Check for the PDF folder 2 and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder2) Then
System.IO.Directory.CreateDirectory(oFolder2)
End If

'Set the PDF target filename for folder 1
oDataMedium.FileName = oPath & "\" & oFileName & "_" & "Rev" &"_" & oRevNum & ".pdf"
'Publish the first document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)


'Set the PDF target filename for folder 2
oDataMedium.FileName = oPath & "\" & oFileName & "_" & "Rev" &"_" & oRevNum & ".pdf"
'Publish the second document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

'------end of iLogic-------

 

0 Likes
353 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @MBJ1975.  I am not sure if this little trick will work OK for you or not, but below is some example iLogic code you could use within your code.  It will look at the name of the last folder in the path of the active document, and adds that folder name to the end of the base path of your second drive, to specify the sub folder that you want the other PDF to go.  Then it checks to see if that folder actually exists in the file system or not.  If it does not exist yet, it will create that folder, because that folder must exist before you can export a PDF into it.

'get name of last folder in a file path, where sPath variable is a file path
Dim sPathFolderName As String = System.IO.Path.GetDirectoryName(oPath)
'add that folder name after your base Folder2 path, to specify where you want the other PDF to go
Dim sFolder2 As String = "G:\SAGS-BIBLIOTEK\" & sPathFolderName
'now check to see if that folder exists yet
If System.IO.Directory.Exists(sFolder2) = False Then
	'if that folder does not exist yet, then create that folder
	System.IO.Directory.CreateDirectory(sFolder2)
End If
'now use the 'sFolder2' variable to specify what folder to save the other PDF  into

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

MBJ1975
Contributor
Contributor

Thanks for your replay.

I am not sure where I should place this into my rule.

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

Hi @MBJ1975.  This time I copied your whole code into a new, empty rule on my end, then deleted some seemingly unused stuff, and deleted several lines of code that were present in your rule in two different places unnecessarily, then inserted my block of code, and changed the variable names being used in my block of code to match your variable names.  Then I tweaked the 'oDataMedium' lines near the end, to use the 'oFolder1' and 'oFolder2' variables, instead of the 'oPath' variable.

 

'------start of iLogic-------
'save a PDF to 2 different folders
oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

'Folder 1
oFolder1 = oPath
oFolder2 = "G:\SAGS-BIBLIOTEK\"

'<<<< INSERTED CODE >>>>
'get name of last folder in a file path, where sPath variable is a file path
Dim sPathFolderName As String = System.IO.Path.GetDirectoryName(oPath)
'add that folder name after your base Folder2 path, to specify where you want the other PDF to go
oFolder2 = oFolder2 & sPathFolderName
'now check to see if that folder exists yet
If System.IO.Directory.Exists(oFolder2) = False Then
	'if that folder does not exist yet, then create that folder
	System.IO.Directory.CreateDirectory(oFolder2)
End If
'<<<< END OF INSERTED CODE >>>>

'Set the PDF target filename for folder 1
oDataMedium.FileName = oFolder1 & "\" & oFileName & "_" & "Rev" &"_" & oRevNum & ".pdf"
'Publish the first document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)


'Set the PDF target filename for folder 2
oDataMedium.FileName = oFolder2 & "\" & oFileName & "_" & "Rev" &"_" & oRevNum & ".pdf"
'Publish the second document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

'------end of iLogic-------

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7

MBJ1975
Contributor
Contributor

Thanks alot.

Unfortunately the PDF file to G:\SAGS-BIBLIOTEK are not been placed in the folder, but it is placed into the G:\SAGS-BIBLIOTEK. The rule "INSERTED CODE" dos not find the correct folder. Will it be better if oFolder2 will try to find the 4-5 numbers (Case no.)?

0 Likes
Message 6 of 7

MBJ1975
Contributor
Contributor

I have now made a new folder onto the C-drive named 55555-TEST. The drawing name is 55555-0000

 

I have made a new simple rule that almost works. The rule is creating a folder onto the G-drive, but it is not using the folder name from the C-drive (55555-TEST). The folder name on the G-drive are part number (55555-0000) and not the folder name (55555-TEST). Can you help me? what is wrong? 

 

Dim FolderName As String = System.IO.Path.GetDirectoryName(oPath)
oPath = ThisDoc.Path
'folder path
Dim oFolder2 As String

oFolder2 = "G:\SAGS-BIBLIOTEK\" & FolderName & ThisDoc.FileName(False) & "\Test\"

If Not System.IO.Directory.Exists(oFolder2) Then
	'create folder
	System.IO.Directory.CreateDirectory(oFolder2)

Else

End If

Dim fileName As String = oFolder2 & ThisDoc.FileName(False) & ".pdf"
0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @MBJ1975.  Sorry for not responding more.  I had a lot to do at work the last few days, so only responding very little on the forums to help others, as time permitted, then I almost forgot about this topic.  I made a mistake in the last code I posted, because I forgot that the GetDirectoryName method returns the whole path of a file, not just the name of the last folder in that path.  There are two different lines of code that I could have used there, instead of that one in Line 20 of the last code I posted.

So...this line of code:

 

Dim sPathFolderName As String = System.IO.Path.GetDirectoryName(oPath)

 

...should have been replaced by one of the following lines of code instead, and that should have fixed the issue.

 

Dim sPathFolderName As String = New System.IO.DirectoryInfo(oPath).Parent.Name

 

or 

 

Dim sPathFolderName As String = System.IO.Path.GetDirectoryName(oPath).Split("\").Last

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes