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();
        }
Exemple #2
0
        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();
        }