#include "cdrapp.h" #include #define _USE_MATH_DEFINES #include // C++ 在 math.h 中定义 M_PI 需要 _USE_MATH_DEFINES 宏 void BeginOpt(corel *cdr) { auto name = _bstr_t("Undo"); cdr->EventsEnabled = false; cdr->ActiveDocument->BeginCommandGroup(name); cdr->ActiveDocument->Unit = cdrMillimeter; cdr->Optimization = true; } void EndOpt(corel *cdr) { cdr->EventsEnabled = true; cdr->Optimization = false; cdr->EventsEnabled = true; cdr->ActiveDocument->ReferencePoint = cdrBottomLeft; cdr->Application->Refresh(); cdr->ActiveDocument->EndCommandGroup(); } bool isDPCurve(IVGShapePtr s) { auto dpc = (s->Type == cdrRectangleShape) || (s->Type == cdrEllipseShape) || (s->Type == cdrCurveShape) || (s->Type == cdrPolygonShape) || (s->Type == cdrBitmapShape); return dpc; } IVGShapePtr CreateBoundary(corel *cdr, IVGShapePtr s) { auto scp = s->CreateBoundary(0, 0, true, false); // 这个 API X7 以上才支持,所以现在直接画矩形 // BoundingBox box; // s->GET_BOUNDING_BOX(box); // auto scp = cdr->ActiveLayer->CreateRectangle2(box.x, box.y, box.w, box.h, // ZERO_4PC); return scp; } // 从矩形边界坐标 获得中心坐标 void calculate_center(const BoundingBox *box, double *center_x, double *center_y) { *center_x = box->x + (box->w / 2); *center_y = box->y + (box->h / 2); } // VGCore::cdrPositionOfPointOverShape VGCore::IVGShape::IsOnShape ( double x, // double y, double HotArea ); VGCore::cdrPositionOfPointOverShape // VGCore::IVGCurve::IsOnCurve ( double x, double y, double HotArea ); bool BoundaryGroup(corel *cdr, IVGShapeRange *sr, IVGShapeRange *srs) { if (sr->Count < 2) return false; BoundingBox box; double x, y; int OnSh = 0; auto bounds = sr->CreateBoundary(0, 0, true, false); // 建立异性边界物件 bounds->Fill->UniformColor->RGBAssign(255, 0, 0); // 填充红色 auto sbox = bounds->BreakApartEx(); // 把边界 拆分为多个边界 用来分组 auto srgp = cdr->CreateShapeRange(); for (int k = 0; k < sbox->Count; k++) { for (int i = 0; i < sr->Count; i++) { auto sh = sr->Shapes->Item[i + 1]; sh->GET_BOUNDING_BOX(box); // 获得物件矩形边界坐标 calculate_center(&box, &x, &y); // 获得物件中心坐标 OnSh = sbox->Shapes->Item[k + 1]->IsOnShape(x, y, -1); if (OnSh) { srgp->Add(sh); } else if (isIntWith(cdr, sbox->Shapes->Item[k + 1], sh)) { srgp->Add(sh); } } // 从Range中移除已分组的图形 sr->RemoveRange(srgp); srs->Add(srgp->Group()); srgp->RemoveAll(); } sbox->Delete(); return true; } void test_IsOnShape(corel *cdr) { BeginOpt(cdr); auto sr = cdr->ActiveSelectionRange; auto srs = cdr->CreateShapeRange(); BoundaryGroup(cdr, sr, srs); srs->CreateSelection(); EndOpt(cdr); } // VGCore::IVGShapePtr VGCore::IVGShape::CreateBoundary ( double x, double y, // VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource ); VGCore::IVGShapePtr // VGCore::IVGShapeRange::CreateBoundary ( double x, double y, VARIANT_BOOL // PlaceOnTop, VARIANT_BOOL DeleteSource ); VARIANT_BOOL // VGCore::IVGCurve::IntersectsWith ( struct IVGCurve * Curve ) bool isIntWith(corel *cdr, IVGShape *s1, IVGShape *s2) { bool isIn = false; if (isDPCurve(s1) && isDPCurve(s2)) { isIn = s1->GetDisplayCurve()->IntersectsWith(s2->GetDisplayCurve()); } else if (isDPCurve(s1)) { // 群组文字和OLE等其他类型,创建一个临时边界范围 auto scp = CreateBoundary(cdr, s2); isIn = s1->GetDisplayCurve()->IntersectsWith(scp->GetDisplayCurve()); scp->Delete(); } else if (isDPCurve(s2)) { auto scp = CreateBoundary(cdr, s1); isIn = scp->GetDisplayCurve()->IntersectsWith(s2->GetDisplayCurve()); scp->Delete(); } else { auto scp = CreateBoundary(cdr, s1); auto scp2 = CreateBoundary(cdr, s2); isIn = scp->GetDisplayCurve()->IntersectsWith(scp2->GetDisplayCurve()); scp->Delete(); scp2->Delete(); } return isIn; } bool Test_Intersect(corel *cdr) { cdr->ActiveDocument->Unit = cdrInch; IVGShapePtr s[3]; IVGShapePtr si[3]; IVGShapePtr sm; double x, y, radius = 1.5; int r, g, b; // 通过将 i 乘以 2.09439507 (120度的弧度),可以将三个椭圆均匀地分布在一个圆上。 // radius = 1.5 则是决定椭圆的大小。 这个数字控制椭圆的半径,也就是椭圆的大小。 // // 创建3个椭圆, 填色 RGB: for (int i = 0; i != 3; i++) { x = cdr->ActivePage->SizeWidth / 2 + 1 * cos(i * M_PI * 2.0 / 3.0); y = cdr->ActivePage->SizeHeight / 2 + 1 * sin(i * M_PI * 2.0 / 3.0); s[i] = MakeEllipse(cdr, x, y, radius); r = 255 * (i == 0); g = 255 * (i == 1); b = 255 * (i == 2); s[i]->Fill->UniformColor->RGBAssign(r, g, b); } // 创建3个相交物件, IVGShape::Intersect 函数原型 // IVGShapePtr IVGShape::Intersect ( struct IVGShape * TargetShape, // VARIANT_BOOL LeaveSource, VARIANT_BOOL LeaveTarget ); for (int i = 0; i != 3; i++) { int n = (i + 1) % 3; si[i] = s[i]->Intersect(s[n], true, true); auto c1 = s[i]->Fill->UniformColor; auto c2 = s[n]->Fill->UniformColor; r = c1->RGBRed + c2->RGBRed; g = c1->RGBGreen + c2->RGBGreen; b = c1->RGBBlue + c2->RGBBlue; si[i]->Fill->UniformColor->RGBAssign(r, g, b); } // 建立相交物件,填充白色 sm = si[1]->Intersect(si[2], true, true); auto c3 = cdr->CreateRGBColor(255, 255, 255); sm->Fill->UniformColor = c3; return true; } bool polygon_gravity_dot(corel *cdr) { cdr->ActiveDocument->Unit = cdrMillimeter; double x, y; // IVGShapePtr ActiveShape; // 获取当前选中的图形 函数原型 auto s = cdr->ActiveShape; // IVGShapeRangePtr IVGApplication::CreateShapeRange ( ); auto srs = cdr->CreateShapeRange(); // 获取节点函数原型 // IVGNodePtr IVGNodes::GetItem ( long Index ); // IVGNodePtr IVGNodeRange::GetItem ( long Index ); for (auto i = 0; i < s->DisplayCurve->Nodes->Count; i++) { auto n = s->DisplayCurve->Nodes->Item[i + 1]; x = n->PositionX; y = n->PositionY; printf("%f, %f\n", x, y); // auto sy = MakeEllipse(cdr, x, y, 5); // sy->Outline->Color->RGBAssign(255, 0, 0); // auto sj = MakeRectangle(cdr, x, y, 10, 10); // sj->Outline->Color->RGBAssign(0, 255, 0); auto l = DrawLine(cdr, x, y, s->CenterX, s->CenterY); srs->Add(l); } // HRESULT IVGShapeRange::CreateSelection ( ); srs->CreateSelection(); return true; } // 建立矩形和圆形函数原型 // IVGShapePtr IVGLayer::CreateRectangle2 ( double x, double y, double Width, // double Height, // double RadiusUL, double RadiusUR, double RadiusLR, // double RadiusLL ); // IVGShapePtr IVGLayer::CreateEllipse2 ( double CenterX, double CenterY, // double Radius1, // double Radius2, double StartAngle, double EndAngle, // VARIANT_BOOL Pie ); IVGShapePtr MakeEllipse(corel *cdr, double x, double y, double r) { return cdr->ActiveLayer->CreateEllipse2(x, y, r, ZERO_4PC); } IVGShapePtr MakeEllipse(corel *cdr, IVGShapePtr s, double r) { double x, y; x = s->CenterX; y = s->CenterY; return cdr->ActiveLayer->CreateEllipse2(x, y, r, ZERO_4PC); } IVGShapePtr MakeRectangle(corel *cdr, double x, double y, double w, double h) { auto sj = cdr->ActiveLayer->CreateRectangle2(x, y, w, h, ZERO_4PC); sj->PutCenterX(x); sj->PutCenterY(y); return sj; } IVGShapePtr MakeRectangle(corel *cdr, IVGShapePtr s, double w, double h) { double x, y; x = s->CenterX; y = s->CenterY; auto sj = cdr->ActiveLayer->CreateRectangle2(x, y, w, h, ZERO_4PC); sj->PutCenterX(x); sj->PutCenterY(y); return sj; } // IVGShapePtr IVGLayer::CreateLineSegment ( double StartX, double StartY, // double EndX, double EndY ); 画一条线,设置轮廓色 M100 IVGShapePtr DrawLine(corel *cdr, double x1, double y1, double x2, double y2) { auto line = cdr->ActiveLayer->CreateLineSegment(x1, y1, x2, y2); line->Outline->Color->CMYKAssign(0, 100, 0, 0); return line; } // IVGShapePtr IVGLayer::FindShape ( _bstr_t Name, enum cdrShapeType Type, long // StaticID, VARIANT_BOOL Recursive ); // IVGShapePtr IVGLayer::CreateCurveSegment ( double StartX, double StartY, // double EndX, double EndY, double StartingControlPointLength, // double StartingControlPointAngle, double // EndingControlPointLength, double // EndingControlPointAngle ); // IVGShapePtr IVGLayer::CreateCurveSegment2 ( double x1, double y1, double // StartingControlPointX, double StartingControlPointY, // double EndingControlPointX, double // EndingControlPointY, double x2, // double y2 );