找回密码
 立即注册

QQ登录

只需一步,快速开始

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

用AcedGrRead()函数实现一拖多

[复制链接]

1

主题

0

回帖

37

积分

管理员

积分
37
发表于 2024-5-4 18:41:33 | 显示全部楼层 |阅读模式
  1. 实现代码:
  2. #include "StdAfx.h"
  3. #include "StdArx.h"
  4. #include "..\..\Common\我的类\Arc.h"
  5. #include <acedads.h>
  6. #include "..\..\Common\Entity\LineUtil.h"
  7. #include <geassign.h>
  8. #include <dbents.h>
  9. #include "..\..\Common\Others\ConvertUtil.h"
  10. #include <gearc2d.h>
  11. #include <dbcurve.h>
  12. #include "..\..\Common\Interaction\SelectUtil.h"
  13. #include "..\..\Common\Document\DwgDatabaseUtil.h"
  14. void ZffMyProjectdrwarc()
  15. {
  16.   AcDbEntity *pEntRef=NULL;
  17.   AcGePoint3d pickPoint;
  18.   AcDbBlockReference *pBlkRef=NULL;
  19.   AcDbObjectId blkDefId;
  20.   AcDbObjectIdArray blkRefIds;
  21.   int i,n=5;
  22.   int track=1,type;
  23.   ads采用point pnt;
  24.   AcGePoint3d pt;
  25.   if (CSelectUtil::PromptSelectEntity(TEXT("\n选择块:"),AcDbBlockReference::desc(),pEntRef,pickPoint))
  26.   {
  27.     pBlkRef=AcDbBlockReference::cast(pEntRef);
  28.     blkDefId=pBlkRef->blockTableRecord();
  29.     pEntRef->close();
  30.     pBlkRef->close();
  31.   }
  32.   acedGetPoint(NULL,"\n请输入一点:",pnt);
  33.   pt.x=pnt[X];
  34.   pt.y=pnt[Y];
  35.   pt.z=pnt[Z];
  36.   AcGePoint2d pt12d=CConvertUtil::ToPoint2d(asPnt3d(pnt));
  37.   AcGePoint2d pt22d=CConvertUtil::ToPoint2d(pt);
  38.   AcDbObjectId lineId=CLineUtil::Add(asPnt3d(pnt),pt);
  39.   AcGeVector2d vec=pt22d-pt12d;
  40.   for (i=1;i<n+1;i++)
  41.   {
  42.     pBlkRef=new AcDbBlockReference(CConvertUtil::ToPoint3d(pt12d+i*vec/(n+1)),blkDefId);
  43.     blkRefIds.append(CDwgDatabaseUtil::PostToModelSpace(pBlkRef));
  44.   }
  45.   pBlkRef->close();
  46.   AcDbLine *pLine=NULL;
  47.   struct resbuf result;
  48.   while(track>0)
  49.   {
  50.     acedGrRead(track,&type,&result);
  51.     pt.x=result.resval.rpoint[X];
  52.     pt.y=result.resval.rpoint[Y];
  53.     pt.z=result.resval.rpoint[Z];
  54.     pt12d=CConvertUtil::ToPoint2d(asPnt3d(pnt));
  55.     pt22d=CConvertUtil::ToPoint2d(pt);
  56.     acdbOpenObject(pLine,lineId,AcDb::kForWrite);
  57.     pLine->setEndPoint(pt);
  58.     for (i=0;i<blkRefIds.length();i++)
  59.     {
  60.       acdbOpenObject(pBlkRef,blkRefIds[i],AcDb::kForWrite);
  61.       pBlkRef->setPosition(CConvertUtil::ToPoint3d(pt12d+(i+1)*(pt22d-pt12d)/(n+1)));
  62.       pBlkRef->close();
  63.     }
  64.     if (type==3)
  65.     {
  66.       track=0;
  67.     }
  68.     pLine->close();
  69.   }
  70.   AcGePoint2d ptOnArc;
  71.   AcDbObjectId arcId;
  72.   double startParam=0,endParam=0,paramInterval;
  73.   track=1;
  74.   while (track>0)
  75.   {
  76.     acedGrRead(track,&type,&result);
  77.     ptOnArc.x=result.resval.rpoint[X];
  78.     ptOnArc.y=result.resval.rpoint[Y];
  79.     if (ptOnArc.x!=pt.x||ptOnArc.y!=pt.y)
  80.     {
  81.       if (arcId.isNull())
  82.       {
  83.         acdbOpenObject(pLine,lineId,AcDb::kForWrite);
  84.         pLine->erase();
  85.         pLine->close();
  86.         arcId=CArc::Add(pt12d,pt22d,pt22d);
  87.       }
  88.       AcGeCircArc2d geArc(pt12d, ptOnArc, pt22d);
  89.       AcGePoint2d ptCenter = geArc.center();
  90.       double radius = geArc.radius();
  91.       AcGeVector2d vecStart(pt12d.x - ptCenter.x, pt12d.y - ptCenter.y);
  92.       AcGeVector2d vecEnd(pt22d.x - ptCenter.x, pt22d.y - ptCenter.y);
  93.       double startAngle = vecStart.angle();
  94.       double endAngle = vecEnd.angle();
  95.       AcDbArc *pArc2=new AcDbArc(CConvertUtil::ToPoint3d(ptCenter),radius,startAngle,endAngle);
  96.       AcGePoint3d ptclose;
  97.       pArc2->getClosestPointTo(CConvertUtil::ToPoint3d(ptOnArc),ptclose);
  98.       pArc2->close();
  99.       AcDbArc *pArc=NULL;
  100.       acdbOpenObject(pArc,arcId,AcDb::kForWrite);
  101.       if (CConvertUtil::ToPoint2d(ptclose).distanceTo(ptOnArc)<0.01)
  102.       {
  103.         pArc->setCenter(CConvertUtil::ToPoint3d(ptCenter));
  104.         pArc->setRadius(radius);
  105.         pArc->setStartAngle(startAngle);
  106.         pArc->setEndAngle(endAngle);
  107.       }
  108.       else
  109.       {
  110.         pArc->setCenter(CConvertUtil::ToPoint3d(ptCenter));
  111.         pArc->setRadius(radius);
  112.         pArc->setStartAngle(endAngle);
  113.         pArc->setEndAngle(startAngle);
  114.       }
  115.       AcDbCurve *pCurve=AcDbCurve::cast(pArc);
  116.       pCurve->getStartParam(startParam);
  117.       pCurve->getEndParam(endParam);
  118.       paramInterval=(endParam-startParam)/(n+1);
  119.       for (i=1;i<n+1;i++)
  120.       {
  121.         double param=startParam+i*paramInterval;
  122.         pCurve->getPointAtParam(param,pt);
  123.         acdbOpenObject(pBlkRef,blkRefIds[i-1],AcDb::kForWrite);
  124.         pBlkRef->setPosition(pt);
  125.         pBlkRef->close();
  126.       }
  127.       pCurve->close();
  128.       pArc->close();
  129.       if (type==3)
  130.       {
  131.         track=0;
  132.       }
  133.     }
  134.   }
  135. }
  136. AcDbObjectId CArc::Add(const AcGePoint2d &ptStart,const AcGePoint2d &ptOnArc,const AcGePoint2d &ptEnd)
  137. {
  138. AcGePoint3d ptOnArc1;
  139. AcGeCircArc2d geArc(ptStart, ptOnArc, ptEnd);
  140. AcGePoint2d ptCenter = geArc.center();
  141. double radius = geArc.radius();
  142. double tol=0.01;
  143. // 计算起始和终止角度
  144. AcGeVector2d vecStart(ptStart.x - ptCenter.x, ptStart.y - ptCenter.y);
  145. AcGeVector2d vecEnd(ptEnd.x - ptCenter.x, ptEnd.y - ptCenter.y);
  146. double startAngle = vecStart.angle();
  147. double endAngle = vecEnd.angle();
  148. AcDbArc *pArc=new AcDbArc(CConvertUtil::ToPoint3d(ptCenter), radius, startAngle, endAngle);
  149. pArc->getClosestPointTo(CConvertUtil::ToPoint3d(ptOnArc),ptOnArc1);
  150. if (CConvertUtil::ToPoint2d(ptOnArc1).distanceTo(ptOnArc)<tol)
  151. {
  152. return CDwgDatabaseUtil::PostToModelSpace(pArc);
  153. }
  154. else
  155. {
  156. AcDbArc *pArc2=new AcDbArc(CConvertUtil::ToPoint3d(ptCenter),radius,endAngle,startAngle);
  157. delete pArc;
  158. return CDwgDatabaseUtil::PostToModelSpace(pArc2);
  159. }
  160. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-1-4 13:21 , Processed in 0.154919 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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