How to Create Grid-to-Grid Dimension from Center of Grids in Revit API

How to Create Grid-to-Grid Dimension from Center of Grids in Revit API

emaguilera
Contributor Contributor
377 Views
1 Reply
Message 1 of 2

How to Create Grid-to-Grid Dimension from Center of Grids in Revit API

emaguilera
Contributor
Contributor

Hello everyone,

I’m working on a Revit add-in to create dimensions between selected grids in the active view. I want each dimension line to be created from the center of each grid rather than the default endpoint.

Here's the code I currently have:

 

public class CreateGridToGridDimension : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIDocument uidoc = commandData.Application.ActiveUIDocument;
        Document doc = uidoc.Document;
        View view = doc.ActiveView;

        ReferenceArray gridReferences = new ReferenceArray();
        IList<Reference> selectedGrids = null;

        try
        {
            selectedGrids = uidoc.Selection.PickObjects(ObjectType.Element, new GridSelectionFilter(), "Seleccione grillas (presione ESC para terminar)");

            if (selectedGrids.Count < 2)
            {
                message = "Debe seleccionar al menos dos grillas.";
                return Result.Failed;
            }

            foreach (Reference gridReference in selectedGrids)
            {
                gridReferences.Append(gridReference);
            }

            using (Transaction t = new Transaction(doc, "Create Perpendicular Grid Dimensions"))
            {
                t.Start();

                for (int i = 0; i < selectedGrids.Count - 1; i++)
                {
                    // Obtener las grillas seleccionadas
                    Grid grid1 = doc.GetElement(selectedGrids[i]) as Grid;
                    Grid grid2 = doc.GetElement(selectedGrids[i + 1]) as Grid;

                    if (grid1 == null || grid2 == null)
                    {
                        message = "No se pudieron obtener las grillas seleccionadas.";
                        t.RollBack();
                        return Result.Failed;
                    }


                    Line dimensionLine = Line.CreateBound(grid1.Curve.GetEndPoint(0), grid2.Curve.GetEndPoint(0));

                    ReferenceArray dimensionReferences = new ReferenceArray();
                    dimensionReferences.Append(selectedGrids[i]);
                    dimensionReferences.Append(selectedGrids[i + 1]);

                    Dimension dimension = doc.Create.NewDimension(view, dimensionLine, dimensionReferences);
                }

                t.Commit();
            }
        }
        catch (Autodesk.Revit.Exceptions.OperationCanceledException)
        {
            return Result.Cancelled; 
        }
        catch (System.Exception ex)
        {
            message = ex.Message; 
            return Result.Failed;
        }

        return Result.Succeeded;
    }
}

 

0 Likes
378 Views
1 Reply
  • c
Reply (1)
Message 2 of 2

ctm_mka
Collaborator
Collaborator

instead of using the endpoints in your code,

 Line dimensionLine = Line.CreateBound(grid1.Curve.GetEndPoint(0), grid2.Curve.GetEndPoint(0));

you can get the midpoint of each curve like this:

Curve.Evaluate(0.5, true)

 

0 Likes