刚接触auto CAD二次开发不久,对这个行业了解还很少,写一些东西,为了记录一下自己的成长历程。刚开始来到公司时,就是要学习AutoCAD ObjectARX这本书,对于一个初学者来说,自己看书还是挺费劲的,所以我就看着书上的代码,在visual studio上敲出来试着运行调试一下。可是我发现,不一定能运行出来,因为每个电脑设置的内部环境不同,所以对于相同的一套代码,不一定能在两台电脑上运行出来,所以需要我们做的是,了解自己用的这个电脑的内部环境,然后将代码改成能适应我们内部环境的代码即可。我刚开始时就会画一些简单的直线、圆、弧线,虽然不是很难,但是画出来之后有一种小小的成就感。我对CAD的这些代码理解的还很浅显,所以我将他记录下来,以便日后更深入理解。如下所列出的几个命令是建立在基类都已经创建好的基础上的,如有错误,欢迎广大读者指正。
“创建一条直线”命令
void createline()
{
AcGePoint3d ptStart(0,0,0);
AcGePoint3d ptEnd(100,100,0);
AcDbLine *pLine = new AcDbLine(ptStart,ptEnd);
CQhDwg::AddEntity(pLine,_T(""));
//以上代码可画出一条线,以下代码也可,下面的更简单
CQhDwg::AddLine(AcGePoint3d(1000,0,0),AcGePoint3d(5000,0,0),_T(""));
}
“创建多段线”命令
CQhCfg.cpp
AcDbObjectId CQhDwg::AddPoLyLine(const AcGePoint3dArray& aryPts,double dWidth,bool bClose/*=true*/,CString strLayer/*=_T*/,AcDbDatabase* pDb/*=NULL*/)
{
AcDbPolyline* pLy=new AcDbPolyline;
for (int i=0;iaddVertexAt(i,CQhDwg::pt3dto2d(aryPts[i]));
pLy->setWidthsAt(i,dWidth,dWidth);
}
pLy->setClosed(bClose);
pLy->setLayer(strLayer);
return AddEntity(pLy,strLayer);
}
ZhidanArchCADCommands.cpp
AcGePoint3dArray ary;
ary.append(AcGePoint3d(200,400,0));
ary.append(AcGePoint3d(500,300,0));
ary.append(AcGePoint3d(600,700,0));
CQhDwg::AddPoLyLine(ary,1);
“画弧线”命令
AcDbObjectId CQhDwg::AddArc(const AcGePoint3d &ptCenter,double radius,double startAngle,double endAngle,CString strLayer/*=_T*/,AcDbDatabase* pDb/*=NULL*/)
{
AcGeVector3d vec(0,0,1);
AcDbArc *pArc = new AcDbArc(ptCenter,vec,radius,startAngle,endAngle);
return AddEntity(pArc);
}
“画圆”命令
AcDbObjectId CQhDwg::AddCircle(const AcGePoint3d ¢erPoint,double radius)
{
AcGeVector3d normal(0,0,1);//确定圆的法向量,也可放到函数变量中;
AcDbCircle *pCircle = new AcDbCircle(centerPoint,normal,radius);
return AddEntity(pCircle);
}
“填充”命令
CQhCfg.cpp
AcDbObjectId CQhDwg::AddHatch(const AcDbObjectIdArray& aryExtIds,CString strHatch,double dSc,CString strLayer/*=_T("")*/,AcDbDatabase* pDb/* =NULL */)
{
AcDbHatch* pHatch=new AcDbHatch;
pHatch->setElevation(0);
pHatch->setPatternAngle(0);
pHatch->setPatternScale(dSc);
pHatch->setAssociative(Adesk::kFalse);
pHatch->setHatchStyle(AcDbHatch::kNormal);
pHatch->appendLoop(AcDbHatch::kExternal,aryExtIds);
pHatch->setPattern(AcDbHatch::kPreDefined,strHatch);
pHatch->evaluateHatch();
return AddEntity(pHatch,strLayer);
}
ZhidanArchCADCommands.cpp
AcDbObjectIdArray ary1;
AcDbObjectId id = CQhDwg::AddLine(AcGePoint3d(0,0,0),AcGePoint3d(100,100,0),_T(""));
AcDbObjectId id1 = CQhDwg::AddLine(AcGePoint3d(0,0,0),AcGePoint3d(200,0,0),_T(""));
AcDbObjectId id2 = CQhDwg::AddLine(AcGePoint3d(100,100,0),AcGePoint3d(200,0,0),_T(""));
ary1.append(id);
ary1.append(id1);
ary1.append(id2);
CQhDwg::AddHatch(ary1,"人工草地",1);
“写文字”命令
AcDbObjectId CQhDwg::AddText(AcGePoint3d pt,const TCHAR* text,TextInfo info,double dAngle,AcDb::TextHorzMode horzmode,AcDb::TextVertMode vertmode,CString strLayer/*=_T("")*/,AcDbDatabase* pDb/* =NULL */)
{
AcDbText* pText=new AcDbText;
pText->setLayer(strLayer);
pText->setTextString(text);
pText->setHeight(info.dTextHigh);
pText->setWidthFactor(info.dTextWidthFactor);
pText->setTextStyle(info.idStyle);
pText->setRotation(dAngle);
pText->setHorizontalMode(horzmode);
pText->setVerticalMode(vertmode);
// pText->setPosition(pt);
pText->setAlignmentPoint(pt);
return AddEntity(pText,strLayer,pDb);
}
ZhidanArchCADCommands.cpp
CQhDwg::AddText(AcGePoint3d(3000,3000,3000),_T("输入你要写的文字"), info,0.5,AcDb::kTextMid,AcDb::kTextVertMid);
“修改线的颜色”命令
void change color()
{
AcGePoint3d ptStart(0,0,0);
AcGePoint3d ptEnd(100,100,0);
AcDbLine *pLine = new AcDbLine(ptStart,ptEnd);
AcDbObjectId id = CQhDwg::AddEntity(pLine,_T(""));
CQhDwg::setColor( id, 1);
}
“选择对象”命令
ads_name ssname;
ads_point pt1,pt2;
pt1[X] = pt1[Y] = pt1[Z] = 0;
pt2[X] = pt2[Y] = 100;
pt2[Z] = 0;
//选择图形中与pt1和pt2组成的窗口相交的所有对象
acedSSGet(_T("C"),pt1,pt2,NULL,ssname);
long length;
acedSSLength(ssname,&length);
acutPrintf(_T("\n实体数:%d"));
acedSSFree(ssname);