BoundaryGroup.cpp 5.3 KB

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