cdrapp.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "cdrapp.h"
  2. #include <stdio.h>
  3. #define _USE_MATH_DEFINES
  4. #include <math.h> // C++ 在 math.h 中定义 M_PI 需要 _USE_MATH_DEFINES 宏
  5. bool isDPCurve(IVGShapePtr s) {
  6. auto dpc = (s->Type == cdrRectangleShape) || (s->Type == cdrEllipseShape) ||
  7. (s->Type == cdrCurveShape) || (s->Type == cdrPolygonShape) ||
  8. (s->Type == cdrBitmapShape);
  9. return dpc;
  10. }
  11. IVGShapePtr CreateBoundary(corel *cdr, IVGShapePtr s) {
  12. // auto scp = s2->CreateBoundary(0, 0 , true, true);
  13. // 这个 API X7 以上才支持,所以现在直接画矩形
  14. BoundingBox box;
  15. s->GET_BOUNDING_BOX(box);
  16. auto scp =
  17. cdr->ActiveLayer->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC);
  18. return scp;
  19. }
  20. // VGCore::IVGShapePtr VGCore::IVGShape::CreateBoundary ( double x, double y,
  21. // VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource ); VGCore::IVGShapePtr
  22. // VGCore::IVGShapeRange::CreateBoundary ( double x, double y, VARIANT_BOOL
  23. // PlaceOnTop, VARIANT_BOOL DeleteSource ); VARIANT_BOOL
  24. // VGCore::IVGCurve::IntersectsWith ( struct IVGCurve * Curve )
  25. void test_IntersectsWith(corel *cdr) {
  26. auto sr = cdr->ActiveSelectionRange;
  27. if (1 < sr->Count) {
  28. bool isIn = false;
  29. auto s1 = sr->Shapes->Item[1];
  30. auto s2 = sr->Shapes->Item[2];
  31. if (isDPCurve(s1) && isDPCurve(s2)) {
  32. isIn = s1->GetDisplayCurve()->IntersectsWith(s2->GetDisplayCurve());
  33. } else if (isDPCurve(s1)) {
  34. // 群组文字和OLE等其他类型,创建一个临时边界范围
  35. auto scp = CreateBoundary(cdr, s2);
  36. isIn = s1->GetDisplayCurve()->IntersectsWith(scp->GetDisplayCurve());
  37. scp->Delete();
  38. } else if (isDPCurve(s2)) {
  39. auto scp = CreateBoundary(cdr, s1);
  40. isIn = scp->GetDisplayCurve()->IntersectsWith(s2->GetDisplayCurve());
  41. scp->Delete();
  42. } else {
  43. auto scp = CreateBoundary(cdr, s1);
  44. auto scp2 = CreateBoundary(cdr, s2);
  45. isIn = scp->GetDisplayCurve()->IntersectsWith(scp2->GetDisplayCurve());
  46. scp->Delete();
  47. scp2->Delete();
  48. }
  49. if (isIn)
  50. Test_Intersect(cdr);
  51. }
  52. }
  53. bool Test_Intersect(corel *cdr) {
  54. cdr->ActiveDocument->Unit = cdrInch;
  55. IVGShapePtr s[3];
  56. IVGShapePtr si[3];
  57. IVGShapePtr sm;
  58. double x, y, radius = 1.5;
  59. int r, g, b;
  60. // 通过将 i 乘以 2.09439507 (120度的弧度),可以将三个椭圆均匀地分布在一个圆上。
  61. // radius = 1.5 则是决定椭圆的大小。 这个数字控制椭圆的半径,也就是椭圆的大小。
  62. // // 创建3个椭圆, 填色 RGB:
  63. for (int i = 0; i != 3; i++) {
  64. x = cdr->ActivePage->SizeWidth / 2 + 1 * cos(i * M_PI * 2.0 / 3.0);
  65. y = cdr->ActivePage->SizeHeight / 2 + 1 * sin(i * M_PI * 2.0 / 3.0);
  66. s[i] = MakeEllipse(cdr, x, y, radius);
  67. r = 255 * (i == 0);
  68. g = 255 * (i == 1);
  69. b = 255 * (i == 2);
  70. s[i]->Fill->UniformColor->RGBAssign(r, g, b);
  71. }
  72. // 创建3个相交物件, IVGShape::Intersect 函数原型
  73. // IVGShapePtr IVGShape::Intersect ( struct IVGShape * TargetShape,
  74. // VARIANT_BOOL LeaveSource, VARIANT_BOOL LeaveTarget );
  75. for (int i = 0; i != 3; i++) {
  76. int n = (i + 1) % 3;
  77. si[i] = s[i]->Intersect(s[n], true, true);
  78. auto c1 = s[i]->Fill->UniformColor;
  79. auto c2 = s[n]->Fill->UniformColor;
  80. r = c1->RGBRed + c2->RGBRed;
  81. g = c1->RGBGreen + c2->RGBGreen;
  82. b = c1->RGBBlue + c2->RGBBlue;
  83. si[i]->Fill->UniformColor->RGBAssign(r, g, b);
  84. }
  85. // 建立相交物件,填充白色
  86. sm = si[1]->Intersect(si[2], true, true);
  87. auto c3 = cdr->CreateRGBColor(255, 255, 255);
  88. sm->Fill->UniformColor = c3;
  89. return true;
  90. }
  91. bool polygon_gravity_dot(corel *cdr) {
  92. cdr->ActiveDocument->Unit = cdrMillimeter;
  93. double x, y;
  94. // IVGShapePtr ActiveShape; // 获取当前选中的图形 函数原型
  95. auto s = cdr->ActiveShape;
  96. // IVGShapeRangePtr IVGApplication::CreateShapeRange ( );
  97. auto srs = cdr->CreateShapeRange();
  98. // 获取节点函数原型
  99. // IVGNodePtr IVGNodes::GetItem ( long Index );
  100. // IVGNodePtr IVGNodeRange::GetItem ( long Index );
  101. for (auto i = 0; i < s->DisplayCurve->Nodes->Count; i++) {
  102. auto n = s->DisplayCurve->Nodes->Item[i + 1];
  103. x = n->PositionX;
  104. y = n->PositionY;
  105. printf("%f, %f\n", x, y);
  106. // auto sy = MakeEllipse(cdr, x, y, 5);
  107. // sy->Outline->Color->RGBAssign(255, 0, 0);
  108. // auto sj = MakeRectangle(cdr, x, y, 10, 10);
  109. // sj->Outline->Color->RGBAssign(0, 255, 0);
  110. auto l = DrawLine(cdr, x, y, s->CenterX, s->CenterY);
  111. srs->Add(l);
  112. }
  113. // HRESULT IVGShapeRange::CreateSelection ( );
  114. srs->CreateSelection();
  115. return true;
  116. }
  117. // 建立矩形和圆形函数原型
  118. // IVGShapePtr IVGLayer::CreateRectangle2 ( double x, double y, double Width,
  119. // double Height,
  120. // double RadiusUL, double RadiusUR, double RadiusLR,
  121. // double RadiusLL );
  122. // IVGShapePtr IVGLayer::CreateEllipse2 ( double CenterX, double CenterY,
  123. // double Radius1,
  124. // double Radius2, double StartAngle, double EndAngle,
  125. // VARIANT_BOOL Pie );
  126. IVGShapePtr MakeEllipse(corel *cdr, double x, double y, double r) {
  127. return cdr->ActiveLayer->CreateEllipse2(x, y, r, ZERO_4PC);
  128. }
  129. IVGShapePtr MakeEllipse(corel *cdr, IVGShapePtr s, double r) {
  130. double x, y;
  131. x = s->CenterX;
  132. y = s->CenterY;
  133. return cdr->ActiveLayer->CreateEllipse2(x, y, r, ZERO_4PC);
  134. }
  135. IVGShapePtr MakeRectangle(corel *cdr, double x, double y, double w, double h) {
  136. auto sj = cdr->ActiveLayer->CreateRectangle2(x, y, w, h, ZERO_4PC);
  137. sj->PutCenterX(x);
  138. sj->PutCenterY(y);
  139. return sj;
  140. }
  141. IVGShapePtr MakeRectangle(corel *cdr, IVGShapePtr s, double w, double h) {
  142. double x, y;
  143. x = s->CenterX;
  144. y = s->CenterY;
  145. auto sj = cdr->ActiveLayer->CreateRectangle2(x, y, w, h, ZERO_4PC);
  146. sj->PutCenterX(x);
  147. sj->PutCenterY(y);
  148. return sj;
  149. }
  150. // IVGShapePtr IVGLayer::CreateLineSegment ( double StartX, double StartY,
  151. // double EndX, double EndY ); 画一条线,设置轮廓色 M100
  152. IVGShapePtr DrawLine(corel *cdr, double x1, double y1, double x2, double y2) {
  153. auto line = cdr->ActiveLayer->CreateLineSegment(x1, y1, x2, y2);
  154. line->Outline->Color->CMYKAssign(0, 100, 0, 0);
  155. return line;
  156. }
  157. // IVGShapePtr IVGLayer::FindShape ( _bstr_t Name, enum cdrShapeType Type, long
  158. // StaticID, VARIANT_BOOL Recursive );
  159. // IVGShapePtr IVGLayer::CreateCurveSegment ( double StartX, double StartY,
  160. // double EndX, double EndY, double StartingControlPointLength,
  161. // double StartingControlPointAngle, double
  162. // EndingControlPointLength, double
  163. // EndingControlPointAngle );
  164. // IVGShapePtr IVGLayer::CreateCurveSegment2 ( double x1, double y1, double
  165. // StartingControlPointX, double StartingControlPointY,
  166. // double EndingControlPointX, double
  167. // EndingControlPointY, double x2,
  168. // double y2 );