public static (string className, SyntaxNode node) Parse(this DictionaryData entry) { if (entry.VR.StartsWith("See")) { return(null, null); } //var vr = entry.VR[0..2]; var vr = entry.VR.Substring(0, 2); var className = VRDictionary.GetVRFromAbbreviation(vr).ToString(); var i = (IDICOMElement)EvilDicomAssembly.CreateInstance($"EvilDICOM.Core.Element.{className}"); if (className == "DateTime") { className = "Element.DateTime"; } var dataType = i.DatType; // hack to make sure we are in right namespace - EvilDICOM has DateTime VR and so does system var dataTypeName = dataType.Name.StartsWith("Nullable") ? "System.DateTime?" : dataType.Name; // initialize strings as empty string instead of null var parameter = G.ParameterDeclaration("data", G.IdentifierName($"params {dataTypeName}[]")); return(className, parameter); }
public static VR PeekVR(DICOMBinaryReader dr) { byte[] vrBytes = dr.Peek(2); char[] vr = Encoding.UTF8.GetChars(vrBytes); VR foundVR = VRDictionary.GetVRFromAbbreviation(new string(vr)); return(foundVR); }
public static VR ReadVR(DICOMBinaryReader dr) { char[] vr = dr.ReadChars(2); VR foundVR = VRDictionary.GetVRFromAbbreviation(new string(vr)); if (foundVR == VR.Null) { throw new Exception(ExceptionHelper.VRReadException); } return(foundVR); }
public static IDICOMElement ToDICOMElement(this XElement xel) { if (xel.Name != "DICOMElement") { throw new ArgumentException("XML element must be of type DICOMElement <DICOMElement ..."); } var attr = xel.Attributes(); if (!attr.Any(a => a.Name == "VR")) { throw new ArgumentException("XML element must have attribute 'VR'."); } var vr = VRDictionary.GetVRFromAbbreviation(attr.First(a => a.Name == "VR").Value); if (!attr.Any(a => a.Name == "Tag")) { throw new ArgumentException("XML element must have attribute 'Tag'."); } var tag = new Tag(attr.First(a => a.Name == "Tag").Value); var data = xel.Elements("Data").Select(d => d.Value).ToArray(); return(ElementFactory.GenerateElementFromStringData(tag, vr, data)); }
public static MemberDeclarationSyntax GenerateProperty(IDICOMElement el) { var entry = DICOMDictionary.Instance.Entries.FirstOrDefault(e => e.Id == el.Tag.CompleteID); if (entry == null) { return(null); } var type = VRDictionary.GetDataTypeFromVR(VRDictionary.GetVRFromAbbreviation(entry.VR)); var propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(type.ToString()), entry.Keyword); if (type.IsGenericType) { propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(type.ToGenericTypeString()), entry.Keyword); } var getTemplate = $"var el = DCM.GetSelector().{entry.Keyword}; return el != null ? el.Data : default({type.ToGenericTypeString()});"; var setTemplate = $"DCM.GetSelector().{entry.Keyword} = DICOMForge.{entry.Keyword}(value);"; if (el is Sequence) { getTemplate = $"return DCM.GetSelector().{entry.Keyword}?.Items.Select(i => new {entry.Keyword.Replace("Sequence", "")}() {{ DCM = i }}).ToList();"; setTemplate = $"DCM.ReplaceOrAdd(DICOMForge.{entry.Keyword}(value.Select(v=>v.DCM).ToArray()));"; propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName($"List<{entry.Keyword.Replace("Sequence", "")}>"), entry.Keyword.Replace("Sequence", "s")); } var getStatement = SyntaxFactory.ParseStatement(getTemplate); var setStatement = SyntaxFactory.ParseStatement(setTemplate); propertyDeclaration = propertyDeclaration .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) .AddAccessorListAccessors( SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithBody(SyntaxFactory.Block(getStatement)), SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithBody(SyntaxFactory.Block(setStatement))); return(propertyDeclaration); }
static void Main(string[] args) { var ionPlan = DomainBuilder.BuildFromFile(@"D:\CALIBRATION_2.5SS_10x10_50MUPS.dcm", "IonPlan"); DomainWriter.WriteObjects(ionPlan, @"D:\DomainObjects"); //SOPClassUIDBuilder.BuildSOPClassUIDs(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\Helpers\SOPClassUID.cs"); //SOPClassUIDBuilder.BuildSOPClassDictionary(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\Helpers\SOPClassDictionary.cs"); //SOPClassUIDBuilder.BuildSOPClass(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\Enums\SOPClass.cs"); var g = GeneratorBuilder.Instance.Generator; //DICOMDefinitionLoader.UpdateCurrentFromWeb(); var dictionary = DICOMDictionary.Instance.Entries; // Get Anonymization tags var anonTags = DICOMDefinitionLoader.LoadAnonymizationTags().ToList(); var forgeNodes = new List <SyntaxNode>(); var tags = new List <SyntaxNode>(); var selectors = new List <SyntaxNode>(); var seqSelectors = new List <SyntaxNode>(); var anonymizationNodes = new List <SyntaxNode>(); List <SyntaxNode> anonProfile = new List <SyntaxNode>(); anonProfile.Add(g.AssignmentStatement(g.IdentifierName("var profile"), g.IdentifierName("new List<IDICOMElement>()"))); foreach (var atag in anonTags) { var action = "AnonymizeAction.REMOVE_ELEMENT"; switch (atag.Metadata) { case "D": action = "AnonymizeAction.DUMMY_DATA"; break; case "Z": case "Z/D": action = "AnonymizeAction.NULL_DATA"; break; case "X/Z": case "X/D": case "X/Z/D": case "X": action = "AnonymizeAction.REMOVE_ELEMENT"; break; case "K": case "C": action = "AnonymizeAction.CLEAN"; break; case "X/Z/U*": case "U": action = "AnonymizeAction.CLEAN"; break; } atag.VR = dictionary.FirstOrDefault(d => d.Id == atag.Id)?.VR; var entry = g.IdentifierName($"yield return new ConfidentialElement(){{Id=\"{atag.Id}\", ElementName=\"{atag.Name}\", VR=VR.{VRDictionary.GetVRFromAbbreviation(atag.VR)}, Action = {action}}} )"); anonProfile.Add(entry); } anonProfile.Add(g.ReturnStatement(g.IdentifierName("profile"))); var method = g.MethodDeclaration("GenerateProfileElements", null, null, g.IdentifierName("List<IDICOMElement>"), Accessibility.Public, DeclarationModifiers.Static, anonProfile); var anonNode = g.ClassDeclaration("AnonStub", null, Accessibility.Public, DeclarationModifiers.Static, null, null, new SyntaxNode[] { method }); var anonMethod = g.CompilationUnit(anonNode).NormalizeWhitespace().ToString(); foreach (var entry in dictionary.Where(d => !string.IsNullOrEmpty(d.Keyword))) { //Build a tag no matter what tags.Add(TagBuilder.Generate(g, entry)); var(cName, parameter) = EntryParser.Parse(g, entry); if (cName != null) { //SELECTOR var(propGetStatements, propSetStatements) = SelectorBuilder.GeneratePropertyStatements(g, cName, entry); var sel = g.PropertyDeclaration(entry.Keyword, g.IdentifierName(cName), Accessibility.Public, DeclarationModifiers.None, propGetStatements, propSetStatements); selectors.Add(sel); seqSelectors.Add(g.PropertyDeclaration(entry.Keyword, g.IdentifierName(cName), Accessibility.Public, DeclarationModifiers.ReadOnly, SelectorBuilder.GenerateSequencePropertyStatements(g, cName, entry), null)); // return _dicom.FindAll("00000000").Select(d => d as UnsignedLong).ToList(); var returnMany = g.ReturnStatement(g.InvocationExpression( g.IdentifierName($"_dicom.FindAll(\"{entry.Id}\").Select(d => d as {cName}).ToList"))); var returnManySeq = g.ReturnStatement(g.InvocationExpression( g.IdentifierName($"Items.FindAll<{cName}>(\"{entry.Id}\").ToList"))); selectors.Add(g.PropertyDeclaration(entry.Keyword + "_", g.IdentifierName($"List<{cName}>"), Accessibility.Public, DeclarationModifiers.ReadOnly, new SyntaxNode[] { returnMany })); var selSeqProp = g.PropertyDeclaration(entry.Keyword + "_", g.IdentifierName($"List<{cName}>"), Accessibility.Public, DeclarationModifiers.ReadOnly, new SyntaxNode[] { returnManySeq }); seqSelectors.Add(selSeqProp); //FORGE var methStatements = new SyntaxNode[] { // return new UnsignedLong { Tag = new Tag("00000001") }; g.AssignmentStatement(g.IdentifierName("var element"), g.ObjectCreationExpression(g.IdentifierName(cName))), g.AssignmentStatement(g.IdentifierName("element.Tag"), g.ObjectCreationExpression(g.IdentifierName("Tag"), g.Argument(RefKind.None, g.LiteralExpression(entry.Id)))), g.AssignmentStatement(g.IdentifierName("element.Data_"), g.IdentifierName("data?.ToList()")), g.ReturnStatement(g.IdentifierName("element")) }; var m = g.MethodDeclaration(entry.Keyword, new SyntaxNode[] { parameter }, null, g.IdentifierName(cName), Accessibility.Public, DeclarationModifiers.Static, methStatements); forgeNodes.Add(m); } } var forgeNode = g.ClassDeclaration("DICOMForge", null, Accessibility.Public, DeclarationModifiers.Static, null, null, forgeNodes); var namespaceDeclaration = g.NamespaceDeclaration("EvilDICOM.Core", forgeNode); forgeNode = g.CompilationUnit(UsingsHelper.GetUsings().Concat(new SyntaxNode[] { namespaceDeclaration })).NormalizeWhitespace(); //public UnsignedLong CommandGroupLength //{ // get { return _dicom.FindFirst("00000000") as UnsignedLong; } //set { _dicom.ReplaceOrAdd(value); } //} var tagHelperNode = g.ClassDeclaration("TagHelper", null, Accessibility.Public, DeclarationModifiers.Static, null, null, tags); namespaceDeclaration = g.NamespaceDeclaration("EvilDICOM.Core.Helpers", tagHelperNode); tagHelperNode = g.CompilationUnit(UsingsHelper.GetUsings().Concat(new SyntaxNode[] { namespaceDeclaration })).NormalizeWhitespace(); var selectorNode = g.ClassDeclaration("DICOMSelector", null, Accessibility.Public, DeclarationModifiers.Partial, null, null, selectors); namespaceDeclaration = g.NamespaceDeclaration("EvilDICOM.Core.Selection", selectorNode); selectorNode = g.CompilationUnit(UsingsHelper.GetUsings().Concat(new SyntaxNode[] { namespaceDeclaration })).NormalizeWhitespace(); var selectorSeqNode = g.ClassDeclaration("SequenceSelector", null, Accessibility.Public, DeclarationModifiers.Partial, g.IdentifierName("AbstractElement<DICOMSelector>"), null, seqSelectors); namespaceDeclaration = g.NamespaceDeclaration("EvilDICOM.Core.Selection", selectorSeqNode); selectorSeqNode = g.CompilationUnit(UsingsHelper.GetUsings().Concat(new SyntaxNode[] { namespaceDeclaration })).NormalizeWhitespace(); //File.WriteAllText(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\DICOMForge.cs", forgeNode.NormalizeWhitespace().ToString()); //File.WriteAllText(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\Helpers\TagHelper.cs", tagHelperNode.NormalizeWhitespace().ToString()); File.WriteAllText(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\Selection\DICOMSelectorProperties.cs", selectorNode.NormalizeWhitespace().ToString()); File.WriteAllText(@"D:\OneDrive\Cardan.Code\Git\Evil-DICOM\EvilDICOM\EvilDICOM\Core\Selection\SequenceSelectorProperties.cs", selectorSeqNode.NormalizeWhitespace().ToString()); }
// TODO public static void GenerateAnonStuff() { var dictionary = DicomDefinitionLoader.LoadCurrentDictionary().ToList(); var anonProfile = new List <SyntaxNode> { G.AssignmentStatement(G.IdentifierName("var profile"), G.IdentifierName("new List<IDICOMElement>()")) }; foreach (var anonTag in DicomDefinitionLoader.LoadAnonymizationTags()) { var action = "AnonymizeAction.REMOVE_ELEMENT"; switch (anonTag.Metadata) { case "D": action = "AnonymizeAction.DUMMY_DATA"; break; case "Z": case "Z/D": action = "AnonymizeAction.NULL_DATA"; break; case "X/Z": case "X/D": case "X/Z/D": case "X": action = "AnonymizeAction.REMOVE_ELEMENT"; break; case "K": case "C": action = "AnonymizeAction.CLEAN"; break; case "X/Z/U*": case "U": action = "AnonymizeAction.CLEAN"; break; } anonTag.VR = dictionary.FirstOrDefault(d => d.Id == anonTag.Id)?.VR; var entry = G.IdentifierName( $"yield return new ConfidentialElement(){{Id=\"{anonTag.Id}\", ElementName=\"{anonTag.Name}\", VR=VR.{VRDictionary.GetVRFromAbbreviation(anonTag.VR)}, Action = {action}}} )"); anonProfile.Add(entry); } anonProfile.Add(G.ReturnStatement(G.IdentifierName("profile"))); var method = G.MethodDeclaration("GenerateProfileElements", null, null, G.IdentifierName("List<IDICOMElement>"), Accessibility.Public, DeclarationModifiers.Static, anonProfile); var anonClass = CodeGenHelper.PublicStaticClass("AnonStub", new[] { method }); var anonMethod = G.CompilationUnit(anonClass).NormalizeWhitespace().ToString(); }