/// <summary> /// Occurs after a new custom XMl part is loaded into the document /// Useful for updating the Navigator /// </summary> /// <param name="NewPart"></param> private void OnPartAfterLoad(CustomXMLPart NewPart) { string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()"; try { var converter = new CMLConverter(); //get the chemistry var chemModel = converter.Import(NewPart.XML); //find out which content control macthes the custom XML part try { // ReSharper disable once InconsistentNaming var matchingCC = (from ContentControl cc in _doc.ContentControls orderby cc.Range.Start where CustomXmlPartHelper.GuidFromTag(cc?.Tag) == CustomXmlPartHelper.GetCmlId(NewPart) select cc).First(); //get the ordinal position of the content control int start = 0; foreach (ContentControl cc in _doc.ContentControls) { if (cc.ID == matchingCC.ID) { break; } start += 1; } //insert the new navigator item at the ordinal position var newNavItem = new NavigatorItem() { CMLId = matchingCC?.Tag, ChemicalStructure = NewPart.XML, XMLPart = NewPart, Name = chemModel.ConciseFormula }; try { NavigatorItems.Insert(start, newNavItem); } catch (ArgumentOutOfRangeException) //can happen when there are more content controls than navigator items { //so simply insert the new navigator item at the end NavigatorItems.Add(newNavItem); } } catch (InvalidOperationException) { //sequence contains no elements - thrown on close //just ignore } } catch (Exception ex) { new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog(); } }
/// <summary> /// Loads up the model initially from the document /// </summary> private void LoadModel() { string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()"; try { var converter = new CMLConverter(); if (NavigatorItems.Any()) { NavigatorItems.Clear(); } if (_doc != null) { Dictionary <string, int> added = new Dictionary <string, int>(); var navItems = from ContentControl ccs in _doc.ContentControls join CustomXMLPart part in _parts on CustomXmlPartHelper.GuidFromTag(ccs?.Tag) equals CustomXmlPartHelper.GetCmlId(part) orderby ccs.Range.Start let chemModel = converter.Import(part.XML) select new NavigatorItem { CMLId = ccs?.Tag, ChemicalStructure = part.XML, XMLPart = part, Name = chemModel.ConciseFormula }; foreach (NavigatorItem navigatorItem in navItems) { string guid = CustomXmlPartHelper.GuidFromTag(navigatorItem.CMLId); if (!string.IsNullOrEmpty(guid)) { if (!added.ContainsKey(guid)) { NavigatorItems.Add(navigatorItem); added.Add(guid, 1); } } } Debug.WriteLine("Number of items loaded = {0}", NavigatorItems.Count); } } catch (Exception ex) { using (var form = new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex)) { form.ShowDialog(); } } }
private void NextControl() { string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()"; NavigatorItem ni = DataContext as NavigatorItem; var currentSelPoint = ActiveDocument.ActiveWindow.Selection; try { var linkedControls = from Word.ContentControl cc in ActiveDocument.ContentControls orderby cc.Range.Start where CustomXmlPartHelper.GuidFromTag(cc?.Tag) == CustomXmlPartHelper.GetCmlId(ni.XMLPart) select cc; // Grab current selection point int current = currentSelPoint.Range.End; foreach (Word.ContentControl cc in linkedControls) { if (cc.Range.Start > current) { cc.Range.Select(); ActiveDocument.ActiveWindow.ScrollIntoView(cc.Range); return; } } // Rewind to Start of document current = 0; foreach (Word.ContentControl cc in linkedControls) { if (cc.Range.Start > current) { cc.Range.Select(); ActiveDocument.ActiveWindow.ScrollIntoView(cc.Range); return; } } } catch (NullReferenceException ex) { using (var form = new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex)) { form.ShowDialog(); } } }