private void createAssyButton_Click(object sender, EventArgs e) { if (propertyNameTextBox.Text == "") { MessageBox.Show("Enter property name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (assyTemplateTextBox.Text == "") { MessageBox.Show("Enter assembly template path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (workDirTextBox.Text == "") { MessageBox.Show("Enter work dir path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } NumberInputForm nif = new NumberInputForm(project, project.assyPrefix, project.assySuffix); nif.ShowDialog(); if (!nif.OK) { return; } SWAssy f = new SWAssy() { project = project, number = nif.number, name = nif.name, prefix = nif.prefix, suffix = nif.suffix, template = assyTemplateTextBox.Text, }; int ret = f.create(); if (ret == SWFile.RET_DUPLICATE_NUMBER) { DialogResult r = MessageBox.Show("Part or Assembly with number entered already exists in database. Create anyway?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (r == DialogResult.Yes) { ret = SWFile.RET_OK; } } if (ret == SWFile.RET_NAME_EXISTS) { MessageBox.Show("Part or Assembly with this name already exists!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ret == SWFile.RET_FILE_EXISTS) { MessageBox.Show("File already exists!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ret == SWFile.RET_OK) { f.save(); f.addToProject(); } dbForm.update(); }
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; }