Пример #1
0
        public void AddResult(DicomDataset ds)
        {
            var referencedInstance = GetReferencedInstsance(ds);
            var referencedSeq      = new fo.DicomSequence(fo.DicomTag.ReferencedInstanceSequence);
            var item = new fo.DicomDataset( );


            referencedInstance.Merge(item);

            _dataset.AddOrUpdate(referencedSeq);
            referencedSeq.Items.Add(item);

            item.AddOrUpdate <string> (fo.DicomTag.RetrieveURI, UrlProvider.GetInstanceUrl(new ObjectId(ds)));


            if (_failureAdded)
            {
                HttpStatus = HttpStatusCode.Accepted;
            }
            else
            {
                HttpStatus = HttpStatusCode.OK;
            }

            _successAdded = true;
        }
Пример #2
0
        protected virtual void ReadVr_SQ(JsonTextReader reader, fo.DicomTag tag, fo.DicomDataset dataset, int level)
        {
            fo.DicomSequence seq = new fo.DicomSequence(tag, new fo.DicomDataset[0]);


            if (reader.Value as string == JsonConstants.ValueField)
            {
                while (reader.Read( ) && reader.TokenType == JsonToken.StartArray)
                {
                    while (reader.Read( ) && reader.TokenType != JsonToken.EndArray)
                    {
                        fo.DicomDataset itemDs = new fo.DicomDataset( );

                        ReadChildren(reader, itemDs, ++level);

                        --level;

                        seq.Items.Add(itemDs);
                    }

                    break;
                }
            }

            dataset.AddOrUpdate(seq);
        }
Пример #3
0
        public void AddResult(DicomDataset ds, Exception ex)
        {
            var referencedInstance = GetReferencedInstsance(ds);
            var failedSeq          = new fo.DicomSequence(fo.DicomTag.FailedSOPSequence);
            var item = new fo.DicomDataset( );


            referencedInstance.Merge(item);

            _dataset.AddOrUpdate(failedSeq);
            failedSeq.Items.Add(item);

            item.AddOrUpdate <UInt16> (fo.DicomTag.FailureReason, 272);  //TODO: for now 272 == "0110 - Processing failure", must map proper result code from org. exception

            item.AddOrUpdate <string> (fo.DicomTag.RetrieveURI, ex.Message);

            if (_successAdded)
            {
                HttpStatus = HttpStatusCode.Accepted;
            }
            else
            {
                HttpStatus = HttpStatusCode.Conflict;  //should figure out the true reason from a wrapped exception code
            }

            _failureAdded = true;
        }
Пример #4
0
        public virtual void ReadData(string tableName, string columnName, object value)
        {
            var column    = SchemaProvider.GetColumn(tableName, columnName);
            var dicomTags = column.Tags;

            if (column.IsKey)
            {
                CurrentData.KeyValue = value.ToString( );
            }

            if (column.IsForeign)
            {
                string keyString = value.ToString( );

                KeyToDataSetCollection resultSet = null;

                if (ResultSets.TryGetValue(column.Table.Parent, out resultSet))
                {
                    fo.DicomDataset foreignDs = (fo.DicomDataset)resultSet[keyString];

                    if (QueryLevelTableName == column.Table.Name)
                    {
                        foreignDs.Merge(CurrentData.CurrentDs);

                        //resultSet[keyString] = CurrentData.CurrentDs ;
                    }
                    else
                    {
                        if (column.Table.IsSequence)
                        {
                            fo.DicomSequence sq   = (fo.DicomSequence)CurrentData.ForeignDs.Get <fo.DicomSequence> (CurrentData.ForeignTagValue);
                            fo.DicomDataset  item = new fo.DicomDataset( );

                            sq.Items.Add(item);

                            CurrentData.CurrentDs.Merge(item);

                            CurrentData.CurrentDs = item;
                        }
                        else if (column.Table.IsMultiValue)
                        {
                            CurrentData.CurrentDs = foreignDs;
                        }
                        else
                        {
                            CurrentData.CurrentDs.Merge(foreignDs);

                            foreignDs.CopyTo(CurrentData.CurrentDs);     //TODO: check if above merge is still necessary with this new CopyTo method
                        }
                    }
                }
            }

            if (null == dicomTags)
            {
                return;
            }

            ReadTags(columnName, value, dicomTags);
        }
