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()); } } }