Create Method


Displacement Element Create Method

Creates a new DisplacementElement as a child of the specified parent DisplacementElement.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntax
public static DisplacementElement Create(
	Document document,
	ICollection<ElementId> elementsToDisplace,
	XYZ displacement,
	View ownerDBView,
	DisplacementElement parentDisplacementElement
)

Parameters

document Document
The Document
elementsToDisplace ICollection ElementId
The elements to be displaced.
displacement XYZ
The translation to be applied to the graphics of the displaced elements.
ownerDBView View
The 3D view which will own the DisplacementElement.
parentDisplacementElement DisplacementElement
An existing DisplacementElement that will be the parent of the one being created. It must be owned by ownerDBView. The relative transform of new DisplacementElement will be concatenated with the absolute transform of the parent DisplacementElement. If the elements specified by displacedElemIds are already displaced by another DisplacementElement, then this must be that element.

Return Value

DisplacementElement
The id of the new DisplacementElement.
Exceptions
Exception Condition
ArgumentException #elementIds# contains no element ids. -or- ownerDBView is not a 3D view. -or- For each individual element in the set elementsToDisplace, isAllowedAsDisplacedElement must return true, and the elements must either not already be displaced in the specified view, or else they must all be displaced by the same displacement element in the view. -or- The DisplacementElement parentDisplacementElement in not owned by the view ownerDBView.
ArgumentNullException A non-optional argument was null
Example
public static void CreateDisplacementAndPath(Document doc, View view)
{
    // Find roof
    FilteredElementCollector fec = new FilteredElementCollector(doc);
    fec.OfClass(typeof(RoofBase));
    RoofBase roof = fec.FirstElement() as RoofBase;

    // Get a geometric reference for the path
    Reference edgeRef = GetHorizontalEdgeReference(roof);

    using (Transaction t = new Transaction(doc, "CreateDisplacementAndPath"))
    {
        t.Start();
        // Create a new top level DisplacementElement
        DisplacementElement dispElem = DisplacementElement.Create(doc, new ElementId[] { roof.Id }, new XYZ(10, 0, 20), view, null);

        // Create the path associated to the element
        DisplacementPath.Create(doc, dispElem, edgeRef, 0.5);
        t.Commit();
    }
}

private static Reference GetHorizontalEdgeReference(Element elem)
{
    //Find target edge from lower face of roof
    Options options = new Options();
    options.ComputeReferences = true;

    GeometryElement geomElem = elem.get_Geometry(options);

    foreach (var geomObj in geomElem)
    {
        if (geomObj is Solid)
        {
            Solid solid = geomObj as Solid;
            var faces = solid.Faces;

            foreach (Face face in faces)
            {
                BoundingBoxUV box = face.GetBoundingBox();
                UV midpoint = (box.Min + box.Max) / 2.0;
                if (face.ComputeNormal(midpoint).Normalize().Z < -0.1) // Downward facing, this is good enough
                {
                    var edgeLoops = face.EdgeLoops;
                    foreach (EdgeArray edgeArray in edgeLoops)
                    {
                        foreach (Edge edge in edgeArray)
                        {
                            // horizontal?
                            if (Math.Abs(edge.AsCurve().ComputeDerivatives(0.0, true).BasisX.DotProduct(XYZ.BasisZ)) - 1 <= 0.00001)
                            {
                                return edge.Reference;
                            }
                        }
                    }
                }
            }
        }
    }

    return null;
}
See Also