Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using corel = Corel.Interop.VGCore;
- using Microsoft.VisualBasic.FileIO;
- using System.Text.RegularExpressions;
- namespace Voyager
- {
- public struct Options
- {
- public string path { get; set; }
- public int start_row { get; set; }
- public string id_column { get; set; }
- public string price_column { get; set; }
- public bool hide_price { get; set; }
- public string lang_1_column { get; set; }
- public string lang_2_column { get; set; }
- }
- public partial class DockerUI : UserControl
- {
- private corel.Application app;
- private Styles.StylesController stylesController;
- public DockerUI(object _app)
- {
- InitializeComponent();
- try
- {
- this.app = _app as corel.Application;
- stylesController = new Styles.StylesController(this.Resources, this.app);
- }
- catch
- {
- global::System.Windows.MessageBox.Show("VGCore Erro");
- }
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- stylesController.LoadThemeFromPreference();
- }
- private Options GetOptions()
- {
- Options opts = new Options();
- opts.hide_price = hidePriceBox.IsChecked.GetValueOrDefault(false);
- opts.lang_1_column = lang1Box.Text;
- opts.lang_2_column = lang2Box.Text;
- opts.price_column = priceBox.Text;
- opts.id_column = idBox.Text;
- opts.path = fileBox.Text;
- if (opts.path.Length == 0)
- {
- throw new System.ArgumentException("Plik CSV musi byc wybrany");
- }
- if (opts.id_column.Length == 0)
- {
- throw new System.ArgumentException("Kolumna z numerem produktu musi byc podana");
- }
- try
- {
- opts.start_row = Int32.Parse(firstRowBox.Text);
- }
- catch
- {
- throw new System.ArgumentException("Pierwszy rzad musi byc liczba. Podano: " + firstRowBox.Text);
- }
- return opts;
- }
- private void RunButton_Click(object sender, RoutedEventArgs e)
- {
- statusField.Content = "";
- Options opts;
- try
- {
- opts = GetOptions();
- }
- catch (Exception ex)
- {
- System.Windows.MessageBox.Show(ex.ToString());
- return;
- }
- /*System.Windows.MessageBox.Show(
- "file: " + opts.path + "\n" +
- "first row: " + opts.start_row + "\n" +
- "id: " + opts.id_column + "\n" +
- "price: " + opts.price_column + "\n" +
- "lang1: " + opts.lang_1_column + "\n" +
- "lang2: " + opts.lang_2_column + "\n");
- */
- Process(opts, (double status) => {
- statusField.Content = "Progress: " + status + "%";
- });
- statusField.Content = "Zakonczone.";
- }
- private void FileButton_Click(object sender, RoutedEventArgs e)
- {
- var dlg = new Microsoft.Win32.OpenFileDialog();
- dlg.FileName = fileBox.Text; // Default file name
- dlg.DefaultExt = ".csv"; // Default file extension
- dlg.Filter = "Comma Separaated Values (.csv)|*.csv"; // Filter files by extension
- Nullable<bool> result = dlg.ShowDialog();
- if (result == true)
- {
- fileBox.Text = dlg.FileName;
- }
- else
- {
- fileBox.Text = "";
- }
- }
- // Corel macro related code:
- public class Data
- {
- public struct Row
- {
- public string price { get; set; }
- public string lang1 { get; set; }
- public string lang2 { get; set; }
- }
- Dictionary<string, Row> data;
- // Returns 0-based column index in excel. Returns -1 if empty.
- private int ColumnNameToIndex(string name_any)
- {
- string name = name_any.ToUpper();
- int number = 0;
- int pow = 1;
- for (int i = name.Length - 1; i >= 0; i--)
- {
- if (name[i] < 'A' || name[i] > 'Z')
- throw new System.ArgumentException("Nazwa kolumny moze byc tylko literami. Znaleziono: " + name_any);
- number += (name[i] - 'A' + 1) * pow;
- pow *= 26;
- }
- return number - 1;
- }
- private string GetField(string field_name, ref string[] fields)
- {
- int index = ColumnNameToIndex(field_name);
- if (index < 0 || index >= fields.Length) return "";
- return fields[index];
- }
- public Data(Options opts)
- {
- data = new Dictionary<string, Row>();
- using (TextFieldParser parser = new TextFieldParser(opts.path))
- {
- parser.TextFieldType = FieldType.Delimited;
- parser.SetDelimiters(";");
- int current_row = 1; // In excel rows are 1-based.
- while (!parser.EndOfData)
- {
- string[] fields = parser.ReadFields();
- if (current_row++ < opts.start_row) continue;
- string long_id = GetField(opts.id_column, ref fields);
- string price = GetField(opts.price_column, ref fields);
- string lang1 = GetField(opts.lang_1_column, ref fields);
- string lang2 = GetField(opts.lang_2_column, ref fields);
- if (long_id.Length == 0) continue;
- Row row = new Row { price = price, lang1 = lang1, lang2 = lang2 };
- //MessageBox.Show("id: '" + short_id + "' long_id: '" + long_id + "' price: '" + price + "' lang1: '" + lang1 + "' lang2: '" + lang2);
- data.Add(long_id, row);
- if (IsLongId(long_id))
- {
- string short_id = ShortId(long_id);
- if (!data.ContainsKey(short_id)) data.Add(short_id, row);
- }
- }
- }
- }
- private string ShortId(string long_id)
- {
- return long_id.Split(new char[] { '-' })[0];
- }
- private bool IsLongId(string long_id)
- {
- return long_id.Contains("-");
- }
- public bool HasProduct(string id)
- {
- return data.ContainsKey(id) || (IsLongId(id) && data.ContainsKey(ShortId(id)));
- }
- public string Price(string id)
- {
- if (!data.ContainsKey(id))
- {
- if (IsLongId(id)) return Price(ShortId(id));
- throw new System.ArgumentException("Bad id: " + id);
- }
- return data[id].price;
- }
- public string Lang1(string id)
- {
- if (!data.ContainsKey(id))
- {
- if (IsLongId(id)) return Price(ShortId(id));
- throw new System.ArgumentException("Bad id: " + id);
- }
- return data[id].lang1;
- }
- public string Lang2(string id)
- {
- if (!data.ContainsKey(id))
- {
- if (IsLongId(id)) return Price(ShortId(id));
- throw new System.ArgumentException("Bad id: " + id);
- }
- return data[id].lang2;
- }
- }
- public class Item
- {
- private corel.Shape code;
- private corel.Shape description;
- public string id { get; set; }
- public Item(corel.Shape _code, corel.Shape _description)
- {
- code = _code;
- description = _description;
- id = code.Text.Contents;
- //MessageBox.Show("" + description.Text.Contents.Count(f => f == '\r'));
- }
- public void ModifyPrice(string price)
- {
- corel.TextRange paragraph = description.Text.Frame.Range.Paragraphs[4];
- paragraph.Text = "Cena: " + price + " PLN";
- corel.Color color = new corel.Color();
- color.CMYKAssign(0, 0, 0, 100);
- paragraph.Fill.ApplyUniformFill(color);
- }
- public void HidePrice()
- {
- corel.TextRange paragraph = description.Text.Frame.Range.Paragraphs[4];
- paragraph.Fill.ApplyNoFill();
- }
- public void ModifyLang1(string content)
- {
- corel.TextRange paragraph = description.Text.Frame.Range.Paragraphs[1];
- paragraph.Text = content + "\r\n";
- }
- public void ModifyLang2(string content)
- {
- corel.TextRange paragraph = description.Text.Frame.Range.Paragraphs[2];
- paragraph.Text = content + "\r\n";
- }
- public void AddAlert()
- {
- corel.Color color = new corel.Color();
- color.CMYKAssign(0, 100, 100, 0);
- foreach (corel.TextRange paragraph in code.Text.Frame.Range.Paragraphs)
- paragraph.Fill.ApplyUniformFill(color);
- foreach (corel.TextRange paragraph in description.Text.Frame.Range.Paragraphs)
- paragraph.Fill.ApplyUniformFill(color);
- }
- }
- private void Startup()
- {
- }
- private void Traverse(corel.Shape parent, List<corel.Shape> shapes)
- {
- shapes.Add(parent);
- if (parent.PowerClip != null)
- {
- foreach (corel.Shape shape in parent.PowerClip.Shapes)
- {
- Traverse(shape, shapes);
- }
- }
- }
- private List<corel.Shape> GetShapes(corel.Page page)
- {
- List<corel.Shape> shapes = new List<corel.Shape>();
- foreach (corel.Layer layer in page.Layers)
- {
- if (layer.IsGuidesLayer) continue;
- foreach (corel.Shape shape in layer.Shapes)
- {
- Traverse(shape, shapes);
- }
- }
- return shapes;
- }
- private List<corel.Shape> GetShapes(corel.Document doc)
- {
- List<corel.Shape> shapes = new List<corel.Shape>();
- foreach (corel.Page page in doc.Pages)
- {
- shapes.AddRange(GetShapes(page));
- }
- return shapes;
- }
- List<Item> GetItems(corel.Page page)
- {
- List<corel.Shape> shapes = GetShapes(page);
- List<corel.Shape> codes = new List<corel.Shape>();
- List<corel.Shape> descs = new List<corel.Shape>();
- foreach (corel.Shape shape in shapes)
- {
- if (shape.Type != corel.cdrShapeType.cdrTextShape) continue;
- if (Regex.Match(shape.Text.Contents, "^[A-Z]{1,2}[0-9]{3,5}(-[0-9]{1,2}/*[A-Z]{0,5})?$").Success)
- {
- codes.Add(shape);
- }
- else if (Regex.Match(shape.Text.Contents, "Cena:").Success)
- {
- descs.Add(shape);
- }
- }
- if (codes.Count != descs.Count)
- {
- //System.Windows.MessageBox.Show("Znaleziono inna ilosc pol tekstowych z numerem produktu, niz pol tekstowych z cena.");
- //string msg = "";
- //foreach (corel.Shape shape in codes) msg += shape.Text.Contents + "\n\n";
- //System.Windows.MessageBox.Show(msg);
- //msg = "";
- //foreach (corel.Shape shape in descs) msg += shape.Text.Contents + "\n\n";
- //System.Windows.MessageBox.Show(msg);
- throw new System.ArgumentException("Did not find correct number of textboxes");
- }
- List<Item> items = new List<Item>();
- HashSet<int> used = new HashSet<int>();
- for (int c = 0; c < codes.Count; ++c)
- {
- int best = -1;
- double closest = 100000000;
- for (int d = 0; d < descs.Count; ++d)
- {
- if (used.Contains(d)) continue;
- // Manhattan distance between the text fields' bottom left and upper left corner.
- double dist = Math.Abs(codes[c].BoundingBox.Bottom - descs[d].BoundingBox.Top) + Math.Abs(codes[c].BoundingBox.Left - descs[d].BoundingBox.Left);
- if (dist < closest) { closest = dist; best = d; }
- }
- if (best == -1) throw new System.ArgumentException("Did not find best match for: " + codes[c].Text.Contents);
- used.Add(best);
- items.Add(new Item(codes[c], descs[best]));
- }
- return items;
- }
- public void Process(Options opts, Action<double> progress_func)
- {
- Data data = new Data(opts);
- HashSet<string> missing_data = new HashSet<string>();
- HashSet<string> bad_pages = new HashSet<string>();
- app.ActiveDocument.BeginCommandGroup();
- int processed_pages = 0;
- foreach (corel.Page page in app.ActiveDocument.Pages)
- {
- List<Item> items;
- try
- {
- items = GetItems(page);
- }
- catch
- {
- bad_pages.Add((processed_pages + 1).ToString());
- continue;
- }
- foreach (Item item in items)
- {
- if (!data.HasProduct(item.id))
- {
- missing_data.Add(item.id + "(strona " + (processed_pages + 1) + ")");
- item.AddAlert();
- continue;
- }
- if (opts.price_column.Length != 0) item.ModifyPrice(data.Price(item.id));
- if (opts.lang_1_column.Length != 0) item.ModifyLang1(data.Lang1(item.id));
- if (opts.lang_2_column.Length != 0) item.ModifyLang2(data.Lang2(item.id));
- if (opts.hide_price) item.HidePrice();
- }
- processed_pages++;
- progress_func((double)(processed_pages) / app.ActiveDocument.Pages.Count);
- }
- app.ActiveDocument.EndCommandGroup();
- if (missing_data.Count > 0)
- {
- System.Windows.MessageBox.Show("Brak danych w arkuszu dla produktow: " + String.Join(", ", missing_data));
- }
- if (bad_pages.Count > 0)
- {
- System.Windows.MessageBox.Show("Ilosc numerow produktow jest inna niz ilosc cen na stronach: " + String.Join(", ", bad_pages));
- }
- if (bad_pages.Count == 0 && missing_data.Count == 0)
- {
- System.Windows.MessageBox.Show("OK!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement