/// <summary> /// The method is used to create alignment between two faces /// </summary> /// <param name="view">the view</param> /// <param name="face1">face1</param> /// <param name="face2">face2</param> public void AddAlignment(View view, Face face1, Face face2) { PlanarFace pFace1 = null; PlanarFace pFace2 = null; if (face1 is PlanarFace) { pFace1 = face1 as PlanarFace; } if (face2 is PlanarFace) { pFace2 = face2 as PlanarFace; } if (pFace1 != null && pFace2 != null) { SubTransaction subTransaction = new SubTransaction(m_document); subTransaction.Start(); m_familyCreator.NewAlignment(view, pFace1.Reference, pFace2.Reference); subTransaction.Commit(); } }
/// <summary> /// Example demonstrating truss creation in the Autodesk Revit API. This example constructs /// a "mono" truss aligned with the reference planes in the (already loaded) truss family /// document. /// </summary> private void MakeNewTruss() { // Constants for arranging the angular truss members double webAngle = 35.0; double webAngleRadians = (180 - webAngle) * Math.PI / 180.0; Autodesk.Revit.DB.XYZ angleDirection = new Autodesk.Revit.DB.XYZ(Math.Cos(webAngleRadians), Math.Sin(webAngleRadians), 0); // Look up the reference planes and view in which to sketch ReferencePlane top = null, bottom = null, left = null, right = null, center = null; View level1 = null; List <Autodesk.Revit.DB.Element> elements = new List <Autodesk.Revit.DB.Element>(); ElementClassFilter refPlaneFilter = new ElementClassFilter(typeof(ReferencePlane)); ElementClassFilter viewFilter = new ElementClassFilter(typeof(View)); LogicalOrFilter filter = new LogicalOrFilter(refPlaneFilter, viewFilter); FilteredElementCollector collector = new FilteredElementCollector(m_document); elements.AddRange(collector.WherePasses(filter).ToElements()); foreach (Element e in elements) { // skip view templates because they're invisible invalid for truss creation View view = e as View; if (null != view && view.IsTemplate) { continue; } // switch (e.Name) { case "Top": top = e as ReferencePlane; break; case "Bottom": bottom = e as ReferencePlane; break; case "Right": right = e as ReferencePlane; break; case "Left": left = e as ReferencePlane; break; case "Center": center = e as ReferencePlane; break; case "Level 1": level1 = e as View; break; } } if (top == null || bottom == null || left == null || right == null || center == null || level1 == null) { throw new InvalidOperationException("Could not find prerequisite named reference plane or named view."); } SketchPlane sPlane = level1.SketchPlane; // Extract the geometry of each reference plane Line bottomLine = GetReferencePlaneLine(bottom); Line leftLine = GetReferencePlaneLine(left); Line rightLine = GetReferencePlaneLine(right); Line topLine = GetReferencePlaneLine(top); Line centerLine = GetReferencePlaneLine(center); // Create bottom chord along "bottom" from "left" to "right" Autodesk.Revit.DB.XYZ bottomLeft = GetIntersection(bottomLine, leftLine); Autodesk.Revit.DB.XYZ bottomRight = GetIntersection(bottomLine, rightLine); ModelCurve bottomChord = MakeTrussCurve(bottomLeft, bottomRight, sPlane, TrussCurveType.BottomChord); if (null != bottomChord) { // Add the alignment constraint to the bottom chord. Curve geometryCurve = bottomChord.GeometryCurve; // Lock the bottom chord to bottom reference plan m_familyCreator.NewAlignment(level1, bottom.GetReference(), geometryCurve.Reference); } // Create web connecting top and bottom chords on the right side Autodesk.Revit.DB.XYZ topRight = GetIntersection(topLine, rightLine); ModelCurve rightWeb = MakeTrussCurve(bottomRight, topRight, sPlane, TrussCurveType.Web); if (null != rightWeb) { // Add the alignment constraint to the right web chord. Curve geometryCurve = rightWeb.GeometryCurve; // Lock the right web chord to right reference plan m_familyCreator.NewAlignment(level1, right.GetReference(), geometryCurve.Reference); } // Create top chord diagonally from bottom-left to top-right ModelCurve topChord = MakeTrussCurve(bottomLeft, topRight, sPlane, TrussCurveType.TopChord); if (null != topChord) { // Add the alignment constraint to the top chord. Curve geometryCurve = topChord.GeometryCurve; // Lock the start point of top chord to the Intersection of left and bottom reference plan m_familyCreator.NewAlignment(level1, geometryCurve.GetEndPointReference(0), left.GetReference()); m_familyCreator.NewAlignment(level1, geometryCurve.GetEndPointReference(0), bottom.GetReference()); // Lock the end point of top chord to the Intersection of right and top reference plan m_familyCreator.NewAlignment(level1, geometryCurve.GetEndPointReference(1), top.GetReference()); m_familyCreator.NewAlignment(level1, geometryCurve.GetEndPointReference(1), right.GetReference()); } // Create angled web from midpoint to the narrow end of the truss Autodesk.Revit.DB.XYZ bottomMidPoint = GetIntersection(bottomLine, centerLine); Line webDirection = Line.CreateUnbound(bottomMidPoint, angleDirection); Autodesk.Revit.DB.XYZ endOfWeb = GetIntersection(topChord.GeometryCurve as Line, webDirection); ModelCurve angledWeb = MakeTrussCurve(bottomMidPoint, endOfWeb, sPlane, TrussCurveType.Web); // Add a dimension to force the angle to be stable even when truss length and height are modified Arc dimensionArc = Arc.Create( bottomMidPoint, angledWeb.GeometryCurve.Length / 2, webAngleRadians, Math.PI, Autodesk.Revit.DB.XYZ.BasisX, Autodesk.Revit.DB.XYZ.BasisY); Dimension createdDim = m_familyCreator.NewAngularDimension( level1, dimensionArc, angledWeb.GeometryCurve.Reference, bottomChord.GeometryCurve.Reference); if (null != createdDim) { createdDim.IsLocked = true; } // Create angled web from corner to top of truss Autodesk.Revit.DB.XYZ bottomRight2 = GetIntersection(bottomLine, rightLine); webDirection = Line.CreateUnbound(bottomRight2, angleDirection); endOfWeb = GetIntersection(topChord.GeometryCurve as Line, webDirection); ModelCurve angledWeb2 = MakeTrussCurve(bottomRight, endOfWeb, sPlane, TrussCurveType.Web); // Add a dimension to force the angle to be stable even when truss length and height are modified dimensionArc = Arc.Create( bottomRight, angledWeb2.GeometryCurve.Length / 2, webAngleRadians, Math.PI, Autodesk.Revit.DB.XYZ.BasisX, Autodesk.Revit.DB.XYZ.BasisY); createdDim = m_familyCreator.NewAngularDimension( level1, dimensionArc, angledWeb2.GeometryCurve.Reference, bottomChord.GeometryCurve.Reference); if (null != createdDim) { createdDim.IsLocked = true; } //Connect bottom midpoint to end of the angled web ModelCurve braceWeb = MakeTrussCurve(bottomMidPoint, endOfWeb, sPlane, TrussCurveType.Web); }