Skip to main content
Inspiring
June 3, 2022
Question

Copying to and centering artwork on artboard using SDK

  • June 3, 2022
  • 2 replies
  • 558 views

Can someone please tell me the best way to copy and center artwork on the artboard using the SDK? I can copy the art just fine and tried everything that I can think of but cannot get the artwork to center. I'm currently using a translation matrix for x, y with no luck. I have the code written already through script which works fine (not with any matrix, just plain old math to calculate x, y), but decided to rewrite the code as a C++ plugin. I'm going to need to scale the artwork a bit as well, but struggling with positioning at the moment. So, is this the best way (transformation/translation matrix) to do what I need or someone has another way?

2 replies

Rick E Johnson
Inspiring
June 4, 2022

Hi Marvin,

 

I believe you use a combination of CORE and SDK, so this CORE code may be useful. To move most basic art objects, I simply use:

tempArt->move(p1, p2); // moves the art the distance and direction of one point to another

 

Pattern and gradient fills don't move with the art for some reason, so I added this, which also uses a transfornation matrix:

 

if (tempArt->artType() == hdi::core::ArtTypePath){
hdi::core::PathStyle pStyl = tempArt->path()->style();
if (pStyl.useFill){
if (pStyl.fill.color.colorType() == hdi::core::ArtColor::TypePattern ||
pStyl.fill.color.colorType() == hdi::core::ArtColor::TypeGradient){
hdi::core::TransformMatrix tmx;
tmx.concatTranslate(p1.x - p2.x, p1.y - p2.y);
(tempArt)->transformWithOptions(tmx, (hdi::core::Art::TransformOptionFillPatterns |
hdi::core::Art::TransformOptionFillGradients));
}
}
}

 

 I hope this helps.

Inspiring
June 5, 2022

Hi Rick,

 

Thanks as always for the help. Yes, you are correct. I am using both CORE and the SDK. I actually started out with a version that uses CORE art object's move method but that was giving me trouble as well so I then tried going through the SDK. Here is my code using move:

 

void copyArtwork1(hdi::core::Art &ptrArt, AIRealRect &abBounds)
{
	AIRealRect abProps;

	double x, y;
	hdi::core::TransformMatrix artMatrix;
	std::auto_ptr<hdi::core::Art> newArt;
	hdi::core::ArtboardPoint p1, p2;

	newArt = ptrArt.duplicate(hdi::core::PaintOrder::PlaceBelow, NULL);

	// Define the transformation matrix for the new art object
	//artMatrix = hdi::core::TransformMatrix();
	//artMatrix.setIdentity();

	// Resize artwork proportionally and center it to artboard
	abProps = getArtboardBounds(abBounds);
	DoubleXY boundsDiff = itemBoundsDiff(newArt);
	DoubleXY artScaleWH = fitArtworkToArtboard(newArt, abProps, boundsDiff); // scale object to fit artboard

	//double artScaleW = artScaleWH.x;
	//double artScaleH = artScaleWH.y;

	//artMatrix.setScale(artScaleW, artScaleH);

	// Get Center position of artwork to artboard
	DoubleXY positionXY = centerArtworkOnArtboard(newArt, abBounds);

	//x = destBounds.left;
	//y = destBounds.top;

	//x = left * .5 + (left / 2);
	//y = top * .5 + (top / 2);
	x = positionXY.x;
	y = positionXY.y;
	//x = 0;
	//y = 0;

	p1.x = 122.5;// x;
	p1.y = -122.5;// y;
	p2.x = 0.0;// x;
	p2.y = -0.0;// y;

	newArt->move(p1, p2);

	//artMatrix.concatTranslate(x, y);
	//artMatrix.concatTranslate(x, y);
	//artMatrix.concatTranslate(std::abs(destBounds.left * .5 + (destBounds.left / 2)), std::abs(destBounds.top * .5 + (destBounds.top / 2)));
	//artMatrix.setTranslate(std::abs(destBounds.left * .5 + (destBounds.left / 2)), std::abs(destBounds.top * .5 + (destBounds.top / 2)));

	//newArt->transform(artMatrix, true);

}

 With this code, I've adapted my routines from my working JS version. However, I have hardcoded values as you can see just to try and get something to happen. Basically, after duplicate, the new art is sitting at it's original coordinates and that's understandable. But the newArt->move() has no effect on changing it's position.

Inspiring
August 6, 2025

Hi Kazon23752888nou4, did you find any workaround for this issue?

I’m trying to load a path/logo from a different .ai file and place it at the center of my currently open document using a C++ plugin. I’m able to duplicate the art and bring it into the current document, but I’m unable to position it using sAITransformArt->TransformArt. Please refer to the attached image .

Any help or workaround would be greatly appreciated. Thanks in advance!

Inspiring
June 4, 2022

Adding a bit more information. As indicated by the images, it doesn't matter what values I use for tx and ty in my matrix definition, x and y of the transformed bounds are always the same. Further, the x and y values shown in illustrator are totally different. I believe from reading other posts that it has something to do with coordinate system. I tried variations but can't get that right. So can anyone please shed some light on this for me? What can I do to align the artwork centered on the artboard?

 

Some of my code:

// Copy artwork to artboard
error = sAIArt->DuplicateArt(srcArt, kPlaceAboveAll, NULL, &destArt);

// Left and Top of artboard
tx = destBounds.left;
ty = destBounds.top;
//tx = 122.5;
//ty = 122.5;

AIRealPoint point;
point.h = tx;
point.v = ty;

error = sAIHardSoft->ConvertCoordinates(point, AICoordinateSystem::kAIDocumentCoordinateSystem, AICoordinateSystem::kAIArtboardCoordinateSystem, true);

tx = point.h;
ty = point.v;

//sAIRealMath->AIRealMatrixSetIdentity(&artMatrix);
sAIRealMath->AIRealMatrixSetTranslate(&artMatrix, tx, ty);
error = sAITransformArt->TransformArt(destArt, &artMatrix, lineScale, kTransformObjects | kTransformChildren);

error = sAIArt->GetArtTransformBounds(destArt, &artMatrix, kTransformObjects | kTransformChildren, &srcBounds);
showAlertMessage("Transform Matrix: \na: "
	+ to_string(artMatrix.a) + "\nb: "
	+ to_string(artMatrix.b) + "\nc: "
	+ to_string(artMatrix.c) + "\nd: "
	+ to_string(artMatrix.d) + "\nx: "
	+ to_string(artMatrix.tx) + "\ny: "
	+ to_string(artMatrix.ty)
	+ "\n\nTransformed Bounds: \nLeft: "
	+ to_string(srcBounds.left) + "\nTop: "
	+ to_string(srcBounds.top) + "\nRight: "
	+ to_string(srcBounds.right) + "\nBottom: "
	+ to_string(srcBounds.bottom));