Exemple #1
0
    private void create_dwg(FileInfo p)
    {
        int    dt          = (int)SolidWorks.Interop.swconst.swDocumentTypes_e.swDocDRAWING;
        int    odo         = (int)SolidWorks.Interop.swconst.swOpenDocOptions_e.swOpenDocOptions_Silent;
        int    err         = 0;
        int    warn        = 0;
        string newName     = p.Name.Replace(".SLDDRW", targetExt);
        string tmpFile     = string.Format(@"{0}\{1}", Path.GetTempPath(), newName);
        string fileName    = p.FullName.Replace(".SLDDRW", targetExt);
        int    saveVersion = (int)swSaveAsVersion_e.swSaveAsCurrentVersion;
        int    saveOptions = (int)swSaveAsOptions_e.swSaveAsOptions_Silent;
        bool   success;

        OnAppend(new AppendEventArgs(string.Format("Creating {0}...",
                                                   p.Name.Replace(@".SLDDRW", targetExt))));
        SwApp.OpenDocSilent(p.FullName, dt, ref odo);
        SwApp.ActivateDoc3(p.FullName,
                           true,
                           (int)SolidWorks.Interop.swconst.swRebuildOnActivation_e.swDontRebuildActiveDoc, ref err);
        success = (SwApp.ActiveDoc as ModelDoc2).SaveAs4(tmpFile, saveVersion, saveOptions, ref err, ref warn);
        try {
            File.Copy(tmpFile, fileName, true);
        } catch (UnauthorizedAccessException uae) {
            throw new Exceptions.BuildPDFException(
                      String.Format("You don't have the reqired permission to access '{0}'.", fileName),
                      uae);
        } catch (ArgumentException ae) {
            throw new Exceptions.BuildPDFException(
                      String.Format("Either '{0}' or '{1}' is not a proper file name.", tmpFile, fileName),
                      ae);
        } catch (PathTooLongException ptle) {
            throw new Exceptions.BuildPDFException(
                      String.Format("Source='{0}'; Dest='{1}' <= One of these is too long.", tmpFile, fileName),
                      ptle);
        } catch (DirectoryNotFoundException dnfe) {
            throw new Exceptions.BuildPDFException(
                      String.Format("Source='{0}'; Dest='{1}' <= One of these is invalid.", tmpFile, fileName),
                      dnfe);
        } catch (FileNotFoundException fnfe) {
            throw new Exceptions.BuildPDFException(
                      String.Format("Crap! I lost '{0}'!", tmpFile),
                      fnfe);
        } catch (IOException) {
            System.Windows.Forms.MessageBox.Show(
                String.Format("If you have the file, '{0}', selected in an Explorer window, " +
                              "you may have to close it.", fileName), "This file is open somewhere.",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        } catch (NotSupportedException nse) {
            throw new Exceptions.BuildPDFException(
                      String.Format("Source='{0}'; Dest='{1}' <= One of these is an invalid format.",
                                    tmpFile, fileName), nse);
        }
    }