cdrapp.cpp 8.1 KB

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