Пример #1
0
        /// <summary>
        /// Creates an xml layout file based on DICOM Structured display.
        /// </summary>
        /// <param name="ds">The ds.</param>
        /// <returns></returns>
        public static Layout ToLayout(this DicomDataSet ds)
        {
            StructuredDisplayImageBoxModule ibModule = new StructuredDisplayImageBoxModule();
            Layout layout = null;

            ds.Get(ibModule);
            if (ibModule != null)
            {
                if (ibModule.StructuredDisplayImageBoxSequence.Count > 0 && ibModule.StructuredDisplayImageBoxSequence != null && ibModule.StructuredDisplayImageBoxSequence.Count > 0)
                {
                    if (!(ibModule.StructuredDisplayImageBoxSequence.Count == 1 && ibModule.StructuredDisplayImageBoxSequence[0].DisplayEnvironmentSpatialPosition == null))
                    {
                        layout = new Layout();

                        using (MemoryStream ms = new MemoryStream())
                        {
                            layout.Icon       = "";
                            layout.Name       = "";
                            layout.TemplateId = ds.GetValue <string>(DicomTag.TemplateIdentifier, string.Empty);
                            foreach (SDImageBox box in ibModule.StructuredDisplayImageBoxSequence)
                            {
                                if (box.DisplayEnvironmentSpatialPosition != null && box.DisplayEnvironmentSpatialPosition.Count == 4)
                                {
                                    string referencedSOPInstanceUID = (box.ReferencedImageSequence != null && box.ReferencedImageSequence.Count > 0) ?
                                                                      box.ReferencedImageSequence[0].ImageSopInstanceReference.ReferencedSopInstanceUid : string.Empty;
                                    DataContracts.ImageBox imageBox = new DataContracts.ImageBox(referencedSOPInstanceUID);

                                    //
                                    // Top and bottom coordinates need to be switched.  In DICOM the lower left is 0,0 in our viewer the lower left is 1,1
                                    //

                                    imageBox.Position.leftTop     = new LeadPointD(box.DisplayEnvironmentSpatialPosition[0], 1 - box.DisplayEnvironmentSpatialPosition[1]);
                                    imageBox.Position.rightBottom = new LeadPointD(box.DisplayEnvironmentSpatialPosition[2], 1 - box.DisplayEnvironmentSpatialPosition[3]);
                                    layout.Boxes.Add(imageBox);
                                }
                            }
                        }
                    }
                }
            }
            return(layout);
        }
Пример #2
0
        public override void OnReceiveCFindRequest(DicomClient Client, byte presentationID, int messageID, string affectedClass, DicomCommandPriorityType priority, DicomDataSet ds)
        {
            string patientId = ds.GetValue <string>(DicomTag.PatientID, string.Empty);

            if (Module.Options.SearchOtherPatientId && !patientId.IsNullOrEmpty())
            {
                MyOtherPatientIds opid = new MyOtherPatientIds();
                ds.Get(opid);

                if (opid.OtherPatientIds == null)
                {
                    opid.OtherPatientIds = new List <string>();
                }

                if (!opid.OtherPatientIds.Contains(patientId))
                {
                    opid.OtherPatientIds.Add(patientId);
                    ds.Set(opid);

                    Logger.Global.SystemMessage(LogType.Information, "CFind request dataset modified: PatientID added to OtherPatientIds", Client.AETitle, ds, null);
                }
            }
            base.OnReceiveCFindRequest(Client, presentationID, messageID, affectedClass, priority, ds);
        }
