用户选择任意元素,生成这个元素的包围盒实体。就这么简单。被curveLoop不封闭整到人傻掉
namespace SizeAdjustment
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class WindowDoorOpening : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
ref string message, ElementSet elements)
{
UIApplication uIApp = commandData.Application;
Application application = uIApp.Application;
UIDocument uIDoc = uIApp.ActiveUIDocument;
Document document = uIDoc.Document;
Selection selection = uIDoc.Selection;
View view = uIDoc.ActiveView;
//【】过滤墙体 用户选择
IList<Element> faces = uIDoc.Selection.PickElementsByRectangle();
Transaction trans = new Transaction(document, "LS");
trans.Start();
var solid = solidBoundingBox(view, faces.First());
var ds7 = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel));
ds7.GetGeneratingElementIds(solid);
ds7.SetShape(new List<GeometryObject>() { solid });
trans.Commit();
return Result.Succeeded;
}
public static Solid solidBoundingBox(View view,Element inputSolid)
{
BoundingBoxXYZ bbox = inputSolid.get_BoundingBox(view);
// corners in BBox coords
XYZ pt0 = new XYZ(bbox.Min.X, bbox.Min.Y, bbox.Min.Z);
XYZ pt1 = new XYZ(bbox.Max.X, bbox.Min.Y, bbox.Min.Z);
XYZ pt2 = new XYZ(bbox.Max.X, bbox.Max.Y, bbox.Min.Z);
XYZ pt3 = new XYZ(bbox.Min.X, bbox.Max.Y, bbox.Min.Z);
//edges in BBox coords
Line edge0 = Line.CreateBound(pt0, pt1);
Line edge1 = Line.CreateBound(pt1, pt2);
Line edge2 = Line.CreateBound(pt2, pt3);
Line edge3 = Line.CreateBound(pt3, pt0);
//create loop, still in BBox coords
List<Curve> edges = new List<Curve>();
edges.Add(edge0);
edges.Add(edge1);
edges.Add(edge2);
edges.Add(edge3);
Double height = bbox.Max.Z - bbox.Min.Z;
CurveLoop baseLoop = CurveLoop.Create(edges);
List<CurveLoop> loopList = new List<CurveLoop>();
loopList.Add(baseLoop);
Solid preTransformBox = GeometryCreationUtilities.CreateExtrusionGeometry(loopList, XYZ.BasisZ, height);
Solid transformBox = SolidUtils.CreateTransformed(preTransformBox, bbox.Transform);
return transformBox;
}
}
}