using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.ApplicationServices;
namespace 绘制位置线_点
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;//获取活动文档
Document doc = uidoc.Document;
//选择元素
Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "请选择族");
Element elem = doc.GetElement(refer);
//获取原点
LocationPoint lc = elem.Location as LocationPoint;
XYZ point = lc.Point;
//创建模型线
Transaction trans = new Transaction(doc);
//CreateModelLine(doc);
NewModelLineXYZ(doc, point);
return Result.Succeeded;
}
private void CreateModelLine(Document RevitDoc)
{
using (Transaction transaction = new Transaction(RevitDoc))
{
transaction.Start("Create Model Line");
Line geoLine = Line.CreateBound(XYZ.BasisY * 10, XYZ.BasisX * 10);
SketchPlane modelSketch = SketchPlane.Create(RevitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero));
ModelCurve modelLine = RevitDoc.Create.NewModelCurve(geoLine, modelSketch);
transaction.Commit();
}
}
private void NewModelLineXYZ(Document RevitDoc, XYZ point)
{
XYZ startpoint1 = new XYZ(point.X, point.Y, point.Z);
XYZ endpoint1 = new XYZ(point.X + 10, point.Y, point.Z);
XYZ startpoint2= new XYZ(point.X, point.Y, point.Z);
XYZ endpoint2 = new XYZ(point.X , point.Y+10, point.Z);
XYZ startpoint3 = new XYZ(point.X, point.Y, point.Z);
XYZ endpoint3= new XYZ(point.X , point.Y, point.Z+10);
using (Transaction transaction = new Transaction(RevitDoc))
{
transaction.Start("Create Model Line");
//创建sketchPlane
SketchPlane modelSketch1 = SketchPlane.Create(RevitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, point));
SketchPlane modelSketch2 = SketchPlane.Create(RevitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, point));
SketchPlane modelSketch3 = SketchPlane.Create(RevitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisX, point));
//创建线
Line Line1 = Line.CreateBound(startpoint1, endpoint1);
Line Line2 = Line.CreateBound(startpoint2, endpoint2);
Line Line3 = Line.CreateBound(startpoint3, endpoint3);
//创建模型线
RevitDoc.Create.NewModelCurve(Line1, modelSketch1);
RevitDoc.Create.NewModelCurve(Line2, modelSketch2);
RevitDoc.Create.NewModelCurve(Line3, modelSketch3);
transaction.Commit();
}
}
}
}