ElementCopying Code Samples

Common case

This example shows how easy it is to copy an element in the common case where you want styles to be copied and the element to be written to the destination model.

void simpleElementCopy (DgnModelP destinationModel, ElementRefP ref, DgnModelRefP sourceModel)
{
Bentley::DgnPlatform::ElementCopyContext copier (destinationModel);
Bentley::DgnPlatform::EditElementHandle copied (ref, sourceModel);
BeAssert (copied.GetElementRef() == ref); // DoCopy starts with the source element.
if (copier.DoCopy (copied) != SUCCESS)
return; // remapping or add failed
BeAssert (copied.GetElementRef() != NULL); // DoCopy transformed the source element into a new element in the destination
BeAssert (copied.GetElementRef() != ref); // DoCopy did not affect the source element.
}

How to copy multiple elements.

In this example, we are given a list of elements to copy. These elements could come from several different sources, and some of these sources could be accessed by reference attachments. ElementCopyContext::DoCopy takes care of transforming each source element from its source model to the destination model. It also takes care of copying the supporting levels, colors, and styles.
void copyMultipleElements (DgnModelP destinationModel, Bentley::DgnPlatform::ElementAgenda const& sourceElements)
{
Bentley::DgnPlatform::ElementCopyContext copier (destinationModel);
for each (Bentley::DgnPlatform::ElementHandle const& sourceElem in sourceElements)
{
Bentley::DgnPlatform::EditElementHandle copied (sourceElem, true);
if (copier.DoCopy (copied) != SUCCESS)
... remapping or add failed
}
Bentley::DgnPlatform::DependencyManager::ProcessAffected (); // trigger dependency pointer remapping
}

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.