CopyElements Method (Document, ICollection(ElementId), Document, Transform, CopyPasteOptions)


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

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 24.0.0.0 (24.0.0.0)
Since: 2014

Syntax

C#
public static ICollection<ElementId> CopyElements(
	Document sourceDocument,
	ICollection<ElementId> elementsToCopy,
	Document destinationDocument,
	Transform transform,
	CopyPasteOptions options
)
Visual Basic
Public Shared Function CopyElements ( _
	sourceDocument As Document, _
	elementsToCopy As ICollection(Of ElementId), _
	destinationDocument As Document, _
	transform As Transform, _
	options As CopyPasteOptions _
) As ICollection(Of ElementId)
Visual C++
public:
static ICollection<ElementId^>^ CopyElements(
	Document^ sourceDocument, 
	ICollection<ElementId^>^ elementsToCopy, 
	Document^ destinationDocument, 
	Transform^ transform, 
	CopyPasteOptions^ options
)

Parameters

sourceDocument
Type: Autodesk.Revit.DB Document
The document that contains the elements to copy.
elementsToCopy
Type: System.Collections.Generic ICollection ElementId
The set of elements to copy.
destinationDocument
Type: Autodesk.Revit.DB Document
The destination document to paste the elements into.
transform
Type: Autodesk.Revit.DB Transform
The transform for the new elements. Can be a null reference ( Nothing in Visual Basic) if no transform is required.
options
Type: Autodesk.Revit.DB CopyPasteOptions
Optional settings. Can be a null reference ( Nothing in Visual Basic) if default settings should be used.

Return Value

The ids of the newly created copied elements.

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.

Examples

Copy 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());
}

Exceptions

Exception Condition
Autodesk.Revit.Exceptions 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.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was null
Autodesk.Revit.Exceptions InvalidOperationException It is not allowed to copy Sketch members between non-parallel sketches. -or- The elements cannot be copied.
Autodesk.Revit.Exceptions OperationCanceledException User cancelled the operation.

See Also