1
0

BoundaryGroup.cpp 7.0 KB

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