Share

ElementTransformUtils.CopyElements(Document, ICollection<ElementId>, Document, Transform, CopyPasteOptions) Method

Copies a set of elements from source document to destination document.


Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 26.4.0.0 (26.4.0.0)

Syntax

C#

public static ICollection<ElementId> CopyElements(
	Document sourceDocument,
	ICollection<ElementId> elementsToCopy,
	Document destinationDocument,
	Transform transform,
	CopyPasteOptions options
)

Parameters

sourceDocument  Document
The document that contains the elements to copy.
elementsToCopy  ICollection<ElementId>
The set of elements to copy.
destinationDocument  Document
The destination document to paste the elements into.
transform  Transform
The transform for the new elements. Can be null if no transform is required.
options  CopyPasteOptions
Optional settings. Can be null if default settings should be used.

Return Value

ICollection<ElementId>
The ids of the newly created copied elements.

Exceptions

ExceptionCondition
ArgumentException The given element id set is empty. -or- One or more elements in elementsToCopy do not exist in the document. -or- Some of the elements cannot be copied, because they are view-specific. -or- The input set of elements contains Sketch members along with other elements or there is no active Sketch edit mode.
ArgumentNullException A non-optional argument was null
InvalidOperationException It is not allowed to copy Sketch members between non-parallel sketches. -or- The elements cannot be copied.
OperationCanceledException User cancelled the operation.

Remarks

Copies are placed at their respective original locations or locations specified by the optional transformation.

This method can be used for copying non-view specific elements only. For copying view-specific elements, use the view-specific form of the CopyElements method.

The destination document can be the same as the source document.

This method performs rehosting of elements where applicable.

Example

C#

// Copy wall element.
public void CopyWall(Autodesk.Revit.DB.Document sourceDocument, Autodesk.Revit.DB.Document destinationDocument, Autodesk.Revit.DB.Wall wall)
{
   Transform transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
   CopyPasteOptions options = new CopyPasteOptions();

   ElementTransformUtils.CopyElements(sourceDocument, new List<ElementId> { wall.Id }, destinationDocument, transform, options);
}

// The method copies existing Sketch curves inside of the Floor sketch.
public void CopySketchMembersInsideOfSketch(Autodesk.Revit.DB.Document document, Autodesk.Revit.DB.Floor floor)
{
   var floorSketch = document.GetElement(floor.SketchId) as Sketch;

   // Start Sketch edit mode to copy Sketch members inside of the Sketch
   SketchEditScope sketchScope = new SketchEditScope(document, "Edit floor sketch");
   sketchScope.Start(floor.SketchId);

   // Start transaction to be able to modify the document
   using (Transaction tr = new Transaction(document, "Copy sketch members"))
   {
      tr.Start();

      var transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
      var copiedIds = ElementTransformUtils.CopyElements(
         document, floorSketch.GetAllElements(), document, transform, new CopyPasteOptions());

      tr.Commit();
   }

   sketchScope.Commit(new CustomFailuresPreprocessor());
}

// The method copies Sketch members from the Floor to the Ceiling Sketch.
public void CopySketchMembersBetweenSketch(Autodesk.Revit.DB.Floor floor, Autodesk.Revit.DB.Ceiling ceiling)
{
   var ceilingSketch = ceiling.Document.GetElement(ceiling.SketchId) as Sketch;
   var floorSketch = floor.Document.GetElement(floor.SketchId) as Sketch;

   // Sketch members can be copied only between parallel Sketches.
   if (!MathComparisonUtils.IsAlmostEqual(
      Math.Abs(floorSketch.SketchPlane.GetPlane().Normal.DotProduct(ceilingSketch.SketchPlane.GetPlane().Normal)), 1.0))
   {
      return;
   }

   // Start Sketch edit mode, for the ceiling sketch, to copy and paste floor Sketch members to the ceiling
   SketchEditScope sketchScope = new SketchEditScope(ceiling.Document, "Edit ceiling sketch");
   sketchScope.Start(ceiling.SketchId);

   // Start transaction to be able to modify the document
   using (Transaction tr = new Transaction(ceiling.Document, "Copy sketch members"))
   {
      tr.Start();

      var transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
      var copiedIds = ElementTransformUtils.CopyElements(
         floor.Document, floorSketch.GetAllElements(), ceiling.Document, transform, new CopyPasteOptions());

      tr.Commit();
   }

   sketchScope.Commit(new CustomFailuresPreprocessor());
}