Пример #5
0
        protected virtual void WriteVR_SQ(fo.DicomSequence element, JsonWriter writer)
        {
            for (int index = 0; index < element.Items.Count; index++)
            {
                StringBuilder sqBuilder = new StringBuilder( );
                StringWriter  sw        = new StringWriter(sqBuilder);

                using (JsonWriter sqWriter = new JsonTextWriter(sw))
                {
                    var item = element.Items[index];


                    sqWriter.Formatting = Formatting.Indented;//TODO: make it an option

                    sqWriter.WriteStartArray( );

                    sqWriter.WriteStartObject( );

                    if (null != item)
                    {
                        WriteChildren(item, sqWriter);
                    }

                    sqWriter.WriteEndObject( );
                    sqWriter.WriteEndArray( );
                }

                WriteSequenceValue(writer, sqBuilder.ToString( ));
            }
        }
Пример #6
0
        private void ReadSequence
        (
            fo.DicomDataset ds,
            XElement element,
            fo.DicomTag tag,
            int level
        )
        {
            fo.DicomSequence seq = new fo.DicomSequence(tag, new fo.DicomDataset[0]);


            foreach (var item in  element.Elements(Constants.ATTRIBUTE_ITEM_NAME))
            {
                fo.DicomDataset itemDs = new fo.DicomDataset( );

                level++;

                ReadChildren(itemDs, item, level);

                level--;

                seq.Items.Add(itemDs);
            }

            ds.AddOrUpdate(seq);
        }
 private void BuildWalkQueue(IEnumerable <DicomItem> dataset)
 {
     foreach (DicomItem item in dataset)
     {
         if (item is DicomElement)
         {
             _items.Enqueue(item);
         }
         else if (item is DicomFragmentSequence)
         {
             DicomFragmentSequence sq = item as DicomFragmentSequence;
             _items.Enqueue(item);
             foreach (IByteBuffer fragment in sq)
             {
                 _items.Enqueue(new DicomFragmentItem(fragment));
             }
             _items.Enqueue(new EndDicomFragment());
         }
         else if (item is DicomSequence)
         {
             DicomSequence sq = item as DicomSequence;
             _items.Enqueue(item);
             foreach (DicomDataset sqi in sq)
             {
                 _items.Enqueue(new BeginDicomSequenceItem(sqi));
                 BuildWalkQueue(sqi);
                 _items.Enqueue(new EndDicomSequenceItem());
             }
             _items.Enqueue(new EndDicomSequence());
         }
     }
 }
Пример #8
0
        protected virtual void WriteVR_SQ(fo.DicomSequence element, XmlWriter writer)
        {
            for (int index = 0; index < element.Items.Count; index++)
            {
                var item = element.Items [index];

                writer.WriteStartElement(Constants.ATTRIBUTE_ITEM_NAME);
                WriteNumberAttrib(writer, index);

                WriteChildren(item, writer);

                writer.WriteEndElement( );
            }
        }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DicomDataset"/> class.
 /// </summary>
 /// <param name="items">A collection of DICOM items.</param>
 public DicomDataset(IEnumerable <DicomItem> items)
     : this()
 {
     if (items != null)
     {
         foreach (var item in items.Where(item => item != null))
         {
             if (item.ValueRepresentation.Equals(DicomVR.SQ))
             {
                 var tag           = item.Tag;
                 var sequenceItems =
                     ((DicomSequence)item).Items.Where(dataset => dataset != null)
                     .Select(dataset => new DicomDataset(dataset))
                     .ToArray();
                 _items[tag] = new DicomSequence(tag, sequenceItems);
             }
             else
             {
                 _items[item.Tag] = item;
             }
         }
     }
 }
Пример #10
0
 public bool OnBeginSequence(DicomSequence sequence)
 {
     return(true);
 }