Пример #3
0
        public static StudyLayout ToStudyLayout(this DicomDataSet ds)
        {
            StructuredDisplayImageBoxModuleEx ibModule = new StructuredDisplayImageBoxModuleEx();
            StudyLayout layout = null;
            Dictionary <string, string> mappedSeries = new Dictionary <string, string>();

            ds.Get(ibModule);
            if (ibModule != null)
            {
                var studyIntanceUID = ds.GetValue <string>(DicomTag.StudyInstanceUID, string.Empty);

                layout = new StudyLayout();

                DateTime dateTime = DateTime.Parse(ds.GetValue <string>(DicomTag.SeriesDate, string.Empty) + " " + ds.GetValue <string>(DicomTag.SeriesTime, string.Empty));

                layout.Name = dateTime.ToString("G");

                if (!string.IsNullOrEmpty(ibModule.ImageDisplayFormat))
                {
                    string   data = ibModule.ImageDisplayFormat.Replace(@"STANDARD\", "");
                    string[] info = data.Split(',');

                    if (info.Count() >= 2)
                    {
                        layout.Columns = Convert.ToInt32(info[0]);
                        layout.Rows    = Convert.ToInt32(info[1]);
                    }
                }

                foreach (ExtendedReferenceSeries series in ibModule.ReferencedSeriesSequence)
                {
                    SeriesInfo info = new SeriesInfo();

                    if (series.ImageBoxNumber.HasValue)
                    {
                        info.ImageBoxNumber = series.ImageBoxNumber.Value;
                    }
                    info.SeriesInstanceUID = series.SeriesInstanceUID;
                    info.StudyInstanceUID  = ds.GetValue <string>(DicomTag.StudyInstanceUID, string.Empty);
                    layout.Series.Add(info);
                }

                foreach (OtherStudyReference studyReference in ibModule.StudyContainingOtherReferences)
                {
                    OtherStudies study = new OtherStudies();

                    study.StudyInstanceUID = studyReference.StudyInstanceUID;
                    foreach (ExtendedReferenceSeries series in studyReference.ReferencedSeriesSequence)
                    {
                        SeriesInfo info = new SeriesInfo();

                        if (series.ImageBoxNumber.HasValue)
                        {
                            info.ImageBoxNumber = series.ImageBoxNumber.Value;
                        }
                        info.SeriesInstanceUID = series.SeriesInstanceUID;
                        info.StudyInstanceUID  = studyReference.StudyInstanceUID;
                        study.Series.Add(info);
                    }
                    layout.OtherStudies.Add(study);
                }

                if (ibModule.StructuredDisplayImageBoxSequence.Count > 0 && ibModule.StructuredDisplayImageBoxSequence != null && ibModule.StructuredDisplayImageBoxSequence.Count > 0)
                {
                    if (!(ibModule.StructuredDisplayImageBoxSequence.Count == 1 && ibModule.StructuredDisplayImageBoxSequence[0].DisplayEnvironmentSpatialPosition == null))
                    {
                        ibModule.StructuredDisplayImageBoxSequence.Reverse();
                        foreach (ExtendedSDImageBox box in ibModule.StructuredDisplayImageBoxSequence)
                        {
                            if (box.DisplayEnvironmentSpatialPosition != null && box.DisplayEnvironmentSpatialPosition.Count == 4)
                            {
                                string referencedSOPInstanceUID = (box.ReferencedImageSequence != null && box.ReferencedImageSequence.Count > 0) ?
                                                                  box.ReferencedImageSequence[0].ImageSopInstanceReference.ReferencedSopInstanceUid : string.Empty;
                                DataContracts.ImageBox imageBox = new DataContracts.ImageBox(referencedSOPInstanceUID);
                                //
                                // Top and bottom coordinates need to be switched.  In DICOM the lower left is 0,0 in our viewer the lower left is 1,1
                                //

                                imageBox.Position.leftTop     = new LeadPointD(box.DisplayEnvironmentSpatialPosition[0], 1 - box.DisplayEnvironmentSpatialPosition[1]);
                                imageBox.Position.rightBottom = new LeadPointD(box.DisplayEnvironmentSpatialPosition[2], 1 - box.DisplayEnvironmentSpatialPosition[3]);

                                if (box.ImageBoxLayoutType.HasValue)
                                {
                                    imageBox.ImageBoxLayoutType = box.ImageBoxLayoutType.Value;
                                }

                                if (box.ImageBoxNumber.HasValue)
                                {
                                    imageBox.ImageBoxNumber = box.ImageBoxNumber.Value;
                                }

                                if (box.ReferencedFirstFrameSequence != null && box.ReferencedFirstFrameSequence.Count > 0)
                                {
                                    ImageSopInstanceReference imgSopRef = box.ReferencedFirstFrameSequence[0];

                                    imageBox.FirstFrame                = new FirstFrame();
                                    imageBox.FirstFrame.SOPClassUID    = imgSopRef.ReferencedSopClassUid;
                                    imageBox.FirstFrame.SOPInstanceUID = imgSopRef.ReferencedSopInstanceUid;
                                    if (imgSopRef.ReferencedFrameNumber != null && imgSopRef.ReferencedFrameNumber.Count > 0)
                                    {
                                        imageBox.FirstFrame.FrameNumber = imgSopRef.ReferencedFrameNumber[0];
                                    }
                                }

                                if (!string.IsNullOrEmpty(box.ImageDisplayFormat))
                                {
                                    string   data = box.ImageDisplayFormat.Replace(@"STANDARD\", "");
                                    string[] info = data.Split(',');

                                    if (info.Count() >= 2)
                                    {
                                        imageBox.ColumnPosition  = Convert.ToInt32(info[0]);
                                        imageBox.RowPosition     = Convert.ToInt32(info[1]);
                                        imageBox.NumberOfRows    = box.Rows.Value;
                                        imageBox.NumberOfColumns = box.Columns.Value;
                                    }
                                }

                                if (box.ReferencedPresentationStateSequence != null && box.ReferencedPresentationStateSequence.Count > 0)
                                {
                                    SopInstanceReference refPS = box.ReferencedPresentationStateSequence[0];

                                    imageBox.ReferencedPresentationStateSOP = refPS.ReferencedSopInstanceUid;
                                }

                                if (box.DisplaySetHorizontalJustification.HasValue)
                                {
                                    imageBox.HorizontalJustification = box.DisplaySetHorizontalJustification.Value;
                                }

                                if (box.DisplaySetVerticalJustification.HasValue)
                                {
                                    imageBox.VerticalJustification = box.DisplaySetVerticalJustification.Value;
                                }

                                if (box.ImageBoxTileHorizontalDimension.HasValue)
                                {
                                    imageBox.ImageBoxTileHorizontalDimension = box.ImageBoxTileHorizontalDimension.Value;
                                }

                                if (box.ImageBoxTileVerticalDimension.HasValue)
                                {
                                    imageBox.ImageBoxTileVerticalDimension = box.ImageBoxTileVerticalDimension.Value;
                                }

                                layout.Boxes.Add(imageBox);
                            }
                        }
                    }
                }
            }
            return(layout);
        }
Пример #4
0
        public DicomCommandStatusType OnNAction(DicomClient Client, byte PresentationId, int MessageID, string AffectedClass, string Instance, int Action, DicomDataSet Request, DicomDataSet Response)
        {
            ClientSession clientSession             = null;
            INActionClientSessionProxy sessionProxy = null;
            DicomCommand patientUpdateCommand       = null;

            CompositeInstanceDataSet.InstanceRow[] instanceRows = null;

            if (_TemporaryDirectory == null)
            {
                _TemporaryDirectory = Client.Server.TemporaryDirectory;
            }

            if (Instance != PatientUpdaterConstants.UID.PatientUpdateInstance)
            {
                return(DicomCommandStatusType.InvalidObjectInstance);
            }

            if (clientSession != null)
            {
                clientSession.NActionResponse -= clientSession_NActionResponse;
                clientSession = null;
            }

            clientSession = new ClientSession(Client);
            clientSession.NActionResponse += new EventHandler <NActionResponseEventArgs>(clientSession_NActionResponse);
            sessionProxy         = new NActionClientSessionProxy(clientSession, PresentationId, MessageID, AffectedClass, Instance, Action, Response);
            patientUpdateCommand = DicomCommandFactory.GetInstance().CreateNActionCommand(sessionProxy, Request);

            _ClientSession = clientSession;
            _SessionProxy  = sessionProxy;


            //
            // If we are deleting we need to get the instance rows
            //
            List <string> studyInstanceUids;

            instanceRows = GetInstanceRows(Action, Request, out studyInstanceUids);

            ConfigureCommand(patientUpdateCommand as PatientUpdaterCommand);
            _ClientSession.ProcessNActionRequest(PresentationId, MessageID, AffectedClass, Instance, Action, patientUpdateCommand).WaitOne();

            if (_Status == DicomCommandStatusType.Success)
            {
                //
                // We need to delete the dicom files according to the user options
                //
                if (Action == PatientUpdaterConstants.Action.DeletePatient || Action == PatientUpdaterConstants.Action.DeleteSeries)
                {
                    if (instanceRows != null)
                    {
                        DicomFileDeleter deleter = new DicomFileDeleter();

                        try
                        {
                            StorageAddInsConfiguration storageSettings = Module.StorageConfigManager.GetStorageAddInsSettings();

                            deleter.DicomFileDeleted      += new EventHandler <Leadtools.Medical.Winforms.EventBrokerArgs.DicomFileDeletedEventArgs>(deleter_DicomFileDeleted);
                            deleter.DicomFileDeleteFailed += new EventHandler <Leadtools.Medical.Winforms.EventBrokerArgs.DicomFileDeletedEventArgs>(deleter_DicomFileDeleteFailed);
                            if (storageSettings != null)
                            {
                                deleter.DeleteFilesOnDatabaseDelete = storageSettings.StoreAddIn.DeleteFiles;
                                deleter.BackupFilesOnDatabaseDelete = storageSettings.StoreAddIn.BackupFilesOnDelete;
                                deleter.BackupFilesOnDeleteFolder   = storageSettings.StoreAddIn.DeleteBackupLocation;
                            }
                            deleter.Delete(null, instanceRows);
                        }
                        catch (Exception e)
                        {
                            Logger.Global.Error(Module.Source, "[Patient Updater] " + e.Message);
                        }
                        finally
                        {
                            deleter.DicomFileDeleted      -= deleter_DicomFileDeleted;
                            deleter.DicomFileDeleteFailed -= deleter_DicomFileDeleteFailed;
                        }
                    }
                }

                //
                // If auto update is enabled we need to forward this message to associated ae title
                //
                if (Module.Options != null && Module.Options.EnableAutoUpdate && Module.UpdateQueue != null)
                {
                    AutoUpdateItem item = new AutoUpdateItem(Client.AETitle)
                    {
                        Action = Action, ClientAE = Client.Server.AETitle
                    };

                    if (Action == PatientUpdaterConstants.Action.MergePatient && (instanceRows != null))
                    {
                        MergePatient mergePatientData = Request.Get <MergePatient>(null);
                        foreach (string studyInstanceUid in studyInstanceUids)
                        {
                            mergePatientData.ReferencedStudySequence.Add(new StudyInstanceReference(studyInstanceUid));
                        }
                        mergePatientData.PatientToMerge.Clear();
                        Request.Set(mergePatientData);
                    }
                    else if (studyInstanceUids.Count > 0)
                    {
                        DeletePatient deletePatientData = Request.Get <DeletePatient>(null);
                        foreach (string studyInstanceUid in studyInstanceUids)
                        {
                            deletePatientData.ReferencedStudySequence.Add(new StudyInstanceReference(studyInstanceUid));
                        }
                        Request.Set(deletePatientData);
                    }


                    // Request.InsertElementAndSetValue(DicomTag.ReferencedStudySequence)
                    item.Dicom = Request;

                    if (ShouldAutoUpdate(item.Action))
                    {
                        Module.UpdateQueue.AddItem(item);
                    }
                }
            }
            return(_Status);
        }
Пример #5
0
        private static CompositeInstanceDataSet.InstanceRow[] GetInstanceRows(int action, DicomDataSet request, out List <string> studyInstanceUids)
        {
            CompositeInstanceDataSet.InstanceRow[] rows = null;
            CompositeInstanceDataSet    ds  = null;
            MatchingParameterCollection mpc = new MatchingParameterCollection();
            MatchingParameterList       mpl = new MatchingParameterList();

            studyInstanceUids = new List <string>();

            if (request == null)
            {
                return(null);
            }

            if (Module.StorageAgent == null)
            {
                return(null);
            }

            switch (action)
            {
            case PatientUpdaterConstants.Action.MergePatient:
                MergePatient mergePatient = request.Get <MergePatient>();
                if (mergePatient.PatientToMerge != null && mergePatient.PatientToMerge.Count > 0)
                {
                    mpl.Add(new Patient()
                    {
                        PatientID = mergePatient.PatientToMerge[0].PatientId
                    });
                    mpc.Add(mpl);
                }
                break;

            case PatientUpdaterConstants.Action.DeletePatient:
                DeletePatient delPatient = request.Get <DeletePatient>();

                mpl.Add(new Patient()
                {
                    PatientID = delPatient.PatientId
                });
                foreach (StudyInstanceReference studyInstanceReference in delPatient.ReferencedStudySequence)
                {
                    mpl.Add(new Study()
                    {
                        StudyInstanceUID = studyInstanceReference.StudyInstanceUID
                    });
                }
                mpc.Add(mpl);
                break;

            case PatientUpdaterConstants.Action.DeleteSeries:
                DeleteSeries delSeries = request.Get <DeleteSeries>();

                mpl.Add(new Series()
                {
                    SeriesInstanceUID = delSeries.SeriesInstanceUID
                });
                mpc.Add(mpl);
                break;

            case PatientUpdaterConstants.Action.ChangePatient:
                ChangePatient changePatient = request.Get <ChangePatient>();
                mpl.Add(new Patient()
                {
                    PatientID = changePatient.OriginalPatientId
                });
                mpc.Add(mpl);
                break;

            default:
                return(null);
            }

            ds = Module.StorageAgent.QueryCompositeInstances(mpc).ToCompositeInstanceDataSet();

            CompositeInstanceDataSet.StudyRow[] studyRows = ds.Study.Rows.OfType <CompositeInstanceDataSet.StudyRow>().ToArray();
            foreach (CompositeInstanceDataSet.StudyRow studyRow in studyRows)
            {
                studyInstanceUids.Add(studyRow.StudyInstanceUID);
            }

            rows = ds.Instance.Rows.OfType <CompositeInstanceDataSet.InstanceRow>().ToArray();

            return(rows);
        }