Create a new stencil and populate master shapes from image files via PowerShell?

Started by PinPinPoola, March 21, 2024, 03:58:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wapperdude

Not taking anything away from Surrogate's response, but since I had syarted this learning experience, I decided to complete the task by including a For loop to iterate through a directory for image files.  Along the way, I discovered that copy/pasting the cmdlets seems to always run, but, thru not so when trying to execute the code same via a script file... until I found this syntax that seems to work  PowerShell and PowerShell ISE:  & 'C:\PowerShellProject\NewTry R6.ps1'  The scriptfile is NewTry R6.ps1, located in directory C:\PowerShellProject.

I'm reasonably confident in the following code.  Use notepad to copy/paste & save as .ps1, not txt, or  may also be copy/pasted into PowerShell directly. 

Some features:
1) the target vssx file must be pre-existing.  Initially, a blank (empty) vssx is fine.
2) the directories and file names are hard coded, so some editing is necessary
3) Visio must be called, even Surrogate's code does this, but Visio is hidden.
4) the code executes without creating any preceding shapes to contain the image file import.
5) it displays the filenames that were processed.
$visApp = New-Object -ComObject Visio.Application
$visApp.Visible = 0
$visApp.AlertResponse = 5
$visDoc = $visApp.Documents.Add("")
   
$stnPath = "C:\PowerShellProject\Pix.vssx"
$stnFile = $visApp.Documents.add($stnPath)
$stnFile.Protection = 2

Get-ChildItem -path "C:\VisioPix" | ForEach-Object {
    $imgFile = $_.Fullname
    $masterName = $_.Basename
    start-sleep -m 500
    $importedShape = $visDoc.Pages.Item(1).Import($imgFile)
    $master = $stnFile.Drop($importedShape, 0, 0)
    $master.name = $masterName
    write-host "Image File: " $masterName
    $importedShape.Delete()
}

$stnFile.SaveAS($stnPath)
$stnFile.Close()

$visApp.Quit()   

# Print a success message
Write-Host "Task execution done."

Visio 2019 Pro

wapperdude

Here's an alternative version of the script.  This version leverages the VisioPS module and its accompanying commands.  It allows more of a Visio feel to the coding. BTW, the start-sleep line in the previous version is not needed, and not included below.  Also, if spaces are removed from the file name, then the "&" and single quotes noted above are unnecessary.

Ed.: Curious note.  Invoking New-VisioDocument actually starts a new Visio session.  The Get-VisioApplication grabs that running session.  Whereas, had New-VisioApplication been executed first, that would also start a new Visio instance without a document reference.  As this is temporary, A New-VisioDocument would still be needed, only now two Visio instances would exist.  Most undesirable.  Hence the code order below is necessary, sufficient, and intentional wrt starting up Visio. 

The rest of the code was generated by 10,000 monkeys working with AI.

Import-Module Visio
$visDoc = New-VisioDocument
$visPg = Get-VisioPage -ActivePage
$visApp = Get-VisioApplication
$visApp.Visible = 0
$visApp.AlertResponse = 5
   
$stnPath = "C:\PowerShellProject\Pix.vssx"
$stnFile = $visApp.Documents.add($stnPath)
$stnFile.Protection = 2

Get-ChildItem -path "C:\VisioPix" | ForEach-Object {
    $imgFile = $_.Fullname
    $masterName = $_.Basename
    $importedShape = $visPg.Import($imgFile)
    $master = $stnFile.Drop($importedShape, 0, 0)
    $master.name = $masterName
    write-host "Image File: " $masterName
    $importedShape.Delete()
}

$stnFile.SaveAS($stnPath)
$stnFile.Close()

Close-VisioApplication

# Print a success message
Write-Host "Task execution done."

Visio 2019 Pro