/// <summary> /// Open a document invisibly. It will not be shown to the user but you will be /// able to interact with it through the API as if it is loaded. /// </summary> /// <param name="sldWorks"></param> /// <param name="toolFile"></param> /// <returns></returns> public static ModelDoc2 OpenInvisibleReadOnly(this ISldWorks sldWorks, string toolFile, bool visible = false, swDocumentTypes_e type = swDocumentTypes_e.swDocPART) { try { if (!visible) { sldWorks.DocumentVisible(false, (int)type); } var spec = (IDocumentSpecification)sldWorks.GetOpenDocSpec(toolFile); if (!visible) { spec.Silent = true; spec.ReadOnly = true; } var doc = SwAddinBase.Active.SwApp.OpenDoc7(spec); doc.Visible = visible; return(doc); } finally { if (!visible) { sldWorks.DocumentVisible (true, (int) type); } } }
/// <summary> /// 静默加载 Iges文件 Tries to load an iges file invisibly. Throws an exception if it doesn't work. /// </summary> /// <param name="sldWorks"></param> /// <param name="igesFile"></param> /// <param name="visible"></param> /// <returns></returns> public static ModelDoc2 LoadIgesInvisible(this ISldWorks sldWorks, string igesFile, bool visible = false) { var swDocPart = (int)swDocumentTypes_e.swDocPART; try { if (!visible) { sldWorks.DocumentVisible(false, swDocPart); } ImportIgesData swImportData = (ImportIgesData)sldWorks.GetImportFileData(igesFile); int err = 0; var newDoc = sldWorks.LoadFile4(igesFile, "r", swImportData, ref err); if (err != 0) { throw new Exception(@"Unable to load file {igesFile"); } return(newDoc); } finally { if (!visible) { sldWorks.DocumentVisible (true, swDocPart); } } }
/// <summary> /// 根据模板创建文档 /// </summary> /// <param name="sldWorks"><see cref="ISldWorks"/></param> /// <param name="templatePath">模板路径 支持的文件类型为<para>.prtdot</para><para>.asmdot</para><para>.drwdot</para></param> /// <param name="hidden">是否对用户可见 默认为不可见</param> /// <returns><see cref="ModelDoc2"/></returns> public static ModelDoc2 CreateDocument (this ISldWorks sldWorks, string templatePath, bool hidden = true) { if (!File.Exists(templatePath)) { throw new FileNotFoundException(string.Format("未找到此种类型的模板文件")); } var type = SWPath.GetTemFileType(templatePath); try { if (hidden) { sldWorks.DocumentVisible(false, (int)type); } //TODO:设置替换字符串 var doc = (ModelDoc2)sldWorks.NewDocument(templatePath, (int)swDwgPaperSizes_e.swDwgPaperA4size, 1, 1); return(doc); } finally { if (hidden) { sldWorks.DocumentVisible (true, (int) type); } } }
public static ModelDoc2 CreateHiddenDocument (this ISldWorks sldWorks, bool hidden = true, swDocumentTypes_e type = swDocumentTypes_e.swDocPART) { try { if (hidden) { sldWorks.DocumentVisible(false, (int)type); } var partTemplateName = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart); var doc = (ModelDoc2)sldWorks.NewDocument(partTemplateName, (int)swDwgPaperSizes_e.swDwgPaperA4size, 1, 1); doc.Visible = false; /* * 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); } } }
private void UpdateCachedBodyIfNeeded(LinkFileMacroFeatureParameters parameters) { LastError = null; IModelDoc2 refDoc = null; bool isRefDocLoaded = false; try { if (File.Exists(parameters.LinkedFilePath)) { LastUpdateStamp = File.GetLastWriteTimeUtc(parameters.LinkedFilePath).Ticks; refDoc = m_App.GetOpenDocumentByName(parameters.LinkedFilePath) as IModelDoc2; isRefDocLoaded = refDoc != null; if (LastUpdateStamp != parameters.FileLastUpdateTimeStamp || (isRefDocLoaded && refDoc.GetSaveFlag()) || CachedBodies == null) { if (!isRefDocLoaded) { m_App.DocumentVisible(false, (int)swDocumentTypes_e.swDocPART); var docSpec = m_App.GetOpenDocSpec(parameters.LinkedFilePath) as IDocumentSpecification; docSpec.Silent = true; docSpec.ReadOnly = true; refDoc = m_App.OpenDoc7(docSpec); if (refDoc == null) { throw new InvalidOperationException($"Failed to load the referenced file ${docSpec.FileName} with error: {(swFileLoadError_e)docSpec.Error}"); } } if (refDoc is IPartDoc) { var bodies = (refDoc as IPartDoc).GetBodies2((int)swBodyType_e.swAllBodies, true) as object[]; if (bodies != null && bodies.Any()) { var resBodies = bodies.Cast <IBody2>().Select(b => b.ICopy()).ToArray(); CachedBodies = resBodies; } else { throw new InvalidOperationException("No bodies in the referenced document"); } } else { throw new InvalidOperationException("Referenced document is not a part"); } } } else { throw new FileNotFoundException($"Linked file '${parameters.LinkedFilePath}' is not found"); } } catch (Exception ex) { LastError = ex; } finally { m_App.DocumentVisible(true, (int)swDocumentTypes_e.swDocPART); if (!isRefDocLoaded && refDoc != null) { m_App.CloseDoc(refDoc.GetTitle()); } } }
/// <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); } } }