Skip to content

Commit

Permalink
Rework Lifecycle.ps1 and sample PDF Job
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanNairz committed Oct 18, 2022
1 parent 31735b5 commit 00b934c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
31 changes: 21 additions & 10 deletions Files/powerEvents/Events/ErpService.Lifecycle.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,33 +117,44 @@ function RestrictItemRelease($items) {
Log -End
}

function StartSynchronizeJob($file)
{
# since Synchronize Properties gets triggered already by powerEvents, disable it in the Vault configuration!
Write-Host "Adding job 'Synchronize Properties' for file '$($file._Name)' to queue."
Add-VaultJob -Name "autodesk.vault.syncproperties" -Parameters @{
"FileVersionIds"=$file.Id;
"QueueCreateDwfJobOnCompletion"=$true} -Description "Synchronize properties of file: '$($file._Name)'"
}

Register-VaultEvent -EventName UpdateFileStates_Post -Action 'AddPdfJob'
function AddPdfJob($files, $successful) {
Log -Begin
if (-not $successful) { return }
$releasedFiles = @($files | Where-Object { $_._Extension -in $supportedPdfExtensions -and $_._ReleasedRevision -eq $true })
Write-Host "Found '$($releasedFiles.Count)' files which are valid to add a PDF job for!"
foreach ($file in $releasedFiles) {
# since Synchronize Properties gets triggered already by powerEvents, disable it in the Vault configuration!
Write-Host "Adding job 'Synchronize Properties' for file '$($file._Name)' to queue."
Add-VaultJob -Name "autodesk.vault.syncproperties" -Parameters @{
"FileVersionIds"=$file.Id;
"QueueCreateDwfJobOnCompletion"=$true} -Description "Synchronize properties of file: '$($file._Name)'"
foreach ($file in $releasedFiles) {
StartSynchronizeJob($file)
$jobType = "ErpService.Create.PDF"
Write-Host "Adding job '$jobType' for file '$($file._Name)' to queue."
Add-VaultJob -Name $jobType -Parameters @{ "EntityId" = $file.Id; "EntityClassId" = "FILE" } -Description "Create PDF for file '$($file._Name)' and upload to ERP system" -Priority 110
Add-VaultJob -Name $jobType -Parameters @{ "EntityId" = $file.Id; "EntityClassId" = "FILE"} -Description "Create PDF for file '$($file._Name)' and upload to ERP system" -Priority 110
}
Log -End
}

Register-VaultEvent -EventName UpdateItemStates_Post -Action 'AddItemPdfJob'
function AddItemPdfJob($items) {
Log -Begin
$releasedItems = @($items | Where-Object { $_._ReleasedRevision -eq $true})
foreach ($item in $releasedItems) {
$jobType = "Erp.Service.CreatePDFFromItem"
Write-Host "Adding job '$jobType' for item '$($item._Name)' to queue."
Add-VaultJob -Name $jobType -Parameters @{ "EntityId" = $item.Id; "EntityClassId" = "ITEM" } -Description "Create PDF for item '$($item._Name)' and upload to ERP system" -Priority 101
$attachedDrawings = Get-VaultItemAssociations -Number $item._Number
Write-Host "Found '$($attachedDrawings.Count)' files which are valid to add a PDF job for!"
foreach ($file in $attachedDrawings) {
StartSynchronizeJob($file)
$jobType = "ErpService.Create.PDF"
Write-Host "Adding job '$jobType' for file '$($file._Name)' to queue."
Add-VaultJob -Name $jobType -Parameters @{ "EntityId" = $file.Id; "EntityClassId" = "FILE"; "ItemNumber" = $item._Number} -Description "Create PDF for file '$($file._Name)' and upload to ERP system" -Priority 110
}
Log -End
}
Log -End
}
36 changes: 16 additions & 20 deletions Files/powerJobs/Jobs/ErpService.Create.PDF.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ $hidePDF = $false
$openReleasedDrawingsFast = $true
#endregion

$localPDFfileLocation = "$workingDirectory\$($file._Name).pdf"
$vaultPDFfileLocation = $file._EntityPath + "/" + (Split-Path -Leaf $localPDFfileLocation)

$logPath = Join-Path $env:LOCALAPPDATA "coolOrange\Projects\ErpService.Create.Pdf-Job.log"
Set-LogFilePath -Path $logPath

Expand All @@ -34,46 +31,45 @@ if ( @("idw", "dwg") -notcontains $file._Extension ) {
ConnectToConfiguredErpServer

$ipjVaultPath = $vault.DocumentService.GetInventorProjectFileLocation()
$localWorkspaceFolder = ($vaultConnection.WorkingFoldersManager.GetWorkingFolder("$/")).FullPath
$localIpjFile = (Save-VaultFile -File $ipjVaultPath -DownloadDirectory $localWorkspaceFolder)[0]
$workingDirectory = ($vaultConnection.WorkingFoldersManager.GetWorkingFolder("$/")).FullPath
$localIpjFile = (Save-VaultFile -File $ipjVaultPath -DownloadDirectory $workingDirectory)[0]

$fastOpen = $openReleasedDrawingsFast -and $file._ReleasedRevision
$downloadedFiles = Save-VaultFile -File $file._FullPath -ExcludeChildren:$fastOpen -ExcludeLibraryContents:$fastOpen
$file = $downloadedFiles | select -First 1
# InventorServer does not support all target & source formats, you can find all supportet formats here:
# https://doc.coolorange.com/projects/powerjobsprocessor/en/stable/jobprocessor/file_conversion/?highlight=InventorServer#supported-format-conversions"
$openResult = Open-Document -LocalFile $file.LocalPath -Options @{ "Project" = $localIpjFile.LocalPath; FastOpen = $fastOpen } -Application InventorServer
$openResult = Open-Document -LocalFile $file.LocalPath -Options @{ "Project" = $localIpjFile.LocalPath; FastOpen = $fastOpen } #-Application InventorServer

$localPDFfileLocation = "$workingDirectory\$($file._Name).pdf"
$vaultPDFfileLocation = $file._EntityPath + "/" + (Split-Path -Leaf $localPDFfileLocation)

if ($openResult) {
if ($openResult.Application.Name -like 'Inventor*') {
$configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF_2D.ini"
}
else {
$configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF.dwg"
}
$configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF.dwg"
}
$exportResult = Export-Document -Format 'PDF' -To $localPDFfileLocation -Options $configFile
if ($exportResult) {
if ($exportResult) {
$PDFfile = Add-VaultFile -From $localPDFfileLocation -To $vaultPDFfileLocation -FileClassification DesignVisualization -Hidden $hidePDF
$file = Update-VaultFile -File $file._FullPath -AddAttachments @($PDFfile._FullPath)

if($Job.ItemNumber)
{
# Make sure you have permissions to modify released items before you use this Job for items with lifecycle change
Update-VaultItem -Number $Job.ItemNumber -AddAttachments @($PDFfile._FullPath)
}

Log -Message "Uploading PDF file $($PDFfile._Name) to ERP system..."
$d = New-ERPObject -EntityType "Document"
$d.FileName = $PDFfile._Name
$d.Number = $file._PartNumber
$d.Description = $file._Description
$d.Description = $file._Description
$uploadPDFToErpResult = Add-ERPMedia -EntitySet "Documents" -Properties $d -ContentType "application/pdf" -File $localPDFfileLocation
}
$closeResult = Close-Document
}

Get-ChildItem -LiteralPath $workingDirectory -Recurse -File -ErrorAction SilentlyContinue `
| Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $workingDirectory -Recurse -Force -ErrorAction SilentlyContinue

Get-ChildItem -LiteralPath $localWorkspaceFolder -Recurse -File `
| Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $localWorkspaceFolder -Recurse -Force -ErrorAction SilentlyContinue

if (-not $openResult) {
throw("Failed to open document $($file.LocalPath)! Reason: $($openResult.Error.Message)")
}
Expand Down

0 comments on commit 00b934c

Please sign in to comment.