找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 179|回复: 0

[每日一码] 获取块的XCLIP边界

[复制链接]

1

主题

0

回帖

33

积分

管理员

积分
33
发表于 2024-3-14 20:56:29 | 显示全部楼层 |阅读模式
  1. static BOOL drawAPlineWithPropertiesFromBlockRef(AcGePoint2dArray pts,
  2.                  AcDbBlockReference* ref, double elevation, AcGeVector3d& normal)
  3.   {
  4.     AcDbPolyline *pl = new AcDbPolyline;
  5.     AcDbObjectId owner;
  6.     pl->setDatabaseDefaults();
  7.     pl->setClosed(Adesk::kTrue);
  8.     pl->setThickness(0.0);
  9.     if (ref != NULL) {
  10.         owner=ref->ownerId();
  11.         pl->setPropertiesFrom(ref);
  12.     }
  13.     pl->setNormal(normal);
  14.     for (int i=0; i < pts.length(); i++)
  15.     {
  16.         pl->addVertexAt(i, pts);
  17.     }
  18.     pl->setElevation(elevation);
  19.     pl->setColorIndex(1); // Red
  20.     AcDbBlockTableRecord *rec=NULL;
  21.     acdbOpenObject(rec, owner, AcDb::kForWrite);
  22.     if (rec != NULL)
  23.     {
  24.         AcDbObjectId id;
  25.         rec->appendAcDbEntity(id, pl);
  26.         pl->close();
  27.         rec->close();
  28.     }
  29.     else
  30.     {
  31.         delete pl;
  32.         return false;
  33.     }
  34.     return true;
  35.   } // End of drawAPlineWithPropertiesFromBlockRef()
  36.   // Get the boundary from a Block Reference given by the blk parameter.
  37.   // Transform that boundary to the WCS.
  38.   //-------------------------------------------------------------------
  39.   static BOOL GetBlockClippingPolyline(ads采用name blk)
  40.   {
  41.     BOOL ret = FALSE;
  42.     AcDbBlockReference *ref = NULL;
  43.     AcDbObjectId insId = AcDbObjectId::kNull;
  44.     acdbGetObjectId(insId, blk);
  45.     if (acdbOpenObject(ref, insId, AcDb::kForRead) != Acad::eOk)
  46.       return ret;
  47.     // Find the clipping object (AcDbSpatialFilter) in the ExtDict of the BlockRef
  48.     AcDbObjectId extDicId = ref->extensionDictionary();
  49.     if( extDicId == AcDbObjectId::kNull )
  50.       return ret;
  51.     AcDbDictionary *extDict=NULL, *acadFilterDict=NULL;
  52.     if (acdbOpenObject(extDict, extDicId, AcDb::kForRead) != Acad::eOk)
  53.         return ret;
  54.     Acad::ErrorStatus err = extDict->getAt(采用T("ACAD采用FILTER"), (AcDbObject*&)acadFilterDict, AcDb::kForRead);
  55.     extDict->close();
  56.     if( err != Acad::eOk )
  57.         return ret;
  58.     AcDbSpatialFilter *filter=NULL;
  59.     err = acadFilterDict->getAt(采用T("SPATIAL"), (AcDbObject*&)filter, AcDb::kForRead);
  60.     acadFilterDict->close();
  61.     if ( err != Acad::eOk)
  62.       return ret;
  63.     // Get the transform matrix stored in the XClip boundary
  64.     AcGeMatrix3d xformInBoundary = filter->getClipSpaceToWCSMatrix( xformInBoundary );
  65.     // and the transform of the BlockRef at the time when the Filter was set
  66.     AcGeMatrix3d xformRefOrig = filter->getOriginalInverseBlockXform( xformRefOrig );
  67.     // Get the transform matrix that the current BlockRef has, so, we can find the difference
  68.     //   with the xformRefOrig
  69.     AcGeMatrix3d refMatrix = ref->blockTransform();
  70.     refMatrix = refMatrix.postMultBy(xformRefOrig );
  71.     // Calculate the final transform matrix which applies to the points
  72.     // returned from filter->getDefinition().
  73.     AcGeMatrix3d finalMatrix = refMatrix.postMultBy(xformInBoundary);
  74.     AcGePoint2dArray pts;
  75.     AcGePoint3dArray pts3d;
  76.     AcGeVector3d normal;
  77.     double elevation = 0, frontClip = 0, backClip = 0;
  78.     Adesk::Boolean enabled  = false;
  79.     // Get all boundary points
  80.     if (filter->getDefinition(pts, normal, elevation, frontClip, backClip, enabled) == Acad::eOk)
  81.     {
  82.       // Rectanglar boundary
  83.       if (pts.length() == 2)
  84.       {
  85.         AcGePoint2d p1(pts[1].x, pts[0].y);
  86.         AcGePoint2d p3(pts[0].x, pts[1].y);
  87.         pts.insertAt(1, p1);
  88.         pts.insertAt(3, p3);
  89.       }
  90.       // Transform all points with the transform matrix we calculated
  91.       for(int i=0; i<pts.length(); i++)
  92.       {
  93.         AcGePoint2d pt2d;
  94.         AcGePoint3d pt3d;
  95.         pt2d = pts;
  96.         pt3d[0] = pt2d[0]; pt3d[1] = pt2d[1]; pt3d[2] = 0;
  97.         pt3d.transformBy(finalMatrix);
  98.         pts3d.append(pt3d);
  99.       }
  100.     }
  101.     // Get the new normal and new ECS information for the polyline.
  102.     AcGeVector3d xfnorm = normal.transformBy(finalMatrix);
  103.     AcGeMatrix3d plineECS;
  104.     AcDbPolyline* pline = new AcDbPolyline();
  105.     pline->setNormal(xfnorm);
  106.     pline->getEcs(plineECS);
  107.     delete pline; pline = NULL;
  108.     AcGeMatrix3d plineECSInv = plineECS.inverse();
  109.     double xfelev = 0.0;
  110.     for (int i = 0; i < pts.length(); i++)
  111.     {
  112.       pts.x = pts3d.x;
  113.       pts.y = pts3d.y;
  114.       if (i == 0)
  115.         xfelev = pts3d.z;
  116.       // Should be identical to within roundoff
  117.       assert(fabs(xfelev - pts3d.z) < 1.0e-10);
  118.     }
  119.     // Show the boundary
  120.     drawAPlineWithPropertiesFromBlockRef(pts, ref, xfelev, xfnorm);
  121.     filter->close();
  122.     ref->close();
  123.     return true;
  124.   } // End of GetBlockClippingPolyline()
  125. 本帖隐藏的内容
  126. // "MClip" test command for the GetBlockClippingPolyline() function.
  127. static void MMRCplusplus采用MClip(void)
  128.   {
  129.     ads采用name en;
  130.     AcGePoint3d pt;
  131.     if (acedEntSel(采用T("\nSelect an INSERT clipped by XCLIP command: "), en, asDblArray(pt)) != RTNORM)
  132.     {
  133.       acutPrintf(采用T("\nNothing selected"));
  134.       return;
  135.     }
  136.     assert(GetBlockClippingPolyline(en)==TRUE);
  137.   }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|膜结构网

GMT+8, 2024-12-29 21:27 , Processed in 0.126033 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表