private void AssignGuid(FamilyInstance fi, Guid guid, Schema instanceSchema, int run, string nickName) { Entity entity = null; try { entity = fi.GetEntity(instanceSchema); } catch (Exception ex) { Debug.Write("Error", ex.Message); } try { if (!entity.IsValid()) { entity = new Entity(instanceSchema); } Field field = instanceSchema.GetField("InstanceID"); entity.Set<string>(field, guid.ToString()); field = instanceSchema.GetField("RunID"); entity.Set<int>(field, run); field = instanceSchema.GetField("NickName"); entity.Set<string>(field, nickName); fi.SetEntity(entity); } catch (Exception ex) { TaskDialog.Show("Error", ex.Message); } }
// The Execute method for the updater public void Execute(UpdaterData data) { try { Document doc = data.GetDocument(); FamilyInstance window = doc.get_Element(m_windowId) as FamilyInstance; Element section = doc.get_Element(m_sectionId); // iterate through modified elements to find the one we want the section to follow foreach (ElementId id in data.GetModifiedElementIds()) { if (id == m_windowId) { //Let's take this out temporarily. bool enableLookup = false; if (enableLookup) { m_schema = Schema.Lookup(m_schemaId); // (new Guid("{4DE4BE80-0857-4785-A7DF-8A8918851CB2}")); } Entity storedEntity = null; storedEntity = window.GetEntity(m_schema); // // first we look-up X-Y-Z parameters, which we know are set on our windows // and the values are set to the current coordinates of the window instance Field fieldPosition = m_schema.GetField("Position"); XYZ oldPosition = storedEntity.Get <XYZ>(fieldPosition, DisplayUnitType.DUT_FEET_FRACTIONAL_INCHES); TaskDialog.Show("Old position", oldPosition.ToString()); LocationPoint lp = window.Location as LocationPoint; XYZ newPosition = lp.Point; // XYZ has operator overloads XYZ translationVec = newPosition - oldPosition; // move the section by the same vector if (!translationVec.IsZeroLength()) { ElementTransformUtils.MoveElement(doc, section.Id, translationVec); } TaskDialog.Show("Moving", "Moving"); // Lookup the normal vector (i,j,we assume k=0) Field fieldOrientation = m_schema.GetField("Orientation"); // Establish the old and new orientation vectors XYZ oldNormal = storedEntity.Get <XYZ>(fieldOrientation, DisplayUnitType.DUT_FEET_FRACTIONAL_INCHES); XYZ newNormal = window.FacingOrientation; // If different, rotate the section by the angle around the location point of the window double angle = oldNormal.AngleTo(newNormal); // Need to adjust the rotation angle based on the direction of rotation (not covered by AngleTo) XYZ cross = oldNormal.CrossProduct(newNormal).Normalize(); double sign = 1.0; if (!cross.IsAlmostEqualTo(XYZ.BasisZ)) { sign = -1.0; } angle *= sign; if (Math.Abs(angle) > 0) { Line axis = doc.Application.Create.NewLineBound(newPosition, newPosition + XYZ.BasisZ); ElementTransformUtils.RotateElement(doc, section.Id, axis, angle); } // update the parameters on the window instance (to be the current position and orientation) storedEntity.Set <XYZ>(fieldPosition, newPosition, DisplayUnitType.DUT_FEET_FRACTIONAL_INCHES); storedEntity.Set <XYZ>(fieldOrientation, newNormal, DisplayUnitType.DUT_FEET_FRACTIONAL_INCHES); window.SetEntity(storedEntity); } } } catch (System.Exception ex) { TaskDialog.Show("Exception", ex.ToString()); } return; }
private List <RunCollection> FindExisting() { // Find existing elements with a matching GUID from the GH component. List <RunCollection> collectedRuns = new List <RunCollection>(); Schema instanceSchema = Schema.Lookup(instanceSchemaGUID); if (instanceSchema == null) { return(collectedRuns); } ElementIsElementTypeFilter filter = new ElementIsElementTypeFilter(false); FilteredElementCollector fiCollector = new FilteredElementCollector(doc); fiCollector.OfClass(typeof(FamilyInstance)); fiCollector.ToElements(); FilteredElementCollector wallCollector = new FilteredElementCollector(doc); wallCollector.OfCategory(BuiltInCategory.OST_Walls); wallCollector.OfClass(typeof(Wall)); wallCollector.ToElements(); FilteredElementCollector floorCollector = new FilteredElementCollector(doc); floorCollector.OfCategory(BuiltInCategory.OST_Floors); floorCollector.OfClass(typeof(Floor)); floorCollector.ToElements(); FilteredElementCollector roofCollector = new FilteredElementCollector(doc); roofCollector.OfCategory(BuiltInCategory.OST_Roofs); roofCollector.OfClass(typeof(RoofBase)); roofCollector.ToElements(); List <Element> elemCollector = new List <Element>(); foreach (Element e in fiCollector) { elemCollector.Add(e); } foreach (Element e in wallCollector) { elemCollector.Add(e); } foreach (Element e in floorCollector) { elemCollector.Add(e); } foreach (Element e in roofCollector) { elemCollector.Add(e); } //FilteredElementCollector elemCollector = new FilteredElementCollector(doc); //elemCollector.WherePasses(filter).ToElements(); // First, find all of the unique componentGUID's that are in the Revit file. List <string> instanceIds = new List <string>(); foreach (Element e in elemCollector) { if (e.Category.Id.IntegerValue == -2000011) { try { Wall w = e as Wall; if (w != null) { Entity entity = w.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (!instanceIds.Contains(tempId)) { instanceIds.Add(tempId); } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else if (e.Category.Id.IntegerValue == -2000032) { try { Floor flr = e as Floor; if (flr != null) { Entity entity = flr.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (!instanceIds.Contains(tempId)) { instanceIds.Add(tempId); } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else if (e.Category.Id.IntegerValue == -2000035) { try { RoofBase r = e as RoofBase; if (r != null) { Entity entity = r.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (!instanceIds.Contains(tempId)) { instanceIds.Add(tempId); } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else { try { FamilyInstance fi = e as FamilyInstance; if (fi != null) { Entity entity = fi.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (!instanceIds.Contains(tempId)) { instanceIds.Add(tempId); } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } } // Create a runCollection for each guid foreach (string id in instanceIds) { RunCollection rc = new RunCollection(); List <Runs> tempRuns = new List <Runs>(); // Find the number of runs per instanceId List <int> runIds = new List <int>(); foreach (Element e in elemCollector) { if (e.Category.Id.IntegerValue == -2000011) { // Walls try { Wall w = e as Wall; if (w != null) { Entity entity = w.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("NickName"); string nickName = entity.Get <string>(f); rc.ComponentGuid = new Guid(tempId); rc.NickName = nickName; f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (!runIds.Contains(tempRunId)) { runIds.Add(tempRunId); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else if (e.Category.Id.IntegerValue == -2000032) { // Floors try { Floor flr = e as Floor; if (flr != null) { Entity entity = flr.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("NickName"); string nickName = entity.Get <string>(f); rc.ComponentGuid = new Guid(tempId); rc.NickName = nickName; f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (!runIds.Contains(tempRunId)) { runIds.Add(tempRunId); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else if (e.Category.Id.IntegerValue == -2000035) { // Roofs try { RoofBase r = e as RoofBase; if (r != null) { Entity entity = r.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("NickName"); string nickName = entity.Get <string>(f); rc.ComponentGuid = new Guid(tempId); rc.NickName = nickName; f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (!runIds.Contains(tempRunId)) { runIds.Add(tempRunId); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else { // Other non-system families try { FamilyInstance fi = e as FamilyInstance; if (fi != null) { Entity entity = fi.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("NickName"); string nickName = entity.Get <string>(f); rc.ComponentGuid = new Guid(tempId); rc.NickName = nickName; f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (!runIds.Contains(tempRunId)) { runIds.Add(tempRunId); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } } foreach (int i in runIds) { List <int> runElemIds = new List <int>(); Runs run = new Runs(); foreach (Element e in elemCollector) { try { if (e.Category.Id.IntegerValue == -2000011) { // Walls try { Wall w = e as Wall; if (w != null) { Entity entity = w.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (tempRunId == i) { if (run.RunName == null || run.RunName == string.Empty) { run.RunId = tempRunId; run.RunName = "Run" + tempRunId.ToString(); run.FamilyType = w.Category.Name + " : " + w.WallType.Name; } runElemIds.Add(w.Id.IntegerValue); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else if (e.Category.Id.IntegerValue == -2000032) { // Floors try { Floor flr = e as Floor; if (flr != null) { Entity entity = flr.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (tempRunId == i) { if (run.RunName == null || run.RunName == string.Empty) { run.RunId = tempRunId; run.RunName = "Run" + tempRunId.ToString(); run.FamilyType = flr.Category.Name + " : " + flr.FloorType.Name; } runElemIds.Add(flr.Id.IntegerValue); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else if (e.Category.Id.IntegerValue == -2000035) { // Roofs try { RoofBase r = e as RoofBase; if (r != null) { Entity entity = r.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (tempRunId == i) { if (run.RunName == null || run.RunName == string.Empty) { run.RunId = tempRunId; run.RunName = "Run" + tempRunId.ToString(); run.FamilyType = r.Category.Name + " : " + r.RoofType.Name; } runElemIds.Add(r.Id.IntegerValue); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } else { // Other non-system families try { FamilyInstance fi = e as FamilyInstance; if (fi != null) { Entity entity = fi.GetEntity(instanceSchema); if (entity.IsValid()) { Field f = instanceSchema.GetField("InstanceID"); string tempId = entity.Get <string>(f); if (tempId == id) { f = instanceSchema.GetField("RunID"); int tempRunId = entity.Get <int>(f); if (tempRunId == i) { if (run.RunName == null || run.RunName == string.Empty) { run.RunId = tempRunId; run.RunName = "Run" + tempRunId.ToString(); run.FamilyType = fi.Symbol.Family.Name + " : " + fi.Symbol.Name; } runElemIds.Add(fi.Id.IntegerValue); } } } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } } catch (Exception ex) { Debug.WriteLine("Error", ex.Message); } } run.ElementIds = runElemIds; tempRuns.Add(run); } rc.Runs = tempRuns; collectedRuns.Add(rc); } return(collectedRuns); }