Getting Element Geometry and AnalyticalModel

Getting Element Geometry and AnalyticalModel

After new elements are created or elements are modified, regeneration and auto-joining of elements is required to propagate the changes throughout the model. Without a regeneration (and auto-join, when relevant), the Geometry property and the AnalyticalModel for Elements are either unobtainable (in the case of creating a new element) or they may be invalid. It is important to understand how and when regeneration occurs before accessing the Geometry or AnalyticalModel of an Element.

Although regeneration and auto-join are necessary to propagate changes made in the model, it can be time consuming. It is best if these events occur only as often as necessary.

Regeneration and auto-joining occur automatically when a transaction that modifies the model is committed successfully, or whenever the Document.Regenerate() or Document.AutoJoinElements() methods are called. Regenerate() and AutoJoinElements() may only be called inside an open transaction. It should be noted that the Regeneration() method can fail, in which case the RegenerationFailedException will be thrown. If this happens, the changes to the document need to be rolled back by rolling back the current transaction or subtransaction.

For more details about the AnalyticalModel object, refer to AnalyticalModel in Revit Structure. For more details about the Geometry property, refer to Geometry.

The following sample program demonstrates how a transaction populates these properties:

Code Region 23-3: Transaction populating Geometry and AnalyticalModel properties

public void TransactionDuringElementCreation(UIApplication uiApplication, Level level)
{
    Autodesk.Revit.DB.Document document = uiApplication.ActiveUIDocument.Document;

    // Build a location line for the wall creation
    XYZ start = new XYZ(0, 0, 0);
    XYZ end = new XYZ(10, 10, 0);
    Autodesk.Revit.DB.Line geomLine = Line.CreateBound(start, end);

    // All and any transaction should be enclosed in a 'using'
    // block or guarded within a try-catch-finally blocks
    // to guarantee that a transaction does not out-live its scope.
    using (Transaction wallTransaction = new Transaction(document, "Creating wall"))
    {
       // To create a wall, a transaction must be first started
       if (wallTransaction.Start() == TransactionStatus.Started)
       {
           // Create a wall using the location line
           Wall wall = Wall.Create(document, geomLine, level.Id, true);

           // the transaction must be committed before you can
           // get the value of Geometry and AnalyticalModel.

           if (wallTransaction.Commit() == TransactionStatus.Committed)
           {
               Autodesk.Revit.DB.Options options = uiApplication.Application.Create.NewGeometryOptions();
               Autodesk.Revit.DB.GeometryElement geoelem = wall.get_Geometry(options);
               Autodesk.Revit.DB.Structure.AnalyticalModel analyticalmodel = wall.GetAnalyticalModel();
           }
       }
    }
}

The transaction timeline for this sample is as follows:

Figure 134: Transaction timeline