Browse Source

使用 CreateBoundary 和 IsOnShape 建立异性边界物件判断物件中心是否在边界内

蘭雅sRGB 2 weeks ago
parent
commit
341f3cff60
3 changed files with 58 additions and 14 deletions
  1. 52 11
      cdrapp.cpp
  2. 1 1
      cdrapp.h
  3. 5 2
      main.cpp

+ 52 - 11
cdrapp.cpp

@@ -11,21 +11,62 @@ bool isDPCurve(IVGShapePtr s) {
   return dpc;
 }
 IVGShapePtr CreateBoundary(corel *cdr, IVGShapePtr s) {
-  //  auto scp = s2->CreateBoundary(0, 0 , true, true);
+  auto scp = s->CreateBoundary(0, 0 , true, false);
+
   //  这个 API X7 以上才支持,所以现在直接画矩形
-  BoundingBox box;
-  s->GET_BOUNDING_BOX(box);
-  auto scp =
-      cdr->ActiveLayer->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC);
+  // BoundingBox box;
+  // s->GET_BOUNDING_BOX(box);
+  // auto scp = cdr->ActiveLayer->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC);
+
   return scp;
 }
-// VGCore::IVGShapePtr VGCore::IVGShape::CreateBoundary ( double x, double y,
-// VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource ); VGCore::IVGShapePtr
-// VGCore::IVGShapeRange::CreateBoundary ( double x, double y, VARIANT_BOOL
-// PlaceOnTop, VARIANT_BOOL DeleteSource ); VARIANT_BOOL
-// VGCore::IVGCurve::IntersectsWith ( struct IVGCurve * Curve )
-void test_IntersectsWith(corel *cdr) {
 
+// 从矩形边界坐标 获得中心坐标
+void calculate_center(const BoundingBox *box, double *center_x, double *center_y) {
+    *center_x = box->x + (box->w / 2);
+    *center_y = box->y + (box->h / 2);
+}
+
+// VGCore::cdrPositionOfPointOverShape VGCore::IVGShape::IsOnShape ( double x, double y, double HotArea ); 
+// VGCore::cdrPositionOfPointOverShape VGCore::IVGCurve::IsOnCurve ( double x, double y, double HotArea );
+
+void test_IsOnShape(corel *cdr){
+  cdr->ActiveDocument->Unit = cdrMillimeter;
+  BoundingBox box;
+  double x, y, r = 0.2;
+  int OnSh = 0;
+  auto sr = cdr->ActiveSelectionRange;
+  auto sbox = sr->CreateBoundary(0, 0, true, false);  // 建立异性边界物件
+  sbox->Fill->UniformColor ->RGBAssign(255, 0, 0);    // 填充红色
+
+  auto srgp = cdr->CreateShapeRange();
+
+  for (int i = 0; i < sr->Count; i++) {
+    auto sh = sr->Shapes->Item[i + 1];
+    sh->GET_BOUNDING_BOX(box);      // 获得物件矩形边界坐标
+    calculate_center(&box, &x, &y);  // 获得物件中心坐标
+
+    OnSh = sbox->IsOnShape(x, y, 1);
+
+    printf("x:%0.2f y:%0.2f  -> %d\n", x , y, OnSh);
+
+
+    if(OnSh){
+      srgp->Add(sh);
+      printf("物件ID %d 在范围内\n", i + 1);
+      cdr->ActiveLayer->CreateEllipse2(x, y, r*10, 0,0,0,0);
+    }
+  }
+  srgp->Group();
+  sbox->Delete();
+}
+
+
+// VGCore::IVGShapePtr VGCore::IVGShape::CreateBoundary ( double x, double y, VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource ); 
+// VGCore::IVGShapePtr VGCore::IVGShapeRange::CreateBoundary ( double x, double y, VARIANT_BOOL PlaceOnTop, VARIANT_BOOL DeleteSource ); 
+// VARIANT_BOOL VGCore::IVGCurve::IntersectsWith ( struct IVGCurve * Curve )
+void test_IntersectsWith(corel *cdr) {
+  
   auto sr = cdr->ActiveSelectionRange;
   if (1 < sr->Count) {
     bool isIn = false;

+ 1 - 1
cdrapp.h

@@ -16,7 +16,7 @@ bool polygon_gravity_dot(corel* cdr);
 
 bool Test_Intersect(corel* cdr);
 void test_IntersectsWith(corel* cdr);
-
+void test_IsOnShape(corel *cdr);
 
 // 重新包装 创建物件函数
 #define ZERO_4PC 0, 0, 0, 0

+ 5 - 2
main.cpp

@@ -14,7 +14,7 @@ int main() {
   return 0;
 }
 void Call_CorelDRAW() {
-  IVGApplicationPtr app(L"CorelDRAW.Application.16");  // 我的电脑 X6 正常 ; 毛子版 CorelDRAW 2020  绑定不上,没法使用
+  IVGApplicationPtr app(L"CorelDRAW.Application.24");  // 我的电脑 X6 正常 ; 毛子版 CorelDRAW 2020  绑定不上,没法使用
   app->Visible = VARIANT_TRUE;
 
   auto doc = app->ActiveDocument;
@@ -29,6 +29,9 @@ void Call_CorelDRAW() {
   // polygon_gravity_dot(cdr);
 
   // // 绘制 RGB 三原色,测试相交建立物件
-  test_IntersectsWith(cdr);
+  // test_IntersectsWith(cdr);
+
+   test_IsOnShape(cdr);
+
 
 }