Skip to content

Commit

Permalink
Merge pull request #231 from coolOrangeLabs/Rework_sampleJobs
Browse files Browse the repository at this point in the history
Rework sample jobs
  • Loading branch information
PatrickGrub authored Nov 14, 2022
2 parents d797d0f + b12c3af commit 8fe6f7c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 130 deletions.
49 changes: 30 additions & 19 deletions Files/powerEvents/Events/ErpService.Lifecycle.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ function Test-ErpItemAndBOMForVaultFileOrVaultItem {
#Attention!!! When changing inside this function please check if you have to change somthing in "Check-Boms" function, beacause the functions are similare
Log -Begin
$number = GetEntityNumber -Entity $Entity
if(-not $number) {
if (-not $number) {
Add-VaultRestriction -EntityName $number -Message "There is no erp material linked to this entity!"
}

$erpMaterial = Get-ERPObject -EntitySet "Materials" -Keys @{ Number = $number }
if ($? -eq $false) {
return
}
}

if (-not $erpMaterial) {
Add-VaultRestriction -EntityName $number -Message "An item with the number '$($number)' does not exist in the ERP system."
Add-VaultRestriction -EntityName $number -Message "An item with the number '$($number)' does not exist in the ERP system."
return
}
}
if ($Entity._Extension -eq "ipt") {
return
}
Expand All @@ -44,15 +44,15 @@ function Test-ErpItemAndBOMForVaultFileOrVaultItem {
#>

$erpBomHeader = Get-ERPObject -EntitySet "BomHeaders" -Keys @{Number = $number } -Expand "BomRows"
if ($? -eq $false) {
return
}
if ($? -eq $false) {
return
}

if (-not $erpBomHeader) {
Log -Message "Bomheader doesn't exist yet and is new!"
Log -Message "Bomheader doesn't exist yet and is new!"
Add-VaultRestriction -EntityName $number -Message "Open the BOM Window, because the ERP BOM is different then in Vault: BOM does not exist in ERP!"
return
}
}

Log -Message "Bom head exists! Check if rows need to be added/updated"
$vaultBomRows = GetVaultBomRows -Entity $Entity
Expand Down 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
}
Log -End
}

Register-VaultEvent -EventName UpdateItemStates_Post -Action 'AddItemPdfJob'
function AddItemPdfJob($items) {
Log -Begin
$releasedItems = @($items | Where-Object { $_._ReleasedRevision -eq $true})
$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
}
94 changes: 0 additions & 94 deletions Files/powerJobs/Jobs/Erp.Service.CreatePDFFromItem.ps1

This file was deleted.

29 changes: 12 additions & 17 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,15 +31,16 @@ if ( @("idw", "dwg") -notcontains $file._Extension ) {
ConnectToPowerGateServer

$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*') {
Expand All @@ -55,25 +53,22 @@ if ($openResult) {
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 8fe6f7c

Please sign in to comment.