Files
envi-code/SourceCode/Code2026/ThunderProof/ThunderPolyLineJig.cpp
T
2026-09-28 15:28:50 +08:00

415 lines
9.4 KiB
C++

#include "stdafx.h"
#include "ThunderPolyLineJig.h"
#include "ThunderXData.h"
#include "ktsetting.h"
#define SUB_ENT_HDL _T("Sub_Ents")
#define SUB_ENT_HDL2 _T("Sub_Ents2")
TelThunderPolyLineJig::TelThunderPolyLineJig()
{
m_pCircle = NULL;
m_pArc = NULL;
m_dHeight = 0.0;
m_pPLine = (AcDbPolyline*)AcDbPolyline::desc()->create();
m_pPLine->setDatabaseDefaults();
KTSetting::InitLayer(LAY_EL_THUNDER_DIM);
m_pPLine->setLayer(LAY_EL_THUNDER_DIM);
}
TelThunderPolyLineJig::~TelThunderPolyLineJig()
{
}
void TelThunderPolyLineJig::start()
{
TCHAR messBuf[80];
_stprintf( messBuf, _T("\n 选择标注的方向: "), m_ptEnd);
setDispPrompt( messBuf );
AcEdJig::DragStatus stat = drag();
if ( kNormal == stat )
{
CThunderXData data;
CStringArray arData;
DeleteOldDim();
DrawText();
append();
//添加保护高度.
CString strHigth;
strHigth.Format(_T("%.2f"),m_dHeight);
arData.Add(_T("FORDEL"));
arData.Add(strHigth);
data.SetBaseInformationExData(m_pPLine->objectId(),arData,TEL_THUNDER_LABLE);
}
else
{
delete m_pPLine;
}
}
AcDbEntity* TelThunderPolyLineJig::entity() const
{
return m_pPLine;
}
AcEdJig::DragStatus TelThunderPolyLineJig::sampler()
{
setUserInputControls( ( UserInputControls )
( kNullResponseAccepted | kAccept3dCoordinates |
kGovernedByOrthoMode | kDontUpdateLastPoint ));
AcGePoint3d ptTemp,ptClosed;
DragStatus stat = acquirePoint(ptTemp);
if (m_pCircle != NULL)
{
m_pCircle->getClosestPointTo(ptTemp, ptClosed);
}
else
{
m_pArc->getClosestPointTo(ptTemp,ptClosed);
}
ptClosed.z = m_ptEnd.z;
if (ptClosed.distanceTo(m_ptEnd) < 1e-3)
{
return kNoChange;
}
m_ptEnd = ptClosed;
return stat;
}
Adesk::Boolean TelThunderPolyLineJig::update()
{
if (m_pPLine != NULL)
{
m_pPLine->reset(Adesk::kFalse,0);
double dDis = 0.0;
AcGePoint3d ptFirst(m_ptFirst.x,m_ptFirst.y,0);
AcGePoint3d ptEnd(m_ptEnd.x,m_ptEnd.y,0);
AcGePoint3d ptCen;
AcDbLine line(ptFirst,ptEnd);
line.getDistAtPoint(ptEnd,dDis);
line.getPointAtDist(dDis*17/20,ptCen);
m_pPLine->addVertexAt(0,m_ptFirst.convert2d(AcGePlane::kXYPlane),0);
m_pPLine->addVertexAt(1,ptCen.convert2d(AcGePlane::kXYPlane),0,dDis*17/20/17,0);
m_pPLine->addVertexAt(2,m_ptEnd.convert2d(AcGePlane::kXYPlane),0);
}
return Adesk::kTrue;
}
void TelThunderPolyLineJig::SetPLinePoints(const AcGePoint3d& ptFirst,const AcGePoint3d& ptEnd)
{
m_ptFirst = ptFirst;
m_ptEnd = ptEnd;
}
void TelThunderPolyLineJig::SetEntity(AcDbCircle *pCircle)
{
m_pCircle = pCircle;
}
void TelThunderPolyLineJig::SetEntity(AcDbArc *pArc)
{
m_pArc = pArc;
}
bool TelThunderPolyLineJig::DocGetMeterUnitDraw()
{
struct resbuf rb;
acedGetVar(_T("INSUNITS"),&rb);
if (rb.resval.rint == AcDb::kUnitsMeters)
{
return true;
}
return false;
}
//画文字
void TelThunderPolyLineJig::DrawText()
{
//得到初始配置信息
int nProRadius = 1,nSignalHeight = 1,nColorIndex = 256;
CString sTextHeight = _T("3");
CRegProperty regProp(GetRegLocation(), TEL_OBJECTKEY);
regProp.SetTopRootKey(HKEY_CURRENT_USER);
CThunderXData data;
AcDbObjectId id;
CString sGroup = _T(""), sType = _T("");
if (m_pCircle != NULL)
{
data.GetXData((AcDbEntity*)m_pCircle, KEY_FOLD_PRO_GROUP, sGroup);
data.GetXData((AcDbEntity*)m_pCircle, KEY_FOLD_THUNDER_ID, id);
data.GetXData(id, TEL_THUNDER_TYPE, sType);
}
else if (m_pArc != NULL)
{
data.GetXData((AcDbEntity*)m_pArc, KEY_FOLD_PRO_GROUP, sGroup);
}
BOOL bEmpty2 = sGroup.IsEmpty();
BOOL bEmpty1 = sType.IsEmpty();
if (!bEmpty1 || !bEmpty2)
{
regProp.GetValue(KEY_FOLD_HORPRO_RADIU,nProRadius);
regProp.GetValue(KEY_FOLD_SINGLEPRO_HEIGHT,nSignalHeight);
regProp.GetValue(KEY_FOLD_FONTLABLE_HEIGHT,sTextHeight);
regProp.GetValue(KEY_FOLD_FONTLABLE_COLOR,nColorIndex);
}
else
{
regProp.GetValue(KEY_HORPRO_RADIU,nProRadius);
regProp.GetValue(KEY_SINGLEPRO_HEIGHT,nSignalHeight);
regProp.GetValue(KEY_FONTLABLE_HEIGHT,sTextHeight);
regProp.GetValue(KEY_FONTLABLE_COLOR,nColorIndex);
}
CString sTextNameR,sTextNameH;
double dDis = m_ptEnd.distanceTo(m_ptFirst);
if (!DocGetMeterUnitDraw())
dDis /= 1000;
// 2012/3/21 for 支持山地坡地计算
int nCheck = -1;
CRegProperty reg(GetRegLocation(), _T("Thunder"));
reg.SetTopRootKey(HKEY_CURRENT_USER);
if (reg.GetValue(KEY_FOLD_HILL_HILLSIDE, nCheck))
{
if (nCheck)
{
dDis *= 0.75;
}
}
sTextNameR.Format(_T("rx = %.2f m"),dDis);
sTextNameH.Format(_T("hx = %.2f m"),m_dHeight);
AcGePoint3d ptFirst(m_ptFirst.x,m_ptFirst.y,0);
AcGePoint3d ptEnd(m_ptEnd.x,m_ptEnd.y,0);
AcGePoint3d ptPositionR,ptPositionH;
AcGeVector3d vec = ptEnd - ptFirst;
AcGeVector3d vecH = vec/2;
AcGeVector3d vecV = vec.perpVector().normalize();
vecV *= DocGetPScale()*(_tstof(sTextHeight)/2 + 1);
double dAng = vec.angleTo(AcGeVector3d::kXAxis);
if (vec.angleTo(AcGeVector3d::kYAxis) > PI/2)
dAng = 2*PI - dAng;
if (vec.angleTo(AcGeVector3d::kXAxis) > PI/2)
dAng += PI;
AcDbObjectId idStyle = KTSetting::InitTextStyle(EL_DIM);
ptPositionR = ptFirst + vecH;
ptPositionR += vecV;
AcDbObjectId idLay = KTSetting::InitLayer(EL_TEXT);
AcDbObjectId idR,idH;
if (nProRadius)
{
AcDbText* pTextR = new AcDbText(Ucs2Wcs(ptPositionR),sTextNameR,idStyle,_tstof(sTextHeight)*DocGetPScale(),dAng);
pTextR->setHorizontalMode(AcDb::kTextMid);
pTextR->setAlignmentPoint(ptPositionR);
pTextR->setLayer(idLay);
pTextR->setColorIndex(nColorIndex);
idR = AddToCurrentSpace(pTextR,Adesk::kTrue);
}
ptPositionH = ptFirst + vecH;
ptPositionH -= vecV;
if (nSignalHeight)
{
AcDbText* pTextH = new AcDbText(Ucs2Wcs(ptPositionH),sTextNameH,idStyle,_tstof(sTextHeight)*DocGetPScale(),dAng);
pTextH->setHorizontalMode(AcDb::kTextMid);
pTextH->setAlignmentPoint(ptPositionH);
pTextH->setLayer(idLay);
pTextH->setColorIndex(nColorIndex);
idH = AddToCurrentSpace(pTextH,Adesk::kTrue);
}
CString sHdl = ConvertIDToHandleString(idH);
data.SetXData(m_pPLine,SUB_ENT_HDL,sHdl);
sHdl = ConvertIDToHandleString(idR);
data.SetXData(m_pPLine,SUB_ENT_HDL2,sHdl);
// CStringArray arData;
// //添加保护高度.
// CString strHigth;
// strHigth.Format(_T("%.2f"),m_dHeight);
// arData.Add(_T("FORDEL"));
// arData.Add(strHigth);
// data.SetBaseInformationExData(idR,arData,TEL_THUNDER_LABLE);
// data.SetBaseInformationExData(idH,arData,TEL_THUNDER_LABLE);
}
//删除以前的标注
void TelThunderPolyLineJig::DeleteOldDim()
{
if (m_pCircle == NULL && m_pArc == NULL)
return;
XPoint pt;
if (m_pCircle)
{
pt = m_pCircle->center();
}
else
{
pt = m_pArc->center();
}
//double dRadius = m_pCircle->radius();
double dRadius = m_ptEnd.distanceTo(pt);
AcDbPolyline *pPLine = new AcDbPolyline;
pt.z = 0;
XPoint ptLeft = pt;
XPoint ptRight = pt;
ptLeft.x -= dRadius;
ptRight.x += dRadius;
AcGeVector3d vt1(ptLeft-pt);
AcGeVector3d vt2(ptRight-pt);
double dAngle1 = vt1.angleTo(vt2);
double dBulge1 = tan(0.25 * dAngle1);
double dAngle2 = vt2.angleTo(vt1);
double dBulge2 = tan(0.25 * dAngle2);
pPLine->addVertexAt(0,ptLeft,dBulge1);
pPLine->addVertexAt(1,ptRight,dBulge2);
pPLine->addVertexAt(2,ptLeft,dBulge1);
PickSet ss(SS_FREE);
RBList rbMask(RTDXF0, _T("TEXT,LWPOLYLINE, CIRCLE"), NULL);
XPoly2D poly;
PolyConvert(*pPLine, poly);
delete pPLine;
XPoint ptLow,ptUp;
poly.GetExtend(ptLow,ptUp);
poly.TransUCS(Groups(0), Groups(1));//Groups(0) wcs, Groups(1) ucs
RBList rbFence = poly.Fence();
AssureInCurrentView(ptLow, ptUp);
ss.Get(_T("CP"), rbFence, NULL, rbMask);
AcDbObjectId id;
CStringArray arData;
AcGePoint3d ptOn;
AcDbObjectId idThunder;
long iLen = ss.Length();
CThunderXData data;
CString sGroup = _T("");
for (long i = 0; i < iLen; ++i)
{
OPENOBJ_BEGIN(ss[i],AcDb::kForWrite,AcDbPolyline,pLine);
if (pLine != NULL)
{
sGroup.Empty();
data.GetXData((AcDbEntity*)pLine, KEY_FOLD_PRO_GROUP, sGroup);
if (sGroup.IsEmpty())
{
//如果不是折线法保护线
CString sHdl;
data.GetXData(pLine,SUB_ENT_HDL,sHdl);
AcDbObjectId idText = ConvertHStringToID(sHdl);
data.GetXData(pLine,SUB_ENT_HDL2,sHdl);
AcDbObjectId idText2 = ConvertHStringToID(sHdl);
AcGePoint3d ptPort;
UINT nIndex = pLine->numVerts();
pLine->getPointAt(nIndex-1,ptPort);
if (m_pArc != NULL)
{
m_pArc->getClosestPointTo(ptPort, ptOn);
ptOn.z = ptPort.z;
if (ptPort.distanceTo(ptOn) < 1e-3)
{
DelObj(idText);
DelObj(idText2);
pLine->erase();
}
}
else if (m_pCircle != NULL)
{
m_pCircle->getClosestPointTo(ptPort, ptOn);
ptOn.z = ptPort.z;
if (ptPort.distanceTo(ptOn) < 1e-3)
{
DelObj(idText);
DelObj(idText2);
pLine->erase();
}
}
}
OPENOBJ_END();
}
}
}
void TelThunderPolyLineJig::DelObj(const AcDbObjectId& id)
{
if (id.isValid())
{
OPENOBJ_BEGIN(id,AcDb::kForWrite,AcDbEntity,pEnt);
if (pEnt != NULL)
{
pEnt->erase();
}
OPENOBJ_END();
}
}
void TelThunderPolyLineJig::SetHeight(double dHeight)
{
m_dHeight = dHeight;
}
CString TelThunderPolyLineJig::ConvertIDToHandleString(const AcDbObjectId& id)
{
CString sHdl = _T("");
if (id.isValid())
{
TCHAR szHandl[50];
AcDbHandle handl = id.handle();
handl.getIntoAsciiBuffer(szHandl);
sHdl = szHandl;
}
return sHdl;
}
AcDbObjectId TelThunderPolyLineJig::ConvertHStringToID(LPCTSTR pszHdl)
{
AcDbObjectId id = AcDbObjectId::kNull;
if (pszHdl != NULL)
{
AcDbHandle hadl(pszHdl);
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
pDb->AcDbDatabase::getAcDbObjectId(id,true,hadl);
}
return id;
}