Search code examples
c#revit-apirevit

How to place element on wall via revit api


I want to place some elements on a wall one after the other.

I am placing a few elements on a wall in my model. I was able to place the first element but have no clue how to place the 2nd and consecutive ones. I have uploaded the source code and image of what I have achieved and what I want to do next. The family is not the hosted one.

public static FamilyInstance PlaceFamily (Wall wall, Family family, Document document)
{

     FamilySymbol symbol = null ;

    foreach (ElementId s in family.GetFamilySymbolIds())
    {              
        symbol = document.GetElement(s) as FamilySymbol;                
        break;            
    }

    LocationCurve locationCurve= wall.Location as LocationCurve;

        XYZ point= locationCurve.Curve.GetEndPoint(0);
        Transaction transaction2 = new Transaction(document, "place Instance");
        transaction2.Start();
        if (!symbol.IsActive)
           symbol.Activate();
        FamilyInstance instance = document.Create.NewFamilyInstance(point, symbol, StructuralType.NonStructural);

        transaction2.Commit();
        return instance;
}

Here is the descriptive image


Solution

  • I found my solution. I just needed to find the next location point. Here is how I did it.

     XYZ p0 = locationCurveOfWall.Curve.GetEndPoint(0);
     XYZ p1 = locationCurveOfWall.Curve.GetEndPoint(1);
    
     XYZ vectorAlongTheWall = new XYZ(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
     XYZ normalizeVectorAlongTheWall = vectorAlongTheWall.Normalize();
    
     XYZ oldStartPoint = p0;
    
     int n2 = (int)Math.Round(wallLength / 4); // 4 is the width of the element
    
    Transaction transaction2 = new Transaction(document, "place Instance");
    transaction2.Start();
    
                    FamilyInstance instance2 = document.Create.NewFamilyInstance(oldStartPoint, symbol, wall, StructuralType.NonStructural);
                    for (int j = 1; j < n2 - 1; j++)
                    {
                        XYZ newStartPoint = new XYZ(oldStartPoint.X + 4 * normalizeVectorAlongTheWall.X, oldStartPoint.Y + 4 * normalizeVectorAlongTheWall.Y, oldStartPoint.Z + 4 * normalizeVectorAlongTheWall.Z);
                        instance2 = document.Create.NewFamilyInstance(newStartPoint, symbol, wall, StructuralType.NonStructural);
                        oldStartPoint = newStartPoint;
                    }
    
    transaction2.Commit();
    
    

    Please suggest a better way.