BIM World
A Professional BIM Learning Platform


Revit Plugin Development: Automated Grid Alignment Made Easy

During project development, meshes are often flipped or selected incorrectly, resulting in unattractive meshes. To address this, a mesh alignment feature has been added. Let’s dive straight into the practical details.

First, some preparation is needed. Create a mesh filtering class, which will be used later. Here is the code:

class GridSelectionFilter : ISelectionFilter
{
    public bool AllowElement(Element element)
    {
        int idValue = element.Category.Id.IntegerValue;
        if (idValue == (int)BuiltInCategory.OST_Grids)
        {
            return true;
        }
        return false;
    }

    public bool AllowReference(Reference reference, XYZ position)
    {
        return false;
    }
}

Next, here is the core grid alignment implementation:

// Create a new grid selection filter
GridSelectionFilter gridSelectionFilter = new GridSelectionFilter();

// Select grids within a frame
IList<Element> selectedGrids = sel.PickElementsByRectangle(gridSelectionFilter, "Select grids");

// Pick a point to serve as the alignment reference
XYZ alignmentPoint = sel.PickPoint("Please select an alignment point");

// Start a transaction to modify grids
Transaction transaction = new Transaction(doc);
transaction.Start("Modify grids");

if (selectedGrids.Count > 1)
{
    foreach (Element elem in selectedGrids)
    {
        Grid grid = elem as Grid;
        Line line = grid.Curve as Line;
        XYZ start = line.GetEndPoint(0);
        XYZ end = line.GetEndPoint(1);

        // Determine which endpoint is closer to the selected alignment point
        double distanceToEnd = alignmentPoint.DistanceTo(end);
        double distanceToStart = alignmentPoint.DistanceTo(start);

        if (distanceToEnd < distanceToStart)
        {
            // End point is closer
            XYZ vectorToPoint = new XYZ(alignmentPoint.X - end.X, alignmentPoint.Y - end.Y, alignmentPoint.Z - end.Z);
            double distance = Math.Sqrt(vectorToPoint.X * vectorToPoint.X + vectorToPoint.Y * vectorToPoint.Y + vectorToPoint.Z * vectorToPoint.Z);

            // Calculate angle between grid direction and vector to alignment point
            double angle = line.Direction.AngleTo(vectorToPoint);

            // Calculate new end point coordinates
            XYZ newEnd = start + (line.Length + distance * Math.Cos(angle)) * line.Direction;

            // Create a new grid line with the updated endpoint
            Line newLine = Line.CreateBound(start, newEnd);
            Grid newGrid = Grid.Create(doc, newLine);
            string originalName = grid.Name;

            // Delete the old grid and assign the original name to the new one
            doc.Delete(grid.Id);
            newGrid.Name = originalName;
        }
        else
        {
            // Start point is closer
            XYZ vectorToPoint = new XYZ(alignmentPoint.X - start.X, alignmentPoint.Y - start.Y, alignmentPoint.Z - start.Z);
            double distance = Math.Sqrt(vectorToPoint.X * vectorToPoint.X + vectorToPoint.Y * vectorToPoint.Y + vectorToPoint.Z * vectorToPoint.Z);

            // Calculate angle between grid direction and vector to alignment point
            double angle = line.Direction.AngleTo(vectorToPoint);

            // Calculate new start point coordinates
            XYZ newStart = end - (line.Length - distance * Math.Cos(angle)) * line.Direction;

            // Create a new grid line with the updated start point
            Line newLine = Line.CreateBound(newStart, end);
            Grid newGrid = Grid.Create(doc, newLine);
            string originalName = grid.Name;

            // Delete the old grid and assign the original name to the new one
            doc.Delete(grid.Id);
            newGrid.Name = originalName;
        }
    }
}

transaction.Commit();

The code above provides a basic approach to grid alignment. If you find any errors or have suggestions for improvements, feel free to leave a comment!

Copyright belongs to the original author. Please include the website address when reposting.

xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit Plugin Development: Automated Grid Alignment Made Easy

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up