Пример #1
0
        /// <summary>
        /// 生成一个随机的临时SolidWorks路径
        /// *.sldpart *.sldasm *.slddrw
        /// </summary>
        /// <param name="docType"></param>
        /// <returns></returns>
        public static string CreateATemFilePath(swDocumentTypes_e docType)
        {
            string FilePath = ExtensionDllPath;

            switch (docType)
            {
            case swDocumentTypes_e.swDocPART:
                FilePath = FilePath + "\\" + Guid.NewGuid().ToString() + SWFile.Part;
                break;

            case swDocumentTypes_e.swDocASSEMBLY:
                FilePath = FilePath + "\\" + Guid.NewGuid().ToString() + SWFile.Assembly;
                break;

            case swDocumentTypes_e.swDocDRAWING:
                FilePath = FilePath + "\\" + Guid.NewGuid().ToString() + SWFile.Drawing;
                break;

            default:
                throw new FileFormatException("不支持的文件类型:" + docType.ToString());
            }
            return(FilePath);
        }
        /// <summary>
        /// 后台创建文档
        /// </summary>
        /// <param name="sldWorks"></param>
        /// <param name="hidden"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ModelDoc2 CreateDocument
            (this ISldWorks sldWorks, bool hidden = true, swDocumentTypes_e type = swDocumentTypes_e.swDocPART)
        {
            try
            {
                if (hidden)
                {
                    sldWorks.DocumentVisible(false, (int)type);
                }

                string templatePath = string.Empty;

                switch (type)
                {
                case swDocumentTypes_e.swDocPART:
                    templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
                    break;

                case swDocumentTypes_e.swDocASSEMBLY:
                    templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly);
                    break;

                case swDocumentTypes_e.swDocDRAWING:
                    templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateDrawing);
                    break;

                default:
                    break;
                }

                if (string.IsNullOrEmpty(templatePath))
                {
                    throw new FileNotFoundException(string.Format("未找到此种类型的模板文件:{0}", type.ToString()));
                }

                if (!File.Exists(templatePath))
                {
                    templatePath =
                        Path.GetDirectoryName(templatePath) + "\\" +
                        Path.GetFileName(templatePath).
                        Replace("零件", "gb_part").
                        Replace("装配体", "gb_assembly").Replace("工程图", "gb_a1");
                }

                if (!File.Exists(templatePath))
                {
                    throw new FileNotFoundException("无法找到SolidWorks文件--" + templatePath);
                }

                //TODO:设置替换字符串
                var doc = (ModelDoc2)sldWorks.NewDocument(templatePath, (int)swDwgPaperSizes_e.swDwgPaperA4size, 1, 1);
                if (hidden)
                {
                    doc.Visible = false;
                    sldWorks.DocumentVisible(false, (int)type);
                }

                /*
                 * ModelView myModelView = null;
                 * myModelView = ((ModelView)(doc.ActiveView));
                 * myModelView.FrameLeft = 0;
                 * myModelView.FrameTop = 0;
                 * myModelView = ((ModelView)(doc.ActiveView));
                 *
                 * myModelView.FrameState = ((int)(swWindowState_e.swWindowMinimized));
                 */
                return(doc);
            }
            finally
            {
                if (hidden)
                {
                    sldWorks.DocumentVisible
                        (true,
                        (int)
                        type);
                }
            }
        }