Extracting the edges from model lines and converting them into model lines is primarily done to facilitate the construction of bridge guardrails. This is because the guardrail path must be on a flat surface, which is currently a challenging problem to solve.
However, the previous issue can still be addressed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
namespace ObtainEdgeLines
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document revitDoc = commandData.Application.ActiveUIDocument.Document; // Get the active document
Application revitApp = commandData.Application.Application; // Get the Revit application
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Selection sel = uiDoc.Selection;
Reference ref1 = sel.PickObject(ObjectType.Element, "Select a family instance");
Element elem = revitDoc.GetElement(ref1);
FamilyInstance familyInstance = elem as FamilyInstance;
Options opt = new Options();
opt.ComputeReferences = true;
opt.DetailLevel = ViewDetailLevel.Fine;
GeometryElement geometryElement = familyInstance.get_Geometry(opt);
Transaction transaction = new Transaction(revitDoc);
transaction.Start("Extract Edges");
foreach (GeometryObject obj in geometryElement)
{
GeometryInstance geoInstance = obj as GeometryInstance;
GeometryElement geoElement = geoInstance.GetInstanceGeometry();
Transform insTransform = geoInstance.Transform;
foreach (GeometryObject obj2 in geoElement)
{
Solid solid = obj2 as Solid;
if (solid != null && solid.Faces.Size > 0)
{
foreach (Edge edge in solid.Edges)
{
Curve curve = edge.AsCurve();
// Uncomment for debugging: TaskDialog.Show("Revit", edge.AsCurve().GetType().ToString());
CreateModelLine(revitDoc, curve, revitApp);
// The following code is commented out as it's not currently used:
// SketchPlane plane = SketchPlane.Create(revitDoc, new Plane(normal, curve.GetEndPoint(0)));
// ModelCurve modelCurve = revitDoc.Create.NewModelCurve(curve, plane);
}
// Additional methods that could be used:
// FindBottomFace(solid);
// FindEdge(solid);
// FindLine(solid);
// FindPoint(solid);
// transformPointAndUaPoint(solid, insTransform);
// TaskDialog.Show("Info", "Processing complete.");
}
}
}
transaction.Commit();
return Result.Succeeded;
}
public void CreateModelLine(Document revitDoc, Curve curve, Application revitApp)
{
XYZ vector1 = GeomUtil.SubXYZ(curve.GetEndPoint(0), curve.GetEndPoint(1));
XYZ vector2 = GeomUtil.SubXYZ(curve.GetEndPoint(0), new XYZ(3, 29, 37));
XYZ normal = GeomUtil.CrossMatrix(vector1, vector2);
SketchPlane sketchPlane = SketchPlane.Create(revitDoc, revitApp.Create.NewPlane(normal, curve.GetEndPoint(0)));
ModelCurve modelCurve = revitDoc.FamilyCreate.NewModelCurve(curve, sketchPlane);
}
/// <summary>
/// Create model lines directly in the project environment
/// </summary>
/// <param name="revitDoc">The active Revit document</param>
/// <param name="curve">The curve to convert into a model line</param>
/// <param name="revitApp">The Revit application instance</param>
public void CreateModelLine2(Document revitDoc, Curve curve, Application revitApp)
{
try
{
XYZ vector1 = GeomUtil.SubXYZ(curve.GetEndPoint(0), curve.GetEndPoint(1));
XYZ vector2 = GeomUtil.SubXYZ(curve.GetEndPoint(0), new XYZ(3, 29, 37));
XYZ normal = GeomUtil.CrossMatrix(vector1, vector2);
SketchPlane sketchPlane = SketchPlane.Create(revitDoc, revitApp.Create.NewPlane(normal, curve.GetEndPoint(0)));
ModelCurve modelCurve = revitDoc.Create.NewModelCurve(curve, sketchPlane);
}
catch
{
XYZ vector1 = GeomUtil.SubXYZ(curve.Tessellate()[0], curve.Tessellate()[1]);
XYZ vector2 = GeomUtil.SubXYZ(curve.GetEndPoint(0), curve.Tessellate()[2]);
XYZ normal = GeomUtil.CrossMatrix(vector1, vector2);
SketchPlane sketchPlane = SketchPlane.Create(revitDoc, revitApp.Create.NewPlane(normal, curve.GetEndPoint(0)));
ModelCurve modelCurve = revitDoc.Create.NewModelCurve(curve, sketchPlane);
}
}
}
}













Must log in before commenting!
Sign Up