/// <summary> /// Creates a piece of furniture. /// </summary> /// <param name="properties"> Properties of the object.</param> private FamilyInstance CreateFurniture(FurnitureProperty properties) { if (properties is null) { throw new ArgumentNullException(nameof(properties)); } // get the properties double rotation = DeegreToRadians(properties.Rotation); XYZ p0 = GetXYZFromProperties(properties.Coordinate.ElementAt(0)); XYZ p1 = GetXYZFromProperties(properties.Coordinate.ElementAt(1)); XYZ point = (p0 + p1) / 2; string fsFamilyName = GetFamilySymbolName(properties.Type); UV offset = GetFamilyOffset(properties.Type); offset = new UV(UnitUtils.ConvertToInternalUnits(offset.U, UnitTypeId.Meters), UnitUtils.ConvertToInternalUnits(offset.V, UnitTypeId.Meters)); offset = VectorManipulator.RotateVector(offset, rotation + baseRotation); // Creates a point above the furniture to serve as a rotation axis XYZ axisPoint = new XYZ(point.X, point.Y, baseLevel.Elevation + 1); Line axis = Line.CreateBound(point, axisPoint); FamilyInstance furniture; try { FamilySymbol familySymbol = revitDB.GetFamilySymbol(fsFamilyName); Autodesk.Revit.DB.Structure.StructuralType structuralType = Autodesk.Revit.DB.Structure.StructuralType.NonStructural; furniture = doc.Create.NewFamilyInstance(point, familySymbol, structuralType); ElementTransformUtils.RotateElement(doc, furniture.Id, axis, rotation + baseRotation); ElementTransformUtils.MoveElement(doc, furniture.Id, VectorManipulator.TransformUVinXYZ(offset)); } catch (Exception e) { throw new Exception("Erro ao inserir mobiliario \"" + fsFamilyName + "\".", e); } return(furniture); }
/// <summary> /// Initialize a StructuralFraming element /// </summary> private void InitStructuralFraming(Autodesk.Revit.DB.Curve curve, Autodesk.Revit.DB.XYZ upVector, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.Structure.StructuralType structuralType, Autodesk.Revit.DB.FamilySymbol symbol) { //Phase 1 - Check to see if the object exists and should be rebound var oldFam = ElementBinder.GetElementFromTrace <Autodesk.Revit.DB.FamilyInstance>(Document); //There was a point, rebind to that, and adjust its position if (oldFam != null) { InternalSetFamilyInstance(oldFam); InternalSetFamilySymbol(symbol); InternalSetCurve(curve); return; } //Phase 2- There was no existing point, create one TransactionManager.Instance.EnsureInTransaction(Document); var creationData = GetCreationData(curve, upVector, level, structuralType, symbol); Autodesk.Revit.DB.FamilyInstance fi; if (Document.IsFamilyDocument) { var elementIds = Document.FamilyCreate.NewFamilyInstances2(new List <FamilyInstanceCreationData>() { creationData }); if (elementIds.Count == 0) { throw new Exception(Properties.Resources.FamilyInstanceCreationFailure); } fi = (Autodesk.Revit.DB.FamilyInstance)Document.GetElement(elementIds.First()); } else { var elementIds = Document.Create.NewFamilyInstances2(new List <FamilyInstanceCreationData>() { creationData }); if (elementIds.Count == 0) { throw new Exception(Properties.Resources.FamilyInstanceCreationFailure); } fi = (Autodesk.Revit.DB.FamilyInstance)Document.GetElement(elementIds.First()); } InternalSetFamilyInstance(fi); TransactionManager.Instance.TransactionTaskDone(); ElementBinder.SetElementForTrace(this.InternalElement); }
/// <summary> /// Internal constructor - creates a single StructuralFraming instance /// </summary> internal StructuralFraming(Autodesk.Revit.DB.Curve curve, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.Structure.StructuralType structuralType, Autodesk.Revit.DB.FamilySymbol symbol) { SafeInit(() => InitStructuralFraming(curve, level, structuralType, symbol)); }
private static FamilyInstanceCreationData GetCreationData(Autodesk.Revit.DB.Curve curve, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.Structure.StructuralType structuralType, Autodesk.Revit.DB.FamilySymbol symbol) { return(new FamilyInstanceCreationData(curve, symbol, level, structuralType)); }
private static FamilyInstanceCreationData GetCreationData(Autodesk.Revit.DB.Curve curve, Autodesk.Revit.DB.XYZ upVector, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.Structure.StructuralType structuralType, Autodesk.Revit.DB.FamilySymbol symbol) { //calculate the desired rotation //we do this by finding the angle between the z axis //and vector between the start of the beam and the target point //both projected onto the start plane of the beam. var zAxis = new XYZ(0, 0, 1); var yAxis = new XYZ(0, 1, 0); //flatten the beam line onto the XZ plane //using the start's z coordinate var start = curve.GetEndPoint(0); var end = curve.GetEndPoint(1); var newEnd = new XYZ(end.X, end.Y, start.Z); //drop end point to plane //catch the case where the end is directly above //the start, creating a normal with zero length //in that case, use the Z axis XYZ planeNormal = newEnd.IsAlmostEqualTo(start) ? zAxis : (newEnd - start).Normalize(); double gamma = upVector.AngleOnPlaneTo(zAxis.IsAlmostEqualTo(planeNormal) ? yAxis : zAxis, planeNormal); return(new FamilyInstanceCreationData(curve, symbol, level, structuralType) { RotateAngle = gamma }); }