/// <summary> /// Loads a specified Revit link external resource. /// </summary> /// <param name="resourceReference">An ExternalResourceReference identifying which particular resource to load.</param> /// <param name="loadContent">An ExternalResourceLoadContent object that will be populated with load data by the /// server. There are different subclasses of ExternalResourceLoadContent for different ExternalResourceTypes.</param> private void LoadRevitLink(ExternalResourceReference resourceReference, ExternalResourceLoadContent loadContent) { LinkLoadContent linkLoadContent = (LinkLoadContent)loadContent; if (linkLoadContent == null) { throw new ArgumentException("Wrong type of ExternalResourceLoadContent (expecting a LinkLoadContent) for Revit links.", "loadContent"); } try { // Copy the file from the path under the server "root" folder to a secret "cache" folder on the users machine String fullCachedPath = GetFullLinkCachedFilePath(resourceReference); String cacheFolder = System.IO.Path.GetDirectoryName(fullCachedPath); if (!System.IO.Directory.Exists(cacheFolder)) { System.IO.Directory.CreateDirectory(cacheFolder); } String serverLinkPath = GetFullServerLinkFilePath(resourceReference); System.IO.File.Copy(serverLinkPath, fullCachedPath, true); // Overwrite ModelPath linksPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(fullCachedPath); linkLoadContent.SetLinkDataPath(linksPath); loadContent.LoadStatus = ExternalResourceLoadStatus.Success; } catch (System.Exception) { } }
public void LoadResource(Guid loadRequestId, ExternalResourceType resourceType, ExternalResourceReference resourceReference, ExternalResourceLoadContext loadContext, ExternalResourceLoadContent content) { LinkLoadContent linkLoadContent = (LinkLoadContent)content; string file = resourceReference.GetReferenceInformation()["File"]; DBUtils.DBItem dbItem = DBUtils.FindDBItem(file); ModelPath linksPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(dbItem.Path); linkLoadContent.SetLinkDataPath(linksPath); content.LoadStatus = ExternalResourceLoadStatus.Success; return; }
/// <summary> /// Reports the results of loads from the DB server (SampleExternalResourceServer). /// This method should be implemented to provide UI to communicate success or failure /// of a particular external resource load operation to the user. /// </summary> /// <param name="doc">The Revit model into which the External Resource was loaded. /// </param> /// <param name="loadDataList">Contains a list of ExternalResourceLoadData with results /// for all external resources loaded by the DB server. It is possible for the DB server /// to have loaded more than one resource (for example, loading several linked files /// when a host file is opened by the user). /// </param> public void HandleLoadResourceResults(Document doc, IList <ExternalResourceLoadData> loadDataList) { foreach (ExternalResourceLoadData data in loadDataList) { ExternalResourceType resourceType = data.ExternalResourceType; // This message will be posted in a dialog box at the end of this method. String myMessage = String.Empty; ExternalResourceLoadContext loadContext = data.GetLoadContext(); ExternalResourceReference desiredRef = data.GetExternalResourceReference(); ExternalResourceReference currentlyLoadedRef = loadContext.GetCurrentlyLoadedReference(); LoadOperationType loadType = loadContext.LoadOperationType; switch (loadType) { case LoadOperationType.Automatic: myMessage = "This is an Automatic load operation. "; break; case LoadOperationType.Explicit: myMessage = "This is an Explicit load operation. "; break; default: myMessage = "There is no load type information!! "; break; } bool bUnrecognizedStatus = false; if (data.LoadStatus == ExternalResourceLoadStatus.ResourceAlreadyCurrent) { if (data.GetLoadContext().LoadOperationType == LoadOperationType.Explicit) { string resourcePath = currentlyLoadedRef.InSessionPath; myMessage += "\n No new changes to load for link: " + resourcePath; } else { continue; } } else if (data.LoadStatus == ExternalResourceLoadStatus.Uninitialized) { myMessage += "\n The load status is uninitialized - this generally shouldn't happen"; } else if (data.LoadStatus == ExternalResourceLoadStatus.Failure) { myMessage += "\n The load failed and the reason is unknown."; } else if (data.LoadStatus == ExternalResourceLoadStatus.Success) { if (resourceType == ExternalResourceTypes.BuiltInExternalResourceTypes.KeynoteTable) { string resourcePath = data.GetExternalResourceReference().InSessionPath; myMessage += "\n Version " + data.GetLoadContent().Version + " of keynote data \'" + resourcePath + "\' has been loaded successfully"; } else if (resourceType == ExternalResourceTypes.BuiltInExternalResourceTypes.RevitLink) { string resourcePath = data.GetExternalResourceReference().InSessionPath; LinkLoadContent ldrlc = (LinkLoadContent)(data.GetLoadContent()); string destinationPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(ldrlc.GetLinkDataPath()); myMessage += "\n Version " + data.GetLoadContent().Version + " of the file: " + resourcePath + " has been downloaded into the cached folder: " + destinationPath + " for this Revit Link."; } } else { myMessage += "Unrecognized external resource load status."; bUnrecognizedStatus = true; } if (!bUnrecognizedStatus && resourceType == ExternalResourceTypes.BuiltInExternalResourceTypes.RevitLink) { // For Revit links, the UI server can also obtain a RevitLinkLoadResult which contains detailed // information about the status of the attempt to load the local copy of the link into Revit. LinkLoadContent ldrlc = (LinkLoadContent)(data.GetLoadContent()); LinkLoadResult loadResult = ldrlc.GetLinkLoadResult(); if (loadResult != null) { myMessage += "\n LinkLoadResultType: " + loadResult.LoadResult.ToString("g"); } } System.Windows.Forms.MessageBox.Show(myMessage, "UI Server for SDK Sample External Resource Server"); } }