Sample.CreatePDF creates PDF of outdated drawings when openReleasedDrawingsFast is set to false

When the setting "$openReleasedDrawingsFast = $false" is used in the "Sample.CreatePDF" job the resulting PDF shows out dated data

Issue

When the setting "$openReleasedDrawingsFast = $false" is used in the "Sample.CreatePDF" job the resulting PDF shows out dated data.

Cause

This happens because the job doesn't specify a project file per default and outdated data is stored in the local Inventor project path. The way Inventor resolution works is that files in its currently active projects are used first. The sample job though downloads all of the dependencies into a temporary directory, so they will never be found by Inventor if a duplicate is found in the project path.

Solution

Removing local files

One quick solution is to clean all the files from the local Vault workspace. If Inventor doesn't find any outdated files in its workspace it will keep searching in the directory next and below the currently opened document.

Modifying the job to use a project file

Disabling fastOpen

If you are reading this article fastOpen should be disabled already, but it is important to mention this step as non of the following changes will have any effect if fastOpen is active.

$openReleasedDrawingsFast = $false

 

Changing download directory of all files to the local Vault workspace

To do this remove the -DownloadDirectory parameter from the Save-VaultFile cmdlet. If it isn't set the local Vault workspace will be used. The local Vault workspace is used in this case as this allows to download necessary style libraries without having to handle that in the code also. 
In addition to that we have seen a case where the path resolution didn't work properly in C:\TEMP. 

$file = (Save-VaultFile -File $file._FullPath -DownloadDirectory $workingDirectory -ExcludeChildren:$fastOpen -ExcludeLibraryContents:$fastOpen)[0]

to

$file = (Save-VaultFile -File $file._FullPath -ExcludeChildren:$fastOpen -ExcludeLibraryContents:$fastOpen)[0]

 

Downloading the project file from Vault

Before the Save-VaultFile cmdlet  add the following code

$ipjVaultPath = $vault.DocumentService.GetInventorProjectFileLocation()
$localIpjFile = (Save-VaultFile -File $ipjVaultPath)[0] # Calling Save-VaultFile without -DownloadDirectory will automatically select the Vault local workspace

 

Ensuring no data from previous jobs still exists

After downloading the project file add the following code. It will generate the local Inventor workspace and remove it if it exists. It won't touch library files as they usually stay identical between jobs.

$vaultWorkspace = ($vaultConnection.WorkingFoldersManager.GetWorkingFolder("$")).FullPath # Gets the local workspace path of the Vault root
$InventorWorkspaceFoldername = [System.IO.Path]::GetFileNameWithoutExtension($localIpjFile._Name) # Gets the filename of the project file without extension. This is usually equal to the Inventor workspace folder name
$InventorWorkspaceFolder = Join-Path -Path $vaultWorkspace -ChildPath $InventorWorkspaceFoldername

if (Test-Path $InventorWorkspaceFolder) {
  Remove-Item -LiteralPath $InventorWorkspaceFolder -Recurse -Force # Removes just the Inventor workspace folder to ensure no data of previous jobs interferes with the current job. Libraries are untouched.
}

 

Tell the Open-Document cmdlet to use the project file

Change

$openResult = Open-Document -LocalFile $file.LocalPath -Options @{ FastOpen = $fastOpen }
to
$openResult = Open-Document -LocalFile $file.LocalPath -Options @{ FastOpen = $fastOpen; Project = $localIpjFile.LocalPath }

 

Updated sample job

The following script shows a completed updated sample job. Also some variable names were changed to make the job more concise and an additional check for successful file download

 
#==============================================================================#
# (c) 2022 coolOrange s.r.l.                                                   #
#                                                                              #
# THIS SCRIPT/CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER    #
# EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES  #
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.   #
#==============================================================================#

# Required in the powerJobs Settings Dialog to determine the entity type for lifecycle state change triggers
# JobEntityType = FILE

#region Settings
# To include the Revision of the main file in the PDF name set Yes, otherwise No
$pdfFileNameWithRevision = $false

# The character used to separate file name and Revision label in the PDF name such as hyphen (-) or underscore (_)
$pdfFileNameRevisionSeparator = "_"

# To include the file extension of the main file in the PDF name set Yes, otherwise No
$pdfFileNameWithExtension = $true

# To add the PDF to Vault set Yes, to keep it out set No
$addPDFToVault = $true

# To attach the PDF to the main file set Yes, otherwise No
$attachPDFToVaultFile = $true

# Specify a Vault folder in which the PDF should be stored (e.g. $/Designs/PDF), or leave the setting empty to store the PDF next to the main file
$pdfVaultFolder = ""

# Specify a network share into which the PDF should be copied (e.g. \\SERVERNAME\Share\Public\PDFs\)
$pdfNetworkFolder = ""

# To enable faster opening of released Inventor drawings without downloading and opening their model files set Yes, otherwise No
$openReleasedDrawingsFast = $false

#endregion Settings

