Wednesday, May 8, 2019

More PowerShell DICOM

# PowerShell DICOM

# This little script becomes handy for example for a daily test of your PACS
# It generates a 1000x1000 image with date and key attributes (Study, Series and Instance UID) burned in
# This script uses HRZ's RZDCX.DLL.

# Create DICOM Object
$DCM = New-Object -com rzdcx.DCXOBJ

# UID Generator
$UID = New-Object -com rzdcx.DCXUID

# Create new Key UID's
$studyInstanceUID = $UID.CreateUID(1)
$seriesInstanceUID = $UID.CreateUID(2)
$sopInstanceUID = $UID.CreateUID(3)

# Add minimal elements to the dataset
$tagValues = @(
0x00080016, "1.2.840.10008.5.1.4.1.1.7", 
0x00080018, $sopInstanceUID,
0x00080020, ($studyDate = Get-Date -UFormat "%Y%m%d"),
0x00080030, "",
0x00080050, "ACCNUMHRZTEST",
0x00080060, "SC",
0x00080064, "WSD",
0x00080090, "HRZ",
0x00100010, "HRZ^TEST",
0x00100020, "HRZTEST",
0x00100030, "20100101",
0x00100040, "O",
0x0020000d, $studyInstanceUID,
0x0020000e, $seriesInstanceUID,
0x00200010, "123",
0x00200011, "1",
0x00200013, "1")

$E = New-Object -com rzdcx.DCXELM
for (($i=0); ($i -lt $tagValues.Count); ($i+=2))
{
    $E.Init($tagValues[$i])
    $E.Value = $tagValues[$i+1]
    $DCM.insertElement($E)
}

# Create 1000x1000 Image with text printed on it
$rows = 1000
$cols = 1000

# jpeg image
Add-Type -AssemblyName System.Drawing
$imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
$format = $imageFormat::jpeg


$uidType = "rzdcx.UID_TYPE" -as [type]

$createDate = Get-Date
$bmp = new-object System.Drawing.Bitmap $cols,$rows
$font = new-object System.Drawing.Font Consolas,24 
$brushBg = [System.Drawing.Brushes]::Yellow 
$brushFg = [System.Drawing.Brushes]::Black 
$graphics = [System.Drawing.Graphics]::FromImage($bmp
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height) 
$graphics.DrawString($createDate, $font,$brushFg,10,10
$font = new-object System.Drawing.Font Consolas,8 
$graphics.DrawString("StudyInstance UID: " + $studyInstanceUID, $font,$brushFg, 10,110)  
$graphics.DrawString("Series Instance UID: " + $studyInstanceUID, $font,$brushFg,10,210)  
$graphics.DrawString("SOP Instance UID: " + $studyInstanceUID, $font,$brushFg,10,310)  
$graphics.DrawString("TARGET AET: " + "PACS-A", $font, $brushFg, 10, 410)  

$graphics.Dispose() 

# Save the JPEG image
$filename = "$home\HRZTEST.jpg"
$bmp.Save($filename, $format

# Insert it to the DICOM object
$DCM.SetJpegFrames($filename);

# Save the DICOM file
$dcmfile = $filename + ".dcm"
$DCM.saveFile($dcmfile)

# Send it to the PACS
$REQ = New-Object -com rzdcx.DCXREQ

$succeeded = ""
$failed = ""

# Start logging for network comunication and save to log file

$APP = New-Object -com rzdcx.DCXAPP
$APP.LogLevel = 5 # Debug

$logFile = "$home\SendTestImage.log"

$APP.StartLogging($logFile)

$REQ.Echo("HRZ-TEST", "PACS-A", "localhost", 104)
$REQ.Send("HRZ-TEST", "PACS-A", "localhost", 104, $dcmfile, [ref] $succeeded, [ref] $failed)

# Print results
$filename
$dcmfile
"Succeeded: " + $succeeded
"Failed: " + $failed

# Stop logging
$App.StopLogging()

"Done!"






Tuesday, January 15, 2019

PowerShell DICOM Scripting

Disclosure: This post is purely technical!
Assumption: you know your way around PowerShell or how to get there and a bit of DICOM and our RZDCX API.

If your system is x64 (probably) than make sure to regsvr32 radix.dll the x64 version.

Let's PowerShell!!!

# download rzdcx
Invoke-WebRequest -Uri http://downloads.roniza.com/rzdcx/2.0.8.7/RZDCX_2087.zip -OutFile ./rzdcx.zip

# unzip it
Expand-Archive ./rzdcx.zip -DestinationPath ./rzdcx

# regsvr32 win32 version
$rzdcx32 = Resolve-Path .\rzdcx\win32\rzdcx.dll
Start-Process regsvr32 -verb runAs -argumentlist $rzdcx32

$rzdcx64 = Resolve-Path .\rzdcx\x64\rzdcx.dll
Start-Process regsvr32 -verb runAs -argumentlist $rzdcx64

# Create DICOM Object
$DCM = New-Object -com rzdcx.DCXOBJ

# I assume you have a directory called DICOM with DICOM files in it
$files = @(Get-ChildItem ".\DICOM\*")

# Write headers
"filename, Patient Name, Patient ID"

# For each file extract and print patient name and patient id
foreach ($file in $files) {
  $DCM.openFile($file)
  $patname = $DCM.GetElement([int32]::Parse(00100010,'HexNumber')).Value
  $patid   = $DCM.GetElement([int32]::Parse(00100020,'HexNumber')).Value
  Write-Host $file "," $patname "," $patid
}

Try it!