/// <summary> /// Link in the new created document to parent document. /// </summary> /// <param name="originalIFCFileName">The full path to the original IFC file. Same as baseLocalFileName if the IFC file is not on a server.</param> /// <param name="baseLocalFileName">The full path to the IFC file on disk.</param> /// <param name="ifcDocument">The newly imported IFC file document.</param> /// <param name="originalDocument">The document to contain the IFC link.</param> /// <param name="useExistingType">True if the RevitLinkType already exists.</param> /// <param name="doSave">True if we should save the document. This should only be false if we are reusing a cached document.</param> /// <returns>The element id of the RevitLinkType for this link operation.</returns> public static ElementId LinkInFile(string originalIFCFileName, string baseLocalFileName, Document ifcDocument, Document originalDocument, bool useExistingType, bool doSave) { bool saveSucceded = true; string fileName = GenerateRevitFileName(baseLocalFileName); if (doSave) { SaveAsOptions saveAsOptions = new SaveAsOptions(); saveAsOptions.OverwriteExistingFile = true; try { ifcDocument.SaveAs(fileName, saveAsOptions); } catch { saveSucceded = false; } if (!saveSucceded) { try { string tempPathDir = Path.GetTempPath(); string fileNameOnly = Path.GetFileName(fileName); string intermediateFileName = tempPathDir + fileNameOnly; ifcDocument.SaveAs(tempPathDir + fileNameOnly, saveAsOptions); File.Copy(intermediateFileName, fileName); Application application = ifcDocument.Application; ifcDocument.Close(false); ifcDocument = application.OpenDocumentFile(fileName); File.Delete(intermediateFileName); saveSucceded = true; } catch (Exception ex) { // We still want to close the document to prevent having a corrupt model in memory. saveSucceded = false; Importer.TheLog.LogError(-1, ex.Message, false); } } } if (!ifcDocument.IsLinked) { ifcDocument.Close(false); } ElementId revitLinkTypeId = ElementId.InvalidElementId; if (!saveSucceded) { return(revitLinkTypeId); } bool doReloadFrom = useExistingType && !Importer.TheOptions.CreateLinkInstanceOnly; if (Importer.TheOptions.RevitLinkFileName != null) { FilePath originalRevitFilePath = new FilePath(Importer.TheOptions.RevitLinkFileName); revitLinkTypeId = RevitLinkType.GetTopLevelLink(originalDocument, originalRevitFilePath); } ModelPath path = ModelPathUtils.ConvertUserVisiblePathToModelPath(originalIFCFileName); // Relative path type only works if the model isn't in the cloud. As such, we'll try again if the // routine returns an exception. ExternalResourceReference ifcResource = null; for (int ii = 0; ii < 2; ii++) { PathType pathType = (ii == 0) ? PathType.Relative : PathType.Absolute; try { ifcResource = ExternalResourceReference.CreateLocalResource(originalDocument, ExternalResourceTypes.BuiltInExternalResourceTypes.IFCLink, path, pathType); break; } catch { ifcResource = null; } } if (ifcResource == null) { Importer.TheLog.LogError(-1, "Couldn't create local IFC cached file. Aborting import.", true); } if (!doReloadFrom) { Transaction linkTransaction = new Transaction(originalDocument); linkTransaction.Start(Resources.IFCLinkFile); try { if (revitLinkTypeId == ElementId.InvalidElementId) { RevitLinkOptions options = new RevitLinkOptions(true); LinkLoadResult loadResult = RevitLinkType.CreateFromIFC(originalDocument, ifcResource, fileName, false, options); if ((loadResult != null) && (loadResult.ElementId != ElementId.InvalidElementId)) { revitLinkTypeId = loadResult.ElementId; } } if (revitLinkTypeId != ElementId.InvalidElementId) { RevitLinkInstance.Create(originalDocument, revitLinkTypeId); } Importer.PostDelayedLinkErrors(originalDocument); linkTransaction.Commit(); } catch (Exception ex) { linkTransaction.RollBack(); throw ex; } } else // reload from { // For the reload from case, we expect the transaction to have been created in the UI. if (revitLinkTypeId != ElementId.InvalidElementId) { RevitLinkType existingRevitLinkType = originalDocument.GetElement(revitLinkTypeId) as RevitLinkType; if (existingRevitLinkType != null) { existingRevitLinkType.UpdateFromIFC(originalDocument, ifcResource, fileName, false); } } } return(revitLinkTypeId); }
public void Execute(UIApplication app) { GetLogs.SetNewLog(null, "--", ProjectDto.Id); var stp_watch = new Stopwatch(); stp_watch.Start(); Document doc = app.ActiveUIDocument.Document; foreach (ModelDto model in ModelList) { using (Transaction loadDoc = new Transaction(doc, "Load Link Documents")) { loadDoc.Start(); RevitLinkOptions rvtLinkOptions = new RevitLinkOptions(false); ModelPath modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(model.ModelPath); LinkLoadResult linkType = RevitLinkType.Create(doc, modelPath, rvtLinkOptions); RevitLinkInstance openedDocument = RevitLinkInstance.Create(doc, linkType.ElementId); Document docLink = openedDocument.GetLinkDocument(); stp_watch.Stop(); GetLogs.SetNewLog(stp_watch.Elapsed.ToString(), "Добавление модели: " + model.ModelName, ProjectDto.Id); stp_watch.Restart(); stp_watch.Start(); /* Получить все экземплярый и записать их в бд */ List <Element> instances = FindElements(docLink, GetCategoryFilter(), true).Where(x => x != null).ToList(); dataBaseFamilies.AddFamilies(instances.Select(x => new FamilyDto() { Categoty = x.Category.Name, FamilyInstId = x.Id.IntegerValue, FamilyTypeId = x.GetTypeId().IntegerValue, ModelId = model.Id }).ToList()); stp_watch.Stop(); GetLogs.SetNewLog(stp_watch.Elapsed.ToString(), "Создание нового списка семейств и запись в БД. Количество семейств: " + instances.Count, ProjectDto.Id); stp_watch.Restart(); stp_watch.Start(); var FamlistFroDb = dataBaseFamilies.GetFamilyInstances(model); List <ParameterInstanceDto> ParamsInstList = new List <ParameterInstanceDto>(); List <ParameterTypeDto> ParamsTypeList = new List <ParameterTypeDto>(); foreach (Element elem in instances) { var famId = FamlistFroDb.Where(x => x.FamilyInstId == elem.Id.IntegerValue).FirstOrDefault().Id; ParamsInstList.AddRange(elem.Parameters.ToList().Select(x => new ParameterInstanceDto() { Name = x.Definition.Name, FamilyId = famId }).ToList()); } stp_watch.Stop(); GetLogs.SetNewLog(stp_watch.Elapsed.ToString(), "Создание списка параметров экземпляров. Количество параметров: " + ParamsInstList.Count, ProjectDto.Id); stp_watch.Restart(); stp_watch.Start(); foreach (Element elem in instances) { var famId = FamlistFroDb.Where(x => x.FamilyInstId == elem.Id.IntegerValue).FirstOrDefault().Id; var elemType = docLink.GetElement(elem.GetTypeId()); if (elemType != null) { ParamsTypeList.AddRange((elemType.Parameters.ToList().Select(x => new ParameterTypeDto() { Name = x.Definition.Name, FamilyId = famId }).ToList())); } } stp_watch.Stop(); GetLogs.SetNewLog(stp_watch.Elapsed.ToString(), "Создание списка параметров типов. Количество параметров: " + ParamsTypeList.Count, ProjectDto.Id); stp_watch.Restart(); stp_watch.Start(); dataBaseParameters.AddParameterInstances(ParamsInstList); stp_watch.Stop(); GetLogs.SetNewLog(stp_watch.Elapsed.ToString(), "Выгрузка параметров экземпляров в БД", ProjectDto.Id); stp_watch.Restart(); stp_watch.Start(); dataBaseParameters.AddParameterTypes(ParamsTypeList); stp_watch.Stop(); GetLogs.SetNewLog(stp_watch.Elapsed.ToString(), "Выгрузка параметров типов в БД", ProjectDto.Id); loadDoc.RollBack(); } } stp_watch.Stop(); //GetLogs.SetNewLog(stp_watch.Elapsed.TotalMinutes.ToString(), "Выгрузка параметров", ProjectDto.Id); //TaskDialog.Show("Время выгрузки", upload_link_model.ToString()); }
void MiroReloadLinks(IList <RevitLinkType> fecLinkTypes) { // Loop all RVT Links foreach (RevitLinkType typeLink in fecLinkTypes) { // ... // Skip1 - not IsFromRevitServer if (!typeLink.IsFromRevitServer()) { //… continue; } // Skip2 - not ExternalFileReference // 99% it would already skip above as // RevitServer MUST be ExternalFileReference, // but leave just in case... ExternalFileReference er = typeLink.GetExternalFileReference(); if (er == null) { // ... continue; } // If here, we can cache ModelPath related // info and show to user regardless if we skip // on next checks or not.... ModelPath mp = er.GetPath(); string userVisiblePath = ModelPathUtils .ConvertModelPathToUserVisiblePath(mp); // Skip3 - if ModelPath is NOT Server Path // 99% redundant as we already checked raw // RevitLinkType for this, but keep // just in case... if (!mp.ServerPath) { // ... continue; } // Skip4 - if NOT "NOT Found" problematic one // there is nothing to fix if (er.GetLinkedFileStatus() != LinkedFileStatus.NotFound) { // ... continue; } // Skip5 - if Nested Link (can’t (re)load these!) if (typeLink.IsNestedLink) { // ... continue; } // If here, we MUST offer user to "Reload from..." // ... //RevitLinkLoadResult res = null; // 2017 LinkLoadResult res = null; // 2018 try { // This fails for problematic Server files // since it also fails on "Reload" button in // UI (due to the GUID issue in the answer) //res = typeLink.Reload(); // This fails same as above :-(! //res = typeLink.Load(); // This WORKS! // Basically, this is the equivalent of UI // "Reload from..." + browsing to the *same* // Saved path showing in the manage Links // dialogue. // ToDo: Check if we need to do anything // special with WorksetConfiguration? // In tests, it works fine with the // default c-tor. ModelPath mpForReload = ModelPathUtils .ConvertUserVisiblePathToModelPath( userVisiblePath); res = typeLink.LoadFrom(mpForReload, new WorksetConfiguration()); Util.InfoMsg(string.Format( "Result = {0}", res.LoadResult)); } catch (Exception ex) { Debug.Print(ex.Message); } } // foreach typeLink }
/// <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"); } }