📄 uiddictionary.cs
字号:
line = textReader.ReadLine(); lineNumber++; } } protected virtual void LoadFromXml(TextReader textReader) { XmlTextReader xmlTextReader = new XmlTextReader(textReader); string uid = null; string name = null; string type = null; string retired = null; while (xmlTextReader.Read()) { switch(xmlTextReader.Name) { case "DictionaryEntry": retired = xmlTextReader["retired"]; break; case "Uid": xmlTextReader.MoveToContent(); uid = xmlTextReader.ReadString(); break; case "Name": xmlTextReader.MoveToContent(); name = xmlTextReader.ReadString(); break; case "Type": xmlTextReader.MoveToContent(); type = xmlTextReader.ReadString(); break; } if (uid != null && name != null && type != null) { try { UidDictionaryEntry entry = new UidDictionaryEntry(uid, name.Trim(), type.Trim(), retired); Add(entry); } catch (Exception e) { throw new DicomException("Wrong entry at UID " + uid + ": " + e.Message); } uid = name = type = retired = null; } } xmlTextReader.Close(); } /// <summary> /// Re-creates a UID dictionary instance and fills it /// with entries from the specified UID dictionary file /// of given file format. /// </summary> public void LoadFrom(string fileName, DictionaryFileFormat fileFormat) { if (! IsEmpty) Clear(); StreamReader streamReader = new StreamReader(fileName); switch (fileFormat) { case DictionaryFileFormat.BinaryFile: LoadFromBinary(streamReader); break; case DictionaryFileFormat.PropertyFile: LoadFromProperty(streamReader); break; case DictionaryFileFormat.CsvFile: LoadFromCsv(streamReader); break; case DictionaryFileFormat.XmlFile: LoadFromXml(streamReader); break; } streamReader.Close(); if (global == null) Global = this; } private void SaveAsBinary(StreamWriter streamWriter, UidDictionaryEntry[] entryArray) { streamWriter.AutoFlush = true; BinaryWriter binaryWriter = new BinaryWriter(streamWriter.BaseStream); foreach (UidDictionaryEntry entry in entryArray) { binaryWriter.Write(entry.Uid.Value.Length); streamWriter.Write(entry.Uid.Value); binaryWriter.Write(entry.Name.Length); streamWriter.Write(entry.Name); binaryWriter.Write((int) entry.Type); binaryWriter.Write(entry.IsRetired); } } private void SaveAsProperty(TextWriter textWriter, UidDictionaryEntry[] entryArray) { textWriter.WriteLine("# " + fileComment); foreach (UidDictionaryEntry entry in entryArray) { if (entry.IsRetired) textWriter.WriteLine(entry.Uid + " = " + entry.Name + ", " + UidType.GetName(typeof(UidType), entry.Type) + ", RET"); else textWriter.WriteLine(entry.Uid + " = " + entry.Name + ", " + UidType.GetName(typeof(UidType), entry.Type)); } } private void SaveAsCsv(TextWriter textWriter, UidDictionaryEntry[] entryArray) { textWriter.WriteLine("# " + fileComment); foreach (UidDictionaryEntry entry in entryArray) { if (entry.IsRetired) textWriter.WriteLine(entry.Uid + "; " + entry.Name + "; " + UidType.GetName(typeof(UidType), entry.Type) + "; RET"); else textWriter.WriteLine(entry.Uid + "; " + entry.Name + "; " + UidType.GetName(typeof(UidType), entry.Type)); } } protected virtual void SaveAsXml(TextWriter textWriter, UidDictionaryEntry[] entryArray) { XmlTextWriter xmlTextWriter = new XmlTextWriter(textWriter); xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.Indentation = 4; xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteComment(" " + fileComment + " "); xmlTextWriter.WriteStartElement("DicomUidDictionary"); foreach (UidDictionaryEntry entry in entryArray) { xmlTextWriter.WriteStartElement("DictionaryEntry"); if (entry.IsRetired) xmlTextWriter.WriteAttributeString("retired", "true"); xmlTextWriter.WriteElementString("Uid", entry.Uid.ToString()); xmlTextWriter.WriteElementString("Name", entry.Name); xmlTextWriter.WriteElementString("Type", UidType.GetName(typeof(UidType), entry.Type)); xmlTextWriter.WriteEndElement(); } xmlTextWriter.WriteEndElement(); xmlTextWriter.Close(); } /// <summary> /// Saves the entire UID dictionary instance content to /// file using specified file format. /// </summary> public void SaveTo(string fileName, DictionaryFileFormat fileFormat) { StreamWriter streamWriter = new StreamWriter(fileName); switch (fileFormat) { case DictionaryFileFormat.BinaryFile: SaveAsBinary(streamWriter, ToArray()); break; case DictionaryFileFormat.PropertyFile: SaveAsProperty(streamWriter, ToArray()); break; case DictionaryFileFormat.CsvFile: SaveAsCsv(streamWriter, ToArray()); break; case DictionaryFileFormat.XmlFile: SaveAsXml(streamWriter, ToArray()); break; } streamWriter.Close(); } /// <summary> /// Returns the entire UID dictionary as array of /// <see cref="UidDictionaryEntry" />. /// </summary> public UidDictionaryEntry[] ToArray() { UidDictionaryEntry[] entryArray = new UidDictionaryEntry[Count]; hashTable.Values.CopyTo(entryArray, 0); Array.Sort(entryArray); return entryArray; } /// <summary> /// Adds a new UID dictionary entry to a UID /// dictionary instance. /// </summary> public void Add(UidDictionaryEntry entry) { if (entry != null) { if ( ! Contains(entry.Uid)) hashTable.Add(entry.Uid.ToString(), entry); else throw new DicomException( "UID already exists in UID dictionary.", "entry.Uid", entry.Uid.ToString()); } else throw new DicomException("entry is null.", "entry"); } /// <summary> /// Clears all UID dictionary properties. /// </summary> public void Clear() { hashTable.Clear(); } /// <summary> /// Determines whether a DICOM UID already is in use within a UID /// dictionary instance. /// </summary> public bool Contains(Uid uid) { return hashTable.Contains(uid.ToString()); } /// <summary> /// Returns a dictionary entry by specified DICOM UID. /// </summary> public UidDictionaryEntry GetDictionaryEntry(Uid uid) { return this[uid]; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -