#include "StdAfx.h" #include "StdArx.h" #include "UIITable.h" #include "KTCadUtility.h" #include #include "KTCADUtilityEx.h" #include "AecDbShEarthWorm.h" extern int DLLIMPEXP acedEWEntSel(LPCTSTR pszPrompt,AcDbObjectIdArray& arIDs,bool bOnLine = false); int DLLIMPEXP GetConnectedWorms(const AcDbObjectId& idHit,AcDbObjectIdArray& arIds); static Acad::ErrorStatus AddToModelSpace(AcDbEntity* pEnt,AcDbDatabase* pDb = NULL,bool bClose = true) { if (pDb == NULL) pDb = acdbHostApplicationServices()->workingDatabase(); Acad::ErrorStatus es; AcDbBlockTable* pTbl = NULL; if ( Acad::eOk == (es = pDb->getBlockTable(pTbl,AcDb::kForRead) ) ) { AcDbBlockTableRecord* pBTR = NULL; if (Acad::eOk == (es = pTbl->getAt(ACDB_MODEL_SPACE,pBTR,AcDb::kForWrite) ) ) { es = pBTR->appendAcDbEntity(pEnt); if (bClose) pEnt->close(); pBTR->close(); } pTbl->close(); } return es; } typedef struct PtWithParam { AcGePoint3d pt; double dParam; }PTWITHPARAM; static bool MySort(PtWithParam& fr,PtWithParam& nx) { if (fr.dParam - nx.dParam < 1.e-3) return true;//up sort return false; } static bool MySortByX(AcGePoint3d& pt1,AcGePoint3d& pt2) { if (pt1.x - pt2.x < 1.e-3) return true;//up sort return false; } static void SortPointsByX(AcGePoint3dArray& arPts) { std::vector arVtPts; AcGePoint3d ptT; for (int i = 0;i < arPts.length();i++) { ptT = arPts.at(i); arVtPts.push_back(ptT); } std::sort(arVtPts.begin(),arVtPts.end(),MySortByX); arPts.removeAll(); for(int k = 0;k < arVtPts.size();k++) { arPts.append(arVtPts.at(k)); } } static void SortPoints(AcGePoint3dArray& arPts,const AcGeVector2d& vtWorm,const AcGePoint2d& ptFr,bool bCloseToPlan) { //校正到管廊面上 AcGeVector3d vtNorm(vtWorm.x,vtWorm.y,0.0); vtNorm.rotateBy(PI / 2.0,AcGeVector3d::kZAxis); AcGePlane plan(AcGePoint3d(ptFr.x,ptFr.y,0.0),vtNorm); std::vector arPtPms; AcDbXline xline; xline.setBasePoint(AcGePoint3d(ptFr.x,ptFr.y,0.0)); xline.setUnitDir(AcGeVector3d(vtWorm.x,vtWorm.y,0.0)); PtWithParam pm; AcGePoint3d ptGe,ptT; for (int i = 0;i < arPts.length();i++) { ptT = arPts.at(i); ptT = plan.closestPointTo(ptT); xline.getClosestPointTo(ptT,ptGe); xline.getParamAtPoint(ptGe,pm.dParam); pm.pt = ptT; arPtPms.push_back(pm); } std::sort(arPtPms.begin(),arPtPms.end(),MySort); arPts.removeAll(); for(int k = 0;k < arPtPms.size();k++) { arPts.append(arPtPms.at(k).pt); } } static bool GetDefaultTextStyleInfo(AcGePoint3d ptGeTxt0,AcGePoint3d ptGeTxt1,AcDbObjectId& idStyle,AcDbObjectId& idLayer,CString& sStyle,double& dTxtH,double& dTblH,double& dTblW) { dTxtH = 2.5; idStyle = acdbCurDwg()->textstyle(); idLayer = acdbCurDwg()->clayer(); struct resbuf* pRb = acutBuildList(RTDXF0, _T("TEXT"), 0); ads_name ss,ent; acedSSGet(_T("C"),asDblArray(ptGeTxt0),asDblArray(ptGeTxt1),pRb,ss);//在绘制过程中 ssget CAD 某个版本可能崩溃 acutRelRb(pRb); Adesk::Int32 lLen = 0L; acedSSLength(ss,&lLen); //acutPrintf( _T("\nText count = %d"),lLen); if (lLen > 0) { acedSSName(ss,0,ent); AcDbObjectId id; acdbGetObjectId(id,ent); AcDbEntity* pEnt = NULL; if (Acad::eOk == acdbOpenAcDbEntity(pEnt,id,AcDb::kForRead)) { AcDbText* pText = AcDbText::cast(pEnt); if (pText != NULL) { dTxtH = pText->height(); idStyle = pText->textStyle(); idLayer = pText->layerId(); } pEnt->close(); } } acedSSFree(ss); AcGiTextStyle giStyle; fromAcDbTextStyle(giStyle,idStyle); sStyle = giStyle.styleName(); return true; } static bool acdbPrintf(LPCTSTR pszText) { if (pszText == NULL) return false; ads_point pt; int nRes = acedGetPoint(NULL,_T("\n请选择屏幕一点放置文本:"),pt); if (nRes == RTNORM) { //先获取管廊附近的文字样式 //获取绘制表格的文字高度 图层 样式 //字体大小取 2.5 的倍数 //获取一个桩号 double dTxtH = 2.5; double dTblH = 2.5; double dTblW = 12.5; CString sStyle = _T("Standard"); AcDbObjectId idStyle,idLayer; AcGePoint3d ptGeTxtp[2];//ucs struct resbuf screensize, viewsize; ads_getvar(_T("SCREENSIZE"), &screensize); ads_getvar(_T("VIEWSIZE"), &viewsize); double dH = 0.0,dW = 0.0; dH = viewsize.resval.rreal; dW = screensize.resval.rpoint[X] / screensize.resval.rpoint[Y]*dH; ptGeTxtp[0] = asPnt3d(pt); ptGeTxtp[1] = asPnt3d(pt); ptGeTxtp[0].x -= 0.5 * dW; ptGeTxtp[0].y -= 0.5 * dH; ptGeTxtp[1].x += 0.5 * dW; ptGeTxtp[1].y += 0.5 * dH; GetDefaultTextStyleInfo(ptGeTxtp[0],ptGeTxtp[1],idStyle,idLayer,sStyle,dTxtH,dTblH,dTblW); double dAngle = AcGeVector3d::kXAxis.angleTo(acdbCurDwg()->ucsxdir(),AcGeVector3d::kZAxis); AcDbText* pText = new AcDbText; ads_point ptWcs; acdbUcs2Wcs(pt,ptWcs,false); pText->setPosition(asPnt3d(ptWcs)); pText->setTextStyle(idStyle); pText->setTextString(pszText); pText->setHeight(dTxtH); pText->setWidthFactor(0.75); pText->setRotation(dAngle); pText->setLayer(idLayer); AddToModelSpace(pText); return true; } return false; } //管廊混泥土体积 void EW_GLTJCmdFnc() { TCHAR sbuf1[20]; struct resbuf eb1; eb1.restype = 0; _tcscpy(sbuf1, _T("AEC_EARTHWORM") ); eb1.resval.rstring = sbuf1; eb1.rbnext = NULL; ads_name ss1,ent; LPCTSTR pszMsg[] = {_T("\n\n请选择多端管廊实体:"), _T("")}; int nRes = acedSSGet(_T(":$:S"), pszMsg, NULL, &eb1, ss1); if (nRes == RTNORM) { #if ARX <= 20 long lLen = 0L; #else Adesk::Int32 lLen = 0L; #endif AcDbObjectId id; acedSSLength(ss1,&lLen); double dVol = 0.0; AcDbEntity* pEnt = NULL; for (int i = 0;i < lLen;i++) { acedSSName(ss1,i,ent); acdbGetObjectId(id,ent); if ( Acad::eOk == acdbOpenAcDbEntity(pEnt,id,AcDb::kForWrite)) { AecDbEarthWorm* pWorm = AecDbEarthWorm::cast(pEnt); if (pWorm != NULL) { dVol += pWorm->GetVolume(); } AecDbShEarthWorm* pShWorm = AecDbShEarthWorm::cast(pEnt); if (pShWorm != NULL) { pShWorm->GetVolume(); } pEnt->close(); } } acedSSFree(ss1); CString sText; sText.Format(_T("\n管廊混泥土体积 : %g 方"),dVol); //acutPrintf(sText); acdbPrintf(sText); } } //总挖土方 void EW_ZWTFCmdFnc() { //1.选择管廊 //2.选择道路中心线 //3.选择图框 //4.出图到本地 ads_name ent; ads_point pt; AcGeVector2d vtWorm; AcGePoint2d ptFrom; //1.选择管廊 int nRes = acedEntSel(_T("\n请选择管廊:"),ent,pt); if (nRes == RTNORM) { ads_point pt2; acdbUcs2Wcs(pt,pt2,false); AcDbObjectId idEw; acdbGetObjectId(idEw,ent); int nSectinStyle = 0; AcDbCurve* pBaseCurve = NULL; AcDbCurve* pOldRoadCur = NULL; double dVol = 0.0; AcDbEntity* pEnt = NULL; if ( Acad::eOk == acdbOpenAcDbEntity(pEnt,idEw,AcDb::kForRead)) { AecDbEarthWorm* pWorm = AecDbEarthWorm::cast(pEnt); if (pWorm == NULL) { AecDbShEarthWorm* pShWorm = AecDbShEarthWorm::cast(pEnt); if (pShWorm != NULL) { dVol = pShWorm->GetDigVolume(); } else { pEnt->close(); acutPrintf(_T("\n选择的实体不是管廊!")); return; } } else { dVol = pWorm->GetDigVolume(); } pEnt->close(); } CString sText; sText.Format(_T("\n总挖土方: %.3f 方"),dVol); acdbPrintf(sText); } } //填埋土方 void EW_TMTFCmdFnc() { //1.选择管廊 //2.选择道路中心线 //3.选择图框 //4.出图到本地 ads_name ent; ads_point pt; AcGeVector2d vtWorm; AcGePoint2d ptFrom; //1.选择管廊 int nRes = acedEntSel(_T("\n请选择管廊:"),ent,pt); if (nRes == RTNORM) { ads_point pt2; acdbUcs2Wcs(pt,pt2,false); AcDbObjectId idEw; acdbGetObjectId(idEw,ent); int nSectinStyle = 0; AcDbCurve* pBaseCurve = NULL; AcDbEntity* pEnt = NULL; AcDbCurve* pRoadCur = NULL; double dVol = 0.0; if ( Acad::eOk == acdbOpenAcDbEntity(pEnt,idEw,AcDb::kForRead)) { AecDbEarthWorm* pWorm = AecDbEarthWorm::cast(pEnt); if (pWorm == NULL) { AecDbShEarthWorm* pShWorm = AecDbShEarthWorm::cast(pEnt); if (pShWorm != NULL) { //获取管廊方向 dVol = pShWorm->GetFillVolume(); } else { pEnt->close(); acutPrintf(_T("\n选择的实体不是管廊!")); return; } } else { dVol = pWorm->GetFillVolume(); } pEnt->close(); } //acutPrintf(_T("\n填埋土方: %.3f 方"),dVol); CString sText; sText.Format(_T("\n填埋土方: %.3f 方"),dVol); acdbPrintf(sText); } } void EW_WBMJCmdFnc() { ads_name ent; ads_point pt; AcGeVector2d vtWorm; AcGePoint2d ptFrom; //1.选择管廊 int nRes = acedEntSel(_T("\n请选择管廊:"),ent,pt); if (nRes == RTNORM) { ads_point pt2; acdbUcs2Wcs(pt,pt2,false); AcDbObjectId idEw; acdbGetObjectId(idEw,ent); AcDbObjectIdArray arIds; GetConnectedWorms(idEw,arIds); double dArea = 0.0; AcDbEntity* pEnt = NULL; for (int i = 0;i < arIds.length();i++) { if (Acad::eOk == acdbOpenAcDbEntity(pEnt,arIds.at(i),AcDb::kForRead)) { AecDbEarthWorm* pWorm = AecDbEarthWorm::cast(pEnt); if (pWorm != NULL) { dArea += pWorm->GetOutSideArea(); } AecDbShEarthWorm* pShWorm = AecDbShEarthWorm::cast(pEnt); if (pShWorm != NULL) { dArea += pShWorm->GetOutSideArea(); } pEnt->close(); } } //acutPrintf(_T("\n外表面积: %.3f 平方"),dArea); CString sText; sText.Format(_T("\n外表面积: %.3f 平方"),dArea); acdbPrintf(sText); } } void EW_NBMJCmdFnc() { ads_name ent; ads_point pt; AcGeVector2d vtWorm; AcGePoint2d ptFrom; //1.选择管廊 int nRes = acedEntSel(_T("\n请选择管廊:"),ent,pt); if (nRes == RTNORM) { ads_point pt2; acdbUcs2Wcs(pt,pt2,false); AcDbObjectId idEw; acdbGetObjectId(idEw,ent); AcDbObjectIdArray arIds; GetConnectedWorms(idEw,arIds); double dArea = 0.0; AcDbEntity* pEnt = NULL; for (int i = 0;i < arIds.length();i++) { if (Acad::eOk == acdbOpenAcDbEntity(pEnt,arIds.at(i),AcDb::kForRead)) { AecDbEarthWorm* pWorm = AecDbEarthWorm::cast(pEnt); if (pWorm != NULL) { dArea += pWorm->GetInSideArea(); } AecDbShEarthWorm* pShWorm = AecDbShEarthWorm::cast(pEnt); if (pShWorm != NULL) { dArea += pShWorm->GetInSideArea(); } pEnt->close(); } } //acutPrintf(_T("\n内表面积: %.3f 平方"),dArea); CString sText; sText.Format(_T("\n内表面积: %.3f 平方"),dArea); acdbPrintf(sText); } }