$pdfFileName = [System.IO.Path]::GetFileNameWithoutExtension($file._Name)
if ($pdfFileNameWithRevision) {
  $pdfFileName += $pdfFileNameRevisionSeparator + $file._Revision
}

if ($pdfFileNameWithExtension) {
  $pdfFileName += "." + $file._Extension
}
$pdfFileName += ".pdf"

if ([string]::IsNullOrWhiteSpace($pdfVaultFolder)) {
  $pdfVaultFolder = $file._FolderPath
}

Write-Host "Starting job 'Create PDF as visualization attachment' for file '$($file._Name)' ..."

if ( @("idw", "dwg") -notcontains $file._Extension ) {
  Write-Host "Files with extension: '$($file._Extension)' are not supported"
  return
}
if (-not $addPDFToVault -and -not $pdfNetworkFolder) {
  throw("No output for the PDF is defined in ps1 file!")
}
if ($pdfNetworkFolder -and -not (Test-Path $pdfNetworkFolder)) {
  throw("The network folder '$pdfNetworkFolder' does not exist! Correct pdfNetworkFolder in ps1 file!")
}

$fastOpen = $openReleasedDrawingsFast -and $file._ReleasedRevision

$ipjVaultPath = $vault.DocumentService.GetInventorProjectFileLocation()
$localIpjFile = (Save-VaultFile -File $ipjVaultPath)[0] # Calling Save-VaultFile without -DownloadDirectory will automatically select the Vault local workspace

$vaultWorkspace = ($vaultConnection.WorkingFoldersManager.GetWorkingFolder("$")).FullPath # Gets the local workspace path of the Vault root
$InventorWorkspaceFoldername = [System.IO.Path]::GetFileNameWithoutExtension($localIpjFile._Name) # Gets the filename of the project file without extension. This is usually equal to the Inventor workspace folder name
$InventorWorkspaceFolder = Join-Path -Path $vaultWorkspace -ChildPath $InventorWorkspaceFoldername

if (Test-Path -LiteralPath $InventorWorkspaceFolder) {
  Remove-Item -LiteralPath $InventorWorkspaceFolder -Recurse -Force # Removes just the Inventor workspace folder to ensure no data of previous jobs interferes with the current job. Libraries are untouched.
}

$localFile = (Save-VaultFile -File $file._FullPath -ExcludeChildren:$fastOpen -ExcludeLibraryContents:$fastOpen)[0]
if (-not (Test-Path -LiteralPath $localFile.LocalPath)) {
  throw "Couldn't download '$($file._FullPath)' to '$($localFile.LocalPath)'"
}

$openResult = Open-Document -LocalFile $localFile.LocalPath -Options @{ FastOpen = $fastOpen; Project = $localIpjFile.LocalPath }

if ($openResult) {
  $localPDFfileLocation = "$workingDirectory\$pdfFileName"
  if ($openResult.Application.Name -like 'Inventor*') {
      $configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF_2D.ini"
  }
  else {
      $configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF.dwg"
    }

  if (-not(Test-Path -LiteralPath $workingDirectory)) {
      $null = New-Item -Path $workingDirectory -ItemType Directory # In the default job $workingDirectory folder is created by the first call to Save-VaultFile. As we changed that to use the Vault workspace we must create it ourselves, lest the export fails
    }

    $exportResult = Export-Document -Format 'PDF' -To $localPDFfileLocation -Options $configFile

  if ($exportResult) {
      if ($addPDFToVault) {
          $pdfVaultFolder = $pdfVaultFolder.TrimEnd('/')
          Write-Host "Add PDF '$pdfFileName' to Vault: $pdfVaultFolder"
          $PDFfile = Add-VaultFile -From $localPDFfileLocation -To "$pdfVaultFolder/$pdfFileName" -FileClassification DesignVisualization
          if ($attachPDFToVaultFile) {
              $null = Update-VaultFile -File $localFile._FullPath -AddAttachments @($PDFfile._FullPath)
          }
        }

      if ($pdfNetworkFolder) {
          Write-Host "Copy PDF '$pdfFileName' to network folder: $pdfNetworkFolder"
          Copy-Item -Path $localPDFfileLocation -Destination $pdfNetworkFolder -ErrorAction Continue -ErrorVariable "ErrorCopyPDFToNetworkFolder"
      }
    }

  $closeResult = Close-Document
}

if (-not $openResult) {
  throw("Failed to open document $($localFile.LocalPath)! Reason: $($openResult.Error.Message)")
}
if (-not $exportResult) {
  throw("Failed to export document $($localFile.LocalPath) to $localPDFfileLocation! Reason: $($exportResult.Error.Message)")
}
if (-not $closeResult) {
  throw("Failed to close document $($localFile.LocalPath)! Reason: $($closeResult.Error.Message))")
}

if ($ErrorCopyPDFToNetworkFolder) {
  throw("Failed to copy PDF file to network folder '$pdfNetworkFolder'! Reason: $($ErrorCopyPDFToNetworkFolder)")
}

Write-Host "Completed job 'Create PDF as visualization attachment'"