How to set Parameter values using IUpdater when an element is being added

How to set Parameter values using IUpdater when an element is being added

sensiblehira
Advocate Advocate
213 Views
1 Reply
Message 1 of 2

How to set Parameter values using IUpdater when an element is being added

sensiblehira
Advocate
Advocate

Hello,

I am trying to use my updater to set parameter values of my element. To identify the value that needs to be assigned to the element's parameters the element must be placed first. The trigger for this updater must be called by two change types: 1- Element Addition and 2-  Geometry Change.

My updater is working fine with geometry change but is not assigning the values when the element is being added.

 

*Please can anyone clarify, do I need to create a new Transaction in the execute method of updaters, (currently two of the updaters are working fine without the transaction but i am confused)

following is the code for the execute method of the updater.

public void Execute(UpdaterData data)
{
    Document doc = data.GetDocument();

    foreach (ElementId id in data.GetModifiedElementIds())
    {
        Element element = doc.GetElement(id);

        if (element is FamilyInstance familyInstance)
        {
            //check if parameters for location and rotation exist

            if (familyInstance.LookupParameter("X_Coordinate") != null)  //check for X_Coordinate Parameter
            {
                Parameter x_param = familyInstance.GetParameters("X_Coordinate")[0];
                x_param.Set((familyInstance.Location as LocationPoint).Point.X);
            }
            if (familyInstance.LookupParameter("Z_Coordinate") != null) //check for Z_Coordinate Parameter
            {
                Parameter y_param = familyInstance.GetParameters("Z_Coordinate")[0];
                y_param.Set((familyInstance.Location as LocationPoint).Point.Z);
            }
            if (familyInstance.LookupParameter("Y_Coordinate") !=  null)  //check for Y_Coordinate Parameter
            {
                Parameter z_param = familyInstance.GetParameters("Y_Coordinate")[0];
                z_param.Set((familyInstance.Location as LocationPoint).Point.Y);
            }
            if (familyInstance.LookupParameter("Rotation") != null)  //check for Rotation Parameter
            {
                Parameter rotation = familyInstance.GetParameters("Rotation")[0];
                rotation.Set(0);
            }
        }
    }
}

 

0 Likes
Accepted solutions (1)
214 Views
1 Reply
Reply (1)
Message 2 of 2

ricaun
Advisor
Advisor
Accepted solution

The UpdaterData have the method GetAddedElementIds, you are using the only GetModifiedElementIds.

 

You could do something like this:

public void Execute(UpdaterData data)
{
    var document = data.GetDocument();
    var ids = new List<ElementId>();
    ids.AddRange(data.GetAddedElementIds());
    ids.AddRange(data.GetModifiedElementIds());
    foreach(var id in ids)
    {
        var element = document.GetElement(id);
        //...
    }
}

 

No, you don't need to create a transaction, basically the IUpdater is injected inside a transaction that trigger the updater. In the moment the Transaction execute the Commit method.

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes