📄 dxcommands.cpp
字号:
//
// ObjectARX defined commands
#include "StdAfx.h"
#include "StdArx.h"
#include <math.h>
#include "geassign.h"
#define PI 3.14159265
struct DxPt
{
char pcPtName[256];
double fJj;
double fWj;
double dDist;
};
DxPt *pDxPts = NULL;
int nRecords=0;
void GetPt()
{
nRecords = 0;
CFileDialog FileDlg(TRUE,"打开",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"csv");
FileDlg.DoModal();
CString strFileName=FileDlg.GetFileName();
if(strFileName.IsEmpty())
return;
FILE* fPtFile=fopen(strFileName,"r"); //一定要使用二进制打开否则读写会出错
if (fPtFile==NULL)
{
AfxMessageBox("打开导线点数据失败");
return;
}
char strName[256];
float fJj=0.0,fWj=0.0;
int nResult = 1;
CString strTemp="",
strPtName="",
strJJ="",
strWJ="",
strDist="",
strTemp2="";
int nCount = 0;
while(nResult!=EOF)
{
nResult = fscanf(fPtFile,"%s\n",strName);
nCount ++;
}
if (nCount==0)
return;
if (pDxPts!=NULL)
{
delete[] pDxPts;
pDxPts = NULL;
}
pDxPts = new DxPt[nCount];
nCount = 0;
fseek(fPtFile,0,SEEK_SET);
nResult = 1;
while(nResult!=EOF)
{
nResult = fscanf(fPtFile,"%s",strName);
strTemp.Format("%s",strName);
strTemp.TrimLeft();
strTemp.TrimRight();
int nPos = strTemp.Find(",",0);
strPtName = strTemp.Left(nPos+1);
strTemp.Replace(strPtName,"");
strPtName.Replace(",","");
nPos = strTemp.Find(",",0);
strWJ = strTemp.Left(nPos+1);
strTemp.Replace(strWJ,"");
strWJ.Replace(",","");
nPos = strTemp.Find(",",0);
strJJ = strTemp.Left(nPos+1);
strTemp.Replace(strJJ,"");
strJJ.Replace(",","");
strTemp.Replace(",","");
strDist = strTemp;
sprintf(pDxPts[nCount].pcPtName ,strPtName);
pDxPts[nCount].fWj = atof(strWJ);
pDxPts[nCount].fJj = atof(strJJ);
pDxPts[nCount].dDist = atof(strDist);
nCount++;
}
nRecords = nCount;
}
// 向AUTOCAD 数据库中添加实体对象的通用函数
AcDbObjectId AddEntityToDbs(AcDbEntity *pEntity)
{
acDocManager->lockDocument(acDocManager->curDocument(),AcAp::kWrite);
AcDbObjectId objId;
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForRead);
AcDbBlockTableRecord *pSpaceRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pSpaceRecord,AcDb::kForWrite);
pSpaceRecord->appendAcDbEntity(objId, pEntity);
pSpaceRecord->close();
pBlockTable->close();
pEntity->close();
acDocManager->unlockDocument(acDocManager->curDocument());
return objId;
}
AcDbObjectId makeline(AcGePoint3d& startPt,AcGePoint3d& endPt)
{
AcDbLine *pLine = new AcDbLine(startPt,endPt);
AcDbObjectId LId ;
LId=AddEntityToDbs(pLine) ;
return LId ;
}
// 绘文本
AcDbObjectId maketext(AcGePoint3d& pos, ACHAR* ctext,double ang,double texth ,int icolor,int mode,ACHAR * textStyle,char *cLayerName)
{
AcDbObjectId TId;
AcDbText *pText = new AcDbText;
AcDbTextStyleTable *pTextStyleTbl;
AcDbObjectId pTextStyleTblId;
acdbHostApplicationServices()->workingDatabase()->getTextStyleTable(pTextStyleTbl,AcDb::kForWrite);
if(pTextStyleTbl->getAt(textStyle,pTextStyleTblId)!= Acad::eOk)
{
AcDbTextStyleTableRecord *pTextStyleTblRcd=new AcDbTextStyleTableRecord;
pTextStyleTblRcd->setName(textStyle);
ACHAR TextStyleFile[256];
pTextStyleTblRcd->setFileName(TextStyleFile);
pTextStyleTblRcd->setBigFontFileName(TextStyleFile);
pTextStyleTblRcd->setFont(_T("standard.shx"),0,0,134,2); //rs9.shx
pTextStyleTblRcd->setFileName(textStyle);
pTextStyleTblRcd->setFont(textStyle,0,0,134,2);
pTextStyleTblRcd->setXScale(0.8);
pTextStyleTbl->add(pTextStyleTblRcd);
pTextStyleTblRcd->close();
pTextStyleTbl->getAt(textStyle,pTextStyleTblId);
}
pTextStyleTbl->close();
if (mode==1) // 左对齐
{ pText->setHorizontalMode(AcDb::kTextLeft); }
else if (mode==2) // 中对齐
{ pText->setHorizontalMode(AcDb::kTextCenter); }
else if (mode==3) // 中对齐
{ pText->setHorizontalMode(AcDb::kTextMid); }
else if (mode==5)
{
pText->setHorizontalMode(AcDb::kTextLeft);
pText->setVerticalMode(AcDb::kTextTop);
}
else // // 右对齐
{
pText->setHorizontalMode(AcDb::kTextRight); };
pText->setColorIndex(icolor);
if (mode==1) { pText->setPosition(pos); }
else { pText->setAlignmentPoint(pos); };
pText->setTextStyle(pTextStyleTblId);
pText->setRotation(ang);
pText->setHeight(texth);
pText->setWidthFactor(0.8);
pText->setTextString(ctext);
pText->setLayer(cLayerName);
TId= AddEntityToDbs(pText);
return TId;
}
// This is command 'BZDX'
void XBBZDX()
{
//获取数据
GetPt();
//标注
for (int k=1; k<nRecords; k++)
{
AcGePoint3d startPt,endPt,TextPt;
startPt.x = pDxPts[k-1].fWj;
startPt.y = pDxPts[k-1].fJj;
endPt.x = pDxPts[k].fWj;
endPt.y = pDxPts[k].fJj;
//直线连接
makeline(startPt,endPt);
AcGePoint2d ptOut,ptBase;
//写字
ads_point Pt1,Pt2;
Pt1[X] = startPt.x;
Pt1[Y] = startPt.y;
Pt2[X] = endPt.x;
Pt2[Y] = endPt.y;
double dAngle = ads_angle(Pt1,Pt2);
double dDeltaAngle = 0.5*PI;
if (pDxPts[k].dDist>0)
{
Pt1[0] = endPt.x + 25*cos(dAngle-0.5*PI);
Pt1[1] = endPt.y + 25*sin(dAngle-0.5*PI);
}
TextPt.x = Pt2[0] - 1.5*cos(dAngle);
TextPt.y = Pt2[1] - 1.5*sin(dAngle);
TextPt.x = TextPt[0] + 5.0*cos(dAngle+0.5*PI);
TextPt.y = TextPt[1] + 5.0*sin(dAngle+0.5*PI);
CString strMile=pDxPts[k].pcPtName;
int nPlusPos = strMile.Find("+",0);
strMile.Delete(0,nPlusPos);
char cMile[128];
sprintf(cMile,"%s",strMile);
maketext(TextPt,cMile,dAngle+0.5*PI,6,0,1,"Standard","0");
TextPt.x = Pt2[0] + 6.0*cos(dAngle);
TextPt.y = Pt2[1] + 6.0*sin(dAngle);
TextPt.x = TextPt[0] + 5.0*cos(dAngle+0.5*PI);
TextPt.y = TextPt[1] + 5.0*sin(dAngle+0.5*PI);
char cDist[128];
sprintf(cDist,"%0.1lf",pDxPts[k].dDist);
maketext(TextPt,cDist,dAngle+dDeltaAngle,6,0,1,"Standard","0");
AcDbPolyline *pTheLine = NULL;
pTheLine = new AcDbPolyline;
AcDbPolyline *pRecTangle = NULL;
pRecTangle = new AcDbPolyline;
//////////////////////////////////////////////////////////////////////////
//求四个角点坐标
AcGePoint2d pt1,pt2,pt3,pt4,pt;
pt.x = endPt.x;
pt.y = endPt.y;
pt4.x = pt.x + cos(dAngle-0.75*PI)*1.414;
pt4.y = pt.y + sin(dAngle-0.75*PI)*1.414;
pt3.x = pt.x + cos(dAngle+0.75*PI)*1.414;
pt3.y = pt.y + sin(dAngle+0.75*PI)*1.414;
pt2.x = pt.x + cos(dAngle+0.25*PI)*1.414;
pt2.y = pt.y + sin(dAngle+0.25*PI)*1.414;
pt1.x = pt.x + cos(dAngle-0.25*PI)*1.414;
pt1.y = pt.y + sin(dAngle-0.25*PI)*1.414;
pRecTangle->addVertexAt(0,pt1);
pRecTangle->addVertexAt(1,pt2);
pRecTangle->addVertexAt(2,pt3);
pRecTangle->addVertexAt(3,pt4);
pRecTangle->addVertexAt(4,pt1);
AddEntityToDbs(pRecTangle);
//中心点
if (pDxPts[k].dDist<0)
{
pTheLine->addVertexAt(0,pt);
ptOut.x = endPt.x + cos(dAngle+0.5*PI)*29.0;
ptOut.y = endPt.y + sin(dAngle+0.5*PI)*29.0;
pTheLine->addVertexAt(1,ptOut);
}
else
{
ptOut.x = endPt.x + cos(dAngle-0.5*PI)*29.0;
ptOut.y = endPt.y + sin(dAngle-0.5*PI)*29.0;
pTheLine->addVertexAt(0,ptOut);
pTheLine->addVertexAt(1,pt);
}
AddEntityToDbs(pTheLine) ;
}
if (pDxPts!=NULL)
{
delete[] pDxPts;
pDxPts = NULL;
}
ads_command(RTSTR,"zoom",RTSTR,"E",0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -