void setConditionsStatus() { if (pages == null) { return; } pages.ActiveTemplate = getTemplateFromUI(false); foreach (DataGridViewRow r in conditions.Rows) { setConditionStatus(r); } List <int> conditionAnchorIds = new List <int>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c != null && c.IsSet()) { conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value)); } } foreach (int anchorId in conditionAnchorIds.Distinct()) { _setAnchorStatus(anchorId); } }
static bool parseWithSubstitutedAnchorIds(string expression) { BooleanEngine be = new BooleanEngine(); be.expression = Regex.Replace(expression, @"\s", "", RegexOptions.Singleline); be.move2NextToken(false); return(be.parse(true)); }
void setCurrentConditionRow(DataGridViewRow row) { if (settingCurrentConditionRow) { return; } try { settingCurrentConditionRow = true; currentConditionRow = row; if (row == null) { conditions.ClearSelection(); conditions.CurrentCell = null; return; } conditions.CurrentCell = conditions[0, row.Index]; Template.Condition c = (Template.Condition)row.Tag; bool firstAnchor = true; foreach (int ai in BooleanEngine.GetAnchorIds(c.Value)) { Template.Anchor a = getAnchor(ai, out DataGridViewRow r); if (r == null) { continue; } if (firstAnchor) { firstAnchor = false; showAnchorRowAs(ai, rowStates.Linked, true); setCurrentAnchorRow(ai, true); clearImageFromBoxes(); } else { showAnchorRowAs(ai, rowStates.Condition, false); } findAndDrawAnchor(ai); } if (firstAnchor) { showAnchorRowAs(null, rowStates.NULL, true); setCurrentAnchorRow(null, true); } setCurrentFieldRow(null); setConditionStatus(currentConditionRow); } finally { settingCurrentConditionRow = false; } }
public bool IsCondition(string conditionName) { if (string.IsNullOrWhiteSpace(conditionName)) { throw new Exception("Condition name is not specified."); } var c = PageCollection.ActiveTemplate.Conditions.FirstOrDefault(a => a.Name == conditionName); if (string.IsNullOrWhiteSpace(c.Value)) { throw new Exception("Condition '" + conditionName + "' is not set."); } return(BooleanEngine.Parse(c.Value, this)); }
void setConditionRow(DataGridViewRow row, Template.Condition c) { row.Tag = c; c.Value = BooleanEngine.GetFormatted(c.Value); row.Cells["Name2"].Value = c.Name; row.Cells["Value2"].Value = c.Value; if (loadingTemplate) { return; } if (row == currentConditionRow) { setCurrentConditionRow(row); } }
internal Template GetTemplateFromUI(bool saving) { Template t = new Template(); if (saving && string.IsNullOrWhiteSpace(name.Text)) { throw new Exception("Name is empty!"); } t.Name = name.Text.Trim(); t.TextAutoInsertSpace = new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpaceThreshold.Value, Representative = Regex.Unescape(textAutoInsertSpaceRepresentative.Text), }; t.CvImageScalePyramidStep = (int)CvImageScalePyramidStep.Value; t.TesseractPageSegMode = (Tesseract.PageSegMode)TesseractPageSegMode.SelectedItem; if (SingleFieldFromFieldImage.Checked) { t.FieldOcrMode |= Template.FieldOcrModes.SingleFieldFromFieldImage; } else { t.FieldOcrMode &= ~Template.FieldOcrModes.SingleFieldFromFieldImage; } if (ColumnFieldFromFieldImage.Checked) { t.FieldOcrMode |= Template.FieldOcrModes.ColumnFieldFromFieldImage; } else { t.FieldOcrMode &= ~Template.FieldOcrModes.ColumnFieldFromFieldImage; } bitmapPreparationForm.SetTemplate(t); bool? removeNotLinkedAnchors = null; List <int> conditionAnchorIds = null; if (saving) { conditionAnchorIds = new List <int>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c != null && c.IsSet()) { conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value)); } } conditionAnchorIds = conditionAnchorIds.Distinct().ToList(); } t.Anchors = new List <Template.Anchor>(); foreach (DataGridViewRow r in anchors.Rows) { Template.Anchor a = (Template.Anchor)r.Tag; if (a == null) { continue; } if (saving) { if (!a.IsSet()) { throw new Exception("Anchor[Id=" + a.Id + "] is not set!"); } if (!conditionAnchorIds.Contains(a.Id)) { bool engaged = false; foreach (DataGridViewRow rr in anchors.Rows) { Template.Anchor a_ = (Template.Anchor)rr.Tag; if (a_ == null) { continue; } if (a_.ParentAnchorId == a.Id) { engaged = true; break; } } if (!engaged) { foreach (DataGridViewRow rr in fields.Rows) { Template.Field m = (Template.Field)rr.Tag; if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id)) { engaged = true; break; } } if (!engaged) { if (a.Id != t.ScalingAnchorId) { if (removeNotLinkedAnchors == null) { removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Remove them?"); } if (removeNotLinkedAnchors == true) { continue; } } } } } } t.Anchors.Add((Template.Anchor)Serialization.Json.Clone2(a)); } t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList(); if (saving) { t.GetScalingAnchor();//check is it is correct; } t.Conditions = new List <Template.Condition>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c == null) { continue; } if (saving) { if (!c.IsSet()) { throw new Exception("Condition[name=" + c.Name + "] is not set!"); } BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id)); } t.Conditions.Add(Serialization.Json.Clone(c)); } if (saving) { var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault(); if (dcs != null) { throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!"); } } t.Fields = new List <Template.Field>(); foreach (DataGridViewRow r in fields.Rows) { Template.Field f = (Template.Field)r.Tag; if (f == null) { continue; } if (saving && !f.IsSet()) { throw new Exception("Field[" + r.Index + "] is not set!"); } if (saving) { foreach (int?ai in new List <int?> { f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id }) { if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null) { throw new Exception("Anchor[Id=" + ai + " does not exist."); } } } t.Fields.Add(Serialization.Json.Clone(f)); } if (saving) { if (t.Fields.Count < 1) { throw new Exception("Fields is empty!"); } //var dfs = t.Fields.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault(); //if (dfs != null) // throw new Exception("Field '" + dfs.First().Name + "' is duplicated!"); //foreach (string columnOfTable in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable != null).Select(x => x.ColumnOfTable)) //{ // Dictionary<string, List<Template.Field>> fieldName2orderedFields = new Dictionary<string, List<Template.Field>>(); // foreach (Template.Field.PdfText pt in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable == columnOfTable)) // { // List<Template.Field> fs; // if (!fieldName2orderedFields.TryGetValue(pt.Name, out fs)) // { // fs = new List<Template.Field>(); // fieldName2orderedFields[pt.Name] = fs; // } // fs.Add(pt); // } // int definitionCount = fieldName2orderedFields.Max(x => x.Value.Count()); // foreach (string fn in fieldName2orderedFields.Keys) // if (definitionCount > fieldName2orderedFields[fn].Count) // throw new Exception("Field '" + fn + "' is column of table " + columnOfTable + " and so it must have the same number of definitions as the rest column fields!"); //} //foreach (Template.Field f0 in t.Fields) //{ // int tableDefinitionsCount = t.Fields.Where(x => x.Name == f0.Name).Count(); // string fn = t.Fields.Where(x => x.ColumnOfTable == f0.Name).GroupBy(x => x.Name).Where(x => x.Count() > tableDefinitionsCount).FirstOrDefault()?.First().Name; // if (fn != null) // throw new Exception("Field '" + fn + "' is a column of table field " + f0.Name + " so " + f0.Name + " must have number of definitions not less then that of " + fn + "."); //} //it is done only to avoid unneeded OCR //string fn = t.Fields.Where(x => x.ColumnOfTable != null && (x.DefaultValueType != Template.Field.ValueTypes.PdfText && x.DefaultValueType != Template.Field.ValueTypes.PdfTextLines)).FirstOrDefault()?.Name; //if (fn != null) // throw new Exception("Field '" + fn + "' is a column of a table field. Its default value type can be either " + Template.Field.ValueTypes.PdfText + " or " + Template.Field.ValueTypes.PdfTextLines); } if (saving) { t.Editor = new Template.EditorSettings { TestFile = testFile.Text, TestPictureScale = pictureScale.Value, ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked, ShowFieldTextLineSeparators = ShowFieldTextLineSeparators.Checked, CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked, }; } return(t); }
internal Template GetTemplateFromUI(bool saving) { Template t = new Template(); if (saving && string.IsNullOrWhiteSpace(name.Text)) { throw new Exception("Name is empty!"); } t.Name = name.Text.Trim(); t.TextAutoInsertSpace = new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpace_Threshold.Value, IgnoreSourceSpaces = textAutoInsertSpace_IgnoreSourceSpaces.Checked, //Representative//default }; t.TesseractPageSegMode = (Tesseract.PageSegMode)TesseractPageSegMode.SelectedItem; t.AdjustLineBorders = AdjustLineBorders.Checked; t.SingleFieldFromFieldImage = SingleFieldFromFieldImage.Checked; t.ColumnCellFromCellImage = ColumnCellFromCellImage.Checked; if (CharSizeFilterMinWidth.Value > 0 || CharSizeFilterMaxWidth.Value > 0 || CharSizeFilterMinHeight.Value > 0 || CharSizeFilterMaxHeight.Value > 0) { t.CharFilter = new CharFilter { MinWidth = (float)CharSizeFilterMinWidth.Value, MaxWidth = (float)CharSizeFilterMaxWidth.Value, MinHeight = (float)CharSizeFilterMinHeight.Value, MaxHeight = (float)CharSizeFilterMaxHeight.Value } } ; if (LinePaddingY.Value > 0) { t.LinePaddingY = (int)LinePaddingY.Value; } bitmapPreparationForm.SetTemplate(t); bool? removeNotLinkedAnchors = null; List <int> conditionAnchorIds = null; if (saving) { conditionAnchorIds = new List <int>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c != null && c.IsSet()) { conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value)); } } conditionAnchorIds = conditionAnchorIds.Distinct().ToList(); } t.Anchors = new List <Template.Anchor>(); foreach (DataGridViewRow r in anchors.Rows) { Template.Anchor a = (Template.Anchor)r.Tag; if (a == null) { continue; } if (saving) { if (!a.IsSet()) { throw new Exception("Anchor[Id=" + a.Id + "] is not set!"); } if (!conditionAnchorIds.Contains(a.Id)) { bool engaged = false; foreach (DataGridViewRow rr in anchors.Rows) { Template.Anchor a_ = (Template.Anchor)rr.Tag; if (a_ == null) { continue; } if (a_.ParentAnchorId == a.Id) { engaged = true; break; } } if (!engaged) { foreach (DataGridViewRow rr in fields.Rows) { Template.Field m = (Template.Field)rr.Tag; if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id)) { engaged = true; break; } } if (!engaged) { if (a.Id != t.ScalingAnchorId) { if (removeNotLinkedAnchors == null) { removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Remove them?", this); } if (removeNotLinkedAnchors == true) { continue; } } } } } } t.Anchors.Add((Template.Anchor)Serialization.Json.Clone2(a)); } t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList(); if (saving) { t.GetScalingAnchor();//check is it is correct; } t.Conditions = new List <Template.Condition>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c == null) { continue; } if (saving) { if (!c.IsSet()) { throw new Exception("Condition[name=" + c.Name + "] is not set!"); } BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id)); } t.Conditions.Add((Template.Condition)Serialization.Json.Clone2(c)); } if (saving) { var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault(); if (dcs != null) { throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!"); } } t.Fields = new List <Template.Field>(); foreach (DataGridViewRow r in fields.Rows) { Template.Field f = (Template.Field)r.Tag; if (f == null) { continue; } if (saving && !f.IsSet()) { throw new Exception("Field[" + r.Index + "] is not set!"); } if (saving) { foreach (int?ai in new List <int?> { f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id }) { if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null) { throw new Exception("Anchor[Id=" + ai + " does not exist."); } } } t.Fields.Add((Template.Field)Serialization.Json.Clone2(f)); } if (saving) { if (t.Fields.Count < 1) { throw new Exception("Fields is empty!"); } var fs = t.Fields.GroupBy(x => x.Name).Where(x => x.GroupBy(a => a.GetType()).Count() > 1).FirstOrDefault(); if (fs != null) { throw new Exception("Definitions of the field '" + fs.First().Name + "' are not of the same type!"); } } if (saving) { t.Editor = new Template.EditorSettings { //TestFile = testFile.Text, TestPictureScale = pictureScale.Value, ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked, ShowFieldTextLineSeparators = ShowFieldTextLineSeparators.Checked, CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked, }; } return(t); } }
Template getTemplateFromUI(bool saving) { Template t = new Template(); if (saving && string.IsNullOrWhiteSpace(name.Text)) { throw new Exception("Name is empty!"); } t.Name = name.Text.Trim(); t.TextAutoInsertSpace = new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpaceThreshold.Value, Representative = textAutoInsertSpaceRepresentative.Text, }; t.PageRotation = (Template.PageRotations)pageRotation.SelectedIndex; t.AutoDeskew = autoDeskew.Checked; t.AutoDeskewThreshold = (int)autoDeskewThreshold.Value; bool?removeNotLinkedAnchors = null; t.Anchors = new List <Template.Anchor>(); List <int> conditionAnchorIds = null; if (saving) { conditionAnchorIds = new List <int>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c != null && c.IsSet()) { conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value)); } } conditionAnchorIds = conditionAnchorIds.Distinct().ToList(); } foreach (DataGridViewRow r in anchors.Rows) { Template.Anchor a = (Template.Anchor)r.Tag; if (a == null) { continue; } if (saving) { if (!a.IsSet()) { throw new Exception("Anchor[Id=" + a.Id + "] is not set!"); } bool engaged = false; if (conditionAnchorIds.Contains(a.Id)) { engaged = true; } if (!engaged) { foreach (DataGridViewRow rr in anchors.Rows) { Template.Anchor a_ = (Template.Anchor)rr.Tag; if (a_ == null) { continue; } if (a_.ParentAnchorId == a.Id) { engaged = true; break; } } } if (!engaged) { foreach (DataGridViewRow rr in fields.Rows) { Template.Field m = (Template.Field)rr.Tag; if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id)) { engaged = true; break; } } } if (!engaged) { if (removeNotLinkedAnchors == null) { removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Should they be removed?"); } if (removeNotLinkedAnchors == true) { continue; } } } t.Anchors.Add(a); } t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList(); t.Conditions = new List <Template.Condition>(); foreach (DataGridViewRow r in conditions.Rows) { Template.Condition c = (Template.Condition)r.Tag; if (c == null) { continue; } if (saving) { if (!c.IsSet()) { throw new Exception("Condition[name=" + c.Name + "] is not set!"); } BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id)); } t.Conditions.Add(c); } if (saving) { var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault(); if (dcs != null) { throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!"); } } t.Fields = new List <Template.Field>(); foreach (DataGridViewRow r in fields.Rows) { Template.Field f = (Template.Field)r.Tag; if (f == null) { continue; } if (saving && !f.IsSet()) { throw new Exception("Field[" + r.Index + "] is not set!"); } if (saving) { foreach (int?ai in new List <int?> { f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id }) { if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null) { throw new Exception("Anchor[Id=" + ai + " does not exist."); } } } t.Fields.Add(f); } if (saving) { if (t.Fields.Count < 1) { throw new Exception("Fields is empty!"); } //var dfs = t.Fields.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault(); //if (dfs != null) // throw new Exception("Field '" + dfs.First().Name + "' is duplicated!"); //foreach (string columnOfTable in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable != null).Select(x => x.ColumnOfTable)) //{ // Dictionary<string, List<Template.Field>> fieldName2orderedFields = new Dictionary<string, List<Template.Field>>(); // foreach (Template.Field.PdfText pt in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable == columnOfTable)) // { // List<Template.Field> fs; // if (!fieldName2orderedFields.TryGetValue(pt.Name, out fs)) // { // fs = new List<Template.Field>(); // fieldName2orderedFields[pt.Name] = fs; // } // fs.Add(pt); // } // int definitionCount = fieldName2orderedFields.Max(x => x.Value.Count()); // foreach (string fn in fieldName2orderedFields.Keys) // if (definitionCount > fieldName2orderedFields[fn].Count) // throw new Exception("Field '" + fn + "' is column of table " + columnOfTable + " and so it must have the same number of definitions as the rest column fields!"); //} foreach (Template.Field f0 in t.Fields) { int tableDefinitionsCount = t.Fields.Where(x => x.Name == f0.Name).Count(); string fn = t.Fields.Where(x => x.ColumnOfTable == f0.Name).GroupBy(x => x.Name).Where(x => x.Count() > tableDefinitionsCount).FirstOrDefault()?.First().Name; if (fn != null) { throw new Exception("Field '" + fn + "' is a column of table field " + f0.Name + " so " + f0.Name + " must have number of definitions not less then that of " + fn + "."); } } } if (saving) { t.Editor = new Template.EditorSettings { TestFile = testFile.Text, TestPictureScale = pictureScale.Value, ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked, CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked, }; } return(t); }