Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ICP CON import] Create device name matching file #1510

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using EasyEPlanner.ProjectImportICP;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyEplannerTests.ProjectImportICPTest
{
public class DeviceNameMatchingSaverTest
{
[Test]
public void SaveImportDevices()
{
var devices = new List<ImportDevice>()
{
new ImportDevice() { WagoType = "V", FullNumber = 101, Object = "LINE1", Type = "V", Number = 1, Description = "desc LINE1V1"},
new ImportDevice() { WagoType = "V", FullNumber = 102, Object = "LINE1", Type = "V", Number = 2, Description = "desc; LINE1V2"},
};

var filePath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "DeviceNameMatching.csv");

DeviceNameMatchingSaver.Save(filePath, devices);

Assert.AreEqual(
"Название в ICP CON;Новое название;Описание\r\n" +
"V101;LINE1V1;desc LINE1V1\r\n" +
"V102;LINE1V2;\"desc; LINE1V2\"\r\n",
File.ReadAllText(filePath));
}
}
}
28 changes: 28 additions & 0 deletions src/EasyEPlanner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>libs\Aga.Controls.dll</HintPath>
</Reference>
<Reference Include="CsvHelper, Version=33.0.0.0, Culture=neutral, PublicKeyToken=8c4959082be5c823, processorArchitecture=MSIL">
<HintPath>packages\CsvHelper.33.0.1\lib\net47\CsvHelper.dll</HintPath>
</Reference>
<Reference Include="Eplan.EplApi.AFu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57aaa27e22f7b107, processorArchitecture=AMD64" />
<Reference Include="Eplan.EplApi.Baseu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57aaa27e22f7b107, processorArchitecture=AMD64" />
<Reference Include="Eplan.EplApi.DataModelu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57aaa27e22f7b107, processorArchitecture=AMD64" />
Expand All @@ -80,6 +83,12 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>LuaInterface\LuaInterface.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.HashCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bcl.HashCode.1.1.1\lib\net461\Microsoft.Bcl.HashCode.dll</HintPath>
</Reference>
<Reference Include="ObjectListView, Version=2.9.1.23884, Culture=neutral, PublicKeyToken=8176a5df63fd6f38, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>libs\ObjectListView.dll</HintPath>
Expand All @@ -97,10 +106,26 @@
<HintPath>libs\Spire.XLS.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Memory.4.5.0\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -117,10 +142,12 @@
<Compile Include="EProjectManager\IEProjectManager.cs" />
<Compile Include="InterprojectExchange\IInterprojectExchange.cs" />
<Compile Include="InterprojectExchange\Models\ICurrentProjectModel.cs" />
<Compile Include="Main\ImportIcpCreateDeviceNameMathchingFile.cs" />
<Compile Include="Main\ImportIcpCreateRenamingMap.cs" />
<Compile Include="Main\ImportIcpWagoProject.cs" />
<Compile Include="Main\ModifyIcpChbase.cs" />
<Compile Include="ProjectImportICP\BindingImporter.cs" />
<Compile Include="ProjectImportICP\DeviceNameMatchingSaver.cs" />
<Compile Include="ProjectImportICP\DevicesImporter.cs" />
<Compile Include="Device\IDevice.cs" />
<Compile Include="Device\IIOChannel.cs" />
Expand Down Expand Up @@ -504,6 +531,7 @@
<None Include="Lua\sys_wago_modules_importer.lua">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<None Include="Resources\main.ico" />
</ItemGroup>
<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/Main/AddInModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public bool OnInitGui()
true, false);

menuID = oMenu.AddMenuItem(
"Создать файл переименования старых устройств",
"Создать/изменить файл переименования устройств",
nameof(ImportIcpCreateRenamingMap),
"Создать файл переименования старых устройств", menuID, 1,
"Создать/изменить файл переименования устройств", menuID, 1,
false, false);

menuID = oMenu.AddMenuItem(
"Модифицировать базу каналов",
nameof(ModifyIcpChbase),
"Модифицировать базу каналов", menuID, 1,
"Документ соответствия названий устройств CSV",
nameof(ImportIcpCreateDeviceNameMathchingFile),
"Документ соответствия названий устройств CSV", menuID, 1,
false, false);

menuID = oMenu.AddMenuItem(
Expand Down
69 changes: 69 additions & 0 deletions src/Main/ImportIcpCreateDeviceNameMathchingFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using CsvHelper.Configuration;
using CsvHelper;
using EasyEPlanner.ProjectImportICP;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.DataModel;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EplanDevice;
using StaticHelper;

namespace EasyEPlanner
{
[ExcludeFromCodeCoverage]
public class ImportIcpCreateDeviceNameMathchingFile : IEplAction
{
~ImportIcpCreateDeviceNameMathchingFile() { }

public bool Execute(ActionCallingContext oActionCallingContext)
{
try
{
Project currentProject = EProjectManager.GetInstance()
.GetCurrentPrj();
if (currentProject == null)
{
MessageBox.Show("Нет открытого проекта!", "EPlaner",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return true;
}

var openFileDialog = new SaveFileDialog()
{
Title = "Сохранение файла соответствия наименований устройств",
Filter = "CSV file (*.scv)|*.csv",
};

if (openFileDialog.ShowDialog() != DialogResult.Cancel)
{
DeviceNameMatchingSaver.Save(openFileDialog.FileName, DeviceManager.GetInstance().Devices);
}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return true;
}

public bool OnRegister(ref string Name, ref int Ordinal)
{
Name = nameof(ImportIcpCreateDeviceNameMathchingFile);
Ordinal = 30;

return true;
}

public void GetActionProperties(ref ActionProperties actionProperties)
{
}
}
}
19 changes: 18 additions & 1 deletion src/Main/ImportIcpWagoProject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BrightIdeasSoftware;
using CsvHelper;
using CsvHelper.Configuration;
using EasyEPlanner.ProjectImportICP;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.DataModel;
Expand Down Expand Up @@ -77,6 +79,21 @@ public bool Execute(ActionCallingContext oActionCallingContext)

new BindingImporter(modulesImporter.ImportModules, devicesImporter.ImportDevices).Bind();

if (DialogResult.Yes == MessageBox.Show("Сохранить файл соответствия наименования устройств?",
"EPlanner", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
var openFileDialog = new SaveFileDialog()
{
Title = "Сохранение файла соответствия наименования устройств",
Filter = "CSV file (*.scv)|*.csv",
};

if (openFileDialog.ShowDialog() != DialogResult.Cancel)
{
DeviceNameMatchingSaver.Save(openFileDialog.FileName, devicesImporter.ImportDevices);
}
}

Logs.EnableButtons();
Logs.ShowLastLine();
Logs.SetProgress(100);
Expand All @@ -101,4 +118,4 @@ public void GetActionProperties(ref ActionProperties actionProperties)
{
}
}
}
}
119 changes: 119 additions & 0 deletions src/ProjectImportICP/DeviceNameMatchingSaver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using CsvHelper.Configuration;
using CsvHelper;
using EplanDevice;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StaticHelper;
using System.Diagnostics.CodeAnalysis;

namespace EasyEPlanner.ProjectImportICP
{
public static class DeviceNameMatchingSaver
{
/// <summary>
/// Название 1й колонки в CSV
/// </summary>
static public readonly string IcpNameColumn = "Название в ICP CON";

/// <summary>
/// Название 2й колонки в CSV
/// </summary>
static public readonly string NewNameColumn = "Новое название";

/// <summary>
/// Название 3й колонки в CSV
/// </summary>
static public readonly string DescriptionColumn = "Описание";

/// <summary>
/// Сохранить CSV-файл соответствия названий устройств между <br/>
/// ICP CON проектом и новым проектом
/// </summary>
/// <param name="path">Путь для сохранения файла</param>
/// <param name="devices">Устройства</param>
public static void Save(string path, IEnumerable<ImportDevice> devices)
=> Save<CsvImportDeviceMap, ImportDevice>(path, devices);

/// <summary>
/// Сохранить CSV-файл соответствия названий устройств между <br/>
/// ICP CON проектом и новым проектом
/// </summary>
/// <param name="path">Путь для сохранения файла</param>
/// <param name="devices">Устройства</param>
public static void Save(string path, IEnumerable<IODevice> devices)
=> Save<CsvIODeviceMap, IODevice>(path, devices);

/// <summary>
/// Сохранить CSV-файл соответствия названий устройств между <br/>
/// ICP CON проектом и новым проектом
/// </summary>
/// <typeparam name="TMap">Карта записи объектов в CSV</typeparam>
/// <typeparam name="TDevice">Тип устройства (<see cref="IODevice"/>/<see cref="ImportDevice"/>) </typeparam>
/// <param name="path">Путь для сохранения файла</param>
/// <param name="devices">Список устройства типа <typeparamref name="TDevice"/></param>
private static void Save<TMap, TDevice>(string path, IEnumerable<TDevice> devices)
where TMap : ClassMap<TDevice>
{
using (StreamWriter writer = new StreamWriter(path))
using (CsvWriter csvWriter =
new CsvWriter(writer, new CsvConfiguration(System.Globalization.CultureInfo.CurrentCulture)
{
Delimiter = ";",
Encoding = Encoding.UTF8,

}))
{
csvWriter.Context.RegisterClassMap<TMap>();
csvWriter.WriteRecords(devices);
}
}
}

public class CsvImportDeviceMap : ClassMap<ImportDevice>
{
public CsvImportDeviceMap()
{
Map(m => m.FullNumber)
.Index(0)
.Name(DeviceNameMatchingSaver.IcpNameColumn)
.Convert(m => $"{m.Value.WagoType}{m.Value.FullNumber}");

Map(m => m.Number)
.Index(1)
.Name(DeviceNameMatchingSaver.NewNameColumn)
.Convert(m => $"{m.Value.Object}{m.Value.Type}{m.Value.Number}");

Map(m => m.Description)
.Index(2)
.Name(DeviceNameMatchingSaver.DescriptionColumn);
}
}

[ExcludeFromCodeCoverage]
public class CsvIODeviceMap : ClassMap<IODevice>
{
private static readonly ApiHelper ApiHelper = new ApiHelper();

public CsvIODeviceMap()
{
Map(m => m.EplanName)
.Index(0)
.Name(DeviceNameMatchingSaver.IcpNameColumn)
.Convert(m => ApiHelper.GetSupplementaryFieldValue(m.Value.EplanObjectFunction, 10));

Map(m => m.Name)
.Index(1)
.Name(DeviceNameMatchingSaver.NewNameColumn);

Map(m => m.Description)
.Index(2)
.Name(DeviceNameMatchingSaver.DescriptionColumn);
}
}

}
12 changes: 12 additions & 0 deletions src/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CsvHelper" version="33.0.1" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net472" />
<package id="Microsoft.Bcl.HashCode" version="1.1.1" targetFramework="net472" />
<package id="Microsoft.CSharp" version="4.7.0" targetFramework="net472" />
<package id="System.Buffers" version="4.4.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.0" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
</packages>