Use Case
| • | You want to take full advantage of what's provided to give the impression of seamless integration with ClearCanvas Workstation. |
Remarks
By now, you should understand the basics: you search for studies using the study locator service and open them with the automation service. Now, we can quickly go over the other functionality that is available.
Activating a Viewer Window
In going through the examples, you may have noticed that the OpenStudies method returns a result, which includes an object of type Viewer. This object is essentially a reference to the viewer window that the study (or studies) was opened in. Suppose, in your application, the user is switching between two windows containing information for 2 different patients. And, suppose, the corresponding studies are open in 2 different windows in the viewer. To ensure that when the user switches to one of these windows in your application, the viewer does the same, you can simply use this Viewer object directly, like so:
using System;
using System.ServiceModel;
using ClearCanvas.ImageViewer.Services.Automation;
internal static class AutomationHelper
{
public static void ActivateViewer(Viewer viewer)
{
ViewerAutomationServiceClient client = new ViewerAutomationServiceClient();
try
{
client.Open();
ActivateViewerRequest request = new ActivateViewerRequest();
request.Viewer = viewer;
client.ActivateViewer(request);
client.Close();
}
catch (Exception e)
{
client.Abort();
MessageBox.Show(e.Message);
}
}
}
Closing a Viewer Window
To close a viewer window, follow the example above for activating a viewer window, but call CloseViewer instead. You may want to close a viewer window, for example, when the user closes the corresponding patient window in your application.
Finding a Viewer Window
It's not necessarily convenient to keep these Viewer references hanging around when you're unlikely to use them very much. Not to mention, the user can close the windows, leaving you with an invalid reference. So, the automation service provides the GetActiveViewers method, allowing you to retrieve all available Viewers at any time.
The following code will find all the Viewers for a given 'primary' Study Instance Uid:
using System;
using System.Collections.Generic;
using System.ServiceModel;
using ClearCanvas.ImageViewer.Services.Automation;
internal static class AutomationHelper
{
public static List<Viewer> GetViewers(string primaryStudyInstanceUid)
{
ViewerAutomationServiceClient client = new ViewerAutomationServiceClient();
List<Viewer> viewers = new List<Viewer>();
try
{
client.Open();
GetActiveViewersResult result = client.GetActiveViewers();
foreach (Viewer viewer in result.ActiveViewers)
{
if (viewer.PrimaryStudyInstanceUid == primaryStudyInstanceUid)
viewers.Add(viewer);
}
client.Close();
}
catch(Exception)
{
client.Abort();
throw;
}
return viewers;
}
}
Making it Even Easier
If you are using the CC Framework directly, there is a hidden gem called the ViewerAutomationBridge. Without going into too much detail, what it essentially does is implements the most common use cases without you, the developer, having to worry too much about how it's done. Granted it's simple enough to use the automation and study locator services individually, but the automation bridge makes it that much simpler. Here's an example of how you might use the automation bridge:
using System;
using System.Collections.Generic;
using System.ServiceModel;
using ClearCanvas.Dicom.ServiceModel.Query;
using ClearCanvas.ImageViewer.Services.Automation;
internal static class AutomationHelper
{
public static void OpenPatient(string patientId)
{
using (ViewerAutomationBridge bridge = new ViewerAutomationBridge
(
new ViewerAutomationServiceClient(), new StudyRootQueryServiceClient()
))
{
bridge.OpenStudiesByPatientId(patientId);
}
}
}
So what does the bridge do here? First, it automatically queries the study locator service for the studies belonging to the patient with the supplied Patient Id. But, in addition to that, it also sorts the studies in descending order by Study Date and Time. This is because, normally, the user wants to see the most recent study for a patient, and the viewer takes the first study and makes it the 'primary' study; in other words, the one that is displayed first and is assumed to be of primary interest.
There are a number of other convenience methods the viewer automation bridge provides, but we won't go into them all here as they are pretty self-explanatory.
As was alluded to in several of the previous sections, there is a test automation client in the code base that is a fully functional application that automates the viewer. The project is called ClearCanvas.ImageViewer.DesktopServices.Automation.TestClient.csproj. In order to use it, you must first build ImageViewer.sln. Then, you should simply be able to open the project in Visual Studio and hit F5 to start it.