熟悉了一下acedGrRead()函数的用法之后自己就试着做了一个一拖多的简单动态画图效果,感觉用这个函数实现一拖多比较蠢,比较无脑! 效果图如下:
实现代码:
#include "StdAfx.h" #include "StdArx.h" #include "..\..\Common\我的类\Arc.h" #include <acedads.h> #include "..\..\Common\Entity\LineUtil.h" #include <geassign.h> #include <dbents.h> #include "..\..\Common\Others\ConvertUtil.h" #include <gearc2d.h> #include <dbcurve.h> #include "..\..\Common\Interaction\SelectUtil.h" #include "..\..\Common\Document\DwgDatabaseUtil.h"
void ZffMyProjectdrwarc() {
AcDbEntity *pEntRef=NULL; AcGePoint3d pickPoint; AcDbBlockReference *pBlkRef=NULL; AcDbObjectId blkDefId; AcDbObjectIdArray blkRefIds; int i,n=5; int track=1,type; ads_point pnt; AcGePoint3d pt; if (CSelectUtil::PromptSelectEntity(TEXT("\n选择块:"),AcDbBlockReference::desc(),pEntRef,pickPoint)) { pBlkRef=AcDbBlockReference::cast(pEntRef); blkDefId=pBlkRef->blockTableRecord(); pEntRef->close(); pBlkRef->close(); } acedGetPoint(NULL,"\n请输入一点:",pnt); pt.x=pnt[X]; pt.y=pnt[Y]; pt.z=pnt[Z]; AcGePoint2d pt12d=CConvertUtil::ToPoint2d(asPnt3d(pnt)); AcGePoint2d pt22d=CConvertUtil::ToPoint2d(pt); AcDbObjectId lineId=CLineUtil::Add(asPnt3d(pnt),pt); AcGeVector2d vec=pt22d-pt12d; for (i=1;i<n+1;i++) { pBlkRef=new AcDbBlockReference(CConvertUtil::ToPoint3d(pt12d+i*vec/(n+1)),blkDefId); blkRefIds.append(CDwgDatabaseUtil::PostToModelSpace(pBlkRef)); } pBlkRef->close();
AcDbLine *pLine=NULL; struct resbuf result; while(track>0) { acedGrRead(track,&type,&result); pt.x=result.resval.rpoint[X]; pt.y=result.resval.rpoint[Y]; pt.z=result.resval.rpoint[Z]; pt12d=CConvertUtil::ToPoint2d(asPnt3d(pnt)); pt22d=CConvertUtil::ToPoint2d(pt); acdbOpenObject(pLine,lineId,AcDb::kForWrite); pLine->setEndPoint(pt); for (i=0;i<blkRefIds.length();i++) { acdbOpenObject(pBlkRef,blkRefIds[i],AcDb::kForWrite); pBlkRef->setPosition(CConvertUtil::ToPoint3d(pt12d+(i+1)*(pt22d-pt12d)/(n+1))); pBlkRef->close(); }
if (type==3) { track=0; } pLine->close(); } AcGePoint2d ptOnArc; AcDbObjectId arcId; double startParam=0,endParam=0,paramInterval; track=1; while (track>0) { acedGrRead(track,&type,&result); ptOnArc.x=result.resval.rpoint[X]; ptOnArc.y=result.resval.rpoint[Y]; if (ptOnArc.x!=pt.x||ptOnArc.y!=pt.y) { if (arcId.isNull()) { acdbOpenObject(pLine,lineId,AcDb::kForWrite); pLine->erase(); pLine->close(); arcId=CArc::Add(pt12d,pt22d,pt22d); } AcGeCircArc2d geArc(pt12d, ptOnArc, pt22d); AcGePoint2d ptCenter = geArc.center(); double radius = geArc.radius(); AcGeVector2d vecStart(pt12d.x - ptCenter.x, pt12d.y - ptCenter.y); AcGeVector2d vecEnd(pt22d.x - ptCenter.x, pt22d.y - ptCenter.y); double startAngle = vecStart.angle(); double endAngle = vecEnd.angle();
AcDbArc *pArc2=new AcDbArc(CConvertUtil::ToPoint3d(ptCenter),radius,startAngle,endAngle); AcGePoint3d ptclose; pArc2->getClosestPointTo(CConvertUtil::ToPoint3d(ptOnArc),ptclose); pArc2->close(); AcDbArc *pArc=NULL; acdbOpenObject(pArc,arcId,AcDb::kForWrite); if (CConvertUtil::ToPoint2d(ptclose).distanceTo(ptOnArc)<0.01) { pArc->setCenter(CConvertUtil::ToPoint3d(ptCenter)); pArc->setRadius(radius); pArc->setStartAngle(startAngle); pArc->setEndAngle(endAngle); } else { pArc->setCenter(CConvertUtil::ToPoint3d(ptCenter)); pArc->setRadius(radius); pArc->setStartAngle(endAngle); pArc->setEndAngle(startAngle); } AcDbCurve *pCurve=AcDbCurve::cast(pArc); pCurve->getStartParam(startParam); pCurve->getEndParam(endParam); paramInterval=(endParam-startParam)/(n+1); for (i=1;i<n+1;i++) { double param=startParam+i*paramInterval; pCurve->getPointAtParam(param,pt); acdbOpenObject(pBlkRef,blkRefIds[i-1],AcDb::kForWrite); pBlkRef->setPosition(pt); pBlkRef->close(); } pCurve->close(); pArc->close(); if (type==3) { track=0; } } } }
AcDbObjectId CArc::Add(const AcGePoint2d &ptStart,const AcGePoint2d &ptOnArc,const AcGePoint2d &ptEnd) { AcGePoint3d ptOnArc1; AcGeCircArc2d geArc(ptStart, ptOnArc, ptEnd); AcGePoint2d ptCenter = geArc.center(); double radius = geArc.radius(); double tol=0.01; // 计算起始和终止角度 AcGeVector2d vecStart(ptStart.x - ptCenter.x, ptStart.y - ptCenter.y); AcGeVector2d vecEnd(ptEnd.x - ptCenter.x, ptEnd.y - ptCenter.y); double startAngle = vecStart.angle(); double endAngle = vecEnd.angle();
AcDbArc *pArc=new AcDbArc(CConvertUtil::ToPoint3d(ptCenter), radius, startAngle, endAngle); pArc->getClosestPointTo(CConvertUtil::ToPoint3d(ptOnArc),ptOnArc1);
if (CConvertUtil::ToPoint2d(ptOnArc1).distanceTo(ptOnArc)<tol) { return CDwgDatabaseUtil::PostToModelSpace(pArc); } else { AcDbArc *pArc2=new AcDbArc(CConvertUtil::ToPoint3d(ptCenter),radius,endAngle,startAngle); delete pArc; return CDwgDatabaseUtil::PostToModelSpace(pArc2);
} }
|
请发表评论