Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Dimension between two points

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
3970 Views, 2 Replies

Create Dimension between two points

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

}


}

}

 

2 REPLIES 2
Message 2 of 3
RPTHOMAS108
in reply to: Anonymous

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

        }
Message 3 of 3
brady0542
in reply to: RPTHOMAS108

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report