2021 Method
Changes 0
M

DisplacementElement.Create

Description:
Creates a new DisplacementElement as a child of the specified parent DisplacementElement.
public static DisplacementElement Create(
	Document document,
	ICollection<ElementId> elementsToDisplace,
	XYZ displacement,
	View ownerDBView,
	DisplacementElement parentDisplacementElement
)
  • document
    The Document
  • ICollection<ElementId>
    elementsToDisplace
    The elements to be displaced.
  • displacement
    The translation to be applied to the graphics of the displaced elements.
  • ownerDBView
    The 3D view which will own the DisplacementElement.
  • parentDisplacementElement
    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.
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;
}
  • #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.
  • A non-optional argument was NULL