Unable to transform Art duplicated from different document - C++ Plugin
Hello everyone,
My objective is to duplicate the existing artboard items and replace an art item with a different one from another .ai file using a C++ plugin. I am able to duplicate the artboard items and also duplicate another art item from a different .ai file, but I am not able to transform the newly duplicated art to the position of the original art.
Below is my code:
ASErr ReplaceFromExternalFile(const ai::FilePath& sourceFilePath, AIArtHandle oldArtHandle)
{
ASErr error = kNoErr;
//Store the current document handle
AIDocumentHandle destDoc;
sAIDocument->GetDocument(&destDoc);
//Open source document containing new logo
AIDocumentHandle sourceDoc;
error = sAIDocumentList->Open(sourceFilePath, kAIUnknownColorModel, kDialogOff, false, &sourceDoc);
if (error || !sourceDoc) return error;
//Get first art in source document
sAIDocumentList->Activate(sourceDoc, false);
//Find the art handle to be used in Destination document
AILayerHandle sourceRoot;
error = sAILayer->GetFirstLayer(&sourceRoot);
if (error || !sourceRoot) return error;
AIArtHandle newLogoArt = nullptr;
error = sAIArt->GetFirstArtOfLayer(sourceRoot, &newLogoArt);
if (error || !newLogoArt) return error;
while (newLogoArt != nullptr)
{
short artType;
error = sAIArt->GetArtType(newLogoArt, &artType);
if (error) return error;
if (artType != kUnknownArt)
{
break; // found usable art
}
error = sAIArt->GetArtSibling(newLogoArt, &newLogoArt);
if (error) return error;
}
if (!newLogoArt)
{
sAIDocumentList->Close(sourceDoc);
return kNotFoundErr;
}
//Acitvate the destination document
error = sAIDocumentList->Activate(destDoc, true);
if (error) return error;
//Get the layer handle and group
AILayerHandle layerDest;
AIArtHandle artDestGroup;
error = sAILayer->GetFirstLayer(&layerDest);
if (error || !layerDest) return error;
sAIArt->GetFirstArtOfLayer(sourceRoot, &artDestGroup);
// Duplicate the art in destination document
AIArtHandle pastedArt = nullptr;
error = sAIArt->DuplicateArt(newLogoArt, kPlaceAboveAll, artDestGroup, &pastedArt);
if (error) return error;
//Transform to the old logo position
AIReal dx = _newArtBoardRect.left;// -newBounds.left * scaleX;
AIReal dy = _newArtBoardRect.top;// -newBounds.bottom * scaleY;
transformArtToNewPosition(pastedArt, dx, dy);
//Added below line to check transform is working or not
AIReal dx = _newArtBoardRect.left;
AIReal dy = _newArtBoardRect.top;
transformArtToNewPosition(oldLogoArtHandle, dx, dy);
//Delete old logo ---
//error = sAIArt->DeleteArt(oldLogoArtHandle);
//if (error) return error;
//Clean up ---
sAIDocumentList->Close(sourceDoc); // close the source logo file
return kNoErr;
}Also added code for the transformArtToNewPosition and please find below:
ASErr transformArtToNewPosition(AIArtHandle art, AIReal tx, AIReal ty)
{
ASErr result = kNoErr;
AIRealRect artBounds;
AIRealPoint artCenter;
AIRealMatrix artMatrix;
AIReal lineScale;
ai::int32 transformFlags = kTransformObjects | kScaleLines | kTransformChildren;
short artType;
ASErr error = sAIArt->GetArtType(art, &artType);
if (error == kGroupArt)
{
return error;
}
lineScale = (sAIRealMath->AIRealSqrt(1)) * (sAIRealMath->AIRealSqrt(1));
result = sAIArt->GetArtBounds(art, &artBounds);
artCenter.h = artBounds.left + (artBounds.right - artBounds.left) / 2;
artCenter.v = artBounds.bottom + (artBounds.top - artBounds.bottom) / 2;
// move object so that the centerpoint is at the origin
sAIRealMath->AIRealMatrixSetIdentity(&artMatrix);
sAIRealMath->AIRealMatrixSetTranslate(&artMatrix, -artCenter.h, -artCenter.v);
sAIRealMath->AIRealMatrixConcatTranslate(&artMatrix, tx, ty);
sAIRealMath->AIRealMatrixConcatRotate(&artMatrix, 0);
sAIRealMath->AIRealMatrixConcatScale(&artMatrix, 1, 1);
sAIRealMath->AIRealMatrixConcatTranslate(&artMatrix, artCenter.h, artCenter.v);
result = sAITransformArt->TransformArt(art, &artMatrix, lineScale, transformFlags);
return result;
} With the above transformArtToNewPosition code, I can transform the oldLogoArtHandle, but I cannot change the position of the newly duplicated art. Please check the attached image for the current output of my plugin.
Could anyone shed some light on this or provide any input to help achieve my requirement? Thanks in advance!
