解决H钢结构标高
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document revitDoc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector beamCL = new FilteredElementCollector(revitDoc).OfCategory(BuiltInCategory.OST_StructuralFraming).OfClass(typeof(FamilyInstance));//获取视图中所有的梁
View view = commandData.Application.ActiveUIDocument.ActiveView;
Reference instance = commandData.Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "选择一个梁");
if (instance != null)
{
FamilyInstance familyInstance = revitDoc.GetElement(instance.ElementId) as FamilyInstance;
Options options = new Options()
{
ComputeReferences = true,
DetailLevel = ViewDetailLevel.Fine
};
GeometryElement geometryElement = familyInstance.get_Geometry(options);
GetBeamFaces(geometryElement);
faces.OrderBy(p=>p.Origin.Z);
faces.Reverse();
LocationCurve curve = familyInstance.Location as LocationCurve;
XYZ origin = curve.Curve.Evaluate(0.5, true);
Reference faceRef = faces[0].Reference;
string referStr = faceRef.ConvertToStableRepresentation(revitDoc);
string actualStr = $"{instance.ConvertToStableRepresentation(revitDoc)}:0:INSTANCE:{referStr}";
Reference reference = Reference.ParseFromStableRepresentation(revitDoc, actualStr);
using (Transaction tr = new Transaction(revitDoc, "lg"))
{
tr.Start();
try
{
SpotDimension spotDimension = revitDoc.Create.NewSpotElevation(view, reference, origin, origin, origin, origin, false);
}
catch (Exception ex)
{
TaskDialog.Show("提示", $"{ex.Message}");
}
tr.Commit();
}
}
return Result.Succeeded;
}
List<PlanarFace> faces = new List<PlanarFace>();
private void GetBeamFaces(GeometryElement geometryElement)
{
foreach (GeometryObject item in geometryElement)
{
Solid solid = item as Solid;
if (solid != null)
{
foreach (Face face in solid.Faces)
{
PlanarFace planarFace = face as PlanarFace;
if (planarFace != null)
{
if (planarFace.FaceNormal.IsAlmostEqualTo(-XYZ.BasisZ))
{
faces.Add(planarFace);
}
}
}
}
GeometryInstance geometryInstance = item as GeometryInstance;
if (geometryInstance != null)
{
GeometryElement subGE = geometryInstance.GetInstanceGeometry();
GetBeamFaces(subGE);
}
}
}