/// <summary> /// Creates a revit link. /// </summary> /// <param name="doc"></param> /// <param name="linkPath"></param> /// <returns></returns> public static void AddRevitLink(this Document doc, string linkPath) { if (doc == null) { throw new ArgumentNullException(nameof(doc)); } if (linkPath == null) { throw new ArgumentNullException(nameof(linkPath)); } var filePathl = new FilePath(linkPath); var linkOption = new RevitLinkOptions(false); doc.AutoTransaction(() => { var result = RevitLinkType.Create(doc, filePathl, linkOption); var instance = RevitLinkInstance.Create(doc, result.ElementId); if (!(doc.GetElement(instance.GetTypeId()) is RevitLinkType type)) { return; } type.AttachmentType = AttachmentType.Attachment; type.PathType = PathType.Relative; }); }
public void Load_Form(UIApplication uiapp, Document doc) { try { data_revit_link item = (data_revit_link)link_file.SelectedItem; doc.Delete(item.type.Id); ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(path); RevitLinkOptions rlo = new RevitLinkOptions(false); var linkType = RevitLinkType.Create(doc, mp, rlo); var instance = RevitLinkInstance.Create(doc, linkType.ElementId); List <Document> docs = new List <Document>(); foreach (Document d in uiapp.Application.Documents) { docs.Add(d); } item.document = docs.First(y => y.Title + ".rvt" == item.name); item.type = doc.GetElement(linkType.ElementId) as RevitLinkType; link_file.Items.Refresh(); MessageBox.Show(item.document.PathName); } catch (Exception ex) { MessageBox.Show(ex.Message); link_file.SelectedItem = null; } }
/// <summary> /// Creates an instance for the Revit file selected /// </summary> /// <param name="doc">Document to which the link should be added</param> /// <param name="revitFilePath">the full path of the Revit link to be added</param> private static void InstanceMaker(Document doc, string revitFilePath) { try { using (Transaction tr = new Transaction(doc)) { tr.Start("Revit files are being linked..."); // Cycle through the list // Set the standard options and behavior for the links FilePath fp = new FilePath(revitFilePath); RevitLinkOptions rlo = new RevitLinkOptions(false); // Create new revit link and store the path to the file as either absolute or relative RevitLinkLoadResult result = RevitLinkType.Create(doc, fp, rlo); ElementId linkId = result.ElementId; // Create the Revit Link Instance (this also automatically sets the link Origin-to-Origin) // Pin the Revit link as well RevitLinkInstance linkInstance = RevitLinkInstance.Create(doc, linkId); linkInstance.Pinned = true; tr.Commit(); } } catch (Exception) { // not sure what exceptions may occur // make sure that whatever happens is logged for future troubleshooting } }
public ElementId CreateRevitLink(Document doc, string pathName) { FilePath path = new FilePath(pathName); RevitLinkOptions options = new RevitLinkOptions(false); // Create new revit link storing absolute path to file LinkLoadResult result = RevitLinkType.Create(doc, path, options); return(result.ElementId); }
public void Link_File(Document doc) { try { ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(path); RevitLinkOptions rlo = new RevitLinkOptions(false); var linkType = RevitLinkType.Create(doc, mp, rlo); var instance = RevitLinkInstance.Create(doc, linkType.ElementId); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Link in the new created document to parent document. /// </summary> /// <param name="baseFileName">The full path to the IFC file.</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 baseFileName, Document ifcDocument, Document originalDocument, bool useExistingType, bool doSave) { bool saveSucceded = true; string fileName = GenerateRevitFileName(baseFileName); if (doSave) { SaveAsOptions saveAsOptions = new SaveAsOptions(); saveAsOptions.OverwriteExistingFile = true; try { ifcDocument.SaveAs(fileName, saveAsOptions); } catch (Exception ex) { // We still want to close the document to prevent having a corrupt model in memory. Importer.TheLog.LogError(-1, ex.Message, false); saveSucceded = 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); } if (!doReloadFrom) { Transaction linkTransaction = new Transaction(originalDocument); linkTransaction.Start(Resources.IFCLinkFile); try { if (revitLinkTypeId == ElementId.InvalidElementId) { RevitLinkOptions options = new RevitLinkOptions(true); RevitLinkLoadResult loadResult = RevitLinkType.CreateFromIFC(originalDocument, baseFileName, 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, baseFileName, fileName, false); } } } return(revitLinkTypeId); }
/// <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()); }
/// <summary> /// Link in the new created document to parent document. /// </summary> /// <param name="baseFileName">The full path to the IFC file.</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> public static void LinkInFile(string baseFileName, Document ifcDocument, Document originalDocument, bool useExistingType, bool doSave) { bool saveSucceded = true; string fileName = GenerateRevitFileName(baseFileName); if (doSave) { SaveAsOptions saveAsOptions = new SaveAsOptions(); saveAsOptions.OverwriteExistingFile = true; try { ifcDocument.SaveAs(fileName, saveAsOptions); } catch (Exception ex) { // We still want to close the document to prevent having a corrupt model in memory. IFCImportFile.TheLog.LogError(-1, ex.Message, false); saveSucceded = false; } } if (!ifcDocument.IsLinked) ifcDocument.Close(false); if (!saveSucceded) return; bool doReloadFrom = useExistingType && !Importer.TheOptions.CreateLinkInstanceOnly; ElementId revitLinkTypeId = ElementId.InvalidElementId; if (Importer.TheOptions.RevitLinkFileName != null) { FilePath originalRevitFilePath = new FilePath(Importer.TheOptions.RevitLinkFileName); revitLinkTypeId = RevitLinkType.GetTopLevelLink(originalDocument, originalRevitFilePath); } if (!doReloadFrom) { Transaction linkTransaction = new Transaction(originalDocument); linkTransaction.Start(Resources.IFCLinkFile); try { if (revitLinkTypeId == ElementId.InvalidElementId) { RevitLinkOptions options = new RevitLinkOptions(true); RevitLinkLoadResult loadResult = RevitLinkType.CreateFromIFC(originalDocument, baseFileName, 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, baseFileName, fileName, false); } } }