[CONNECT C++] Copy context resizes the elements x10

Hello,

I'm trying to use the ElementCopyContext class in order to copy each element from an attached DWG file. Later I'd like to import only some of the elements.

The problem is the final result, it is ten time bigger than the attached file. I tried to use SetTransformToDestination(true) in order to fix this, but it's not better.

Any idea? Thanks by advance!

for (PersistentElementRefP const& elemRef : modelP->GetElementsCollection())
{
  DgnPlatform::EditElementHandle* elmhandle = new DgnPlatform::EditElementHandle(elemRef->GetElementId(), modelP);
  DgnPlatform::ElementCopyContext copyContext(ACTIVEMODEL);
  copyContext.SetSourceModelRef(elmhandle->GetModelRef());
  copyContext.SetTransformToDestination(true);
  copyContext.SetWriteElements(true);
  copyContext.DoCopy(*elmhandle);
}

Parents
  • Does your variable modelP point to the active DGN model or to the DWG attachment?

    DgnPlatform::EditElementHandle* elmhandle = new DgnPlatform::EditElementHandle(elemRef->GetElementId(), modelP);

    You've created a memory leak with new EditElementHandle.  Just declare and construct an EditElementHandle on the stack...

    DgnPlatform::EditElementHandle elmhandle  (elemRef->GetElementId(), modelP);

    An ElementHandle (and its sub-class EditElementHandle) is a smart pointer to an element descriptor.  It's destructor handles memory deallocation for you. 

    You're doing unnecessary work by reallocating the ElementCopyContext on each pass through your for loop...

    DgnPlatform::ElementCopyContext copyContext(ACTIVEMODEL);
    copyContext.SetSourceModelRef(modelP);
    copyContext.SetTransformToDestination(true);
    copyContext.SetWriteElements(true);
    for (PersistentElementRefP const& elemRef : modelP->GetElementsCollection())
    {
      DgnPlatform::EditElementHandle elmhandle (elemRef->GetElementId(), modelP);
      copyContext.DoCopy (elmhandle);
    }
    

     
    Regards, Jon Summers
    LA Solutions

  • Does your variable modelP point to the active DGN model or to the DWG attachment?

    You were on the right track with this Jon.

    They aren't cloning using the attachment modelRef as the source and are instead supplying the DgnModel the attachment refers to, so there's no reference transform.

    A DgnAttachment is a DgnModelRef, and that's what they should read the element from and supply as the source modelRef for the copy context.

    struct   DgnAttachment : DgnModelRef

    HTH

    -B

    NOTE: Calling ApplyTransform is not recommended in the general case, when cloning from 2d to 3d, you can't apply a 3d transform to a 2d element, copy context will handle this case correctly.



Reply
  • Does your variable modelP point to the active DGN model or to the DWG attachment?

    You were on the right track with this Jon.

    They aren't cloning using the attachment modelRef as the source and are instead supplying the DgnModel the attachment refers to, so there's no reference transform.

    A DgnAttachment is a DgnModelRef, and that's what they should read the element from and supply as the source modelRef for the copy context.

    struct   DgnAttachment : DgnModelRef

    HTH

    -B

    NOTE: Calling ApplyTransform is not recommended in the general case, when cloning from 2d to 3d, you can't apply a 3d transform to a 2d element, copy context will handle this case correctly.



Children
No Data