namespace PickObject
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
using (Transaction tx = new Transaction(doc))
{
tx.Start("Start");
PickPoint(uidoc, app);
tx.Commit();
}
return Result.Succeeded;
}
public void PickPoint(UIDocument uidoc, Application app) {
View activeView = uidoc.ActiveView;
SketchPlane sketch = activeView.SketchPlane;
ObjectSnapTypes snapTypes = ObjectSnapTypes.Endpoints | ObjectSnapTypes.Intersections | ObjectSnapTypes.Points | ObjectSnapTypes.Perpendicular;
XYZ startPoint;
XYZ endPoint;
Plane geometryPlane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero);
sketch = SketchPlane.Create(uidoc.Document, geometryPlane);
uidoc.Document.ActiveView.SketchPlane = sketch;
uidoc.Document.ActiveView.ShowActiveWorkPlane();
try
{
startPoint = uidoc.Selection.PickPoint(snapTypes, "Select start point");
endPoint = uidoc.Selection.PickPoint(snapTypes, "Select end point");
}
catch(Autodesk.Revit.Exceptions.OperationCanceledException oc)
{
Console.WriteLine(oc.Message);
return;
}
catch(Autodesk.Revit.Exceptions.InvalidOperationException oe)
{
Console.WriteLine(oe.Message);
TaskDialog.Show("Revit", "No work plane set in current view.");
return;
}
catch (Autodesk.Revit.Exceptions.ArgumentNullException n)
{
Console.WriteLine(n.Message);
return;
} //Выбор точек
double dist = startPoint.DistanceTo(endPoint);
string distance = "Distance is " + dist.ToString();
string strCoords = "Selected start point is " + startPoint.ToString() + "\nSelected end point is " + endPoint.ToString() + distance;
Line line = Line.CreateBound(startPoint, endPoint);
CreateLinearDimension(uidoc.Document, startPoint, endPoint, sketch, app);
TaskDialog.Show("Revit", strCoords);
}
public Dimension CreateLinearDimension(
Document doc, XYZ pt1, XYZ pt2, SketchPlane sketch, Application app)
{
// first create line
Line line = Line.CreateBound(pt1, pt2);
ModelCurve modelcurve = doc.Create
.NewModelCurve(line, sketch);
ReferenceArray ra = new ReferenceArray();
ra.Append(modelcurve.GeometryCurve.Reference);
return doc.Create.NewDimension(doc.ActiveView, line, ra);
}
}
}