void CommitInstance ( Document doc, IGH_DataAccess DA, int Iteration, Rhino.Geometry.Plane plane ) { var element = PreviousElement(doc, Iteration); try { if (!plane.IsValid) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, string.Format("Parameter '{0}' is not valid.", Params.Input[0].Name)); } else { var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { plane = plane.Scale(scaleFactor); } element = SketchPlane.Create(doc, plane.ToHost()); } } catch (Exception e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); } finally { ReplaceElement(doc, DA, Iteration, element); } }
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; } }
void CommitInstance ( Document doc, IGH_DataAccess DA, int Iteration, Rhino.Geometry.Plane plane ) { var element = PreviousElement(doc, Iteration); try { if (!plane.IsValid) { throw new Exception(string.Format("Parameter '{0}' is not valid.", Params.Input[0].Name)); } var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { plane = plane.Scale(scaleFactor); } if (element is SketchPlane sketchPlane) { bool pinned = element.Pinned; element.Pinned = false; var plane0 = sketchPlane.GetPlane(); using (var plane1 = plane.ToHost()) { if (!plane0.Normal.IsParallelTo(plane1.Normal)) { var axisDirection = plane0.Normal.CrossProduct(plane1.Normal); double angle = plane0.Normal.AngleTo(plane1.Normal); using (var axis = Line.CreateUnbound(plane0.Origin, axisDirection)) ElementTransformUtils.RotateElement(Revit.ActiveDBDocument, element.Id, axis, angle); plane0 = sketchPlane.GetPlane(); } { double angle = plane0.XVec.AngleOnPlaneTo(plane1.XVec, plane1.Normal); if (angle != 0.0) { using (var axis = Line.CreateUnbound(plane0.Origin, plane1.Normal)) ElementTransformUtils.RotateElement(Revit.ActiveDBDocument, element.Id, axis, angle); } } var trans = plane1.Origin - plane0.Origin; if (!trans.IsZeroLength()) { ElementTransformUtils.MoveElement(Revit.ActiveDBDocument, element.Id, trans); } } element.Pinned = pinned; } else { element = CopyParametersFrom(SketchPlane.Create(doc, plane.ToHost()), element); } ReplaceElement(doc, DA, Iteration, element); } catch (Exception e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); ReplaceElement(doc, DA, Iteration, null); } }
void CommitInstance ( Document doc, IGH_DataAccess DA, int Iteration, Rhino.Geometry.Plane location, Autodesk.Revit.DB.FamilySymbol familySymbol, Autodesk.Revit.DB.HostObject host, Autodesk.Revit.DB.Level level ) { var element = PreviousElement(doc, Iteration); if (!element?.Pinned ?? false) { ReplaceElement(doc, DA, Iteration, element); } else { try { var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { location = location.Scale(scaleFactor); } if (!location.IsValid) { throw new Exception(string.Format("Parameter '{0}' must be a valid point.", Params.Input[0].Name)); } if (familySymbol == null) { throw new Exception(string.Format("Parameter '{0}' must be a FamilySymbol point.", Params.Input[1].Name)); } if (level == null) { throw new Exception(string.Format("Parameter '{0}' must be a valid Level.", Params.Input[2].Name)); } if (host == null && familySymbol.Family.FamilyPlacementType == FamilyPlacementType.OneLevelBasedHosted) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, string.Format("Parameter '{0}' must be a valid HostObject.", Params.Input[3].Name)); } if (!familySymbol.IsActive) { familySymbol.Activate(); } if (element is FamilyInstance && familySymbol.Id != element.GetTypeId()) { var newElmentId = element.ChangeTypeId(familySymbol.Id); if (newElmentId != ElementId.InvalidElementId) { element = doc.GetElement(newElmentId); } } 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.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.Id) { levelParam.Set(level.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(), familySymbol, host, level, 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) { element = CopyParametersFrom(doc.GetElement(newElementIds.First()), element); element?.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM).Set(level.Id); } else { element = null; } } 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; } ReplaceElement(doc, DA, Iteration, element); } catch (Exception e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); ReplaceElement(doc, DA, Iteration, null); } } }
void ReconstructSketchPlaneByPlane ( Document doc, ref Autodesk.Revit.DB.Element element, Rhino.Geometry.Plane plane ) { if (!plane.IsValid) { ThrowArgumentException(nameof(plane), "Plane is not valid."); } var scaleFactor = 1.0 / Revit.ModelUnits; if (scaleFactor != 1.0) { plane = plane.Scale(scaleFactor); } if (element is SketchPlane sketchPlane) { bool pinned = element.Pinned; element.Pinned = false; var plane0 = sketchPlane.GetPlane(); using (var plane1 = plane.ToHost()) { if (!plane0.Normal.IsParallelTo(plane1.Normal)) { var axisDirection = plane0.Normal.CrossProduct(plane1.Normal); double angle = plane0.Normal.AngleTo(plane1.Normal); using (var axis = Line.CreateUnbound(plane0.Origin, axisDirection)) ElementTransformUtils.RotateElement(doc, element.Id, axis, angle); plane0 = sketchPlane.GetPlane(); } { double angle = plane0.XVec.AngleOnPlaneTo(plane1.XVec, plane1.Normal); if (angle != 0.0) { using (var axis = Line.CreateUnbound(plane0.Origin, plane1.Normal)) ElementTransformUtils.RotateElement(doc, element.Id, axis, angle); } } var trans = plane1.Origin - plane0.Origin; if (!trans.IsZeroLength()) { ElementTransformUtils.MoveElement(doc, element.Id, trans); } } element.Pinned = pinned; } else { ReplaceElement(ref element, SketchPlane.Create(doc, plane.ToHost())); } }