Пример #11
0
        public static List <DicomDataset> GetWorkList(DicomCFindRequest cfind)
        {
            var worklistItems = new List <DicomDataset>();

            try
            {
                if (cfind != null)
                {
                    int studyNumber = 0;
                    cfind.OnResponseReceived = (DicomCFindRequest rq, DicomCFindResponse rp) =>
                    {
                        Logger.Info($"OnResponseReceived..................");
                        Log.Loger($"OnResponseReceived..................");
                        if (rp.Dataset != null)
                        {
                            try
                            {
                                bool canAddWL = true;
                                //仅到检
                                if (_isOnlyArrived)
                                {
                                    string status = rp.Dataset.GetSequence(DicomTag.ScheduledProcedureStepSequence).First().GetSingleValueOrDefault(DicomTag.ScheduledProcedureStepStatus, string.Empty);
                                    //if (status != "ARRIVED")
                                    //{
                                    //    continue;
                                    //}
                                    canAddWL = (status == "ARRIVED");
                                }

                                if (canAddWL)
                                {
                                    Logger.Info($"Before Convert..................");
                                    Log.Loger($"Before Convert..................");
                                    string log = LogToConsole(rp.Dataset);
                                    Logger.Info(log);
                                    Log.Loger(log);

                                    #region PatientName转换
                                    string oldName      = rp.Dataset.GetValue <string>(Dicom.DicomTag.PatientName, 0);
                                    string newName      = oldName;
                                    string initialsName = string.Empty;
                                    int    sepIndex     = oldName.IndexOf("^");
                                    string surName      = oldName;
                                    string firstName    = string.Empty;
                                    string lastName     = string.Empty;
                                    if (sepIndex >= 0)
                                    {
                                        //surName = oldName.Substring(0, sepIndex);
                                        firstName = oldName.Split('^')[0];
                                        lastName  = oldName.Split('^')[1];
                                        if (string.IsNullOrEmpty(lastName))
                                        {
                                            surName = firstName;
                                        }
                                        else
                                        {
                                            //若拼音码为首字母,则不取
                                            if (firstName.Length == lastName.Length)
                                            {
                                                surName = firstName;
                                            }
                                            else
                                            {
                                                surName = lastName;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        firstName = oldName;
                                    }

                                    //取首字母,保持AccessionNumber一致
                                    if (IsChinese(firstName))
                                    {
                                        initialsName = NPinyin.Pinyin.GetInitials(firstName, true, 1);
                                        initialsName = initialsName.ToUpper();
                                    }

                                    if (IsChinese(surName))
                                    {
                                        //string newSurName = NPinyin.Pinyin.GetPinyin(surName, Encoding.Unicode, true);
                                        string newSurName = NPinyin.Pinyin.GetPinyin(surName, true, 1);
                                        newSurName = newSurName.ToUpper();
                                        //oldName = oldName.Replace(surName, newSurName);
                                        newName = newSurName;

                                        //取首字母
                                        //initialsName = NPinyin.Pinyin.GetInitials(surName, Encoding.Unicode, true);
                                        //initialsName = initialsName.ToUpper();
                                    }
                                    else
                                    {
                                        newName = surName;
                                    }

                                    rp.Dataset.AddOrUpdate(Dicom.DicomTag.PatientName, newName);
                                    #endregion

                                    #region PtianetID修改
                                    if (!_isNotConvertPatientId)
                                    {
                                        string patientId      = rp.Dataset.GetSingleValueOrDefault(DicomTag.OtherPatientIDsRETIRED, string.Empty);
                                        string newNameNoSpace = newName;
                                        if (newNameNoSpace.Substring(newNameNoSpace.Length - 1, 1) == " ")
                                        {
                                            newNameNoSpace = newNameNoSpace.Remove(newNameNoSpace.Length - 1);
                                        }
                                        string newPatientId = $"{newNameNoSpace}({patientId})";
                                        if (_deviceType == "MR")
                                        {
                                            newPatientId = _deviceType + " " + newPatientId;
                                        }
                                        rp.Dataset.AddOrUpdate(Dicom.DicomTag.PatientID, newPatientId);
                                    }
                                    #endregion

                                    #region Modality置空
                                    rp.Dataset.GetSequence(Dicom.DicomTag.ScheduledProcedureStepSequence).Items[0].AddOrUpdate(Dicom.DicomTag.Modality, _deviceType);
                                    #endregion

                                    #region SpecificCharacterSet字符集转换
                                    rp.Dataset.AddOrUpdate(Dicom.DicomTag.SpecificCharacterSet, "ISO_IR 100");
                                    #endregion

                                    #region AccessionNumber

                                    string accessionNumber = rp.Dataset.GetSingleValueOrDefault <string>(Dicom.DicomTag.AccessionNumber, "");
                                    if (string.IsNullOrEmpty(accessionNumber))
                                    {
                                        string scheduledProcedureStepStartTime = rp.Dataset.GetSequence(DicomTag.ScheduledProcedureStepSequence).First().GetSingleValueOrDefault(DicomTag.ScheduledProcedureStepStartTime, string.Empty);
                                        if (scheduledProcedureStepStartTime.Length > 4)
                                        {
                                            scheduledProcedureStepStartTime.Remove(4);
                                        }
                                        accessionNumber = _deviceType + initialsName + scheduledProcedureStepStartTime;
                                        rp.Dataset.AddOrUpdate(Dicom.DicomTag.AccessionNumber, accessionNumber);
                                    }
                                    #endregion

                                    #region StudyInstanceUID
                                    string studyInstanceUID = rp.Dataset.GetSingleValueOrDefault <string>(Dicom.DicomTag.StudyInstanceUID, "");
                                    if (string.IsNullOrEmpty(studyInstanceUID))
                                    {
                                        //rp.Dataset.AddOrUpdate(Dicom.DicomTag.StudyInstanceUID, rp.Dataset.GetValue<string>(Dicom.DicomTag.SOPInstanceUID, 0) + "." + (studyNumber++).ToString());
                                        string sOPInstanceUID = rp.Dataset.GetSingleValueOrDefault <string>(Dicom.DicomTag.SOPInstanceUID, "");
                                        if (sOPInstanceUID.Contains("."))
                                        {
                                            sOPInstanceUID = sOPInstanceUID.Remove(sOPInstanceUID.LastIndexOf('.'));
                                        }
                                        //rp.Dataset.AddOrUpdate(Dicom.DicomTag.StudyInstanceUID, rp.Dataset.GetValue<string>(Dicom.DicomTag.SOPInstanceUID, 0) + "." + (studyNumber++).ToString());
                                        rp.Dataset.AddOrUpdate(Dicom.DicomTag.StudyInstanceUID, sOPInstanceUID);
                                    }
                                    #endregion

                                    #region Set AETitle
                                    if (!string.IsNullOrEmpty(_setAETitle))
                                    {
                                        rp.Dataset.GetSequence(Dicom.DicomTag.ScheduledProcedureStepSequence).Items[0].AddOrUpdate(Dicom.DicomTag.ScheduledStationAETitle, _setAETitle);
                                    }
                                    #endregion

                                    #region Set Scheduled Protocol Code Sequence
                                    Dicom.DicomSequence spcs = new Dicom.DicomSequence(Dicom.DicomTag.ScheduledProtocolCodeSequence);
                                    if (!rp.Dataset.GetSequence(Dicom.DicomTag.ScheduledProcedureStepSequence).Items[0].TryGetSequence(Dicom.DicomTag.ScheduledProtocolCodeSequence, out spcs))
                                    {
                                        Dicom.DicomDataset dd = new DicomDataset();
                                        dd.AddOrUpdate(Dicom.DicomTag.CodeValue, "");
                                        dd.AddOrUpdate(Dicom.DicomTag.CodingSchemeDesignator, "");
                                        dd.AddOrUpdate(Dicom.DicomTag.CodingSchemeVersion, "");
                                        dd.AddOrUpdate(Dicom.DicomTag.CodeMeaning, "");
                                        spcs = new Dicom.DicomSequence(Dicom.DicomTag.ScheduledProtocolCodeSequence, new DicomDataset[] { dd });
                                        rp.Dataset.GetSequence(Dicom.DicomTag.ScheduledProcedureStepSequence).Items[0].Add(Dicom.DicomTag.ScheduledProtocolCodeSequence, dd);
                                        //rp.Dataset.GetSequence(Dicom.DicomTag.ScheduledProtocolCodeSequence).Items.Add(dd);
                                    }

                                    #endregion

                                    Logger.Info($"After Convert..................");
                                    Log.Loger($"After Convert..................");
                                    log = LogToConsole(rp.Dataset);
                                    Logger.Info(log);
                                    Log.Loger(log);
                                    Logger.Info($"\r\n");
                                    Log.Loger($"\r\n");

                                    worklistItems.Add(rp.Dataset);
                                }
                            }
                            catch (Exception ex)
                            {
                                string log = " Convert Failed: \r\n" + ex.Message;
                                Logger.Info($"\r\n {log}");
                                Log.Loger($"\r\n {log}");
                            }
                        }
                    };

                    var client = new DicomClient();
                    client.AddRequest(cfind);
                    Logger.Info($"Sending request async..................");
                    Log.Loger($"Sending request async..................");
                    client.SendAsync(_serverIP, _serverPort, false, _clientAET, _serverAET).GetAwaiter().GetResult();
                }
            }
            catch (Exception ex)
            {
                string log = " Convert Failed: \r\n" + ex.Message;
                Logger.Info($"\r\n {log}");
                Log.Loger($"\r\n {log}");
            }

            return(worklistItems);
        }