private void ClipboardCopyRtf(Range range, IswItem currentItem) { try { InternalClipboardCopyRtf(range); } catch (DllNotFoundException ex) { System.Windows.MessageBox.Show("Exception: " + ex.Message + " " + range.Start.ToString()); throw; } catch (Exception ex) { if (_retryCount < 5) { _retryCount++; System.Threading.Thread.Sleep(10); ClipboardCopyRtf(range, currentItem); } else { System.Windows.MessageBox.Show("Exception: " + ex.Message + " " + range.Start.ToString()); RtfData = ""; if (!ex.Message.Contains("size of the rvfz cannot exceed") && !ex.Message.Contains("OpenClipboard Failed")) { throw; } //Write error message to description } } }
private void WriteDescription(IswItem parentItem, WordItem wordItem, IswItem newItem) { try { try { newItem.Description = SWDescription.MakeDescription(SWUtility.RtfToRvfz(ConcatenateParagraphsRtf(wordItem.DescriptionParagraphs))); } catch (Exception ex) { LogToDescription(String.Format("Couldn't write description to {0} ({1}) as {2}", newItem.Name, newItem.HandleStr, newItem.swItemType.SID)); MessageBox.Show(ex.Message); } if (parentItem.IsSID(DocumentItemSid)) { parentItem.AddPart(TitelPartSid, newItem); } else { parentItem.AddPart(SubSectionSid, newItem); } //LogToDescription(String.Format("Created {0} ({1}) as {2}", newItem.Name, newItem.HandleStr, newItem.swItemType.SID)); //LogToDescription(String.Format("Added {0} ({1}) to {2} at {3} ", newItem.Name, newItem.HandleStr, parentItem.Name, Convert.ToString(DateTime.Now))); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); LogToDescription(String.Format("Exception on part to item {0}: {1}", parentItem.Name, ex.Message)); } }
public SwParagraph(Range range, IswItem currentItem) { Text = range.Text.Trim(); Style = new SwStyle((Microsoft.Office.Interop.Word.Style)range.Paragraphs[1].get_Style(), (int)range.Paragraphs[1].OutlineLevel); OutlineLevel = range.Paragraphs[1].OutlineLevel; ClipboardCopyRtf(range, currentItem); }
private IswItem WriteItem(IswItem parentItem, WordItem wordItem) { IswItem newItem = parentItem.HomeLibrary.CreateItem(ParagraphItemSid, wordItem.MainParagraph.Text); WriteDescription(parentItem, wordItem, newItem); return(newItem); }
public Import(IswItem topItem, List <SwParagraph> paragraphs, List <SwStyle> styles, List <SwStyle> description, string hostName, ThreadedWindowWrapper wrap) { LogToDescription("Importing from Office started at " + Convert.ToString(DateTime.Now)); var currentItem = topItem; List <WordItem> wordItems = CreateWordItems(paragraphs, description); double progress = 0; double step = (double)100 / (double)wordItems.Count; wrap.SetProgress(Convert.ToInt32(progress).ToString()); progress += step; int handledItems = 0; WdOutlineLevel currentOutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText; WdOutlineLevel lastOutlineLevel = 0; Stack <IswItem> parentItems = new Stack <IswItem>(); parentItems.Push(topItem); foreach (WordItem wordItem in wordItems) { if (wordItem.MainParagraph == null) { topItem.Description = SWDescription.MakeDescription(SWUtility.RtfToRvfz(ConcatenateParagraphsRtf(wordItem.DescriptionParagraphs))); continue; } currentOutlineLevel = wordItem.MainParagraph.OutlineLevel; if (currentOutlineLevel > lastOutlineLevel) // Sublevel -> Add to the stack { currentItem = WriteItem(parentItems.Peek(), wordItem); parentItems.Push(currentItem); } else // Higher level -> Remove items from the stack { for (WdOutlineLevel i = currentOutlineLevel; (i <= lastOutlineLevel && parentItems.Count > 1); i++) { parentItems.Pop(); } currentItem = WriteItem(parentItems.Peek(), wordItem); parentItems.Push(currentItem); } lastOutlineLevel = currentOutlineLevel; wrap.SetProgress(Convert.ToInt32(progress).ToString()); progress += step; wrap.SetStatus(string.Format("Imported {0} paragraphs of {1}", ++handledItems, wordItems.Count)); } LogToDescription("Script terminated gracefully at " + Convert.ToString(DateTime.Now)); WriteLogToTopItem(topItem); }
private IswItem CreateWordItem(IswItem currentItem, SwParagraph paragraph, string createSID, /* ref string oldVersionID, */ string descriptionRtf) { IswItem newItem; newItem = currentItem.HomeLibrary.CreateItem(createSID, paragraph.Text); try { newItem.Description = SWDescription.MakeDescription(SWUtility.RtfToRvfz(descriptionRtf)); } catch (Exception ex) { LogToDescription(String.Format("Couldn't write description to {0} ({1}) as {2}", newItem.Name, newItem.HandleStr, newItem.swItemType.SID)); MessageBox.Show(ex.Message); } return(newItem); }
/// <summary> /// Use paragraph sign for finding paragraphs. Instead of doc.Paragraphs. Much faster in large documents. /// </summary> /// <param name="doc"></param> /// <returns></returns> private List <SwParagraph> GetParagraphs(Document doc, IswItem currentItem, ThreadedWindowWrapper wrap) { List <SwParagraph> retParagraphs = new List <SwParagraph>(); Range range = doc.Content; int totalLength = range.End; Range temprange; Find find = range.Find; find.Text = "^p"; find.ClearFormatting(); object missing = Type.Missing; try { find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); int start = 0; while (range.Find.Found) { if (range.Start < start) { break; } temprange = doc.Range(start, range.End); retParagraphs.Add(new SwParagraph(temprange, currentItem)); wrap.SetProgress((range.End * 100 / totalLength).ToString()); start = range.End;// -1; range.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } } catch (DllNotFoundException) { throw; } catch (Exception ex) { System.Windows.MessageBox.Show("Exception: " + ex.Message + " " + range.Start.ToString()); throw; } return(retParagraphs); }
private void OpenItem() { if (XidText == null) { return; } long handle; string handleStr = XidText; int pos = XidText.LastIndexOf('/'); if (pos >= 0) { handleStr = XidText.Substring(pos + 1); } if (SWHandleUtility.TryParseHandle(handleStr, out handle)) { CurrentItem = SystemWeaverAPI.SWConnection.Instance.Broker.GetItem(handle); } }
public void BrowseFile() { OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "M File (*.m)|*.m"; fd.InitialDirectory = "C:\\"; fd.Title = "Browse .M File"; _itemCount = 0; lbl_LibraryName.Content = _item.HomeLibrary.Name.ToString(); if (fd.ShowDialog() == DialogResult.OK) { txtBox_FilePath.Text = fd.FileName; txtBoxM_FileContent.Text = "Parameters found:" + Environment.NewLine + ("-----------------------------") + Environment.NewLine; try { using (StreamReader reader = new StreamReader(txtBox_FilePath.Text)) { string entry; // [Calibration Parameters] & [Local Implementation Signals] string[] validParameters = new string[] { "ie", "ke", "ka", "kn", "kt", "ne", "na", "nt", "ve", "va" }; string lastLine = File.ReadLines(fd.FileName).Last(); if (lastLine == "") { lastLine = "BREAK OUT OF LOOP SINCE LAST LINE WAS EMPTY"; } _includeParameter = false; _parameterName = ""; _datatype = ""; _includedParameterType = ""; _newImplementationAtom = _item.HomeLibrary.CreateItem("5ATM", "import_result_to_be_deleted"); while ((entry = reader.ReadLine()) != null) { // Search for ex: [DPSCie_b_ResetStatePropulsion_ob = mpt.Parameter;] and remove everything after "=" if (entry.Contains("mpt.Parameter") || entry.Contains("mpt.Signal")) { if (entry.Substring(4, 2) == (validParameters[0])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "5CAL"; // Calibration' } else if (entry.Substring(4, 2) == (validParameters[1])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "5CAL"; // Calibration } else if (entry.Substring(4, 2) == (validParameters[2])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "5CAL"; // Calibration } else if (entry.Substring(4, 2) == (validParameters[3])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "5CAL"; // Calibration } else if (entry.Substring(4, 2) == (validParameters[4])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "5CAL"; // Calibration } else if (entry.Substring(4, 2) == (validParameters[5])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "8CBDS"; // Signal } else if (entry.Substring(4, 2) == (validParameters[6])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "8CBDS"; // Signal } else if (entry.Substring(4, 2) == (validParameters[7])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "8CBDS"; // Signal } else if (entry.Substring(4, 2) == (validParameters[8])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "8CBDS"; // Signal } else if (entry.Substring(4, 2) == (validParameters[9])) { _includeParameter = true; _parameterName = entry.Remove(entry.IndexOf('=')).Trim(); _includedParameterType = "8CBDS"; // Signal } else { _includeParameter = false; } } if (_includeParameter) { // Extract Datatype if (entry.Contains(_parameterName + ".DataType")) { string[] equal_split = entry.Split('='); if (equal_split.Count() > 1) { string[] equal_rightside_split = equal_split[1].Split('\''); if (equal_rightside_split.Count() > 2) { _datatype = equal_rightside_split[1]; } } } } // Create new Locals & Calibration parameters if ((entry == "") && _includeParameter || lastLine == entry) { if (_includedParameterType == "5CAL") // Calibration { _newCalibrationParameter = _newImplementationAtom.HomeLibrary.CreateItem("5CAL", _parameterName); txtBoxM_FileContent.Text += "Calibration: " + _parameterName + Environment.NewLine + " " + _datatype + Environment.NewLine; _newCalibrationParameter.SetAttributeWithSID("4CCT", _datatype); _newImplementationAtom.AddPart("5CAM", _newCalibrationParameter, null); _itemCount++; } else if (_includedParameterType == "8CBDS") // Signal { _newLocalImplementationSignal = _newImplementationAtom.HomeLibrary.CreateItem("8CBDS", _parameterName); txtBoxM_FileContent.Text += "Signal: " + _parameterName + Environment.NewLine + " " + _datatype + Environment.NewLine; _newLocalImplementationSignal.SetAttributeWithSID("4CCT", _datatype); _newImplementationAtom.AddPart("6IML", _newLocalImplementationSignal, null); _itemCount++; } _includeParameter = false; } } lbl_ItemCount.Content = _itemCount + " Parameters imported"; } } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); } } }
public void SetCurrentItem(IswItem item) { _item = item; }
public bool SupportsItem(IswItem AItem) { return(AItem.IsSID("I")); }
private void WriteLogToTopItem(IswItem topItem) { topItem.Description = SWDescription.MakeDescription(SWUtility.PlainTextToRvfz(_logTxt)); }
public ReadWord(string sFileName, IswItem currentItem, ThreadedWindowWrapper wrap, out bool failedWithLoadDLL) { failedWithLoadDLL = false; Application word = new Application(); Document doc = null; int progress = 0; object fileName = sFileName; // Define an object to pass to the API for missing parameters object missing = System.Type.Missing; object readOnly = true; try { doc = word.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); String read = string.Empty; List <SwParagraph> data = new List <SwParagraph>(); data = GetParagraphs(doc, currentItem, wrap); Paragraphs = data; List <SwStyle> styles = new List <SwStyle>(); foreach (var p in Paragraphs) { var name = p.Style.Name; foreach (var s in styles) { if (s.Name.Equals(p.Style.Name)) { name = ""; } } if (name.Length > 0) { styles.Add(new SwStyle(name, (int)p.OutlineLevel)); } } StylesInUse = styles; //List<SwStyle> styles = new List<SwStyle>(); //foreach (var p in Paragraphs) //{ // var name = p.Style.Name; // foreach (var s in styles) // { // if (s.Name.Equals(p.Style.Name)) // name = ""; // } // if (name.Length > 0) // styles.Add(new SwStyle(name)); //} //StylesInUse = styles; } catch (DllNotFoundException) { failedWithLoadDLL = true; return; } catch (Exception ex) { if (doc != null) { ((_Document)doc).Close(); } ((_Application)word).Quit(); System.Diagnostics.Debug.WriteLine(ex.Message); throw; } ((_Document)doc).Close(); ((_Application)word).Quit(); wrap.SetProgress(progress.ToString()); progress += 20; wrap.SetProgress(progress.ToString()); progress += 20; }