private void chooseFile(SWFile f) { OpenFileDialog fd = new OpenFileDialog(); fd.Multiselect = false; if (f.typeString == new SWPart().typeString) { fd.Filter = "SolidWorks Part|*.sldprt"; } else { fd.Filter = "Solidworks Assembly|*.sldasm"; } DialogResult r = fd.ShowDialog(); if (r == DialogResult.OK) { if (!(fd.FileName.ToLower().StartsWith(project.workDir.ToLower()))) { MessageBox.Show("File is not in the workdir!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } f.file = Path.GetFileName(fd.FileName); updateFile(currentContextFile); } }
public void searchForMissingAssemblyFiles(out int numFound) { numFound = 0; List <SWFile> toRemove = new List <SWFile>(); List <SWFile> toAdd = new List <SWFile>(); foreach (SWFile f in files) { if (f.docType == SWFileType.PART && f.getStatus().Type == SWFileStatusType.FILE_MISSING) { string probableName = f.prefix + SWFile.fourDigit(f.number) + f.suffix + new SWAssy().extension; if (File.Exists(Path.Combine(workDir, probableName))) { SWPart f_old = f as SWPart; SWAssy f_new = (SWAssy)f_old; f_new.file = probableName; toRemove.Add(f); toAdd.Add(f_new); numFound += 1; } } } foreach (SWFile f in toRemove) { f.removeFromProject(); } foreach (SWFile f in toAdd) { f.addToProject(); } }
public ChangePropertyForm(Project Project, SWFile File) { InitializeComponent(); project = Project; file = File; this.valueGroupBox.Text = Settings.Default.propertyName; this.valueTextBox.Text = this.file.name; }
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) { return; } currentContextFile = getFileByRowIndex(e.RowIndex); openFile(currentContextFile); }
private void openFile(SWFile f) { int ret = f.open(); if (ret == SWFile.RET_FILE_MISSING) { MessageBox.Show(String.Format("Can\'t find file {0}", f.file), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void createEmptyRow(int n) { if (n > 9999) { return; } DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dataGridView1, "", "", SWFile.fourDigit(n), "", "", "", ""); dataGridView1.Rows.Add(row); }
public void Export(string path) { FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read); StreamWriter sw = new StreamWriter(fs, Encoding.Default); foreach (SWFile file in project.files.OrderBy <SWFile, int>(x => x.number).ToList <SWFile>()) { sw.WriteLine(String.Format("{1}{0}{2}", ";", file.prefix + SWFile.fourDigit(file.number) + file.suffix, file.name)); } sw.Close(); fs.Close(); }
private void dataGridView1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { DataGridView.HitTestInfo ht; ht = dataGridView1.HitTest(e.X, e.Y); if (ht.Type == DataGridViewHitTestType.Cell) { currentContextFile = getFileByRowIndex(ht.RowIndex); contextMenuStrip1.Items[changePropertyValueMenuItem.Name].Text = String.Format(contextMenuStrip1.Items[changePropertyValueMenuItem.Name].Text, Settings.Default.propertyName); // Enable items by default foreach (object x in contextMenuStrip1.Items) { if (x.GetType() == toolStripSeparator1.GetType()) { continue; } ToolStripMenuItem q = (ToolStripMenuItem)x; q.Enabled = true; } if (currentContextFile != null) { contextMenuStrip1.Items[chooseFileManuallyToolStripMenuItem.Name].Enabled = false; if (currentContextFile.getStatus().Type == SWFileStatusType.FILE_MISSING) { contextMenuStrip1.Items[chooseFileManuallyToolStripMenuItem.Name].Enabled = true; } } else { foreach (object x in contextMenuStrip1.Items) { if (x.GetType() == toolStripSeparator1.GetType()) { break; } ToolStripMenuItem q = (ToolStripMenuItem)x; q.Enabled = false; } } if (Settings.Default.showEmptyRows) { showEmptyRowsToolStripMenuItem.Checked = true; } contextMenuStrip1.Show(dataGridView1, e.Location); } } }
public void update() { string filter = filterTextBox.Text.ToLower(); dataGridView1.Columns[nameColumn.Name].HeaderText = Settings.Default.propertyName; dataGridView1.Rows.Clear(); List <SWFile> files_s = project.files.OrderBy <SWFile, int>(x => x.number).Where( x => x.name.ToLower().Contains(filter) || SWFile.fourDigit(x.number).Contains(filter) || x.prefix.ToLower().Contains(filter) || x.suffix.ToLower().Contains(filter) || x.file.ToLower().Contains(filter) ).ToList <SWFile>(); countLabel.Text = String.Format("Count: {0}", files_s.Count); int n = 0; foreach (SWFile f in files_s) { n++; while (f.number < n) { n--; } DataGridViewRow row = new DataGridViewRow(); SWFileStatus s = f.getStatus(); f.lastShownStatus = s; if (Settings.Default.showEmptyRows && filter == "") { while (f.number > n) { this.createEmptyRow(n); n++; } } row.CreateCells(dataGridView1, f.typeString, f.prefix, SWFile.fourDigit(f.number), f.suffix, f.name, f.file, s.ToString()); if (row.Cells.Count == 0) { // Happens when updating closed form continue; } row.Tag = f; row.Cells[dataGridView1.Columns["statusColumn"].Index].Style.ForeColor = s.Color; dataGridView1.Rows.Add(row); } }
/// <summary> /// Updates file's name, path and status in grid view. Use only from thread that created the UI !!! /// </summary> /// <param name="f">File to update</param> public void updateFile(SWFile f) { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Tag == f) { row.Cells[typeColumn.Index].Value = f.typeString; row.Cells[nameColumn.Index].Value = f.name; row.Cells[filepath.Index].Value = f.file; SWFileStatus s = f.getStatus(); if (s != f.lastShownStatus) { row.Cells[statusColumn.Index].Value = s.ToString(); row.Cells[dataGridView1.Columns["statusColumn"].Index].Style.ForeColor = s.Color; } f.lastShownStatus = s; break; } } }
private void fileImportButton_Click(object sender, EventArgs e) { if (propertyNameTextBox.Text == "") { MessageBox.Show("Enter property name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } OpenFileDialog fd = new OpenFileDialog(); fd.Multiselect = true; fd.Filter = "SolidWorks Part|*.sldprt|Solidworks Assembly|*.sldasm"; DialogResult result = fd.ShowDialog(); if (result == DialogResult.OK) { foreach (String FileName in fd.FileNames) { if (!(FileName.ToLower().StartsWith(project.workDir.ToLower()))) { MessageBox.Show(String.Format("File {0} is not in the workdir! Not adding this file.", FileName), "", MessageBoxButtons.OK, MessageBoxIcon.Warning); continue; } string[] p = SWFile.getFileParams(Path.GetFileNameWithoutExtension(FileName)); if (p[1] == "") { MessageBox.Show(String.Format("File {0} name does not include a four-digit part number! Not adding this file.", FileName), "", MessageBoxButtons.OK, MessageBoxIcon.Warning); continue; } SWFile f = null; if (FileName.ToLower().EndsWith(".sldprt")) { f = new SWPart() { project = project, number = Convert.ToInt32(p[1]), name = "", prefix = p[0], suffix = p[2], template = null, }; } else if (FileName.ToLower().EndsWith(".sldasm")) { f = new SWAssy() { project = project, number = Convert.ToInt32(p[1]), name = "", prefix = p[0], suffix = p[2], template = null, }; } else { MessageBox.Show(String.Format("{0} is not SolidWorks Part or Assembly!", FileName), "", MessageBoxButtons.OK, MessageBoxIcon.Warning); continue; } int ret = f.create(); if (ret == SWFile.RET_DUPLICATE_NUMBER) { DialogResult r = MessageBox.Show(String.Format("{0}: Part or Assembly with this number already exists in database. Add anyway?", FileName), "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (r == DialogResult.Yes) { ret = SWFile.RET_OK; } } if (ret == SWFile.RET_NAME_EXISTS) { MessageBox.Show(String.Format("{0}: Part or Assembly with this prefix number suffix combination already exists! Not adding this file.", FileName), "", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } if (ret == SWFile.RET_FILE_EXISTS) { // Yes, we are adding it right now:) ret = SWFile.RET_OK; } if (ret == SWFile.RET_OK) { f.getNameFromFile(); f.addToProject(); } } } dbForm.update(); }
public void open(string path) { numbers = new List <int>(); filenames = new List <string>(); files = new List <SWFile>(); file = path; if (!File.Exists(path)) { throw new ProjectReadException("Can\'t find file " + path); } XmlDocument d = new XmlDocument(); try { d.Load(file); } catch (XmlException exc) { throw new ProjectReadException(exc.Message); } XmlNode root = d.DocumentElement; if (root.Name != "swpiProject") { throw new ProjectReadException("Root node missing."); } workDir = readSingleNode(d, "descendant::workDir"); partPrefix = readSingleNode(d, "descendant::partPrefix"); partSuffix = readSingleNode(d, "descendant::partSuffix"); assyPrefix = readSingleNode(d, "descendant::assyPrefix"); assySuffix = readSingleNode(d, "descendant::assySuffix"); // Read elements... foreach (XmlNode fileNode in d.SelectNodes("descendant::files/file")) // no need to throw exceptions here, it is ok for "files" node not to exist { SWFile f = null; string t = readSingleNode(fileNode, "descendant::type"); SWFileType tp = getSWFileTypeFromString(t); if (tp == SWFileType.PART) { f = new SWPart() { project = this, name = readSingleNode(fileNode, "descendant::name"), prefix = readSingleNode(fileNode, "descendant::prefix"), suffix = readSingleNode(fileNode, "descendant::suffix"), file = readSingleNode(fileNode, "descendant::path"), }; } else if (tp == SWFileType.ASSEMBLY) { f = new SWAssy() { project = this, name = readSingleNode(fileNode, "descendant::name"), prefix = readSingleNode(fileNode, "descendant::prefix"), suffix = readSingleNode(fileNode, "descendant::suffix"), file = readSingleNode(fileNode, "descendant::path"), }; } else { throw new ProjectReadException("Wrong file type: " + t); } try { f.number = Convert.ToInt32(fileNode.SelectSingleNode("descendant::number").InnerText); } catch (FormatException) { throw new ProjectReadException(String.Format("\"{0}\" can't be a number.", fileNode.SelectSingleNode("descendant::number").InnerText)); } files.Add(f); numbers.Add(f.number); filenames.Add(f.prefix + SWFile.fourDigit(f.number) + f.suffix); } while (Settings.Default.RecentProjects.Contains(path)) { Settings.Default.RecentProjects.Remove(path); } Settings.Default.RecentProjects.Insert(0, path); while (Settings.Default.RecentProjects.Count >= 5) { Settings.Default.RecentProjects.RemoveAt(4); } _openedOK = true; }
/// <summary> /// Schedules update of file's name, path and status in grid view. Use this if calling update from foreign thread. /// </summary> /// <param name="f">File to update</param> public void scheduleFileUpdate(SWFile f) { filesToUpdate.Add(f); }
private void newProjectButton_Click(object sender, EventArgs e) { project.createNew(); project.workDir = ""; foreach (DataGridViewRow row in dataGridView1.Rows) { SWPart f = new SWPart(); f.project = project; f.file = ""; int cellNo = 1; foreach (DataGridViewCell cell in row.Cells) { if (cell.Value == null) { continue; } importFieldValue cellMapping; if (mapping.TryGetValue(cellNo, out cellMapping)) { switch (cellMapping) { case importFieldValue.PREFIX_NUMBER_SUFFIX: string[] p = SWFile.getFileParams(cell.Value.ToString()); if (p[1] == "") { MessageBox.Show(String.Format("{0} is wrong format for PREFIX_NUMBER_SUFFIX", cell.Value.ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } f.prefix = p[0]; f.number = Convert.ToInt32(p[1]); f.suffix = p[2]; break; case importFieldValue.NAME: f.name = cell.Value.ToString(); break; case importFieldValue.PREFIX: f.prefix = cell.Value.ToString(); break; case importFieldValue.SUFFIX: f.suffix = cell.Value.ToString(); break; case importFieldValue.NUMBER: try { f.number = Convert.ToInt32(cell.Value.ToString()); } catch (FormatException) { MessageBox.Show(String.Format("Can\'t convert {0} to a number!", cell.Value.ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } break; case importFieldValue.PATH_RELATIVE: f.file = cell.Value.ToString(); break; } } cellNo += 1; } if (f.number > 0 && f.number <= 9999) { if (f.file == "") { // Guess file name f.file = f.prefix + SWFile.fourDigit(f.number) + f.suffix + f.extension; } if (f.file.ToLower().EndsWith(".sldasm")) { SWAssy fa = (SWAssy)f; fa.addToProject(); } else { f.addToProject(); } } } Form1 form = Form1.getForm(); form.activateUI(); if (!form.dbForm.Visible) { form.prep_DBForm(); } form.dbForm.update(); form.dbForm.Show(); MessageBox.Show(String.Format("Imported {0} files.", project.countFiles().ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); }