Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

1344 lines
32 KiB
C++

#include "stdafx.h"
#include "CKTDimUtility.h"
#include "KTPipeOper.h"
#include "KTFitting.h"
#include "UserHelp.h"
#include "imgdef.h" // AcDbRasterImageDef and reactors,
#include "imgent.h"
int CKTDimUtility::KTPipeDimTxt(CString& sTopDimTxt,CString& sBomDimTxt,AcGePoint3d& pt3d,double dPipeW,AcGeVector3d& vc,double& dpipedn,double& dangel,int& npipe)
{
AcDbObjectId idpipe;
// int npipe1=0;
// AcGePoint3d pt3d1;
int nresult = acedGetPoint(NULL,_T("\n选择插入点:"),asDblArray(pt3d));
if (nresult != RTNORM) return 0;
if ( KTPipeOper::FindPipeCen(pt3d,idpipe,npipe,dPipeW+5)) //是管子dPipeW是线宽,npipe是单双线属性等
{
if (!PIDLineTools::FormatPipeDimTxt(idpipe,sTopDimTxt,sBomDimTxt))
{
acutPrintf(_T("\n请选择管道中心线!"));
return 0;
}
CKTDimUtility::SetELDrawgraphic(sTopDimTxt); //替换标高图形字符
CKTDimUtility::SetELDrawgraphic(sBomDimTxt);
AecDbObjXDataMap xData1;
xData1.Read(idpipe,PIDPIPE_APP);
//2.扩展数据转为pipe参数结构
WPipe::PipeParam param1;
KTFitting::XData2PipeParam(&param1,&xData1, NULL);
AcGePoint3d ptDir;
CString sdn,sValveEL;
int nEL=0;
xData1.GetAt(PIPE_DIR,ptDir);//矢量
xData1.GetAt(PIPE_DN,sdn);//公称直径
sdn.TrimRight(_T('0'));
sdn.TrimRight(_T('.'));
xData1.GetAt(PIPE_ELBASE,nEL);//标高基准
xData1.GetAt(PIPE_EL,sValveEL);//标高
vc = ptDir.asVector();
dpipedn=_ttof(sdn);
// npipe = npipe1;
// pt3d=pt3d1;
CKTDimUtility::GetPipeAngle(dangel,vc);
return 1;
}
else
{
return 0;
}
}
int CKTDimUtility::KTPipeDimTxtEL(CString& sValveEL,int& nEL,AcGePoint3d& pt3d,double dPipeW,AcGeVector3d& vc,double& dpipedn,double& dangel,int& npipe)
{
AcDbObjectId idpipe;
int nresult = acedGetPoint(NULL,_T("\n选择插入点:"),asDblArray(pt3d));
if (nresult != RTNORM) return 0;
if ( KTPipeOper::FindPipeCen(pt3d,idpipe,npipe,dPipeW+5)) //是管子dPipeW是线宽,npipe是单双线属性等
{
AecDbObjXDataMap xData1;
xData1.Read(idpipe,PIDPIPE_APP);
//2.扩展数据转为pipe参数结构
WPipe::PipeParam param1;
KTFitting::XData2PipeParam(&param1,&xData1, NULL);
AcGePoint3d ptDir;
CString sdn;
xData1.GetAt(PIPE_DIR,ptDir);//矢量
xData1.GetAt(PIPE_DN,sdn);//公称直径
sdn.TrimRight(_T('0'));
sdn.TrimRight(_T('.'));
xData1.GetAt(PIPE_ELBASE,nEL);//标高基准
xData1.GetAt(PIPE_EL,sValveEL);//标高
vc = ptDir.asVector();
dpipedn=_ttof(sdn);
CKTDimUtility::GetPipeAngle(dangel,vc);
return 1;
}
else
{
return 0;
}
}
//得到垂足
AcGePoint3d CKTDimUtility::MirrorPt(AcGePoint3d pt1,AcGePoint3d pt2,AcGePoint3d ptUser,AcGeVector3d vector)
{
AcGeVector3d vector1;
vector1 = pt2 - pt1;
AcGePlane plane(pt1,vector1,vector);
return ptUser.mirror(plane);
}
//P3d_to_P2d
AcGePoint2d CKTDimUtility::P3d_to_P2d(AcGePoint3d pt)
{
AcGePoint2d P2d;
for(int i=0;i<2;i++)
{
P2d.operator[](i)=pt.operator[](i);
}
return P2d;
}
/////////////////////////////////////////////////////////
//P2d_to_P3d
AcGePoint3d CKTDimUtility::P2d_to_P3d(AcGePoint2d pt)
{
AcGePoint3d P3d;
for(int i=0;i<2;i++)
{
P3d.operator[](i)=pt.operator[](i);
}
return P3d;
}
void CKTDimUtility::Getinit(AecDbObjXDataMap& xInitDataMap,double& dpipew,double& dtxtH,double& dtxtW,int& nDimType)
{
PIDLineTools::ReSet(xInitDataMap);
if (!KTModelLayoutXData::ReadModelXData(KT_PIPEINIT,xInitDataMap))
{
acedAlert(_T("\n系统未初始化!"));
return ;
}
CString sTrade,sPro,sWeldD,sPipeD,sScale,sPipeW,stxtH,stxtW;
int nELType;
xInitDataMap.GetAt(DOC_TRADE,sTrade);
xInitDataMap.GetAt(DOC_PROJECT,sPro);
xInitDataMap.GetAt(DOC_WELDDIA,sWeldD);
xInitDataMap.GetAt(DOC_PIPEMAXDIA,sPipeD);
xInitDataMap.GetAt(DOC_PLOTWIDTH,sPipeW);
xInitDataMap.GetAt(DOC_DIMTXTH,stxtH);
xInitDataMap.GetAt(DOC_DIMTXTF,stxtW);
xInitDataMap.GetAt(DOC_ELTYPE,nELType);
xInitDataMap.GetAt(DOC_SCALE,sScale);
xInitDataMap.GetAt(DOC_DIMTERM,nDimType);
dpipew=_ttof(sPipeW)*ScaletoDouble(sScale);
dtxtH=_ttof(stxtH)*ScaletoDouble(sScale);
dtxtW=_ttof(stxtW)*dtxtH;
}
AcDbEntity * CKTDimUtility::DrowARC(AcGePoint3d ptHead,AcGePoint3d ptMid,AcGePoint3d ptTail,double dR)
{
AcGeCircArc3d * pArc3d = new AcGeCircArc3d();
AcGeCircArc2d pArc2d(CKTDimUtility::P3d_to_P2d(ptHead),
CKTDimUtility::P3d_to_P2d(ptMid),
CKTDimUtility::P3d_to_P2d(ptTail));
if(pArc2d.isClockWise())
{
pArc3d->set(ptTail,ptMid,ptHead);
}
else
{
pArc3d->set(ptHead,ptMid,ptTail);
}
double dAng =AcGeVector3d::kXAxis.angleTo( (pArc3d->startPoint() - pArc3d->center()),AcGeVector3d::kZAxis);
AcDbArc * pArc = new AcDbArc(pArc3d->center(),pArc3d->radius(),
pArc3d->startAng()+dAng,pArc3d->endAng()+dAng);
delete pArc3d;
return pArc;
}
void CKTDimUtility::MakeTangentLn(AcDbArc* pArc,AcGePoint3d& ptSta,AcGePoint3d& ptMid,AcGePoint3d& ptEnd,AcDbVoidPtrArray& arCurPtrs)
{
//求两端切线的交点
AcGeVector3d vtTan1 = ptSta - pArc->center();
vtTan1.rotateBy(PI_2,AcGeVector3d::kZAxis);
AcGeLine3d ln1(ptSta,vtTan1);
AcGeVector3d vtTan2 = ptEnd - pArc->center();
vtTan2.rotateBy(PI_2,AcGeVector3d::kZAxis);
AcGeLine3d ln2(ptEnd,vtTan2);
AcGePoint3d ptInt;
ln1.intersectWith(ln2,ptInt);
AcDbLine* pLn = new AcDbLine(ptSta,ptInt);
arCurPtrs.append(pLn);
pLn = new AcDbLine(ptInt,ptEnd);
arCurPtrs.append(pLn);
}
void CKTDimUtility::WipeOutArc(AcGePoint3d m_ptBase,AcGePoint3d ptEnd,double m_dD,AcDbVoidPtrArray& arCurPtrs)
{
AcGePoint3d ptCen,ptArc1[3],ptArc2[3],ptArc3[3];
double dArcR = m_dD / 2.0;
AcGeVector3d m_vcDir = m_ptBase - ptEnd;
AcDbObjectId id;
//arc1
AcGeVector3d vect = m_vcDir;
vect.rotateBy(PI / 6.0,AcGeVector3d::kZAxis);
vect.normalize();
vect *= dArcR;
ptCen = m_ptBase + vect;
vect = -m_vcDir;
vect.normalize();
vect *= dArcR;
ptArc1[1] = ptCen + vect;
ptArc1[0] = m_ptBase;
ptArc1[2] = CKTDimUtility::MirrorPt(ptCen,ptArc1[1],ptArc1[0]);
//arc2
vect = m_vcDir;
vect.normalize();
vect *= 100.0;
AcGePoint3d ptTemp = m_ptBase + vect;
ptArc2[2] = m_ptBase;
ptArc2[0] = CKTDimUtility::MirrorPt(m_ptBase,ptTemp,ptArc1[2]);
ptArc2[1] = CKTDimUtility::MirrorPt(m_ptBase,ptTemp,ptArc1[1]);
//arc3
ptArc3[0] = ptArc2[0];
ptArc3[2] = m_ptBase;
ptArc3[1] = CKTDimUtility::MirrorPt(m_ptBase,ptArc2[0],ptArc2[1]);
AcDbEntity* pEnt = CKTDimUtility::DrowARC(ptArc1[0],ptArc1[1],ptArc1[2],m_dD/4.0);
AcDbArc* pArc = AcDbArc::cast(pEnt);
CKTDimUtility::MakeTangentLn(pArc,ptArc1[0],ptArc1[1],ptArc1[2],arCurPtrs);
delete pArc;
pEnt = CKTDimUtility::DrowARC(ptArc2[0],ptArc2[1],ptArc2[2],m_dD/4.0);
pArc = AcDbArc::cast(pEnt);
CKTDimUtility::MakeTangentLn(pArc,ptArc2[0],ptArc2[1],ptArc2[2],arCurPtrs);
delete pArc;
//pEnt = CKTDimUtility::DrowARC(ptArc3[0],ptArc3[1],ptArc3[2],m_dD/4.0);
}
void CKTDimUtility::DrowPBreak(AcGePoint3d m_ptBase,AcGePoint3d ptEnd,double m_dD,bool m_bDrawTail,int nFlag,AcDbObjectIdArray &idGroup)
{
AcGePoint3d ptCen,ptArc1[3],ptArc2[3],ptArc3[3];
double dArcR = m_dD / 2.0;
AcGeVector3d m_vcDir = m_ptBase - ptEnd;
AcDbObjectId id;
//arc1
AcGeVector3d vect = m_vcDir;
vect.rotateBy(PI / 6.0,AcGeVector3d::kZAxis);
vect.normalize();
vect *= dArcR;
ptCen = m_ptBase + vect;
vect = -m_vcDir;
vect.normalize();
vect *= dArcR;
ptArc1[1] = ptCen + vect;
ptArc1[0] = m_ptBase;
ptArc1[2] = CKTDimUtility::MirrorPt(ptCen,ptArc1[1],ptArc1[0]);
//arc2
vect = m_vcDir;
vect.normalize();
vect *= 100.0;
AcGePoint3d ptTemp = m_ptBase + vect;
ptArc2[2] = m_ptBase;
ptArc2[0] = CKTDimUtility::MirrorPt(m_ptBase,ptTemp,ptArc1[2]);
ptArc2[1] = CKTDimUtility::MirrorPt(m_ptBase,ptTemp,ptArc1[1]);
//arc3
ptArc3[0] = ptArc2[0];
ptArc3[2] = m_ptBase;
ptArc3[1] = CKTDimUtility::MirrorPt(m_ptBase,ptArc2[0],ptArc2[1]);
AcDbEntity* pEnt = CKTDimUtility::DrowARC(ptArc1[0],ptArc1[1],ptArc1[2],m_dD/4.0);
CKTDimUtility::addToModelSpace(pEnt,id);
idGroup.append(id);
pEnt = CKTDimUtility::DrowARC(ptArc2[0],ptArc2[1],ptArc2[2],m_dD/4.0);
CKTDimUtility::addToModelSpace(pEnt,id);
idGroup.append(id);
pEnt = CKTDimUtility::DrowARC(ptArc3[0],ptArc3[1],ptArc3[2],m_dD/4.0);
CKTDimUtility::addToModelSpace(pEnt,id);
idGroup.append(id);
//
if(m_bDrawTail == 1)//画尾线
{
AcGePoint3d pt[2];
pt[0] = ptArc1[2];
vect = m_vcDir;
vect.normalize();
vect *= m_dD / 4.0;
pt[1] = pt[0] + vect;
AcDbObjectId idTemp1;
AcDbLine* pline = new AcDbLine(pt[0],pt[1]);
CKTDimUtility::addToModelSpace(pline,id);
idGroup.append(id);
pt[0] = ptArc3[0];
pt[1] = ptArc3[0] + vect;
pline = new AcDbLine(pt[0],pt[1]);
CKTDimUtility::addToModelSpace(pline,id);
idGroup.append(id);
}
if (nFlag == 1)
{
AcDbObjectId idPoint = AcDbObjectId::kNull;
AcDbObjectId idLayer=CKTDimUtility::SetLayer(_T("LAY_DPoint"));
AcDbPoint *pPoint = new AcDbPoint (m_ptBase);
pPoint->setLayer(idLayer);
AecDbObjXDataMap xData1;
xData1.SetAt(_T("点类型"),_T("标注点"));
xData1.SetAt(_T("名称"),_T("管件"));
xData1.Write(pPoint,_T("KTParamPart"));
CKTDimUtility::addToModelSpace(pPoint,idPoint);
idGroup.append(idPoint);
}
}
AcDbObjectId CKTDimUtility::GetTempEntityId(AcRxClass *pClass, const TCHAR *szBlockName, BOOL *bFirstNew)
{
AcDbEntity *pEnt;
AcDbBlockTable *pBlockTable;
AcDbBlockTableRecord *pBlockTableRecord;
acdbCurDwg()->getBlockTable(pBlockTable,AcDb::kForRead);
AcDbObjectId idResult;
if (pBlockTable->has(szBlockName)) //图块已经存在!
{
pBlockTable->getAt(szBlockName,pBlockTableRecord,AcDb::kForRead);
AcDbBlockTableRecordIterator* pBlockIterator;
pBlockTableRecord->newIterator(pBlockIterator,Adesk::kTrue,Adesk::kFalse);
int id = 0;
for (pBlockIterator->start(); !pBlockIterator->done(); pBlockIterator->step())
{
pBlockIterator->getEntity(pEnt,AcDb::kForRead, Adesk::kTrue);
if (pEnt->isKindOf(pClass))
{
idResult = pEnt->objectId();
id = 1;
}
pEnt->close();
if (id)
{
break;
}
}
pBlockTable->close();
pBlockTableRecord->close();
if (id == 0)
{
acdbCurDwg()->getBlockTable(pBlockTable,AcDb::kForWrite);
VERIFY(pBlockTable->getAt(szBlockName,pBlockTableRecord,AcDb::kForWrite) == Acad::eOk);
//构造实体
AcRxObject *pObj = pClass->create();
if (bFirstNew)
{
*bFirstNew = TRUE;
}
pEnt = AcDbEntity::cast(pObj);
VERIFY(pBlockTableRecord->appendAcDbEntity(pEnt) == Acad::eOk);
idResult = pEnt->objectId();
pEnt->close();
pBlockTable->close();
pBlockTableRecord->close();
}
delete pBlockIterator;
}
else
{
pBlockTable->close();
acdbCurDwg()->getBlockTable(pBlockTable,AcDb::kForWrite);
pBlockTableRecord = new AcDbBlockTableRecord;
AcGePoint3d origin(0.0,0.0,0.0);
pBlockTableRecord->setOrigin(origin);
pBlockTableRecord->setName(szBlockName);
//构造实体
AcRxObject *pObj = pClass->create();
if (bFirstNew)
{
*bFirstNew = TRUE;
}
pEnt = AcDbEntity::cast(pObj);
pBlockTable->add(pBlockTableRecord);
pBlockTable->close();
pBlockTableRecord->appendAcDbEntity(idResult, pEnt);
pBlockTableRecord->close();
pEnt->close();
}
return idResult;
}
int CKTDimUtility::AddWipeOut(AcGePoint3d ptFrom,AcGePoint3d ptTo,double dDN,AcDbObjectIdArray& idGroup,BOOL bIsArcStyle)
{
//From,to向中间靠近一下
dDN += 1.4;
AcGePoint3d ptmid = CKTDimUtility::Mid(ptFrom,ptTo);
AcGeVector3d vtT = ptmid - ptFrom;
vtT.normalize();
vtT *= 1.1;
ptFrom += vtT;
vtT = ptmid - ptTo;
vtT.normalize();
vtT *= 1.1;
ptTo += vtT;
AcDbVoidPtrArray arCurPtrs;
AcGePoint3d pt1,pt2;
AcDbLine* pLine = NULL;
if (bIsArcStyle)
{
CKTDimUtility::WipeOutArc(ptFrom,ptmid,dDN,arCurPtrs);
CKTDimUtility::WipeOutArc(ptTo,ptmid,dDN,arCurPtrs);
}
else
{
//矩形遮挡
vtT = ptTo - ptFrom;
vtT.rotateBy(PI_2,AcGeVector3d::kZAxis);
vtT.normalize();
pt1 = ptFrom - vtT * dDN * 0.5;
pt2 = ptFrom + vtT * dDN * 0.5;
pLine = new AcDbLine(pt1,pt2);
arCurPtrs.append(pLine);
pt1 = ptTo - vtT * dDN * 0.5;
pt2 = ptTo + vtT * dDN * 0.5;
pLine = new AcDbLine(pt1,pt2);
arCurPtrs.append(pLine);
}
AcGeVector3d vtDir = ptTo - ptFrom;
vtDir.normalize();
vtDir *= dDN * 0.5;
vtDir.rotateBy(PI_2,AcGeVector3d::kZAxis);
pt1 = ptFrom + vtDir;
pt2 = ptTo + vtDir;
pLine = new AcDbLine(pt1,pt2);
arCurPtrs.append(pLine);
pt1 = ptFrom - vtDir;
pt2 = ptTo - vtDir;
pLine = new AcDbLine(pt1,pt2);
arCurPtrs.append(pLine);
AcDbEntity* pWipeOut = NULL;
AcDbPolyline* pPoly = UserHelp::ChangeToPolyLine(arCurPtrs);
if (pPoly != NULL)
{
pPoly->setClosed(Adesk::kTrue);
AcRxClass* pClass = AcRxClass::cast(acrxClassDictionary->at(_T("AcDbWipeout")));
if (pClass != NULL)
{
BOOL bFirstNew=FALSE;
//建立record, 并加入pClass->create,返回id
AcDbObjectId idEnt = GetTempEntityId(pClass, _T("_$TMP_BLOCK"), &bFirstNew);
AcDbRasterImage* pAcDbRasterImage = NULL;
if (Acad::eOk == acdbOpenAcDbEntity((AcDbEntity*&)pAcDbRasterImage,idEnt,AcDb::kForWrite))
{
if(bFirstNew)
{
pAcDbRasterImage->setDatabaseDefaults();
}
//pAcDbRasterImage->setClipBoundaryToWholeImage();
AcGeMatrix3d matPixelToModel, matModelToPixel;
pAcDbRasterImage->getPixelToModelTransform(matPixelToModel);
matModelToPixel = matPixelToModel.invert();
AcGePoint3d pt;
AcGePoint2dArray ptArray;
for(int i = 0;i < pPoly->numVerts(); i++ )
{
pPoly->getPointAt(i,pt);
pt = pt.transformBy(matModelToPixel);
ptArray.append(AcGePoint2d(pt.x,pt.y));
}
ptArray.append(ptArray.first());
pAcDbRasterImage->setClipBoundary(AcDbRasterImage::kPoly, ptArray);
pAcDbRasterImage->setDisplayOpt(AcDbRasterImage::kTransparent,false);
pWipeOut = (AcDbEntity*)pAcDbRasterImage->clone();
pAcDbRasterImage->close();
}
}
//新建一个不打印的图层 打印不起作用??
//AcDbObjectId idLay = KTSetting::InitLayer(_T("LAY_BREAK"),false);
//pWipeOut->setLayer(idLay);
AcDbObjectId idTmp;
addToModelSpace(pWipeOut,idTmp,true);
idGroup.append(idTmp);
//测试
//AcDbObjectId idPl;
//addToModelSpace(pPoly,idPl,true);
delete pPoly;
}
AcDbCurve* pCurve = NULL;
while(arCurPtrs.length()>0)
{
pCurve = (AcDbCurve*)arCurPtrs.first();
arCurPtrs.removeFirst();
delete pCurve;
}
return 0;
}
void CKTDimUtility::DrowPTemp(AcDbObjectIdArray& idGroup,AcGePoint3d m_ptBase,AcGePoint3d ptEnd,double m_dD,bool m_bDrawTai)
{
AcGePoint3d ptCen,ptArc1[3],ptArc2[3],ptArc3[3];
double dArcR = m_dD / 2.0;
AcGeVector3d m_vcDir = ptEnd - m_ptBase;
AcDbObjectId id;
//arc1
AcGeVector3d vect = m_vcDir;
vect.rotateBy(PI / 6.0,AcGeVector3d::kZAxis);
vect.normalize();
vect *= dArcR;
ptCen = m_ptBase + vect;
vect = -m_vcDir;
vect.normalize();
vect *= dArcR;
ptArc1[1] = ptCen + vect;
ptArc1[0] = m_ptBase;
ptArc1[2] = CKTDimUtility::MirrorPt(ptCen,ptArc1[1],ptArc1[0]);
//arc2
vect = m_vcDir;
vect.normalize();
vect *= 100.0;
AcGePoint3d ptTemp = m_ptBase + vect;
ptArc2[2] = m_ptBase;
ptArc2[0] = CKTDimUtility::MirrorPt(m_ptBase,ptTemp,ptArc1[2]);
ptArc2[1] = CKTDimUtility::MirrorPt(m_ptBase,ptTemp,ptArc1[1]);
//arc3
ptArc3[0] = ptArc2[0];
ptArc3[2] = m_ptBase;
ptArc3[1] = CKTDimUtility::MirrorPt(m_ptBase,ptArc2[0],ptArc2[1]);
AcDbEntity* pEnt = CKTDimUtility::DrowARC(ptArc1[0],ptArc1[1],ptArc1[2],m_dD/4.0);
CKTDimUtility::addToModelSpace(pEnt,id);
idGroup.append(id);
pEnt = CKTDimUtility::DrowARC(ptArc2[0],ptArc2[1],ptArc2[2],m_dD/4.0);
CKTDimUtility::addToModelSpace(pEnt,id);
idGroup.append(id);
pEnt = CKTDimUtility::DrowARC(ptArc3[0],ptArc3[1],ptArc3[2],m_dD/4.0);
CKTDimUtility::addToModelSpace(pEnt,id);
idGroup.append(id);
if (m_bDrawTai)
{
AcGePoint3d pt[2];
pt[0] = ptArc1[2];
vect = m_vcDir;
vect.normalize();
vect *= 2*(m_ptBase.distanceTo(ptEnd));
pt[1] = pt[0] + vect;
AcDbObjectId idTemp1;
AcDbPolyline* pPline = new AcDbPolyline();
pPline->addVertexAt(0,CKTDimUtility::P3d_to_P2d(pt[0]));
pPline->addVertexAt(1,CKTDimUtility::P3d_to_P2d(pt[1]));
pPline->setConstantWidth(0.0);
CKTDimUtility::addToModelSpace(pPline,id);
idGroup.append(id);
pt[0] = ptArc3[0];
pt[1] = ptArc3[0] + vect;
pPline = new AcDbPolyline();
pPline->addVertexAt(0,CKTDimUtility::P3d_to_P2d(pt[0]));
pPline->addVertexAt(1,CKTDimUtility::P3d_to_P2d(pt[1]));
pPline->setConstantWidth(0.0);
CKTDimUtility::addToModelSpace(pPline,id);
idGroup.append(id);
}
}
int CKTDimUtility::GetVector(AcDbObjectId id, AcGeVector3d& V0,double& dAngle,AcGePoint3d pt)
{
V0=AcGeVector3d::kXAxis;
AcGePoint3d pts,pte;
double dp = -1;
AcDbLine *pLine = NULL;
if (acdbOpenObject(pLine,id, AcDb::kForRead)!=Acad::eOk) return 0;
pts=pLine->startPoint();
pte=pLine->endPoint();
pLine->close();
V0= pte - pts;
dAngle=V0.angleTo(AcGeVector3d::kXAxis,-AcGeVector3d::kZAxis);
if (dAngle>PI/2&&dAngle<=PI)
{
dAngle=dAngle - PI;
V0=-V0;
}
else if (dAngle>PI&&dAngle<=3*PI/2)
{
dAngle=dAngle-PI;
V0=-V0;
}
else if (dAngle>3*PI/2&&dAngle<=2*PI)
{
dAngle=dAngle-2*PI;
}
return 1;
}
void CKTDimUtility::SetELDrawgraphic(CString& sText)
{
int nStart=-1;
nStart = sText.Find(_T("^"),0);
if (nStart==-1)
{
return;
}
int nELmode=_ttoi(sText.Mid(nStart + 1,1));
if (nELmode==3) {sText.Replace(_T("^3^"),DIM_EL);}
else if (nELmode==0) {sText.Replace(_T("^0^"),DIM_TOP_EL);}
else if (nELmode==1) {sText.Replace(_T("^1^"),DIM_CEN_EL);}
else if (nELmode==2) {sText.Replace(_T("^2^"),DIM_BOM_EL);}
}
AcGePoint3d CKTDimUtility::Mid(AcGePoint3d pt1,AcGePoint3d pt2)
{
AcGePoint3d pt;
pt.x = (pt1.x + pt2.x) / 2;
pt.y = (pt1.y + pt2.y) / 2;
pt.z = pt1.z;
return pt;
}
Acad::ErrorStatus CKTDimUtility::addToModelSpace(AcDbEntity * pEntity,AcDbObjectId& objectID,bool bClose)
{
AcDbBlockTable * pBlockTable = NULL;
Acad::ErrorStatus eStatus;
eStatus = acdbCurDwg()->getSymbolTable(pBlockTable,AcDb::kForRead);
if(eStatus != Acad::eOk)
{
return eStatus;
}
AcDbBlockTableRecord * pBlockTableRecord = NULL;
eStatus = pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
if(eStatus != Acad::eOk)
{
pBlockTable->close();
return eStatus;
}
pBlockTable->close();
eStatus = pBlockTableRecord->appendAcDbEntity(objectID,pEntity);
if (bClose == true)
{
pEntity->close();
}
pBlockTableRecord->close();
return Acad::eOk;
}
AcDbObjectId CKTDimUtility::SetLayer(CString sLayer)
{
AcDbObjectId idLayer = AcDbObjectId::kNull;
idLayer = GetLayerId(sLayer);
if(idLayer != AcDbObjectId::kNull)
{
AcDbLayerTableRecord * pLayerTableRecord = NULL;
Acad::ErrorStatus es = acdbOpenObject(pLayerTableRecord,idLayer,AcDb::kForRead);//
if(es == Acad::eOk)
{
AcDbObjectId idLt = pLayerTableRecord->linetypeObjectId();
acdbCurDwg()->setClayer(idLayer);
AcCmColor clr = pLayerTableRecord->color();
pLayerTableRecord->close();
}
}
return idLayer;
}
AcDbObjectId CKTDimUtility::GetLayerId(CString sLayer)
{
// AcDbLayerTable * pLayerTable = NULL;
// acdbCurDwg()->getLayerTable(pLayerTable,AcDb::kForRead);
//
// AcDbObjectId recordId;
// pLayerTable->getAt((LPCTSTR)sLayer,recordId);
// pLayerTable->close();
// return recordId;
return KTSetting::InitLayer(sLayer);
}
AcDbObjectId CKTDimUtility::SetLayer(CString sLayer,AcGiWorldDraw* mode)
{
AcDbObjectId idLayer = AcDbObjectId::kNull;
idLayer = GetLayerId(sLayer);
if(idLayer != AcDbObjectId::kNull)
{
AcDbLayerTableRecord * pLayerTableRecord = NULL;
Acad::ErrorStatus es = acdbOpenObject(pLayerTableRecord,idLayer,AcDb::kForRead);//
if(es == Acad::eOk)
{
AcDbObjectId idLt = pLayerTableRecord->linetypeObjectId();
acdbCurDwg()->setClayer(idLayer);
AcCmColor clr = pLayerTableRecord->color();
if(mode != NULL)
{
mode->subEntityTraits().setLayer(idLayer);
mode->subEntityTraits().setColor(clr.colorIndex());
if(idLt != AcDbObjectId::kNull)
{
mode->subEntityTraits().setLineType(idLt);
struct resbuf rb;
acedGetVar(_T("LTSCALE"),&rb);
double dLTScale = rb.resval.rreal;
mode->subEntityTraits().setLineTypeScale(dLTScale);
}
}
pLayerTableRecord->close();
}
}
return idLayer;
}
BOOL CKTDimUtility::CreateGroup(AcDbObjectIdArray& objIds,LPCTSTR pGroupName,AcDbObjectId &GroupID)
{
if (objIds.isEmpty()) return FALSE;
AcDbDictionary *pGroupDict;
AcDbGroup *pGroup;
if (acdbCurDwg()->getGroupDictionary(pGroupDict, AcDb::kForWrite)!=Acad::eOk) return FALSE;
if (pGroupDict->has(pGroupName))//判断组是否有相同的名字
{
acedAlert(_T("组已经存在!请更改组名."));
pGroupDict->close();
return FALSE;
}
else
{
pGroup= new AcDbGroup();
pGroup->append(objIds);
if (pGroupName==_T(""))//匿名组的建立
{
pGroupDict->setAt(_T("tempgroupname"), pGroup, GroupID);
pGroupDict->close();
pGroup->setAnonymous();
}
else
{
pGroupDict->setAt(pGroupName, pGroup, GroupID);
pGroupDict->close();
}
pGroup->close();
return TRUE;
}
}
AcGePoint3d CKTDimUtility::GetIntersectWith(AcGePoint3d pt,AcGePoint3d pt1,AcDbEntity* pEnt)
{
AcGePoint3d ptIntersect;
AcDbPolyline* pGeLine=new AcDbPolyline();
pGeLine->addVertexAt(0,CKTDimUtility::P3d_to_P2d(pt));
pGeLine->addVertexAt(1,CKTDimUtility::P3d_to_P2d(pt1));
AcGePoint3d ptSBreak;
AcGePoint3d ptEBreak;
AcDbPolyline * pPline = AcDbPolyline::cast(pEnt);
if (pPline!=NULL) //如果是PL则管道为单线
{
pPline->getStartPoint(ptSBreak);
pPline->getEndPoint(ptEBreak);
pPline->close();
}
else
{
AcDbLine * pLine = AcDbLine::cast(pEnt);
ptSBreak=pLine->startPoint();
ptEBreak=pLine->endPoint();
pLine->close();
}
pEnt->close();
AcDbPolyline* pGeLine1=new AcDbPolyline();
pGeLine1->addVertexAt(0,CKTDimUtility::P3d_to_P2d(ptSBreak));
pGeLine1->addVertexAt(1,CKTDimUtility::P3d_to_P2d(ptEBreak));
AcGePoint3dArray arPts;
arPts.setLogicalLength (0);
pGeLine->intersectWith(pGeLine1,AcDb::kExtendBoth,arPts);
if (arPts.isEmpty())
{
pGeLine1->close();
pGeLine->close();
return pt1;
}
else
{
ptIntersect = arPts.at(0);
}
pGeLine1->close();
pGeLine->close();
return ptIntersect;
}
void CKTDimUtility::Pipedimside(AcGePoint3dArray& arDimpts,CStringArray& arstexts)
{
CString sTopDimTxt,sBomDimTxt;
ads_point adsPt1,adsPt2;
acedInitGet(RSG_NOZERO,NULL);
CString sTip = _T("\n指定对角点:");
int nResult = acedGetPoint(NULL, sTip, adsPt1);
if(nResult != RTNORM)
{
acutPrintf(_T("\n*取消*"));
return;
}
nResult = acedGetCorner(adsPt1,sTip,adsPt2);
if(nResult != RTNORM)
{
acutPrintf(_T("\n*取消*"));
return;
}
ads_name adsSS,ename;
acedSSGet(_T("C"),adsPt1, adsPt2, NULL, adsSS);
AcDbObjectId idEnt;
CString sName,stext,stextbot,stext1;
Adesk::Int32 lLen = 0;
acedSSLength(adsSS, &lLen);
AcGePoint3dArray ptPipeArray;
ptPipeArray.setLogicalLength(0);
AcGePoint3d ptS,ptE;
for(long lIndex = 0;lIndex < lLen;lIndex ++)
{
acedSSName(adsSS,lIndex,ename);
acdbGetObjectId(idEnt, ename);
if (!idEnt.isValid())
{
acedSSFree(adsSS);
continue;
}
AcDbEntity* pEnt;
if (acdbOpenObject(pEnt,idEnt, AcDb::kForRead)!= Acad::eOk) return;
sBomDimTxt=_T("");
sTopDimTxt=_T("");
//1.读扩展数据
AecDbObjXDataMap xData1;
xData1.Read(pEnt,PIDPIPE_APP);
//2.扩展数据转为pipe参数结构
WPipe::PipeParam param1;
KTFitting::XData2PipeParam(&param1,&xData1, NULL);
//判断是管件或管子类型
int nKind = -1;
xData1.GetAt(PIPE_FITTING,nKind);
KTFitting::Fitting eType = (KTFitting::Fitting)nKind;
if (eType == KTFitting::kPipe) //是管子等
{
if (!PIDLineTools::FormatPipeDimTxt(pEnt,sTopDimTxt,sBomDimTxt))
{
acutPrintf(_T("\n请选择管道中心线!"));
pEnt->close();
return ;
}
AcDbPolyline * pPline = AcDbPolyline::cast(pEnt);
if (pPline!=NULL) //如果是PL则管道为单线
{
pPline->getStartPoint(ptS);
pPline->getEndPoint(ptE);
pPline->close();
}
else
{
AcDbLine * pLine = AcDbLine::cast(pEnt);
ptS=pLine->startPoint();
ptE=pLine->endPoint();
pLine->close();
}
ptPipeArray.append(ptS);
ptPipeArray.append(ptE);
arstexts.Add(sTopDimTxt);
pEnt->close();
continue;
}
else
{
pEnt->close();
continue;
}
}
acedSSFree(adsSS);
//过滤在coner之外的点
AcGePoint3d pt1,pt2;
pt1 = asPnt3d(adsPt1);
pt2 = asPnt3d(adsPt2);
pt1.z = pt2.z = ptS.z;
PointsInCorner(pt1,pt2,ptPipeArray);
arDimpts.setLogicalLength(0);
arDimpts.append(ptPipeArray);
}
void CKTDimUtility::PointsInCorner(AcGePoint3d pt1,AcGePoint3d pt2,AcGePoint3dArray& arDimpts)
{
int nSize = arDimpts.length();
int i = 0;
AcGePoint3d ptTemp;
double dMinx = __min(pt1.x,pt2.x);
double dMaxx = __max(pt1.x,pt2.x);
double dMiny = __min(pt1.y,pt2.y);
double dMaxy = __max(pt1.y,pt2.y);
for(i = 0;i<nSize;i++)
{
ptTemp = arDimpts.at(i);
if( ( ptTemp.x - dMinx > 0.000001 && ptTemp.x - dMaxx < 0.000001) &&
( ptTemp.y - dMiny > 0.000001 && ptTemp.y - dMaxy < 0.000001) )
{
}
else
{
arDimpts.removeAt(i);
nSize -= 1;
i --;
}
}
}
double CKTDimUtility::ScaletoDouble(CString sScale)
{
int nIndex = sScale.Find(':',0);
double dDocScale = 1.0;
if (nIndex != -1)
{
dDocScale = _ttof(sScale.Mid(nIndex + 1)) / _ttof(sScale.Mid(0,nIndex));
}
return dDocScale;
}
int CKTDimUtility::GetPipeAngle(double& dAngle,AcGeVector3d& vc)
{
vc.normalize();
dAngle = vc.angleTo(AcGeVector3d::kXAxis,-AcGeVector3d::kZAxis);
if (dAngle>PI/2&&dAngle<=PI)
{
dAngle=dAngle - PI;
vc=-vc;
}
else if (dAngle>PI&&dAngle<=3*PI/2)
{
dAngle=dAngle-PI;
vc=-vc;
}
else if (dAngle>3*PI/2&&dAngle<=2*PI)
{
dAngle=dAngle-2*PI;
}
return 0;
}
int CKTDimUtility::GetValveDim(AcGePoint3d pt,CString& sValvename,CString& sDn)
{
AcDbObjectId idTemp;
CString sContent;
idTemp=GetObjectId(pt);
AcDbObjectId idGroup = GetGroupId(idTemp);
if (idGroup !=AcDbObjectId::kNull)
{
AcDbGroup* pGroup = NULL;
acdbOpenObject(pGroup,idGroup,AcDb::kForRead);
AcDbGroupIterator* pIter = pGroup->newIterator();
//AcDbObject* pObj;
AecDbObjXDataMap objXData;
for(;!pIter->done();pIter->next())
{
idTemp = pIter->objectId();
if (objXData.Read(idTemp,_T("KTParamPart")))
{
objXData.GetAt(_T("点类型"),sContent);
if (sContent.CompareNoCase(_T("材料点"))==0)
{
objXData.GetAt(_T("名称"),sContent);
if (sContent.CompareNoCase(_T("阀门"))==0)
{
objXData.GetAt(_T("类型"),sValvename);
objXData.GetAt(_T("公称直径"),sDn);
delete pIter;
pGroup->close();
return 1;
}
}
}
}
delete pIter;
pGroup->close();
}
return 0;
}
int CKTDimUtility::GetValveEL(AcGePoint3d pt,CString& sValvename)
{
AcDbObjectId idTemp;
CString sContent;
idTemp=GetObjectId(pt);
AcDbObjectId idGroup = GetGroupId(idTemp);
if (idGroup !=AcDbObjectId::kNull)
{
AcDbGroup* pGroup = NULL;
acdbOpenObject(pGroup,idGroup,AcDb::kForRead);
AcDbGroupIterator* pIter = pGroup->newIterator();
//AcDbObject* pObj;
AecDbObjXDataMap objXData;
for(;!pIter->done();pIter->next())
{
idTemp = pIter->objectId();
if (objXData.Read(idTemp,_T("KTParamPart")))
{
objXData.GetAt(_T("点类型"),sContent);
if (sContent.CompareNoCase(_T("材料点"))==0)
{
CString stext;
objXData.GetAt(_T("厚度"),stext);
sValvename.Format(_T("EL%s"),stext);
delete pIter;
pGroup->close();
return 1;
}
}
}
delete pIter;
pGroup->close();
}
return 0;
}
AcDbObjectId CKTDimUtility::GetGroupId(AcDbObjectId objId)
{
AcDbObjectId persObjId;
AcDbObject *pEnt;
if (Acad::eOk != acdbOpenObject(pEnt, objId, AcDb::kForRead))
{
return AcDbObjectId::kNull;
}
AcDbDictionary *pGroup;
const AcDbVoidPtrArray *pReactors;
pReactors = pEnt->reactors();
if (pReactors == NULL)
{
pEnt->close();
return AcDbObjectId::kNull;
}
pEnt->close();
int length = pReactors->length();
void *pSomething;
for (int i = 0; i < length; ++i)
{
pSomething = (AcDbObject*) pReactors->at(i);
if (acdbIsPersistentReactor(pSomething))
{
persObjId = acdbPersistentReactorObjectId(pSomething);
AcDbObject *pReactor;
if (Acad::eOk != acdbOpenObject(pReactor, persObjId, AcDb::kForRead))
{
continue;
}
if (pReactor->isKindOf(AcDbGroup::desc()))
{
AcDbObjectId groupId = pReactor->ownerId();
if (Acad::eOk != acdbOpenObject(pGroup, groupId, AcDb::kForRead))
{
pReactor->close();
continue;
}
LPTSTR groupName;
if (Acad::eOk != (pGroup->nameAt(persObjId, groupName)))
{
pReactor->close();
pGroup->close();
continue;
}
pGroup->close();
}
pReactor->close();
}
}
return persObjId;
}
AcDbObjectId CKTDimUtility::GetObjectId(AcGePoint3d pt)
{
ads_name ss,entres;
ads_point pt1,pt2;
AcDbObjectId id;
struct resbuf* pResbuf = acutBuildList(RTDXF0,_T("LWPOLYLINE,POINT,CIRCLE,ARC,LINE,ELLIPSE"),NULL);
pt1[X] = pt.x;
pt1[Y] = pt.y;
pt1[Z] = pt.z;
acdbWcs2Ucs(pt1,pt1,false);
ads_point_set(pt1,pt2);
double dAperture = CKTCadUtility::GetApertureSize();
pt1[X] -= dAperture;
pt1[Y] += dAperture;
pt2[X] += dAperture;
pt2[Y] -= dAperture;
actrTransactionManager->flushGraphics();//这样就可以选择刚建立的pline了.
if(acedSSGet(_T("_C"),pt1,pt2,pResbuf,ss)==RTNORM)
{
acedSSName(ss,0,entres);
if (acdbGetObjectId(id,entres)!=Acad::eOk)
{
acedSSFree(ss);
acedSSFree(entres);
return AcDbObjectId::kNull ;
}
acedSSFree(entres);
acedSSFree(ss);
acutRelRb(pResbuf);
return id;
}
acedSSFree(ss);
acutRelRb(pResbuf);
return AcDbObjectId::kNull;
}
BOOL CKTDimUtility::FindPipeCenPt(AcGePoint3d& ptGeIn,AcDbObjectId& idCen,int& npipe,double dPipeW,AcGePoint3d& pts,AcGePoint3d& pte)
{
//该点附近有管线,根据管线确定中线
ads_point pt1,pt2;
pt1[X] = ptGeIn.x;pt1[Y] = ptGeIn.y;pt1[Z] = ptGeIn.z;
pt2[X] = ptGeIn.x;pt2[Y] = ptGeIn.y;pt2[Z] = ptGeIn.z;
//范围为一个普捉框的大小
pt1[X] -= dPipeW/2;pt1[Y] += dPipeW/2;
pt2[X] += dPipeW/2;pt2[Y] -= dPipeW/2;
acdbWcs2Ucs(pt1,pt1,false);
acdbWcs2Ucs(pt2,pt2,false);
// struct resbuf* pRb = NULL;
// pRb = acutBuildList(RTDXF0,_T("LINE,POLYLINE"),NULL);
actrTransactionManager->flushGraphics();//这样就可以选择刚建立的pline了.
ads_name ssname,ent;
acedSSGet(_T("C"),pt1,pt2,NULL,ssname);
Adesk::Int32 lLen = 0;
acedSSLength(ssname,&lLen);
AcDbEntity* pEnt = NULL;
AcDbObjectId id;
AcDbObjectId idCl = KTSetting::InitLayer(_T("LAY_CL"));
for(int i = 0;i < lLen;i++)
{
acedSSName(ssname,i,ent);
if (Acad::eOk == acdbGetObjectId(id,ent))
{
if (Acad::eOk == acdbOpenAcDbEntity(pEnt,id,AcDb::kForRead) )
{
// if (idCl == pEnt->layerId()) //是中心线
// {
//验证扩展数据是管子中心线
AecDbObjXDataMap objXData;
if (objXData.Read(pEnt,PIDPIPE_APP))
{
int nKind = -1;
objXData.GetAt(PIPE_FITTING,nKind);
if (nKind == 0) //是管子中心线
{
AcDbPolyline * pPline = AcDbPolyline::cast(pEnt);
if (pPline!=NULL) //如果是PL则管道为单线
{
npipe = 0;
pPline->getStartPoint(pts);
pPline->getEndPoint(pte);
pPline->getClosestPointTo(ptGeIn,ptGeIn);
pPline->close();
}
else
{
AcDbLine * pLine = AcDbLine::cast(pEnt);
if (pLine!=NULL) //如果是PL则管道为单线
{
npipe = 1;
pts=pLine->startPoint();
pte=pLine->endPoint();
pLine->getClosestPointTo(ptGeIn,ptGeIn);
pLine->close();
}
}
idCen = id;
pEnt->close();
acedSSFree(ssname);
// acutRelRb(pRb);
return TRUE;
}
// }
}
//是管线
pEnt->close();
}
}
}
acedSSFree(ssname);
// acutRelRb(pRb);
return FALSE;
}
BOOL CKTDimUtility::DrawArrow(AcGePoint3d ptBase,AcGePoint3d ptDynamic,int nDimType,AcDbObjectId idLayer,AcDbObjectId &id)
{
if( nDimType == 2 )
{
AcGeVector3d Vtemp;
AcGePoint3d pttemp;
pttemp = ptDynamic;
pttemp.z = ptBase.z;
Vtemp = pttemp - ptBase;
Vtemp.normalize();
AcDbPolyline* pArrow = new AcDbPolyline();
pArrow->addVertexAt(0,CKTDimUtility::P3d_to_P2d(ptBase));
pArrow->addVertexAt(1,CKTDimUtility::P3d_to_P2d(ptBase + Vtemp*60));
pArrow->setLayer(idLayer);
pArrow->setWidthsAt(0,0.0,30);
CKTDimUtility::addToModelSpace(pArrow,id);
return TRUE;
}
return FALSE;
}
int CKTDimUtility::GetDimTextLen(LPCTSTR pszDimText)
{
CString sDimText = pszDimText;
int nLen = sDimText.GetLength();
int nFind = sDimText.Find(_T("+220"),0);
if (nFind > -1)
{
nLen -= 6;
}
return nLen;
}