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

374 lines
9.4 KiB
C++

#include "StdAfx.h"
#include "DbMTextNew.h"
void GetLableString(PickSet &ssLine, PickSet &ssLabel, std::vector<std::pair<CString, AcGePoint3d> > &mapLabel)
{
Entity entLine, entLabel;
AcGePoint3d ptStart, ptEnd, ptLabel, ptLabelEnd;
for (long ll = 0l; ll < ssLabel.Length(); ll++)
{
entLabel = ssLabel[ll];
OPENOBJ_BEGIN(entLabel, AcDb::kForRead, TDbAxisLabelSet, pLabel);
if(pLabel)
{
XCurveSegment curSeg;
TDbText text;
CString sNum;
AcDbLine *pLine = NULL;
AcDbArc *pArc = NULL;
// 遍历轴号
for (int i = 0; i < pLabel->GetAxisNum(); i++)
{
curSeg = pLabel->GetAxis(i);
ptLabel = curSeg.GetStartPoint();
ptLabelEnd = curSeg.GetEndPoint();
pLabel->GetText(i, text);
sNum = text.GetText();
if (sNum.Compare(_T("5")) == 0)
{
XCircle c;
c.SetCenterPt(ptLabel);
c.SetRadius(1000);
c.Make();
}
// 获取最近轴线
if (GetClosestAxis(ssLine, ptLabel, entLine));
{
OPENOBJ_BEGIN(entLine, AcDb::kForRead, AcDbEntity, pEnt);
if(pLine = AcDbLine::cast(pEnt))
{
ptStart = pLine->startPoint();
ptEnd = pLine->endPoint();
}
else if (pArc = AcDbArc::cast(pEnt))
{
pArc->getStartPoint(ptStart);
pArc->getEndPoint(ptEnd);
}
else
{
continue;
}
OPENOBJ_END();
if (ptStart.distanceTo(ptLabel) > 0.001 && ptStart.distanceTo(ptLabelEnd) > 0.001
&& ptEnd.distanceTo(ptLabelEnd) > 0.001 && ptEnd.distanceTo(ptLabel) > 0.001)
{
continue;
}
ptStart.z = 0;
ptEnd.z = 0;
mapLabel.push_back(std::make_pair(sNum, ptStart));
mapLabel.push_back(std::make_pair(sNum, ptEnd));
ssLine -= entLine;
}
}
}
OPENOBJ_END();
}
}
void CreatText(const std::vector<std::pair<CString, AcGePoint3d> > &mapLabel)
{
std::vector<std::pair<CString, AcGePoint3d> >::const_iterator it;
AcGePoint3d ptLoc;
TDbText *pText = NULL;
CString sText;
for (it = mapLabel.begin(); it != mapLabel.end(); it++)
{
pText = new TDbText;
if (pText != NULL)
{
pText->SetLocation(it->second);
sText = it->first;
AcDbObjectId idTextStyle=InitTwtTextStyle(g_csSheetStyle);
pText->SetStyleId(idTextStyle);
pText->SetHeight(10);
pText->SetAlignment(XExplodeDraw::kBottomLeft);
pText->SetRotation(Ucs2Wcs(0.0));
pText->setLayer(InitLayer(LAY_WELL_TEXT));
pText->setColorIndex(2);
pText->SetText((const TCHAR*)sText);
AddToCurrentSpace(pText, Adesk::kTrue);
}
}
}
void test1()
{
PickSet ssLine(SS_FREE);
PickSet ssLabel(SS_FREE);
RBList rbMask1 = ads_buildlist( -4, _T("<or"),
-4, _T("<and"), RTDXF0, _T("LINE"), 8, _T("DOTE"), -4, _T("and>"),
-4, _T("<and"), RTDXF0, _T("ARC"), 8, _T("DOTE"), -4, _T("and>"),
-4, _T("or>"), NULL);
RBList rbMask2(RTDXF0, _T("ATS_AXIS_LABEL"), 8, _T("*"), NULL);
if (SelectEntities(_T("\n请选择轴网:"), ssLine, rbMask1) != RTNORM)
{
return;
}
if (SelectEntities(_T("\n请选择轴号:"), ssLabel, rbMask2) != RTNORM)
{
return;
}
if (ssLabel.Length() < 1 || ssLine.Length() < 1)
{
return;
}
std::vector<std::pair<CString, AcGePoint3d> > mapLabel;
GetLableString(ssLine, ssLabel, mapLabel);
CreatText(mapLabel);
}
bool GetPolyById(AcDbObjectId &id, AcDbPolyline *&polyEnt)
{
AcDbBlockReference *pBlkRef = NULL;
AcDbExtents ext;
XPoint ptLow, ptUp;
acdbOpenObject(pBlkRef, id, AcDb::kForWrite);
if (!pBlkRef)
{
return false;
}
double dAngle = pBlkRef->rotation();
pBlkRef->setRotation(Wcs2Ucs(0.0));
pBlkRef->getGeomExtents(ext);
AcGeMatrix3d mat;
mat.setToRotation(dAngle - Wcs2Ucs(0.0), AcGeVector3d::kZAxis, pBlkRef->position());
pBlkRef->transformBy(mat);
pBlkRef->close();
ptUp = ext.maxPoint();
ptLow = ext.minPoint();
ptLow.z = 0;
ptUp.z =0;
XPoint ptMaxLeft(ptLow[0], ptUp[1], 0);
XPoint ptMinRight(ptUp[0], ptLow[1], 0);
polyEnt->addVertexAt(0, ptLow);
polyEnt->addVertexAt(1, ptMinRight);
polyEnt->addVertexAt(2, ptUp);
polyEnt->addVertexAt(3, ptMaxLeft);
polyEnt->setClosed(Adesk::kTrue);
polyEnt->transformBy(mat);
return true;
}
void EarseBlkByPloy(PickSet &ss)
{
// 过滤图块类型 INSERT,AEC_BLOCK_INSERT,ATS_MVINSERT
Entity ent;
TDPtrList<AcDbPolyline> lstPLine;
TDPtrList<AcDbCircle> lstCir;
std::map<AcDbObjectId, AcDbPolyline*> mapEnt;
AcDbPolyline *pLine = NULL;
AcDbCircle *pCir = NULL;
for (long l = 0; l < ss.Length(); l++)
{
ent = ss[l];
// 找出ss中的圆和闭合PLINE
if (ent.Is(_T("LWPOLYLINE")) || ent.Is(_T("CIRCLE")))
{
if (ent.Is(_T("LWPOLYLINE")))
{
XPoly2D poly(ent);
if (!poly.IsClosed())
{
continue;
}
pLine = new AcDbPolyline;
for (int i = 0; i < poly.Length(); i++)
{
XPoint *pt = poly.GetPointListAt(i);
AcGePoint2d pt2d(pt->x, pt->y);
pLine->addVertexAt(i, pt2d);
}
pLine->setClosed(Adesk::kTrue);
lstPLine.Append(pLine);
}
else
{
XCircle cir(ent);
pCir = new AcDbCircle;
pCir->setRadius(cir.GetRadius());
pCir->setCenter(cir.GetCenterPt());
lstCir.Append(pCir);
}
}
else
{
AcDbEntity *pEnt = NULL;
AcDbObjectId id;
AcDbPolyline *polyEnt = new AcDbPolyline;
acdbGetObjectId(id, ent);
if (GetPolyById(id, polyEnt))
{
mapEnt.insert(std::make_pair(id, polyEnt));
}
}
}
// 利用图块外包框当作图块边界
std::map<AcDbObjectId, AcDbPolyline*>::iterator itMap = mapEnt.begin();
AcGePoint3dArray ptArry;
int j = 1;
for (; itMap != mapEnt.end(); itMap++)
{
bool bIsDel = false;
for (int i = 0; i < lstPLine.Length(); i++)//和闭合Pline线比较
{
pLine = NULL;
pLine = lstPLine[i];
ptArry.setLogicalLength(0);
AcDbPolyline *pPoly = (*itMap).second;
Acad::ErrorStatus es = pLine->intersectWith(pPoly, AcDb::kOnBothOperands, ptArry);
if (ptArry.length() < 1 || ptArry.length() > 2)
{
AcGePoint3d ptStart, ptEnd;
(*itMap).second->getPointAt(0, ptStart);
(*itMap).second->getPointAt(2, ptEnd);
AcDbLine line;
line.setStartPoint(ptStart);
line.setEndPoint(ptEnd);
ptArry.setLogicalLength(0);
line.intersectWith(pLine, AcDb::kExtendThis, ptArry);
if (ptArry.length() == 2)
{
AcGeVector3d vec1 = ptStart - ptArry[0];
AcGeVector3d vec2 = ptStart - ptArry[1];
vec1.normalize();
vec2.normalize();
if (vec1 != vec2)
{
ads_name name;
Entity entDel;
acdbGetAdsName(name, itMap->first);
entDel = name;
ss -= entDel;
bIsDel = true;
}
}
}
}
if (bIsDel)
{
continue;
}
for (int j = 0; j < lstCir.Length(); j++)//和圆比较
{
pCir = NULL;
pCir = lstCir[j];
ptArry.setLogicalLength(0);
(*itMap).second->intersectWith(pCir, AcDb::kOnBothOperands, ptArry);
if (ptArry.length() < 1 || ptArry.length() > 2)
{
AcGePoint3d ptStart, ptEnd;
(*itMap).second->getPointAt(0, ptStart);
(*itMap).second->getPointAt(2, ptEnd);
AcDbLine line;
line.setStartPoint(ptStart);
line.setEndPoint(ptEnd);
ptArry.setLogicalLength(0);
line.intersectWith(pCir, AcDb::kExtendThis, ptArry);
if (ptArry.length() == 2)
{
AcGeVector3d vec1 = ptStart - ptArry[0];
AcGeVector3d vec2 = ptStart - ptArry[1];
vec1.normalize();
vec2.normalize();
if (vec1 != vec2)
{
ads_name name;
Entity entDel;
acdbGetAdsName(name, itMap->first);
entDel = name;
ss -= entDel;
}
}
}
}
}
for (itMap = mapEnt.begin(); itMap != mapEnt.end(); itMap++)
{
delete (*itMap).second;
(*itMap).second = NULL;
}
}
void test2()
{
PickSet ss(SS_FREE);
RBList mask=ads_buildlist(RTDXF0, _T("LINE,ARC,CIRCLE,INSERT,AEC_BLOCK_INSERT,ATS_MVINSERT,POLYLINE,LWPOLYLINE"), 8, _T("*TITLE,*BORDER,*图框"), NULL);
if(SelectEntities(_T("\n请选择范围:<整图>"), ss, mask) != RTNORM)
ss.Get(_T("X"), NULL, NULL, mask);
EarseBlkByPloy(ss); // 2012-2-13 郑晓龙增加
}
void test3()
{
TDbMTextNew *pText = new TDbMTextNew;
AcDbObjectId idTextStyle=InitTwtTextStyle(g_csSheetStyle);
pText->SetStyleId(idTextStyle);
pText->SetHeight(0.4);
pText->SetPageWidth(4);
pText->SetLineSpace(1);
pText->SetPageHeight(3, TRUE);
pText->SetPageDist(1, TRUE);
pText->SetPageStyle(1);
pText->SetAlignment(XExplodeDraw::kBottomLeft);
pText->SetRotation(Ucs2Wcs(0.0));
CString sText1 = _T("1.abcdefghigklmn\n2.abcdefghig\n3.std::sort()abcdefghig");
CString sText2 = _T("11.sizeof()是操作符,strlen()是函数;\n12.explicit constructor指明构造函数只能显式使用,目的是为了防止不必要的隐式转化.\n13.将文件间的编译依赖性降至最低;");
pText->SetText(sText2);
pText->setLayer(InitLayer(LAY_WELL_TEXT));
XPoint ptIns;
ads_getpoint(NULL, _T("\nInsert PT"), ptIns);
ptIns = Ucs2Wcs(ptIns);
pText->SetLocation(ptIns);
pText->setColorIndex(1);
AcDbObjectId id = AddToCurrentSpace(pText, Adesk::kTrue);
}
void test()
{
while (true)
{
TCHAR chKey[10];
ads_name ss;
acedInitGet(2, _T("A S D F"));
// ads_point pt;
// int nRet = acedEntSel(_T("\n选择实体或者输入关键字[字符(A)/字符(S)/字符(D)/字符(F)]"), ss, pt);
const TCHAR *szPrompts[2] = { _T("\n选择实体或者输入关键字[字符(A)/字符(S)/字符(D)/字符(F)]"), _T("Remove") };
int nRet = acedSSGet(_T(":$"), szPrompts, NULL, NULL, ss);
if (nRet == RTKWORD)
{
acedGetInput(chKey);
acedPrompt(_T("\nKeyWord!"));
}
else if (nRet == RTCAN)
{
break;
}
Adesk::Int32 nLen = 0;
acedSSLength(ss, &nLen);
acedSSFree(ss);
}
}