/***************************************************/ /**** BHoM side of Revit_Adapter Push ****/ /***************************************************/ public override List <object> Push(IEnumerable <object> objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null) { //Initialize Revit config RevitPushConfig pushConfig = actionConfig as RevitPushConfig; //If internal adapter is loaded call it directly if (InternalAdapter != null) { InternalAdapter.RevitSettings = RevitSettings; return(InternalAdapter.Push(objects, tag, pushType, pushConfig)); } if (!this.IsValid()) { BH.Engine.Reflection.Compute.RecordError("Revit Adapter is not valid. Please check if it has been set up correctly and activated."); return(new List <object>()); } //Reset the wait event m_WaitEvent.Reset(); if (!CheckConnection()) { return(new List <object>()); } //Send data through the socket link m_LinkIn.SendData(new List <object>() { PackageType.Push, objects.ToList(), pushType, pushConfig, RevitSettings }, tag); //Wait until the return message has been recieved if (!m_WaitEvent.WaitOne(TimeSpan.FromMinutes(m_WaitTime))) { BH.Engine.Reflection.Compute.RecordError("The connection with Revit timed out. If working on a big model, try to increase the max wait time"); } //Grab the return objects from the latest package List <object> returnObjs = m_ReturnPackage.ToList(); //Clear the return list m_ReturnPackage.Clear(); RaiseEvents(); //Check if the return package contains error message if (returnObjs.Count == 1 && returnObjs[0] is string) { Engine.Reflection.Compute.RecordError(returnObjs[0] as string); return(new List <object>()); } //Return the package return(returnObjs); }
/***************************************************/ /**** Public Methods ****/ /***************************************************/ public List <IBHoMObject> Create(IEnumerable <IBHoMObject> bHoMObjects, RevitPushConfig pushConfig) { Document document = this.Document; RevitSettings settings = this.RevitSettings.DefaultIfNull(); Dictionary <Guid, List <int> > refObjects = new Dictionary <Guid, List <int> >(); List <IBHoMObject> created = new List <IBHoMObject>(); foreach (IBHoMObject obj in bHoMObjects) { if (Create(obj, document, settings, refObjects) != null) { created.Add(obj); } } return(created); }
/***************************************************/ /**** Public Methods ****/ /***************************************************/ public bool Update(Element element, IBHoMObject bHoMObject, RevitPushConfig pushConfig) { RevitSettings settings = this.RevitSettings.DefaultIfNull(); string tagsParameterName = settings.ParameterSettings.TagsParameter; try { element.IUpdate(bHoMObject, settings, pushConfig.SetLocationOnUpdate); //Assign Tags if (!string.IsNullOrEmpty(tagsParameterName)) { element.SetTags(bHoMObject, tagsParameterName); } return(true); } catch { ObjectNotUpdatedError(element, bHoMObject); return(false); } }
/***************************************************/ /**** Revit side of Revit_Adapter Push ****/ /***************************************************/ public override List <object> Push(IEnumerable <object> objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null) { // Check the document UIDocument uiDocument = this.UIDocument; Document document = this.Document; if (document == null) { BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be removed because Revit Document is null (possibly there is no open documents in Revit)."); return(new List <object>()); } if (document.IsReadOnly) { BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be removed because Revit Document is read only."); return(new List <object>()); } if (document.IsModifiable) { BH.Engine.Reflection.Compute.RecordError("BHoM objects could not be removed because another transaction is open in Revit."); return(new List <object>()); } // If unset, set the pushType to AdapterSettings' value (base AdapterSettings default is FullCRUD). Disallow the unsupported PushTypes. if (pushType == PushType.AdapterDefault) { pushType = PushType.DeleteThenCreate; } else if (pushType == PushType.FullPush) { BH.Engine.Reflection.Compute.RecordError("Full Push is currently not supported by Revit_Toolkit, please use Create, UpdateOnly or DeleteThenCreate instead."); return(new List <object>()); } // Set config RevitPushConfig pushConfig = actionConfig as RevitPushConfig; if (pushConfig == null) { BH.Engine.Reflection.Compute.RecordNote("Revit Push Config has not been specified. Default Revit Push Config is used."); pushConfig = new RevitPushConfig(); } // Suppress warnings if (UIControlledApplication != null && pushConfig.SuppressFailureMessages) { UIControlledApplication.ControlledApplication.FailuresProcessing += ControlledApplication_FailuresProcessing; } // Process the objects (verify they are valid; DeepClone them, wrap them, etc). IEnumerable <IBHoMObject> objectsToPush = ProcessObjectsForPush(objects, pushConfig); // Note: default Push only supports IBHoMObjects. if (objectsToPush.Count() == 0) { BH.Engine.Reflection.Compute.RecordError("Input objects were invalid."); return(new List <object>()); } // Add tag to each object if (!string.IsNullOrWhiteSpace(tag)) { foreach (IBHoMObject obj in objectsToPush) { if (obj.Tags == null) { obj.Tags = new HashSet <string> { tag } } ; else { obj.Tags.Add(tag); } } } // Push the objects string transactionName = "BHoM Push " + pushType; List <IBHoMObject> pushed = new List <IBHoMObject>(); using (Transaction transaction = new Transaction(document, transactionName)) { transaction.Start(); if (pushType == PushType.CreateOnly) { pushed = Create(objectsToPush, pushConfig); } else if (pushType == PushType.CreateNonExisting) { IEnumerable <IBHoMObject> toCreate = objectsToPush.Where(x => x.Element(document) == null); pushed = Create(toCreate, pushConfig); } else if (pushType == PushType.DeleteThenCreate) { List <IBHoMObject> toCreate = new List <IBHoMObject>(); foreach (IBHoMObject obj in objectsToPush) { Element element = obj.Element(document); if (element == null || Delete(element.Id, document, false).Count() != 0) { toCreate.Add(obj); } } pushed = Create(toCreate, pushConfig); } else if (pushType == PushType.UpdateOnly) { foreach (IBHoMObject obj in objectsToPush) { Element element = obj.Element(document); if (element != null && Update(element, obj, pushConfig)) { pushed.Add(obj); } } } transaction.Commit(); } // Switch of warning suppression if (UIControlledApplication != null) { UIControlledApplication.ControlledApplication.FailuresProcessing -= ControlledApplication_FailuresProcessing; } return(pushed.Cast <object>().ToList()); }