Skip to content

Commit

Permalink
Merged PR 658: Fix SDF Export
Browse files Browse the repository at this point in the history
Related work items: #1108
  • Loading branch information
MikeWilliams-UK committed Dec 21, 2023
1 parent 84ed24d commit 3f00015
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 58 deletions.
4 changes: 4 additions & 0 deletions src/Chem4Word.V3-3.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chem4Word.Searcher.ChEBIPlugin", "PlugIns\Searchers\Chem4Word.Searcher.ChEBIPlugin\Chem4Word.Searcher.ChEBIPlugin.csproj", "{0EB3EFF1-FA71-4E65-8B4F-4C6FC632817B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chem4Word.Searcher.ExamplePlugIn", "PlugIns\Searchers\Chem4Word.Searcher.ExamplePlugIn\Chem4Word.Searcher.ExamplePlugIn.csproj", "{9CD3871B-9A42-497B-9004-77C42C856889}"
ProjectSection(ProjectDependencies) = postProject
{1E3FC1F1-06CC-44BD-8004-3EF0CE7EEAA7} = {1E3FC1F1-06CC-44BD-8004-3EF0CE7EEAA7}
{C69BAB8F-4881-4D5F-9929-FC8470DF1E48} = {C69BAB8F-4881-4D5F-9929-FC8470DF1E48}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chem4Word.Searcher.OpsinPlugIn", "PlugIns\Searchers\Chem4Word.Searcher.OpsinPlugIn\Chem4Word.Searcher.OpsinPlugIn.csproj", "{061F7B7B-89AE-4095-B3BB-B297F015A038}"
EndProject
Expand Down
3 changes: 2 additions & 1 deletion src/Chem4Word.V3-3.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INT_ALIGN_SWITCH_SECTIONS/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/USE_INDENT_FROM_VS/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">---------------------------------------------------------------------------&#xD;
Copyright (c) $CURRENT_YEAR$, The .NET Foundation.&#xD;
Copyright (c) ${CurrentDate.Year}, The .NET Foundation.&#xD;
This software is released under the Apache License, Version 2.0.&#xD;
The license and further copyright text can be found in the file LICENSE.md&#xD;
at the root directory of the distribution.&#xD;
---------------------------------------------------------------------------&#xD;
</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
18 changes: 11 additions & 7 deletions src/Chem4Word.V3/Ribbon/Chem4WordRibbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ public static void PerformEdit()
var pc = new WebServices.PropertyCalculator(Globals.Chem4WordV3.Telemetry,
Globals.Chem4WordV3.WordTopLeft,
Globals.Chem4WordV3.AddInInfo.AssemblyVersionNumber);
afterModel.CreatorGuid = Globals.Chem4WordV3.Helper.MachineId;
var changedProperties = pc.CalculateProperties(afterModel);

if (isNewDrawing)
Expand Down Expand Up @@ -1403,15 +1404,18 @@ private void OnLoading_SearchItems(object sender, RibbonControlEventArgs e)
{
foreach (var searcher in Globals.Chem4WordV3.Searchers.OrderBy(s => s.DisplayOrder))
{
var ribbonButton = this.Factory.CreateRibbonButton();
if (searcher.DisplayOrder >= 0)
{
var ribbonButton = this.Factory.CreateRibbonButton();

ribbonButton.Label = searcher.ShortName;
ribbonButton.Tag = searcher.Name;
ribbonButton.SuperTip = searcher.Description;
ribbonButton.Image = searcher.Image;
ribbonButton.Click += OnClick_Searcher;
ribbonButton.Label = searcher.ShortName;
ribbonButton.Tag = searcher.Name;
ribbonButton.SuperTip = searcher.Description;
ribbonButton.Image = searcher.Image;
ribbonButton.Click += OnClick_Searcher;

WebSearchMenu.Items.Add(ribbonButton);
WebSearchMenu.Items.Add(ribbonButton);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Chem4Word.V3/UI/WPF/LibraryEditorControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ private void PerformEdit(ChemistryObject item)
var afterCml = editor.Cml;

var pc = new WebServices.PropertyCalculator(_telemetry, topLeft, Globals.Chem4WordV3.AddInInfo.AssemblyVersionNumber);
afterModel.CreatorGuid = Globals.Chem4WordV3.Helper.MachineId;
pc.CalculateProperties(afterModel);

using (var editLabelsHost =
Expand Down Expand Up @@ -909,6 +910,7 @@ private void OnClick_CalculateButton(object sender, RoutedEventArgs e)
model = protocolBufferConverter.Import(obj.Chemistry);
}

model.CreatorGuid = Globals.Chem4WordV3.Helper.MachineId;
var changed = pc.CalculateProperties(model, showProgress: false);
if (changed > 0)
{
Expand Down
64 changes: 29 additions & 35 deletions src/Chemistry/Chem4Word.Model2/Converters/MDL/CtabProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public override SdfState ImportFromStream(StreamReader reader, Molecule molecule
return result;
}

public void ExportToStream(List<Atom> atoms, List<Bond> bonds, StreamWriter writer)
public void ExportToStream(StreamWriter writer, List<Atom> atoms, List<Bond> bonds, string creator)
{
atomByNumber = new Dictionary<int, Atom>();
bondByNumber = new Dictionary<int, Bond>();
numberByAtom = new Dictionary<Atom, int>();

WriteHeader(writer, atoms.Count, bonds.Count);
WriteHeader(writer, atoms.Count, bonds.Count, creator);
WriteAtoms(writer, atoms);
WriteBonds(writer, bonds);
WriteProperties(writer, atoms);
Expand Down Expand Up @@ -344,16 +344,7 @@ private void ReadBonds(StreamReader reader, int bonds)

#region Exporting From Model

//private void WriteCtab(StreamWriter writer)
//{
// WriteHeader(writer);
// WriteAtoms(writer);
// WriteBonds(writer);
// WriteProperties(writer);
// writer.WriteLine(MDLConstants.M_END);
//}

private void WriteHeader(StreamWriter writer, int atoms, int bonds)
private void WriteHeader(StreamWriter writer, int atoms, int bonds, string creator)
{
// Line 1 - Molecule Name (80)
writer.WriteLine("");
Expand All @@ -373,7 +364,7 @@ private void WriteHeader(StreamWriter writer, int atoms, int bonds)
writer.WriteLine($" Chem4Wrd{SafeDate.ToMdlHeaderTime(DateTime.UtcNow)}");

// Line 3 - Comments (80)
writer.WriteLine("");
writer.WriteLine(Truncate80(creator));

// Counts line
// 01234567890123456789012345678901234567890123456789012345678901234567890123456789
Expand All @@ -389,6 +380,20 @@ private void WriteHeader(StreamWriter writer, int atoms, int bonds)
writer.WriteLine($"{OutputMDLInt(atoms)}{OutputMDLInt(bonds)} 0 0 0 0 V2000");
}

private string Truncate80(string info)
{
string result = string.Empty;

if (!string.IsNullOrEmpty(info))
{
result = info.Length > 80
? info.Substring(0, 80)
: info;
}

return result;
}

private void WriteAtoms(StreamWriter writer, List<Atom> atoms)
{
int i = 0;
Expand Down Expand Up @@ -649,25 +654,6 @@ private void WriteProperties(StreamWriter writer, List<Atom> atoms)
}
}

//private void WriteProperties(StreamWriter writer)
//{
// string p = CreateAtomPropertyLine(MDLConstants.M_CHG);
// if (!string.IsNullOrEmpty(p))
// {
// writer.WriteLine(p);
// }
// p = CreateAtomPropertyLine(MDLConstants.M_ISO);
// if (!string.IsNullOrEmpty(p))
// {
// writer.WriteLine(p);
// }
// p = CreateAtomPropertyLine(MDLConstants.M_RAD);
// if (!string.IsNullOrEmpty(p))
// {
// writer.WriteLine(p);
// }
//}

#endregion Exporting From Model

private string CreateAtomPropertyLines(string propertyType, List<Atom> atoms)
Expand Down Expand Up @@ -695,17 +681,17 @@ private string CreateAtomPropertyLines(string propertyType, List<Atom> atoms)
spin = atom.SpinMultiplicity.Value;
}

if (propertyType == MDLConstants.M_CHG & fCharge != 0)
if (propertyType == MDLConstants.M_CHG && fCharge != 0)
{
values.Add(atom.FormalCharge.Value);
atomNumbers.Add(atomNumber);
}
else if (propertyType == MDLConstants.M_ISO & isotope > 0.0001)
else if (propertyType == MDLConstants.M_ISO && isotope > 0.0001)
{
values.Add(atom.IsotopeNumber.Value);
atomNumbers.Add(atomNumber);
}
else if (propertyType == MDLConstants.M_RAD & spin > 0.0001)
else if (propertyType == MDLConstants.M_RAD && spin > 0.0001)
{
values.Add(atom.SpinMultiplicity.Value);
atomNumbers.Add(atomNumber);
Expand All @@ -716,16 +702,24 @@ private string CreateAtomPropertyLines(string propertyType, List<Atom> atoms)

StringBuilder output = new StringBuilder();

var lineFeedRequired = false;

for (int i = 0; i < (float)count / 8f; i++)
{
int thisLineCount = (count - i * 8) > 8 ? 8 : count - i * 8;
if (lineFeedRequired)
{
output.AppendLine("");
}
output.Append(propertyType + " " + thisLineCount);
for (int j = 0; j < thisLineCount; j++)
{
string atomNumber = OutputMDLInt(atomNumbers[j + i * 8]);
string value = OutputMDLInt(values[j + i * 8]);
output.Append(" " + atomNumber + " " + value);
}

lineFeedRequired = true;
}

return output.ToString().TrimEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public string Export(Model model)
}

CtabProcessor pct = new CtabProcessor();
pct.ExportToStream(atoms, bonds, writer);
pct.ExportToStream(writer, atoms, bonds, model.CreatorGuid);

DataProcessor dp = new DataProcessor(_propertyTypes);
dp.ExportToStream(names, formulas, writer);
Expand Down
1 change: 1 addition & 0 deletions src/Chemistry/Chem4Word.Model2/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ public double FontSize
}
}

public string CreatorGuid { get; set; }
public void SetXamlBondLength(int bondLength)
{
XamlBondLength = bondLength;
Expand Down
1 change: 1 addition & 0 deletions src/Chemistry/Chem4Word.WebServices/PropertyCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public int CalculateProperties(Model inputModel, bool showProgress = true)
var paths = new Dictionary<string, string>();

var tempModel = new Model();
tempModel.CreatorGuid = inputModel.CreatorGuid;

var inputMolecules = inputModel.GetAllMolecules();

Expand Down
4 changes: 0 additions & 4 deletions src/Common/Chem4Word.Telemetry/SystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,6 @@ DateTime LastEventDateTime(string query)
{
result = thisTime;
}
else
{
Debugger.Break();
}
}

eventInstance = elReader.ReadEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<ItemGroup>
<Compile Include="ExampleOptions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Searcher.cs" />
<Compile Include="ExampleSettings.cs">
<SubType>Form</SubType>
Expand All @@ -104,6 +109,10 @@
<EmbeddedResource Include="ExampleSettings.resx">
<DependentUpon>ExampleSettings.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Common\Chem4Word.Core\Chem4Word.Core.csproj">
Expand All @@ -115,6 +124,9 @@
<Name>Chem4Word.Contracts</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Resources\Images\Search.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>if "$(ConfigurationName)" == "Vso-Ci" (
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f00015

Please sign in to comment.