#include "stdafx.h" #include "TLineToPolyLine.h" #include "dbspline.h" //线变复线命令函数 void MakeLineToPolyLine() { static BOOL bDelete = TRUE; static BOOL bLayer = TRUE; static BOOL bColor = TRUE; static BOOL bLineStyle = TRUE; static CString strMerGeprecision = _T("0.025"); CTemporaryResourceOverride Temp; TLineToPolyDlg *pDlg = new TLineToPolyDlg(); pDlg->m_bLayer = bLayer; pDlg->m_bLineStyle = bLineStyle; pDlg->m_bColor = bColor; pDlg->m_bDelete = bDelete; pDlg->m_strMerGeprecision = strMerGeprecision; pDlg->Create(TLineToPolyDlg::IDD,acedGetAcadFrame()); acedGetAcadFrame()->SetFocus(); pDlg->ShowWindow(SW_SHOW); TLineToPolyLine line; line.CreatePolyLine(pDlg); bDelete = line.m_bDelete; bColor = line.m_bColor; bLayer = line.m_bLayer; bLineStyle = line.m_bLineStyle; strMerGeprecision.Format(_T("%.3lf"),line.m_dMerGeprecision); pDlg->DestroyWindow(); delete pDlg; } TLineToPolyLine::TLineToPolyLine(void) { m_dMerGeprecision = 0.025; m_aryLineId.setLogicalLength(0); m_bColor = FALSE; m_bLayer = FALSE; m_bLineStyle = FALSE; m_bDelete = TRUE; } TLineToPolyLine::~TLineToPolyLine(void) { } int TLineToPolyLine::CreatePolyLine(TLineToPolyDlg *pdlg) { SearchIds(); CString str; pdlg->GetDlgItemText(IDC_EDIT_PRECISION,str); m_dMerGeprecision = _tstof(str); m_bDelete = pdlg->m_bDelete; m_bColor = pdlg->m_bColor; m_bLayer = pdlg->m_bLayer; m_bLineStyle = pdlg->m_bLineStyle; CreateLineSegs(); CombinEntids(); if (m_bDelete) { EraseAllLine(); } return 0; } void TLineToPolyLine::DrawPoly(LineSegAry&sourcesegary) { m_ptAryAry.RemoveAll(); int nColor; AcDbObjectId idLayer,idLineStyle; if (sourcesegary.GetSize()!=0) { nColor = sourcesegary[0].m_nLineColor; idLayer = sourcesegary[0].m_idLineLayer; idLineStyle = sourcesegary[0].m_idLineStyle; } FindFitSegs(sourcesegary); if (m_ptAryAry.GetSize()!=0) { int i =0; for (i = 0; i < m_ptAryAry.GetSize();i++) { TBudgePoint3dArry ptary; ptary.append(m_ptAryAry.GetAt(i)); int nlen = ptary.length(); AcDbPolyline *pPolyLine = new AcDbPolyline(nlen); if (nlen>2) { if (ptary[0]==ptary[nlen-1]) { //pPolyLine->set ptary.removeAt(nlen-1); nlen = ptary.length(); pPolyLine->setClosed(Adesk::kTrue); } } int j = 0; for (j= 0;j < nlen;j++) { TBudgePoint3d pt3d = ptary.at(j); AcGePoint2d pt2d; pt2d.x = pt3d.x; pt2d.y = pt3d.y; double dBudge = pt3d.m_dBudge; if (!Equal(dBudge,0,_DIST_SNAP)) { pPolyLine->addVertexAt(j,pt2d,dBudge,0,0); } else { pPolyLine->addVertexAt(j,pt2d,0,0,0); } } pPolyLine->setColorIndex(nColor); pPolyLine->setLinetype(idLineStyle); pPolyLine->setLayer(idLayer); AcDbObjectId idPoly = AddToBlockTable(pPolyLine); pPolyLine->close(); } } } void TLineToPolyLine::CombinEntids() { LineSegAry sourceSegAry; sourceSegAry.Append(m_aryLineSegInfo); if (!m_bColor&&!m_bLayer&&!m_bLineStyle) { DrawPoly(sourceSegAry); } else if (!m_bColor&&!m_bLayer&&m_bLineStyle) { while(sourceSegAry.GetSize()!=0) { TLineSegInfo LineSeg = sourceSegAry[0]; LineSegAry SegAry; SegAry.Add(LineSeg); sourceSegAry.RemoveAt(0); if (sourceSegAry.GetSize()>0) { int i =0; for (i=0;i0) { int i =0; for (i=0;i0) { int i =0; for (i=0;i0) { int i =0; for (i=0;i0) { int i =0; for (i=0;i0) { int i =0; for (i=0;i0) { int i =0; for (i=0;ierase(); pEnt->close(); } } void TLineToPolyLine::SearchIds() { ads_name ssname; struct resbuf *reb; //构造过滤集 reb = acutBuildList(-4,_T(""),RTNONE); ads_printf(_T("请选择要合并的线\n")); int nRes = ads_ssget(NULL,NULL,NULL,reb,ssname); if (nRes ==RTNORM) { long nLen ; Acad::ErrorStatus es; ads_sslength(ssname,&nLen); int i = 0; for (i = 0;i < nLen;i++ ) { ads_name ent; ads_ssname(ssname,i,ent); AcDbObjectId idTem = AcDbObjectId::kNull; es = acdbGetObjectId(idTem,ent); if (es==Acad::eOk) { m_aryLineId.append(idTem); } } ads_ssfree(ssname); acutRelRb(reb); } } void TLineToPolyLine::CreateLineSegs() { Acad::ErrorStatus es; int i = 0; for (i=0;iisA()->name(); TLineSegInfo LineSegInfo; if (strEntName == _T("AcDbLine")) { AcDbLine *pline = (AcDbLine *)pEnt; LineSegInfo.m_idLineSeg = idTem; LineSegInfo.m_nLineColor = pline->colorIndex(); LineSegInfo.m_idLineLayer = pline->layerId(); LineSegInfo.m_idLineStyle = pline->linetypeId(); LineSegInfo.m_ptEnd = pline->endPoint(); LineSegInfo.m_ptStart = pline->startPoint(); m_aryLineSegInfo.Add(LineSegInfo); } else if (strEntName == _T("AcDbArc")) { AcDbArc *pline = (AcDbArc *)pEnt; LineSegInfo.m_idLineSeg = idTem; LineSegInfo.m_nLineColor = pline->colorIndex(); LineSegInfo.m_idLineLayer = pline->layerId(); LineSegInfo.m_idLineStyle = pline->linetypeId(); LineSegInfo.m_bArc = TRUE; LineSegInfo.m_ptMid = pline->center(); LineSegInfo.m_dStartAng = pline->startAngle(); LineSegInfo.m_dEndAng = pline->endAngle(); LineSegInfo.m_vecNormal = pline->normal(); pline->getStartPoint(LineSegInfo.m_ptStart); pline->getEndPoint(LineSegInfo.m_ptEnd); LineSegInfo.m_dBugle = 0; LineSegInfo.m_dMinusBugle =0; m_aryLineSegInfo.Add(LineSegInfo); } else if (strEntName == _T("AcDbPolyline")) { AcDbPolyline *pline = (AcDbPolyline *)pEnt; LineSegInfo.m_idLineSeg = idTem; LineSegInfo.m_nLineColor = pline->colorIndex(); LineSegInfo.m_idLineLayer = pline->layerId(); LineSegInfo.m_idLineStyle = pline->linetypeId(); LineSegInfo.m_bPolyine = TRUE; pline->getStartPoint(LineSegInfo.m_ptStart); pline->getEndPoint(LineSegInfo.m_ptEnd); int nNum = pline->numVerts(); pline->getBulgeAt(0,LineSegInfo.m_dBugle); pline->getBulgeAt(nNum-1,LineSegInfo.m_dMinusBugle); int j =0; for (j=1;jgetPointAt(j,pt2d); pline->getBulgeAt(j,dBulge); ptTem = pt2d; ptTem.m_dBudge = dBulge; LineSegInfo.m_ptFitAry.append(ptTem); } if (LineSegInfo.m_ptStart!=LineSegInfo.m_ptEnd) { m_aryLineSegInfo.Add(LineSegInfo); } } else if (strEntName == _T("AcDb2dPolyline")) { //AcDb2dPolyline *p2dline = (AcDb2dPolyline*)pEnt; pEnt->upgradeOpen(); AcDbPolyline *pline = new AcDbPolyline;//pEnt; es = pline->convertFrom(pEnt);//调用此函数后 pent自动释放 if (es == Acad::eOk) { LineSegInfo.m_idLineSeg = idTem; LineSegInfo.m_nLineColor = pline->colorIndex(); LineSegInfo.m_idLineLayer = pline->layerId(); LineSegInfo.m_idLineStyle = pline->linetypeId(); LineSegInfo.m_bPolyine = TRUE; pline->getStartPoint(LineSegInfo.m_ptStart); pline->getEndPoint(LineSegInfo.m_ptEnd); int nNum = pline->numVerts(); pline->getBulgeAt(0,LineSegInfo.m_dBugle); pline->getBulgeAt(nNum-1,LineSegInfo.m_dMinusBugle); int j =0; for (j=1;jgetPointAt(j,pt2d); pline->getBulgeAt(j,dBulge); ptTem = pt2d; ptTem.m_dBudge = dBulge; LineSegInfo.m_ptFitAry.append(ptTem); } if (LineSegInfo.m_ptStart!=LineSegInfo.m_ptEnd) { m_aryLineSegInfo.Add(LineSegInfo); } pline->close(); continue; } } } pEnt->close(); } } void TLineToPolyLine::FindFitSegs(LineSegAry&sourceSegAry) { while (sourceSegAry.GetSize()!=0) { BOOL bReverse = FALSE; BOOL bEndPt = FALSE; TLineSegInfo LineSeg = sourceSegAry.GetAt(0); sourceSegAry.RemoveAt(0); TBudgePoint3d ptPre = LineSeg.m_ptStart; TBudgePoint3d ptNext = LineSeg.m_ptEnd; AcArray ptAryPre,ptAryNext; if (LineSeg.m_bArc) { ptPre.m_dBudge = GetBulge(LineSeg,TRUE); } else { ptPre.m_dBudge = LineSeg.m_dBugle; } ptAryPre.append(ptPre); BOOL bFindNext = FALSE; FindSeg(ptPre,sourceSegAry,ptAryPre,bEndPt,bReverse,TRUE,bFindNext); ptNext.m_dBudge = LineSeg.m_dMinusBugle; if (LineSeg.m_ptFitAry.length()!=0) { ptAryNext.append(LineSeg.m_ptFitAry); } ptAryNext.append(ptNext); bReverse = FALSE; bEndPt = TRUE; bFindNext = FALSE; FindSeg(ptNext,sourceSegAry,ptAryNext,bEndPt,bReverse,FALSE,bFindNext); if (ptAryNext.length()==1) { ptAryNext[0].m_dBudge = 0; } else if (ptAryNext.length()>1) { if (bFindNext) { int i =0; for (i=0;i &aryLineSegInfo ,AcArray &ptAry,BOOL &bEndPt, BOOL &bReverse,BOOL bPre,BOOL &bFindNext) { //static BOOL bFindNext = FALSE; BOOL bFind = FALSE; int i =0; for (i =0;igetBlockTable(pTable,AcDb::kForRead); if (es == Acad::eOk) { es = pTable->getAt(ACDB_MODEL_SPACE,pTblRecd,AcDb::kForWrite); if (es == Acad::eOk) { es = pTblRecd->appendAcDbEntity(idTem,pEnt); } pTable->close(); pTblRecd->close(); } } return idTem; } double TLineToPolyLine::GetBulge(const TLineSegInfo &LineSeg, bool SEbo) { AcDbEntity *pEnt = NULL; Acad::ErrorStatus es; es = acdbOpenObject(pEnt,LineSeg.m_idLineSeg,AcDb::kForRead); if (es != Acad::eOk) { return 0; } CString strName; strName = pEnt->isA()->name(); if (strName != _T("AcDbArc")) { pEnt->close(); return 0; } AcDbArc *pArc = (AcDbArc*)pEnt; double dBulge = 0; AcGePoint3d pts,pte; pArc->getStartPoint(pts); pArc->getEndPoint(pte); double dDis = pts.distanceTo(pte); double dAngle = pArc->endAngle()-pArc->startAngle(); if (!GreatEqual(dAngle,0)) { dAngle = PI*2 + dAngle; } if (GreatEqual(dAngle,PI)) { double dR = pArc->radius(); if (!LessEqual(dR - dDis/2,0)) { dBulge = pArc->radius()+sqrt(pow(pArc->radius(),2)-pow(dDis/2,2)); } else { dBulge = pArc->radius(); } } else { double dR = pArc->radius(); if (!LessEqual(dR - dDis/2,0)) { dBulge = pArc->radius()-sqrt(pow(pArc->radius(),2)-pow(dDis/2,2)); } else { dBulge = pArc->radius(); } } double dDisa(0),dParam(0); pArc->getEndParam(dParam); pArc->getDistAtParam(dParam,dDisa); AcGePoint3d ptTem ; pArc->getPointAtDist(dDisa/2,ptTem); double dTem = pArc->radius(); //取中点 double TempDouble = 0; if (SEbo == true) { //判断方向 TempDouble = PtSide(pts, ptTem, pte); } else { TempDouble = PtSide(pte, ptTem, pts); } if (TempDouble > 0) { //判断圆弧是凸向哪边 pEnt->close(); return -dBulge / (dDis / 2); } else { pEnt->close(); return dBulge / (dDis / 2); } } double TLineToPolyLine::PtSide(AcGePoint3d pt1, AcGePoint3d pt2, AcGePoint3d pt3) { AcGeVector3d vect1 = pt2 - pt1; AcGeVector3d vect2 = pt3 - pt1; return vect2.x*vect1.y - vect1.x*vect2.y; } void TLineToPolyLine::ReverseLineSeg(TBudgePoint3dArry&ptFitAry) { int i = 0; for (i = 0;i