// The method copies ModelCurves from the document to the Sketch.
public void CopyModelCurvesFromDocumentToSketch(Autodesk.Revit.DB.Sketch sketch, List<ElementId> modelCurveIds)
{
   // Start Sketch edit mode to insert ModelCurves into the Floor Sketch.
   SketchEditScope sketchScope = new SketchEditScope(sketch.Document, "Edit floor sketch");
   sketchScope.Start(sketch.Id);

   // Start transaction to be able to modify the document
   using (Transaction tr = new Transaction(sketch.Document, "Copy sketch members"))
   {
      tr.Start();

      var transform = Transform.CreateTranslation(new XYZ(150, 150, 0));
      var copiedIds = ElementTransformUtils.CopyElements(
         sketch.Document, modelCurveIds, sketch.Document, transform, new CopyPasteOptions());

      tr.Commit();
   }

   // Sketch curves should not have open loops or intersections to be successfully committed.
   sketchScope.Commit(new CustomFailuresPreprocessor());
}

VB

'Copy wall element.
Public Sub CopyWall(ByVal sourceDocument As Autodesk.Revit.DB.Document, ByVal destinationDocument As Autodesk.Revit.DB.Document, ByVal wall As Autodesk.Revit.DB.Wall)
    Dim transform As Transform = Transform.CreateTranslation(New XYZ(150, 150, 0))
    Dim options As CopyPasteOptions = New CopyPasteOptions()
    ElementTransformUtils.CopyElements(sourceDocument, New List(Of ElementId) From {
        wall.Id
    }, destinationDocument, transform, options)
End Sub

'The method copies existing Sketch curves inside of the Floor sketch.
Public Sub CopySketchMembersInsideOfSketch(ByVal document As Autodesk.Revit.DB.Document, ByVal floor As Autodesk.Revit.DB.Floor)
    Dim floorSketch = TryCast(document.GetElement(floor.SketchId), Sketch)

    'Start Sketch edit mode to copy Sketch members inside of the Sketch
    Dim sketchScope As SketchEditScope = New SketchEditScope(document, "Edit floor sketch")
    sketchScope.Start(floor.SketchId)

    'Start Sketch edit mode to copy Sketch members inside of the Sketch
    Using tr As Transaction = New Transaction(document, "Copy sketch members")
        tr.Start()
        Dim transform As Transform = Transform.CreateTranslation(New XYZ(150, 150, 0))
        Dim copiedIds = ElementTransformUtils.CopyElements(document, floorSketch.GetAllElements(), document, transform, New CopyPasteOptions())
        tr.Commit()
    End Using

    sketchScope.Commit(New CustomFailuresPreprocessor())
End Sub

'The method copies Sketch members from the Floor to the Ceiling Sketch.
Public Sub CopySketchMembersBetweenSketch(ByVal floor As Autodesk.Revit.DB.Floor, ByVal ceiling As Autodesk.Revit.DB.Ceiling)
    Dim ceilingSketch = TryCast(ceiling.Document.GetElement(ceiling.SketchId), Sketch)
    Dim floorSketch = TryCast(floor.Document.GetElement(floor.SketchId), Sketch)

    'Sketch members can be copied only between parallel Sketches.
    If Not MathComparisonUtils.IsAlmostEqual(Math.Abs(floorSketch.SketchPlane.GetPlane().Normal.DotProduct(ceilingSketch.SketchPlane.GetPlane().Normal)), 1.0) Then
        Return
    End If

    'Start Sketch edit mode, for the ceiling sketch, to copy and paste floor Sketch members to the ceiling
    Dim sketchScope As SketchEditScope = New SketchEditScope(ceiling.Document, "Edit ceiling sketch")
    sketchScope.Start(ceiling.SketchId)

    'Start transaction to be able to modify the document
    Using tr As Transaction = New Transaction(ceiling.Document, "Copy sketch members")
        tr.Start()
        Dim transform As Transform = Transform.CreateTranslation(New XYZ(150, 150, 0))
        Dim copiedIds = ElementTransformUtils.CopyElements(floor.Document, floorSketch.GetAllElements(), ceiling.Document, transform, New CopyPasteOptions())
        tr.Commit()
    End Using

    sketchScope.Commit(New CustomFailuresPreprocessor())
End Sub

'The method copies ModelCurves from the document to the Sketch.
Public Sub CopyModelCurvesFromDocumentToSketch(ByVal sketch As Autodesk.Revit.DB.Sketch, ByVal modelCurveIds As List(Of ElementId))
    'Start Sketch edit mode to insert ModelCurves into the Floor Sketch.
    Dim sketchScope As SketchEditScope = New SketchEditScope(sketch.Document, "Edit floor sketch")
    sketchScope.Start(sketch.Id)

    'Start transaction to be able to modify the document
    Using tr As Transaction = New Transaction(sketch.Document, "Copy sketch members")
        tr.Start()
        Dim transform As Transform = Transform.CreateTranslation(New XYZ(150, 150, 0))
        Dim copiedIds = ElementTransformUtils.CopyElements(sketch.Document, modelCurveIds, sketch.Document, transform, New CopyPasteOptions())
        tr.Commit()
    End Using

    'Sketch curves should not have open loops or intersections to be successfully committed.
    sketchScope.Commit(New CustomFailuresPreprocessor())
End Sub

See Also

Reference

Was this information helpful?