コード例 #1
0
        /// <summary>
        /// 以默认设置的模板创建文档
        /// </summary>
        /// <param name="sldWorks"><see cref="ISldWorks"/></param>
        /// <param name="type"><see cref="swDocumentTypes_e"/></param>
        /// <returns></returns>
        public static IModelDoc2 CreateDocument(this ISldWorks sldWorks, swDocumentTypes_e 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 (!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);
            }

            return(sldWorks.CreateDocument(templatePath));
        }