Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Create Dimension between two points

Anonymous

Create Dimension between two points

Anonymous
Not applicable

Hello! I want to create Dimension between selected points.

I attach my code.

When I do this code, I catch an exception Autodesk.Revit.Exceptions.InvalidOperationException.

How can I fix it??

#region Namespaces
using System;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

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);

}


}

}

 

0 Likes
Reply
Accepted solutions (1)
4,123 Views
2 Replies
Replies (2)

RPTHOMAS108
Mentor
Mentor
Accepted solution

You need at least two references to create a dimension i.e. two end points of line. Rather than using model line can use existing references.

 

In a 3D view picking points is not obvious unless the sketch plane is set to a face i.e. you set it to zero so you will not see what is being picked beneath all the other objects in the view.

 

 

       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.GetEndPointReference(0));
            ra.Append(modelcurve.GeometryCurve.GetEndPointReference(1));
            return doc.Create.NewDimension(doc.ActiveView, line, ra);

        }
0 Likes

brady0542
Contributor
Contributor

If you have more than two references in the list will the dimension create a string of dimensions to multiple points?

0 Likes