internal void AssertEqualParam(DB.Element expected, DB.Element actual, BuiltInParameter param) { var expecedParam = expected.get_Parameter(param); if (expecedParam == null) { return; } switch (expecedParam.StorageType) { case StorageType.Double: Assert.Equal(expecedParam.AsDouble(), actual.get_Parameter(param).AsDouble(), 4); break; case StorageType.Integer: Assert.Equal(expecedParam.AsInteger(), actual.get_Parameter(param).AsInteger()); break; case StorageType.String: Assert.Equal(expecedParam.AsString(), actual.get_Parameter(param).AsString()); break; case StorageType.ElementId: { var e1 = fixture.SourceDoc.GetElement(expecedParam.AsElementId()); var e2 = fixture.NewDoc.GetElement(actual.get_Parameter(param).AsElementId()); if (e1 is Level l1 && e2 is Level l2) { Assert.Equal(l1.Elevation, l2.Elevation, 3); }
internal void AssertEqualParam(DB.Element expected, DB.Element actual, BuiltInParameter param) { var expecedParam = expected.get_Parameter(param); if (expecedParam == null) { return; } if (expecedParam.StorageType == StorageType.Double) { Assert.Equal(expecedParam.AsDouble(), actual.get_Parameter(param).AsDouble(), 4); } else if (expecedParam.StorageType == StorageType.Integer) { Assert.Equal(expecedParam.AsInteger(), actual.get_Parameter(param).AsInteger()); } else if (expecedParam.StorageType == StorageType.String) { Assert.Equal(expecedParam.AsString(), actual.get_Parameter(param).AsString()); } else if (expecedParam.StorageType == StorageType.ElementId) { var e1 = fixture.SourceDoc.GetElement(expecedParam.AsElementId()); var e2 = fixture.NewDoc.GetElement(actual.get_Parameter(param).AsElementId()); if (e1 is Level l1 && e2 is Level l2) { Assert.Equal(l1.Elevation, l2.Elevation, 3); } else if (e1 != null && e2 != null) { Assert.Equal(e1.Name, e2.Name); } }
public static Dictionary <string, object> ChangeFittingsLevel(List <Revit.Elements.Element> elements, Revit.Elements.Level endLevel) { Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument; string result = ""; Autodesk.Revit.DB.Element ll = doc.GetElement(endLevel.UniqueId.ToString()); double ofEndLevel = ll.get_Parameter(BuiltInParameter.LEVEL_ELEV).AsDouble(); ElementId endLeveliD = ll.Id; try { foreach (Revit.Elements.Element e in elements) { Autodesk.Revit.DB.Element el = doc.GetElement(e.UniqueId.ToString()); double elOffset = el.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).AsDouble(); ElementId startLevel = el.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM).AsElementId(); double ofStartLevel = doc.GetElement(startLevel).get_Parameter(BuiltInParameter.LEVEL_ELEV).AsDouble(); double newOffset = -ofEndLevel + elOffset + ofStartLevel; el.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(newOffset); el.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM).Set(endLeveliD); } result = "Executed"; } catch (Exception ex) { result = "Not executed: " + ex.Message; } return(new Dictionary <string, object> { { "elements", elements }, { "result", result } }); }
public static IEnumerable <Autodesk.Revit.DB.Parameter> GetParameters(this Autodesk.Revit.DB.Element element, ParameterSource parameterSource) { switch (parameterSource) { case ParameterSource.Any: return(Enum.GetValues(typeof(BuiltInParameter)). Cast <BuiltInParameter>(). Select(x => element.get_Parameter(x)). Where(x => x?.HasValue ?? false). Union(element.Parameters.Cast <Autodesk.Revit.DB.Parameter>()). GroupBy(x => x.Id). Select(x => x.First())); case ParameterSource.BuiltIn: return(Enum.GetValues(typeof(BuiltInParameter)). Cast <BuiltInParameter>(). GroupBy(x => x). Select(x => x.First()). Select(x => element.get_Parameter(x)). Where(x => x?.HasValue ?? false)); case ParameterSource.Project: return(element.Parameters.Cast <Autodesk.Revit.DB.Parameter>(). Where(p => !p.IsShared)); case ParameterSource.Shared: return(element.Parameters.Cast <Autodesk.Revit.DB.Parameter>(). Where(p => p.IsShared)); } return(Enumerable.Empty <Autodesk.Revit.DB.Parameter>()); }
void ReconstructBuildingPadByOutline ( Document doc, ref Autodesk.Revit.DB.Element element, IList <Rhino.Geometry.Curve> boundaries, Optional <Autodesk.Revit.DB.BuildingPadType> type, Optional <Autodesk.Revit.DB.Level> level ) { var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { foreach (var boundary in boundaries) { boundary.Scale(scaleFactor); } } var boundaryBBox = Rhino.Geometry.BoundingBox.Empty; foreach (var boundary in boundaries) { boundaryBBox.Union(boundary.GetBoundingBox(true)); } SolveOptionalType(ref type, doc, ElementTypeGroup.BuildingPadType, (document, param) => BuildingPadType.CreateDefault(document), nameof(type)); SolveOptionalLevel(ref level, doc, boundaryBBox.Min.Z, nameof(level)); var curveLoops = boundaries.Select(region => CurveLoop.Create(region.ToHost().SelectMany(x => x.ToBoundedCurves()).ToList())); // Type ChangeElementTypeId(ref element, type.Value.Id); if (element is BuildingPad buildingPad) { buildingPad.SetBoundary(curveLoops.ToList()); } else { ReplaceElement(ref element, BuildingPad.Create(doc, type.Value.Id, level.Value.Id, curveLoops.ToList())); } if (element != null) { element.get_Parameter(BuiltInParameter.TYPE_WALL_CLOSURE).Set(level.Value.Id); element.get_Parameter(BuiltInParameter.LEVEL_PARAM).Set(level.Value.Id); element.get_Parameter(BuiltInParameter.BUILDINGPAD_HEIGHTABOVELEVEL_PARAM).Set(boundaryBBox.Min.Z - level.Value.Elevation); } }
public DB.Parameter GetParameter(DB.Element element) { if (ParameterSharedGUID.HasValue) { return(element.get_Parameter(ParameterSharedGUID.Value)); } if (ParameterBuiltInId != DB.BuiltInParameter.INVALID) { return(element.get_Parameter(ParameterBuiltInId)); } return(element.GetParameter(ParameterName, ParameterType, ParameterBinding, ParameterClass)); }
public static RevitElem ModifyParameter(SerialParameter serialParameter, RevitElem Elem) { RevitParam param = null; RevitDoc doc = Elem.Document; if (serialParameter.IsShared) { param = Elem.get_Parameter(new Guid(serialParameter.GUID)); } else if (serialParameter.Id < 0) { param = Elem.get_Parameter((BuiltInParameter)serialParameter.Id); } else if (serialParameter.Id > 0) { ParameterElement paramElem = (ParameterElement)doc.GetElement(new ElementId(serialParameter.Id)); if (paramElem != null) { Definition def = paramElem.GetDefinition(); param = Elem.get_Parameter(def); } } if (param != null && !param.IsReadOnly) { switch (serialParameter.StorageType) { case "Double": param.Set(Convert.ToDouble(serialParameter.Value)); break; case "ElementId": SerialParameter._ModifyElementIdParameter(param, serialParameter.ValueElemId, doc); break; case "Integer": param.Set(Convert.ToInt32(serialParameter.Value)); break; case "String": default: param.Set(serialParameter.Value); break; } } return(Elem); }
public static RevitElem ModifyParameter(ParameterJSON paramJSON, RevitElem Elem) { RevitParam param = null; if (paramJSON.IsShared) { param = Elem.get_Parameter(new Guid(paramJSON.GUID)); } else if (paramJSON.Id < 0) { param = Elem.get_Parameter((BuiltInParameter)paramJSON.Id); } else if (paramJSON.Id > 0) { revitDoc doc = Elem.Document; ParameterElement paramElem = (ParameterElement)doc.GetElement(new ElementId(paramJSON.Id)); if (paramElem != null) { Definition def = paramElem.GetDefinition(); param = Elem.get_Parameter(def); } } if (param != null && !param.IsReadOnly) { switch (paramJSON.StorageType) { case "Double": param.Set(Convert.ToDouble(paramJSON.Value)); break; case "ElementId": param.Set(new ElementId(Convert.ToInt32(paramJSON.Value))); break; case "Integer": param.Set(Convert.ToInt32(paramJSON.Value)); break; case "String": default: param.Set(paramJSON.Value); break; } } return(Elem); }
private static void _transferParameters(RevitElem SourceElement, RevitElem DestinationElement) { RevitDB.ParameterSet sourceParameters = SourceElement.Parameters; foreach (RevitDB.Parameter sourceParam in sourceParameters) { if (sourceParam.IsReadOnly == false) { RevitDB.Definition def = sourceParam.Definition; RevitDB.Parameter destinationParam = DestinationElement.get_Parameter(def); RevitDB.StorageType st = sourceParam.StorageType; switch (st) { case RevitDB.StorageType.Double: destinationParam.Set(sourceParam.AsDouble()); break; case RevitDB.StorageType.ElementId: destinationParam.Set(sourceParam.AsElementId()); break; case RevitDB.StorageType.Integer: destinationParam.Set(sourceParam.AsInteger()); break; case RevitDB.StorageType.String: destinationParam.Set(sourceParam.AsString()); break; } } } }
private T GetParamValue <T>(DB.Element elem, BuiltInParameter bip) { var rp = elem.get_Parameter(bip); if (rp == null || !rp.HasValue) { return(default);
/// <summary> /// Changes the workset of an element. /// </summary> /// <param name="element">Dynamo Elements.</param> /// <param name="workset">A revit workset</param> /// <returns name="element">The element that was changed. Returns null if the change was unsuccessfull.</returns> public static dynamoElement SetElementWorkset(dynamoElement element, Workset workset) { //Get Revit Document object revitDoc doc = DocumentManager.Instance.CurrentDBDocument; Autodesk.Revit.DB.Element unwrapped = element.InternalElement; WorksetId wId = unwrapped.WorksetId; Autodesk.Revit.DB.Parameter wsParam = unwrapped.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM); if (wsParam == null) { return(null); } if (doc.IsModifiable) { wsParam.Set(workset.internalId.IntegerValue); } else { using (Autodesk.Revit.DB.Transaction tx = new Autodesk.Revit.DB.Transaction(doc)) { tx.Start("Change Element's Workset"); wsParam.Set(workset.internalId.IntegerValue); tx.Commit(); } } return(unwrapped.ToDSType(true));; }
public static Parameter GetParameter(Autodesk.Revit.DB.Element element, RevitParam.ParameterName parameter) { Guid parameterID = RevitParam.ParamterList[parameter].Id; Parameter param = element.get_Parameter(parameterID); return(param); }
protected void PipeHostParameter(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName) { var param = srcElement.get_Parameter(srcParam); if (param != null) { switch (param.StorageType) { case DB.StorageType.None: break; case DB.StorageType.String: DA.SetData(paramName, param.AsString()); break; case DB.StorageType.Integer: DA.SetData(paramName, param.AsInteger()); break; case DB.StorageType.Double: DA.SetData(paramName, param.AsDoubleInRhinoUnits()); break; case DB.StorageType.ElementId: DA.SetData( paramName, Types.Element.FromElementId(doc: srcElement.Document, Id: param.AsElementId()) ); break; } } }
void ReconstructFloorByOutline ( Document doc, ref Autodesk.Revit.DB.Element element, Rhino.Geometry.Curve boundary, Optional <Autodesk.Revit.DB.FloorType> type, Optional <Autodesk.Revit.DB.Level> level, [Optional] bool structural ) { var scaleFactor = 1.0 / Revit.ModelUnits; if ( scaleFactor != 1.0 ? !boundary.Scale(scaleFactor) : true && boundary.IsShort(Revit.ShortCurveTolerance) || !boundary.IsClosed || !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) || boundaryPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0 ) { ThrowArgumentException(nameof(boundary), "Boundary must be an horizontal planar closed curve."); } SolveOptionalType(ref type, doc, ElementTypeGroup.FloorType, nameof(type)); SolveOptionalLevel(ref level, doc, boundary, nameof(level)); var curveArray = boundary.ToHost().ToCurveArray(); var parametersMask = new BuiltInParameter[] { BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM, BuiltInParameter.ELEM_FAMILY_PARAM, BuiltInParameter.ELEM_TYPE_PARAM, BuiltInParameter.LEVEL_PARAM, BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL }; if (type.Value.IsFoundationSlab) { ReplaceElement(ref element, doc.Create.NewFoundationSlab(curveArray, type.Value, level.Value, structural, XYZ.BasisZ), parametersMask); } else { ReplaceElement(ref element, doc.Create.NewFloor(curveArray, type.Value, level.Value, structural, XYZ.BasisZ), parametersMask); } if (element != null) { var boundaryBBox = boundary.GetBoundingBox(true); element.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM).Set(boundaryBBox.Min.Z - level.Value.Elevation); } }
protected void PipeHostParameter <T>(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName) where T : GH_Enumerate, new() { var param = srcElement.get_Parameter(srcParam); if (param != null && param.StorageType == DB.StorageType.Integer) { var enumType = new T(); enumType.Value = param.AsInteger(); DA.SetData(paramName, enumType); } }
private RevitLevel ConvertAndCacheLevel(DB.Element elem, BuiltInParameter bip) { var param = elem.get_Parameter(bip); if (param == null || param.StorageType != StorageType.ElementId) { return(null); } return(ConvertAndCacheLevel(param.AsElementId())); }
/// <summary> /// set parameter whose storage type is double /// </summary> /// <param name="elem">Element has parameter</param> /// <param name="builtInPara">BuiltInParameter to find parameter</param> /// <param name="value">value to set</param> /// <returns>is successful</returns> private bool SetParameter(ModelElement elem, BuiltInParameter builtInPara, double value) { Parameter para = elem.get_Parameter(builtInPara); if (null != para && para.StorageType == StorageType.Double && !para.IsReadOnly) { var result = para.Set(value); return(result); } return(false); }
/// <summary> /// set certain parameter of given element to int value /// </summary> /// <param name="elem">given element</param> /// <param name="paraIndex">BuiltInParameter</param> /// <param name="value"></param> /// <returns></returns> public static bool SetParaInt(Element elem, BuiltInParameter paraIndex, int value) { Parameter para = elem.get_Parameter(paraIndex); if (null == para) { return(false); } para.Set(value); return(true); }
private void UpdateExcelbyRevit() { FilteredElementCollector collector = PCFData.CollectPipeElements(this.Revit.Application.ActiveUIDocument.Document); foreach (DataRow data in DT_Components.Rows) { Autodesk.Revit.DB.Element element = null; IEnumerable <Autodesk.Revit.DB.Element> search = from Autodesk.Revit.DB.Element e in collector where e.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).AsValueString().Equals(data.Field <string>("FAMILYTYPE")) select e; if (search.Any()) { element = search.First(); } if (element != null) { string familyType = element.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).AsValueString(); IEnumerable <RevitParam.ParameterDefinition> FamilySharedParam = from RevitParam.ParameterDefinition pd in RevitParam.ParamterList.Values where pd.Scope == RevitParam.Scope.Share select pd; foreach (RevitParam.ParameterDefinition rvtParam in FamilySharedParam) { string rvtParamName = rvtParam.Name.Replace(RevitParam.Prefix, ""); if (element.get_Parameter(rvtParam.Id) != null) { switch (rvtParam.Type) { case (ParameterType.Text): data[rvtParamName] = element.get_Parameter(rvtParam.Id).AsString(); break; case (ParameterType.Integer): data[rvtParamName] = element.get_Parameter(rvtParam.Id).AsInteger(); break; } } } } } }
void ReconstructRoofByOutline ( Document doc, ref Autodesk.Revit.DB.Element element, Rhino.Geometry.Curve boundary, Optional <Autodesk.Revit.DB.RoofType> type, Optional <Autodesk.Revit.DB.Level> level ) { var scaleFactor = 1.0 / Revit.ModelUnits; if ( scaleFactor != 1.0 ? !boundary.Scale(scaleFactor) : true && boundary.IsShort(Revit.ShortCurveTolerance) || !boundary.IsClosed || !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) || boundaryPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0 ) { ThrowArgumentException(nameof(boundary), "Boundary must be an horizontal planar closed curve."); } SolveOptionalType(ref type, doc, ElementTypeGroup.RoofType, nameof(type)); double minZ = boundary.GetBoundingBox(true).Min.Z; SolveOptionalLevel(ref level, doc, minZ, nameof(level)); var parametersMask = new BuiltInParameter[] { BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM, BuiltInParameter.ELEM_FAMILY_PARAM, BuiltInParameter.ELEM_TYPE_PARAM, BuiltInParameter.LEVEL_PARAM, BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM }; using (var curveArray = boundary.ToHost().ToCurveArray()) { var modelCurveArray = new ModelCurveArray(); ReplaceElement(ref element, doc.Create.NewFootPrintRoof(curveArray, level.Value, type.Value, out modelCurveArray), parametersMask); } if (element != null) { element.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM).Set(minZ - level.Value.Elevation); } }
public Base LocationToSpeckle(DB.Element revitElement) { if (revitElement is FamilyInstance familyInstance) { //vertical columns are point based, and the point does not reflect the actual vertical location if (Categories.columnCategories.Contains(familyInstance.Category) || familyInstance.StructuralType == StructuralType.Column) { return(TryGetLocationAsCurve(familyInstance)); } } var revitLocation = revitElement.Location; switch (revitLocation) { case LocationCurve locationCurve: { var curve = locationCurve.Curve; //apply revit offset as transfrom if (revitElement is DB.Wall) { var offset = revitElement.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble(); XYZ vector = new XYZ(0, 0, offset); Transform tf = Transform.CreateTranslation(vector); curve = curve.CreateTransformed(tf); } return(CurveToSpeckle(curve) as Base); } case LocationPoint locationPoint: { return(PointToSpeckle(locationPoint.Point)); } // TODO what is the correct way to handle this? case null: return(null); default: return(null); } }
protected void PipeHostParameter(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName) { if (srcElement is null) { DA.SetData(paramName, null); return; } var param = srcElement.get_Parameter(srcParam); if (param != null) { switch (param.StorageType) { case DB.StorageType.None: break; case DB.StorageType.String: DA.SetData(paramName, param.AsString()); break; case DB.StorageType.Integer: if (param.Definition.ParameterType == DB.ParameterType.YesNo) { DA.SetData(paramName, param.AsInteger() != 0); } else { DA.SetData(paramName, param.AsInteger()); } break; case DB.StorageType.Double: DA.SetData(paramName, param.AsDoubleInRhinoUnits()); break; case DB.StorageType.ElementId: DA.SetData( paramName, Types.Element.FromElementId(srcElement.Document, param.AsElementId()) ); break; } } }
public static Dictionary <string, object> getRebarProperties(List <Revit.Elements.Element> rebars) { string message = ""; Document doc = DocumentManager.Instance.CurrentDBDocument; DynaFunctions f = new DynaFunctions(); //UIApplication uiapp = DocumentManager.Instance.CurrentUIApplication; //Autodesk.Revit.ApplicationServices.Application app = uiapp.Application; //UIDocument uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument; List <string> rStyle = new List <string>(); List <Revit.Elements.Element> rType = new List <Revit.Elements.Element>(); List <Revit.Elements.Element> hStartType = new List <Revit.Elements.Element>(); List <Revit.Elements.Element> hEndType = new List <Revit.Elements.Element>(); List <string> hStartOrient = new List <string>(); List <string> hEndOrient = new List <string>(); foreach (Revit.Elements.Element r in rebars) { Autodesk.Revit.DB.Element el = doc.GetElement(r.UniqueId.ToString()); rStyle.Add(el.get_Parameter(BuiltInParameter.REBAR_ELEM_HOOK_STYLE).AsValueString()); ElementId eId = el.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsElementId(); rType.Add(doc.GetElement(eId).ToDSType(true)); try { eId = el.get_Parameter(BuiltInParameter.REBAR_ELEM_HOOK_START_TYPE).AsElementId(); hStartType.Add(doc.GetElement(eId).ToDSType(true)); } catch { hStartType.Add(null); } try { eId = el.get_Parameter(BuiltInParameter.REBAR_ELEM_HOOK_END_TYPE).AsElementId(); hEndType.Add(doc.GetElement(eId).ToDSType(true)); } catch { hEndType.Add(null); } hEndOrient.Add(el.get_Parameter(BuiltInParameter.REBAR_ELEM_HOOK_END_ORIENT).AsValueString()); hStartOrient.Add(el.get_Parameter(BuiltInParameter.REBAR_ELEM_HOOK_START_ORIENT).AsValueString()); } return(new Dictionary <string, object> { { "rebarStyle", rStyle }, { "rebarBarType", rType }, { "hookStartType", hStartType }, { "hookEndType", hEndType }, { "hookStartOrientation", hStartOrient }, { "hookEndOrientation", hEndOrient }, //{ "Message", message }, }); }
public static Dictionary <string, object> getRebarDiameter(List <Revit.Elements.Element> rebarBarType) { string message = ""; Document doc = DocumentManager.Instance.CurrentDBDocument; DynaFunctions f = new DynaFunctions(); //UIApplication uiapp = DocumentManager.Instance.CurrentUIApplication; //Autodesk.Revit.ApplicationServices.Application app = uiapp.Application; //UIDocument uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument; List <double> rDiameters = new List <double>(); foreach (Revit.Elements.Element rt in rebarBarType) { Autodesk.Revit.DB.Element el = doc.GetElement(rt.UniqueId.ToString()); rDiameters.Add(f.feetToMillimeter(el.get_Parameter(BuiltInParameter.REBAR_BAR_DIAMETER).AsDouble())); } return(new Dictionary <string, object> { { "rebarDiameter", rDiameters }, //{ "Message", message }, }); }
public static void CopyParametersFrom(this Autodesk.Revit.DB.Element to, Autodesk.Revit.DB.Element from, ICollection <BuiltInParameter> parametersMask = null) { if (from != null && to != null) { foreach (var previousParameter in from.GetParameters(ParameterSource.Any)) { using (previousParameter) using (var param = to.get_Parameter(previousParameter.Definition)) { if (param == null || param.IsReadOnly) { continue; } if (parametersMask != null) { if (param.Definition is InternalDefinition internalDefinition) { if (parametersMask.Contains(internalDefinition.BuiltInParameter)) { continue; } } } switch (previousParameter.StorageType) { case StorageType.Integer: param.Set(previousParameter.AsInteger()); break; case StorageType.Double: param.Set(previousParameter.AsDouble()); break; case StorageType.String: param.Set(previousParameter.AsString()); break; case StorageType.ElementId: param.Set(previousParameter.AsElementId()); break; } } } } }
void ReconstructFamilyInstanceByPointAtHost ( Document doc, ref Autodesk.Revit.DB.Element element, Rhino.Geometry.Plane location, Autodesk.Revit.DB.FamilySymbol type, Optional <Autodesk.Revit.DB.Level> level, [Optional] Autodesk.Revit.DB.Element host ) { var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { location = location.Scale(scaleFactor); } if (!location.IsValid) { ThrowArgumentException(nameof(location), "Should be a valid point or plane."); } SolveOptionalLevel(ref level, doc, location.Origin.Z, nameof(level)); if (host == null && type.Family.FamilyPlacementType == FamilyPlacementType.OneLevelBasedHosted) { ThrowArgumentNullException(nameof(host), $"This family requires a host."); } if (!type.IsActive) { type.Activate(); } ChangeElementTypeId(ref element, type.Id); bool hasSameHost = false; if (element is FamilyInstance familyInstance) { hasSameHost = (familyInstance.Host?.Id ?? ElementId.InvalidElementId) == (host?.Id ?? ElementId.InvalidElementId); if (familyInstance.Host == null) { if (element?.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_PARAM) is Parameter freeHostParam) { var freeHostName = freeHostParam.AsString(); hasSameHost = freeHostName.EndsWith(host?.Name ?? level.Value.Name); } } } if ( hasSameHost && element is FamilyInstance && element.Location is LocationPoint locationPoint ) { using (var levelParam = element.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM)) { if (levelParam.AsElementId() != level.Value.Id) { levelParam.Set(level.Value.Id); doc.Regenerate(); } } var newOrigin = location.Origin.ToHost(); if (!newOrigin.IsAlmostEqualTo(locationPoint.Point)) { element.Pinned = false; locationPoint.Point = newOrigin; element.Pinned = true; } } else { var creationDataList = new List <Autodesk.Revit.Creation.FamilyInstanceCreationData>() { new Autodesk.Revit.Creation.FamilyInstanceCreationData(location.Origin.ToHost(), type, host, level.Value, Autodesk.Revit.DB.Structure.StructuralType.NonStructural) }; ICollection <ElementId> newElementIds = null; if (doc.IsFamilyDocument) { newElementIds = doc.FamilyCreate.NewFamilyInstances2(creationDataList); } else { newElementIds = doc.Create.NewFamilyInstances2(creationDataList); } if (newElementIds.Count != 1) { doc.Delete(newElementIds); throw new InvalidOperationException(); } var parametersMask = new BuiltInParameter[] { BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM, BuiltInParameter.ELEM_FAMILY_PARAM, BuiltInParameter.ELEM_TYPE_PARAM, BuiltInParameter.FAMILY_LEVEL_PARAM }; ReplaceElement(ref element, doc.GetElement(newElementIds.First()), parametersMask); } if (element is FamilyInstance instance && instance.Host == null) { element.Pinned = false; SetTransform(instance, location.Origin.ToHost(), location.XAxis.ToHost(), location.YAxis.ToHost()); element.Pinned = true; } }
internal static DB.Parameter GetParameter(IGH_ActiveObject obj, DB.Element element, IGH_Goo key) { DB.Parameter parameter = null; switch (key as Types.ParameterKey ?? key.ScriptVariable()) { case Types.ParameterKey parameterKey: if (parameterKey.Document.Equals(element.Document)) { if (Enum.IsDefined(typeof(DB.BuiltInParameter), parameterKey.Id.IntegerValue)) { parameter = element.get_Parameter((DB.BuiltInParameter)parameterKey.Id.IntegerValue); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{DB.LabelUtils.GetLabelFor((DB.BuiltInParameter) parameterKey.Id.IntegerValue)}' not defined in 'Element'"); } } else if (element.Document.GetElement(parameterKey.Id) is DB.ParameterElement parameterElement) { parameter = element.get_Parameter(parameterElement.GetDefinition()); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterElement.Name}' not defined in 'Element'"); } } else { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element"); } } else { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"'ParameterKey' doesn't belong same document as 'Element'"); } break; case DB.Parameter param: if (param.Element.Document.Equals(element.Document) && param.Element.Id == element.Id) { parameter = param; } else { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Parameter '{param.Definition.Name}' doesn't belong to 'Element'"); } break; case string parameterName: parameter = element.GetParameter(parameterName, DBX.ParameterClass.Any); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterName}' not defined in 'Element'"); } break; case int parameterId: if (Enum.IsDefined(typeof(DB.BuiltInParameter), parameterId)) { parameter = element.get_Parameter((DB.BuiltInParameter)parameterId); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{DB.LabelUtils.GetLabelFor((DB.BuiltInParameter) parameterId)}' not defined in 'Element'"); } } else if (element.Document.GetElement(new DB.ElementId(parameterId)) is DB.ParameterElement parameterElement) { parameter = element.get_Parameter(parameterElement.GetDefinition()); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterElement.Name}' not defined in 'Element'"); } } else { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element"); } break; case DB.ElementId parameterElementId: if (Enum.IsDefined(typeof(DB.BuiltInParameter), parameterElementId.IntegerValue)) { parameter = element.get_Parameter((DB.BuiltInParameter)parameterElementId.IntegerValue); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{DB.LabelUtils.GetLabelFor((DB.BuiltInParameter) parameterElementId.IntegerValue)}' not defined in 'Element'"); } } else if (element.Document.GetElement(parameterElementId) is DB.ParameterElement parameterElement) { parameter = element.get_Parameter(parameterElement.GetDefinition()); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{parameterElement.Name}' not defined in 'Element'"); } } else { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element"); } break; case Guid guid: parameter = element.get_Parameter(guid); if (parameter is null) { obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Parameter '{guid}' not defined in 'Element'"); } break; default: obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Data conversion failed from {key.TypeName} to Revit Parameter element"); break; } return(parameter); }
/// <summary> /// Set Parameter Value /// </summary> /// <param name="e">element</param> /// <param name="value">value</param> void SetParameter(Autodesk.Revit.DB.Element e, string value) { e.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set(value); }
protected override void TrySolveInstance(IGH_DataAccess DA) { DB.Element element = null; if (!DA.GetData("Element", ref element) || element is null) { return; } // Special cases if (element is DB.FamilyInstance familyInstace) { DA.SetData("Host", Types.HostObject.FromElement(familyInstace.Host)); return; } else if (element is DB.Opening opening) { DA.SetData("Host", Types.HostObject.FromElement(opening.Host)); return; } else if (element.get_Parameter(DB.BuiltInParameter.HOST_ID_PARAM) is DB.Parameter hostId) { DA.SetData("Host", Types.HostObject.FromElementId(element.Document, hostId.AsElementId())); return; } // Search geometrically if (element.get_BoundingBox(null) is DB.BoundingBoxXYZ bbox) { using (var collector = new DB.FilteredElementCollector(element.Document)) { var elementCollector = collector.OfClass(typeof(DB.HostObject)); if (element.Category.Parent is DB.Category hostCategory) { elementCollector = elementCollector.OfCategoryId(hostCategory.Id); } var bboxFilter = new DB.BoundingBoxIntersectsFilter(new DB.Outline(bbox.Min, bbox.Max)); elementCollector = elementCollector.WherePasses(bboxFilter); var classFilter = default(DB.ElementFilter); if (element is DB.FamilyInstance instance) { classFilter = new DB.FamilyInstanceFilter(element.Document, instance.GetTypeId()); } else if (element is DB.Area) { classFilter = new DB.AreaFilter(); } else if (element is DB.AreaTag) { classFilter = new DB.AreaTagFilter(); } else if (element is DB.Architecture.Room) { classFilter = new DB.Architecture.RoomFilter(); } else if (element is DB.Architecture.RoomTag) { classFilter = new DB.Architecture.RoomTagFilter(); } else if (element is DB.Mechanical.Space) { classFilter = new DB.Mechanical.SpaceFilter(); } else if (element is DB.Mechanical.SpaceTag) { classFilter = new DB.Mechanical.SpaceTagFilter(); } else { if (element is DB.CurveElement) { classFilter = new DB.ElementClassFilter(typeof(DB.CurveElement)); } else { classFilter = new DB.ElementClassFilter(element.GetType()); } } foreach (var host in elementCollector.Cast <DB.HostObject>()) { if (host.Id == element.Id) { continue; } if (host.FindInserts(false, true, true, false).Contains(element.Id)) { DA.SetData("Host", Types.HostObject.FromElement(host)); break; } // Necessary to found Panel walls in a Curtain Wall else if (host.GetDependentElements(classFilter).Contains(element.Id)) { DA.SetData("Host", Types.HostObject.FromElement(host)); break; } } } } }
/// <summary> /// set parameter whose storage type is double /// </summary> /// <param name="elem">Element has parameter</param> /// <param name="builtInPara">BuiltInParameter to find parameter</param> /// <param name="value">value to set</param> /// <returns>is successful</returns> private bool SetParameter(ModelElement elem, BuiltInParameter builtInPara, double value) { Parameter para = elem.get_Parameter(builtInPara); if (null != para && para.StorageType == StorageType.Double && !para.IsReadOnly) { var result = para.Set(value); return result; } return false; }
void ReconstructFormByGeometry ( Document doc, ref Autodesk.Revit.DB.Element element, Rhino.Geometry.Brep brep ) { if (!doc.IsFamilyDocument) { throw new InvalidOperationException("This component can only run on a Family document"); } brep.GetUserBoolean(BuiltInParameter.ELEMENT_IS_CUTTING.ToString(), out var cutting); if (brep.Faces.Count == 1 && brep.Faces[0].Loops.Count == 1 && brep.Faces[0].TryGetPlane(out var capPlane)) { using (var sketchPlane = SketchPlane.Create(doc, capPlane.ToPlane())) using (var referenceArray = new ReferenceArray()) { try { foreach (var curve in brep.Faces[0].OuterLoop.To3dCurve().ToCurveMany()) { referenceArray.Append(new Reference(doc.FamilyCreate.NewModelCurve(curve, sketchPlane))); } ReplaceElement ( ref element, doc.FamilyCreate.NewFormByCap ( !cutting, referenceArray ) ); return; } catch (Autodesk.Revit.Exceptions.InvalidOperationException) { doc.Delete(referenceArray.OfType <Reference>().Select(x => x.ElementId).ToArray()); } } } else if (brep.TryGetExtrusion(out var extrusion) && (extrusion.CapCount == 2 || !extrusion.IsClosed(0))) { using (var sketchPlane = SketchPlane.Create(doc, extrusion.GetProfilePlane(0.0).ToPlane())) using (var referenceArray = new ReferenceArray()) { try { foreach (var curve in extrusion.Profile3d(new Rhino.Geometry.ComponentIndex(Rhino.Geometry.ComponentIndexType.ExtrusionBottomProfile, 0)).ToCurveMany()) { referenceArray.Append(new Reference(doc.FamilyCreate.NewModelCurve(curve, sketchPlane))); } ReplaceElement ( ref element, doc.FamilyCreate.NewExtrusionForm ( !cutting, referenceArray, extrusion.PathLineCurve().Line.Direction.ToXYZ(UnitConverter.ToHostUnits) ) ); return; } catch (Autodesk.Revit.Exceptions.InvalidOperationException) { doc.Delete(referenceArray.OfType <Reference>().Select(x => x.ElementId).ToArray()); } } } { var solid = brep.ToSolid(); if (solid != null) { if (element is FreeFormElement freeFormElement) { freeFormElement.UpdateSolidGeometry(solid); } else { ReplaceElement(ref element, FreeFormElement.Create(doc, solid)); if (doc.OwnerFamily.IsConceptualMassFamily) { element.get_Parameter(BuiltInParameter.FAMILY_ELEM_SUBCATEGORY).Set(new ElementId(BuiltInCategory.OST_MassForm)); } } element.get_Parameter(BuiltInParameter.ELEMENT_IS_CUTTING)?.Set(cutting ? 1 : 0); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Failed to convert Brep to Form"); } } }
/// <summary> /// set certain parameter of given element to int value /// </summary> /// <param name="elem">given element</param> /// <param name="paraIndex">BuiltInParameter</param> /// <param name="value"></param> /// <returns></returns> public static bool SetParaInt(Element elem, BuiltInParameter paraIndex, int value) { Parameter para = elem.get_Parameter(paraIndex); if (null == para) { return false; } para.Set(value); return true; }