找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1941|回复: 1

[分享] 求任意实体和块的准确交点

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2013-9-1 16:35:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Intersecting any entity with a BlockReference and retrieving the intersection points
Intersecting a block reference with your entity is tricky. Every time you try to get the intersection points, it will return the intersection of your entity with the bounding box of the block reference. e.g. pLine.->intersectWith( pBlock...) and pBlock->intersectWith( pLine...), just return the intersection of the line entity with the block reference bounding box.
How do you find the real intersection points of an entity with the entities in the block reference?
There are two ways to do this:

1) The first method involves opening the AcDbBlockReference, finding its AcDbBlockTableRecord and then iterating through the entities within the block definition, calling AcDbEntity::intersectWith() on each. This method is fine, except that the code is quite tricky to write. Also you will be checking the entities in the actual block definition so the positions of the entities would then have to be temporarily transformed by the AcDbBlockReference's transformation matrix for the intersectWith to work correctly.  
2) The second way, which is far simpler, is to explode the AcDbBlockReference using the explode() method. This returns an array of entities that would be the exploded version of the AcDbBlockReference. They are even correctly transformed by the blockTransform matrix. All we have to do is iterate through these entities, call intersectWith(), get the results and then delete them.
Here is the relevant code:
//*********************************************************
// get the intersection point of 2 entities and return the
// result in interSectionPoint
// static int GetIntersectionPointsOf (
//      ads_name ename1, ads_name ename2,
//      AcGePoint3dArray &intersectionPoints);

// get the intersection point of 2 AcDbEntity's and return
// the result in interSectionPoint
// static int GetIntersectionPointsOf (
//      AcDbEntity *pEnt1, AcDbEntity *pEnt2,
//      AcGePoint3dArray &intersectionPoints);

// get intersection points from a block
//static int GetIntersectionPointsFromBlockEntities (
//      AcDbEntity *pEnt1,
//      AcDbEntity *pEnt2,
//      AcGePoint3dArray &intersectionPoints);

// draws a little cross at the pnt specified
// static void blip (AcGePoint3d pnt, int colour=4);
//**********************************************************


// This is command 'TEST'
void asdktest()
{
ads_name ename1;
ads_point pnt;
// pick first entity to check
int res = acedEntSel (
  L"\nPick first entity : ", ename1, pnt);
// if the user wants to carry on
if (res == RTNORM)
{
  // hightlight the entity picked
  acedRedraw (ename1, 3);

  ads_name ename2;
  // now pick the second entity
  res = acedEntSel (
   L"\nPick second entity : ", ename2, pnt);
  // if the user wants to carry on
  if (res == RTNORM)
  {
   // hightlight the entity picked
   acedRedraw (ename2, 3);

   AcGePoint3dArray intersectionPoints;
   // get the intersection point of 2
   // entities and return the result in
   // interSectionPoint
   if (GetIntersectionPointsOf (
    ename1,
    ename2,
    intersectionPoints) == RTNORM)
   {
    // see how many points we've got
    int length = intersectionPoints.length ();
    // loop them and create a linked
    // list containing all of the points
    for (int i=0; i<length; ++i)
    {
     // show the intersection
     blip (intersectionPoints);
    }
   }

   // un-hightlight the entity picked
   acedRedraw (ename2, 4);
  }

  // un-hightlight the entity picked
  acedRedraw (ename1, 4);
}
}

//*********************************************
// get the intersection point of 2 enames and
// return the result in interSectionPoint
static int GetIntersectionPointsOf (
ads_name ename1,
ads_name ename2,
AcGePoint3dArray &intersectionPoints)
{
AcDbObjectId objectId1, objectId2;
// get an object id from the first
// ename supplied, test to see if valid
if (acdbGetObjectId (
  objectId1, ename1) != Acad::eOk)
  return (RTERROR);
// get an object id from the second
// ename supplied, test to see if valid
if (acdbGetObjectId (
  objectId2, ename2) != Acad::eOk)
  return (RTERROR);

AcDbEntity *pEnt1 = NULL;
// open the object because we want to extract
// some info out of it like the start
// and end points etc
Acad::ErrorStatus es = acdbOpenObject (
  pEnt1, objectId1, AcDb::kForRead);
// if it opened ok
if (es == Acad::eOk)
{
  // take extra care and make sure
  // we have a valid pointer
  if (pEnt1 != NULL)
  {
   AcDbEntity *pEnt2 = NULL;
   // open the object because we want to
   // extract some info out of it like
   // the start and end points etc
   Acad::ErrorStatus es = acdbOpenObject (
    pEnt2, objectId2, AcDb::kForRead);
   // if it opened ok
   if (es == Acad::eOk)
   {
    // take extra care and make sure
    // we have a valid pointer
    if (pEnt2 != NULL)
    {
     // get intersection points of the
     //2 AcDbEntity's and return the
     // result in interSectionPoint
     int res = GetIntersectionPointsOf (
      pEnt1, pEnt2, intersectionPoints);

     // close the entities after we've finished
     pEnt1->close ();
     pEnt2->close ();

     return (res);
    }
   }
  }
}

return (RTERROR);
}

