BoundaryGroup.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "cdrapp.h"
  2. #include <cerrno>
  3. #include <cmath>
  4. #include <cstddef>
  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. IVGShapePtr scp;
  13. if (cdr->VersionMajor < 17) {
  14. // s->CreateSelection();
  15. // cdr->FrameWork->Automation->InvokeItem( _bstr_t("b0491566-5ffe-450a-b17e-f2f496b4eb22"));
  16. // scp = cdr->ActiveSelectionRange->Shapes->Item[1];
  17. BoundingBox box;
  18. s->GET_BOUNDING_BOX(box);
  19. scp = cdr->ActiveLayer->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC);
  20. }
  21. return scp;
  22. }
  23. // VGCore::IVGShapePtr VGCore::IVGShape::CreateBoundary ( double x, double y, VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource ); VGCore::IVGShapePtr
  24. // VGCore::IVGShapeRange::CreateBoundary ( double x, double y, VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource );
  25. // VARIANT_BOOL VGCore::IVGCurve::IntersectsWith ( struct IVGCurve * Curve )
  26. bool isIntWith(corel *cdr, IVGShape *s1, IVGShape *s2) {
  27. bool isIn = false;
  28. if (isDPCurve(s1) && isDPCurve(s2)) {
  29. isIn = s1->GetDisplayCurve()->IntersectsWith(s2->GetDisplayCurve());
  30. } else if (isDPCurve(s1)) {
  31. // 群组文字和OLE等其他类型,创建一个临时边界范围
  32. auto scp = CreateBoundary(cdr, s2);
  33. isIn = s1->GetDisplayCurve()->IntersectsWith(scp->GetDisplayCurve());
  34. scp->Delete();
  35. } else if (isDPCurve(s2)) {
  36. auto scp = CreateBoundary(cdr, s1);
  37. isIn = scp->GetDisplayCurve()->IntersectsWith(s2->GetDisplayCurve());
  38. scp->Delete();
  39. } else {
  40. auto scp = CreateBoundary(cdr, s1);
  41. auto scp2 = CreateBoundary(cdr, s2);
  42. isIn = scp->GetDisplayCurve()->IntersectsWith(scp2->GetDisplayCurve());
  43. scp->Delete();
  44. scp2->Delete();
  45. }
  46. return isIn;
  47. }
  48. // 从矩形边界坐标 获得中心坐标
  49. void calculate_center(const BoundingBox &box, double &cx, double &cy) {
  50. cx = box.x + (box.w / 2);
  51. cy = box.y + (box.h / 2);
  52. }
  53. // VGCore::cdrPositionOfPointOverShape VGCore::IVGShape::IsOnShape ( double x, double y, double HotArea );
  54. // VGCore::cdrPositionOfPointOverShape VGCore::IVGCurve::IsOnCurve ( double x, double y, double HotArea );
  55. bool BoundaryGroup(corel *cdr, IVGShapeRange *sr, IVGShapeRange *srs) {
  56. if (sr->Count < 2)
  57. return false;
  58. BoundingBox box, bound_box;
  59. double x, y;
  60. int OnSh = 0;
  61. auto red = cdr->CreateCMYKColor(0, 100, 100, 0);
  62. // 处理文字和影响的物件
  63. auto txtbox = cdr->CreateShapeRange();
  64. auto sr_text =
  65. sr->Shapes->FindShapes(_bstr_t(), cdrTextShape, VARIANT_TRUE, _bstr_t());
  66. if (sr_text->Count > 0) {
  67. auto al = cdr->ActiveLayer;
  68. for (auto i = 0; i != sr_text->Count; i++) {
  69. sr_text->Shapes->Item[i + 1]->GET_BOUNDING_BOX(box);
  70. auto s = al->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC);
  71. txtbox->Add(s);
  72. }
  73. sr->AddRange(txtbox);
  74. }
  75. // auto stmp = sr->CreateBoundary(0, 0, true, false); // 建立异性边界物件
  76. // auto ef1 = stmp->CreateContour(
  77. // cdrContourOutside, 0.2, 1, cdrDirectFountainFillBlend, red, NULL, NULL, 0,
  78. // 0, cdrContourSquareCap, cdrContourCornerMiteredOffsetBevel, 15);
  79. // auto bounds = ef1->Separate();
  80. // stmp->Delete();
  81. // // bounds->SetOutlineProperties(0.076, NULL, cdr->CreateCMYKColor(100, 50, 0,
  82. // // 0), NULL, NULL, cdrUndefined, cdrUndefined, cdrOutlineUndefinedLineCaps,
  83. // // cdrOutlineUndefinedLineJoin, NULL, NULL, NULL, NULL, NULL);
  84. // auto sbox = bounds->BreakApartEx();
  85. // sbox->ApplyUniformFill(red);
  86. // 建立辅助的异性边界物件,需要填充颜色,搞了半天才搞定
  87. IVGShapePtr bounds;
  88. if (cdr->VersionMajor < 17) {
  89. sr->CreateSelection();
  90. cdr->FrameWork->Automation->InvokeItem( _bstr_t("b0491566-5ffe-450a-b17e-f2f496b4eb22"));
  91. bounds = cdr->ActiveSelectionRange->Shapes->Item[1];
  92. bounds->OrderToFront();
  93. }
  94. bounds->Fill->UniformColor->RGBAssign(255, 0, 0);
  95. auto sbox = bounds->BreakApartEx(); // 把边界 拆分为多个边界 用来分组
  96. // 删除文字添加的方框
  97. if (sr_text->Count > 0) {
  98. sr->RemoveRange(txtbox);
  99. txtbox->Delete();
  100. }
  101. // 按照边界框异形范围进行分组群组
  102. auto srgp = cdr->CreateShapeRange();
  103. for (int k = 0; k < sbox->Count && sr->Count; k++) {
  104. sbox->Shapes->Item[k + 1]->GET_BOUNDING_BOX(bound_box);
  105. for (int i = 0; i < sr->Count; i++) {
  106. auto sh = sr->Shapes->Item[i + 1];
  107. sh->GET_BOUNDING_BOX(box); // 获得物件矩形边界坐标
  108. calculate_center(box, x, y); // 获得物件中心坐标
  109. OnSh = sbox->Shapes->Item[k + 1]->IsOnShape(x, y, -1);
  110. if (OnSh) {
  111. srgp->Add(sh);
  112. } else if (isOverlapped(box, bound_box)) {
  113. if (isIntWith(cdr, sbox->Shapes->Item[k + 1], sh))
  114. srgp->Add(sh);
  115. }
  116. }
  117. // 从Range中移除已分组的图形
  118. sr->RemoveRange(srgp);
  119. if (srgp->Count > 1) {
  120. srs->Add(srgp->Group());
  121. } else {
  122. srs->AddRange(srgp);
  123. }
  124. srgp->RemoveAll();
  125. }
  126. // 删除辅助的异性边界物件
  127. if(!debug_flg)
  128. sbox->Delete();
  129. return true;
  130. }
  131. // 测试运行 异形群组
  132. void run_BoundaryGroup(corel *cdr) {
  133. auto start = std::chrono::high_resolution_clock::now(); // 开始时间
  134. // if (cdr->VersionMajor < 17) {
  135. // sprintf(infobuf, "异形群组目前只支持X7以上版本!"); return; }
  136. BeginOpt(cdr);
  137. auto sr = cdr->ActiveSelectionRange;
  138. auto srs = cdr->CreateShapeRange();
  139. auto sr_box = cdr->CreateShapeRange();
  140. int cnt = sr->Count;
  141. // 取消选择,速度优化
  142. cdr->ActiveDocument->ClearSelection();
  143. if (cnt > 300) {
  144. // 调用矩形分组,分步执行异形群组
  145. if (BoxGrouping(cdr, sr, sr_box, 0.1)) {
  146. for (int i = 0; i < sr_box->Count; i++) {
  147. auto s = sr_box->Shapes->Item[i + 1];
  148. if (!s->IsSimpleShape) {
  149. auto sr2 = s->UngroupEx();
  150. BoundaryGroup(cdr, sr2, srs);
  151. }
  152. }
  153. }
  154. } else {
  155. BoundaryGroup(cdr, sr, srs);
  156. }
  157. srs->CreateSelection();
  158. // 计算持续时间
  159. double runtime = 0.0;
  160. auto end = std::chrono::high_resolution_clock::now();
  161. std::chrono::duration<double> duration = end - start;
  162. runtime = duration.count();
  163. sprintf(infobuf, "选择物件: %d 个进行异形群组\n群组: %d 组, 时间: %.2f秒", cnt, srs->Count, runtime);
  164. EndOpt(cdr);
  165. }