明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: MIRRO

如何用ARX实现直线的offset偏移功能?

  [复制链接]
发表于 2011-12-21 01:33:18 | 显示全部楼层
本帖最后由 highflybird 于 2011-12-21 01:36 编辑

  1. //选择物体
  2. int ret;
  3. ads_point pt,pt1,pt2;
  4. ads_name ename;
  5. ret = acedEntSel(_T("\n请选择曲线类物体:"),ename,pt);
  6. if (RTNORM != ret)
  7. {
  8. acutPrintf(_T("\n选择无效,程序返回。"));
  9. return;
  10. }
  11. //判断是否曲线类物体
  12. Acad:;ErrorStatus es;
  13. AcDbEntity *pEnt = NULL;
  14. AcDbObjectId id;
  15. es = acdbGetObjectId(id,ename);
  16. es = acdbOpenAcDbEntity(pEnt,id,AcDb::kForRead);
  17. if (es != Acad::eOk)
  18. {
  19. return;
  20. }
  21. if (!pEnt->isKindOf(AcDbCurve::desc()))
  22. {
  23. pEnt->close();
  24. acutPrintf(_T("\n选择物体不是曲线,程序返回。"));
  25. return ;
  26. }
  27. //输入偏移方向
  28. double offDist;
  29. ret = acedGetPoint(NULL,_T("\n点取偏移方向:"),pt1);
  30. if (RTNORM != ret)
  31. {
  32. acutPrintf(_T("\n点取无效,程序返回。"));
  33. return;
  34. }
  35. //输入偏移距离
  36. ret = acedGetDist(pt1,_T("\n偏移距离:"),&offDist);
  37. if (RTNORM != ret)
  38. {
  39. acutPrintf(_T("\n输入距离无效,程序返回。"));
  40. return;
  41. }
  42. //此段用语判断偏移的方向
  43. AcDbCurve * pCurve = (AcDbCurve *)(pEnt);
  44. AcDbVoidPtrArray  Curves;
  45. AcGePoint3d pt3d1 = asPnt3d(pt1);
  46. AcGePoint3d pt3d2,pt3d3;
  47. AcGeVector3d vec,vec1;
  48. AcGeVector3d nrm;
  49. es = pCurve->getClosestPointTo(pt3d1,pt3d2);
  50. es = pCurve->getFirstDeriv(pt3d2,vec);
  51. pt3d3 = pt3d2 + vec;
  52. vec1 = pt3d1 -pt3d2;
  53. double area = vec.y * vec1.x - vec.x * vec1.y ;
  54. //如果是直线或者射线之类的物体,偏移距离需要反号
  55. if (pCurve->isKindOf(AcDbLine::desc()) || pCurve->isKindOf(AcDbXline::desc()) || pCurve->isKindOf(AcDbRay::desc()))
  56. {
  57. offDist = - offDist;
  58. }
  59. //如果是顺时针旋转,则偏移距离需要反号
  60. if (area > 0)
  61. {
  62. es = pCurve->getOffsetCurves(offDist,Curves);
  63. }
  64. else
  65. {
  66. es = pCurve->getOffsetCurves(-offDist,Curves);
  67. }
  68. pCurve->close();
  69. //现在已经得到偏移的曲线集了,剩下的的工作是添加到空间中去。
  70. pEnt = NULL;
  71. AcDbBlockTable *pBlockTable;
  72. acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForRead);
  73. AcDbBlockTableRecord *brec;
  74. resbuf tilemode;
  75. acedGetVar(_T("TILEMODE"),&tilemode);
  76. int tile = tilemode.resval.rint;
  77. if (tile == 1)
  78. pBlockTable->getAt(ACDB_MODEL_SPACE, brec,AcDb::kForWrite);
  79. else
  80. pBlockTable->getAt(ACDB_PAPER_SPACE, brec,AcDb::kForWrite);
  81. pBlockTable->close();
  82. AcDbObjectId entid;
  83. for (int i = 0;i< Curves.length();i++)
  84. {
  85.    pEnt =(AcDbEntity *) Curves[ i ] ;  
  86.    brec->appendAcDbEntity(entid, pEnt);
  87.    pEnt->close();
  88. }
  89. brec->close();
  90. acutPrintf(_T("\n偏移成功!"));



发表于 2011-12-23 09:59:45 | 显示全部楼层
highflybir 发表于 2011-12-20 23:07
已知一个点pt ,和曲线curve,获得pt在曲线的getclosestpointto(最近点),获得此处的firstDeriv,由此得到 ...

谢谢,明白了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 12:55 , Processed in 0.133283 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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