//*********************************************
// get the intersection point of 2 AcDbEntity's
// and return the result in interSectionPoint
static int GetIntersectionPointsOf (
AcDbEntity *pEnt1,
AcDbEntity *pEnt2,
AcGePoint3dArray &intersectionPoints)
{
// if the first entity is a block reference
if (pEnt1->isA() == AcDbBlockReference::desc ())
{
  // get intersection points from a block
  GetIntersectionPointsFromBlockEntities (
   pEnt1, pEnt2, intersectionPoints);
}
// if the first entity is a block reference
else if (
  pEnt2->isA() == AcDbBlockReference::desc ())
{
  // get intersection points from a block
  GetIntersectionPointsFromBlockEntities (
   pEnt2, pEnt1, intersectionPoints);
}
else
{
  // found out where they intersect with each other
  pEnt1->intersectWith (
   pEnt2, AcDb::kOnBothOperands, intersectionPoints);
}

return (RTNORM);
}

//*********************************************
// get intersection points from a block
static int GetIntersectionPointsFromBlockEntities (
AcDbEntity *pEnt1, AcDbEntity *pEnt2,
AcGePoint3dArray &intersectionPoints)
{
// can't handle 2 blocks
if (pEnt2->isA() == AcDbBlockReference::desc ())
{
  acutPrintf (
  L"\nError - this routine can't handle 2 blocks, sorry");
  return (RTERROR);
}

// dynamic cast to BlockReference
AcDbBlockReference *pBlockRef =
  AcDbBlockReference::cast (pEnt1);
if (pBlockRef != NULL)
{
  AcDbVoidPtrArray entitySet;
  // explode the block, this will return a
  // load of pre-transfromed entities for our perusal
  Acad::ErrorStatus es = pBlockRef->explode (entitySet);
  // if it worked ok
  if (es == Acad::eOk)
  {
   // loop round getting the intersection points
   for (long i=0l; i<entitySet.length (); ++i)
   {
    // get a pointer to the entity object
    AcDbEntity *pNewEnt = (AcDbEntity *)entitySet.at (i);

    // recall this function to check the entities
    if (GetIntersectionPointsOf (
     pNewEnt, pEnt2, intersectionPoints) != RTNORM)
    {
     acutPrintf (L"\nError getting objects");
    }

    // clean it up as we don't need it anymore
    delete pNewEnt;
   }
  }
}
return (RTNORM);
}

// draws a little blip at the specified point
//*********************************************
#define pi 3.1415926535897932384626433832795
// draws a little cross at the pnt specified
static void blip (AcGePoint3d pnt, int colour=4)
{
struct resbuf view;
memset (&view, 0, sizeof (struct resbuf));
// get the size of the screen in units
  acedGetVar (L"VIEWSIZE", &view);

ads_point p2, p3, p4, p5;
  acutPolar (asDblArray (pnt),      0.0,
   view.resval.rreal/60.0, p2);
  acutPolar (asDblArray (pnt),   pi/2.0,
   view.resval.rreal/60.0, p3);
  acutPolar (asDblArray (pnt),       pi,
   view.resval.rreal/60.0, p4);
  acutPolar (asDblArray (pnt),pi+pi/2.0,
   view.resval.rreal/60.0, p5);

  acedGrDraw(p2, p4, colour, 0);
  acedGrDraw(p3, p5, colour, 0);
}








评分

参与人数 1D豆 +5 收起 理由
ScmTools + 5 很给力!经验;技术要点;资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2013-9-13 16:36:27 | 显示全部楼层
资料分享~要支持,盖楼~:lol
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-3-28 23:20 , Processed in 0.325149 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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