OBJECT ARX 绘制常用图形 总结

////将图形加载显示到CAD===================
static AcDbObjectId LoadEntity(AcDbEntity* entity){
AcDbBlockTable* pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForRead);


AcDbBlockTableRecord* pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);


AcDbObjectId Id;
pBlockTableRecord->appendAcDbEntity(Id,entity);


pBlockTable->close();


pBlockTableRecord->close();

entity->close();
return Id;
}



////为图形添加标注
static void AddOrdinate(AcGePoint3d ptStart,AcGePoint3d ptEnd,
CString text){


AcDbOrdinateDimension* pDim = new 
AcDbOrdinateDimension(true,ptStart,ptEnd,text);


LoadEntity(pDim);


}




////画线====================
static void TESTlineCmd()
{
//acutPrintf(_T("\nHello"));

ads_point inputStart;
ads_point inputEnd;


AcGePoint3d ptStart;
AcGePoint3d ptEnd;


////用户输入要画的坐标
if(acedGetPoint(NULL,_T("start point"),inputStart) != RTNORM){
return;
}
ptStart[X] = inputStart[X];
ptStart[Y] = inputStart[Y];
ptStart[Z] = inputStart[Z];

if(acedGetPoint(NULL,_T("end point"),inputEnd) != RTNORM){

return;
}
ptEnd[X] = inputEnd[X];
ptEnd[Y] = inputEnd[Y];
ptEnd[Z] = inputEnd[Z];


AcDbLine* pLine = new AcDbLine(ptStart,ptEnd);
pLine->setColorIndex(2);
LoadEntity(pLine);

AddOrdinate(ptStart,ptEnd,_T("hi"));
//AfxMessageBox(_T("命令画了一条线"));
}




////画圆形==========================

static void TESTcirclecmd(){
////垂直平面法向量
AcGeVector3d vec(0,0,1);
////圆心
AcGePoint3d ptCenter(100,100,100);


AcDbCircle* pCircle = new AcDbCircle(ptCenter,vec,1000);

LoadEntity(pCircle);
}


////画圆弧================================


static void TESTarccmd(){
AcGeVector3d vec(0,0,1);
AcGePoint3d ptCenter(500,500,500);
AcDbArc* pArc = new AcDbArc(ptCenter,vec,700,0,90);
LoadEntity(pArc);
}


////画多线段============================


static void TESTpolycmd(){

////构造多点数组
AcGePoint2dArray points;

AcGePoint2d pt1(1000, 1000);
AcGePoint2d pt2(2000, 1800);
AcGePoint2d pt3(500, 2000);
AcGePoint2d pt4(3000, 5000);
points.append(pt1);
points.append(pt2);
points.append(pt3);
points.append(pt4);


////绘制多点线段
AcDbPolyline* pl = new AcDbPolyline(points.length());
for(int i = 0;i < points.length();i ++){

pl->addVertexAt(i,points.at(i),0,0.5,0.5);
}


LoadEntity(pl);
}

////绘制POLY3d线段====================
static void TESTpoly3dcmd(){

////构造多点数组
AcGePoint3dArray points;

AcGePoint3d pt1(10, 10,10);
AcGePoint3d pt2(20, 10,20);
AcGePoint3d pt3(20, 20,30);
points.append(pt1);
points.append(pt2);
points.append(pt3);


////绘制多点线段
AcDb3dPolyline* pl = new AcDb3dPolyline(AcDb::Poly3dType::k3dSimplePoly,points);


LoadEntity(pl);
}


////绘制椭圆=========================
static void TESTellipsecmd(){
////椭圆焦点
/* AcGePoint2d p1(400,400);
AcGePoint2d p2(800,400);*/




////中心点
AcGePoint3d ptCenter(600,400,0);


AcGeVector3d vecNormal(0,0,1);
////长轴
AcGeVector3d majorAxis(1000,0,0);


////通过曲率确定短轴长度
double ratio = 0.6;


AcDbEllipse* ae = new AcDbEllipse(/*AcGePoint3d::kOrigin*/ptCenter,vecNormal,majorAxis,
ratio);
LoadEntity(ae);


}




////创建样条曲线================
static void TESTsplinecmd(){
AcGePoint3d p1(0,0,0),p2(200,600,0),p3(1200,1600,0),
p4(2000,2000,0);
AcGePoint3dArray points;
points.append(p1);
points.append(p2);
points.append(p3);
points.append(p4);

AcDbSpline* as = new AcDbSpline(points);
LoadEntity(as);


}


////创建面域(把用直线创建的闭合图形,生成一个图形的对象)
static void TESTregioncmd(){

ads_name ss;
////提示用户选择对象
int rt = acedSSGet(NULL,NULL,NULL,NULL,ss);
AcDbObjectIdArray ids;


if(rt == RTNORM){

long len;
////获得对象个数
acedSSLength(ss,&len);
for(int i = 0;i < len;i ++){

////先从对象中得到名字
ads_name ent;
acedSSName(ss,i,ent);


////再从名字中取出ID
AcDbObjectId objId;
acdbGetObjectId(objId,ent);
ids.append(objId);
}


}



acedSSFree(ss);


////得到objId数组之后,开始画区域


CreateRegion(ids);
}


////绘制面域
static AcDbObjectIdArray  CreateRegion(AcDbObjectIdArray ids){


AcDbObjectIdArray regionIds;
AcDbVoidPtrArray curves;
AcDbVoidPtrArray regions;
AcDbEntity* pEnt;
AcDbRegion* pRegion;


for(int i = 0;i < ids.length();i ++){

acdbOpenAcDbEntity(pEnt,ids.at(i),AcDb::kForRead);

if(pEnt->isKindOf(AcDbCurve::desc())){

curves.append(static_cast<void*>(pEnt));
}
}


Acad::ErrorStatus es = AcDbRegion::createFromCurves(curves,regions);
if(es == Acad::eOk){

for(int i = 0;i < regions.length();i ++){

pRegion = static_cast<AcDbRegion*>(regions[i]);
pRegion->setDatabaseDefaults();
AcDbObjectId regionId = LoadEntity(pRegion);
regionIds.append(regionId);
}


CString tipStr ;
tipStr.Format(_T("\n已成功创建面域:%d个"),regions.length());

acutPrintf(tipStr);
}
else{


for(int i = 0;i < regions.length();i ++){

delete (AcRxObject*)regions[i];
}
}


for(int i = 0;i < ids.length();i ++){

pEnt = static_cast<AcDbEntity*>(curves[i]);
pEnt->close();
}


return regionIds;
}


//绘制文字===================
static void TESTtextcmd(){

AcGePoint3d ptInsert(0,400,0);

AcDbMText* pMText = new AcDbMText();

////设置多行文字特性


pMText->setContents(_T("hello!!"));
pMText->setLocation(ptInsert);
pMText->setTextHeight(1000);
pMText->setWidth(1000);
pMText->setAttachment(AcDbMText::kBottomLeft);


LoadEntity(pMText);


}




////创建填充======================
static void TESThatchcmd(){

////提示用户选择填充边界
ads_name ss;
int rt = acedSSGet(NULL,NULL,NULL,NULL,ss);
AcDbObjectIdArray objIds;


if(rt == RTNORM){

long len;
acedSSLength(ss,&len);
for(int i = 0;i < len;i ++){

ads_name ent;
acedSSName(ss,i,ent);
AcDbObjectId objId;
acdbGetObjectId(objId,ent);
objIds.append(objId);
}
}
acedSSFree(ss);


////选择完毕,绘制填充


Acad::ErrorStatus es;
AcDbHatch* pHatch = new AcDbHatch();


////选择填充平面
AcGeVector3d normal(0,0,1);
pHatch->setNormal(normal);
pHatch->setElevation(0);


////设置关联性
pHatch->setAssociative(true);

////设置填充图案
pHatch->setPattern(AcDbHatch::kPreDefined,_T(/*"SOLID"*/"ANSI31"));

////设置填充角度
pHatch->setPatternAngle(45);
////设置填充边界


es=pHatch->appendLoop(AcDbHatch::kExternal,objIds);


////显示填充对象


es=pHatch->evaluateHatch();
LoadEntity(pHatch);
}


////交互函数练习==================


static void TESTinputcmd(){
CString outStr;
//acedGetReal
/*ads_real realNum;

acedGetReal(_T("输入一个实数"),&realNum);
outStr.Format(_T("%lf"),realNum);
acutPrintf(outStr);*/
//acedGetInt
/*int iNum;
acedGetInt(_T("输入一个整数"),&iNum);
outStr.Format(_T("%d"),iNum);
acutPrintf(outStr);*/
//acedGetString
   /*TCHAR str[256];
acedGetString(0,_T("输入一个字符串"),str);
acutPrintf(str);*/
//acedGetKword
/* TCHAR kw[256];
acedGetKword(_T("输入一个关键字"),kw);
acutPrintf(kw);*/
//acedGetPoint
/* ads_point pt;
acedGetPoint(NULL,_T("输入一个点"),pt);
outStr.Format(_T("x:%f,y:%f"),pt[X],pt[Y]);
acutPrintf(outStr);*/
//acedGetCorner(要与ads_point联合使用,完成画矩形)
/*ads_point ptCorner;
acedGetCorner(pt,_T("输入对角点"),ptCorner);
outStr.Format(_T("x:%f,y:%f"),ptCorner[X],ptCorner[Y]);
acutPrintf(outStr);*/
//acedGetDist(和ads_point结合使用,实现画线)
/*ads_real ptReal;
acedGetDist(pt,_T("输入目标值"),&ptReal);
outStr.Format(_T("%lf"),ptReal);
acutPrintf(outStr);*/
//acedGetAngle(和ads_point结合使用,实现画线)
/* ads_real agReal;
acedGetAngle(pt,_T("输入一个角度"),&agReal);
outStr.Format(_T("%lf"),agReal);
acutPrintf(outStr);*/




}







//获得一层的所有对象,变色===============
static void TESTgetlayerobjcmd(){



AcDbLayerTable *pLayerTbl;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(
pLayerTbl,AcDb::kForRead);


if(!pLayerTbl->has(_T("测试"))){

acutPrintf(_T("\n当前图形未包含'测试'图层"));
pLayerTbl->close();
return;
}
AcDbObjectId layerId;
pLayerTbl->getAt(_T("测试"),layerId);
pLayerTbl->close();


////获得当前数据库的块表
AcDbBlockTable* pBlkTbl;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(
pBlkTbl,AcDb::kForRead);
////获得模型空间的块表记录
AcDbBlockTableRecord* pBlkTblRcd;
pBlkTbl->getAt(ACDB_MODEL_SPACE,pBlkTblRcd,AcDb::kForRead);
pBlkTbl->close();


////创建块表记录遍历器


AcDbBlockTableRecordIterator* pIter;
pBlkTblRcd->newIterator(pIter);
AcDbEntity* pEnt;
for(pIter->start();!pIter->done();pIter->step()){

pIter->getEntity(pEnt,AcDb::kForWrite);
if(pEnt->layerId() == layerId){


/*AcDbLine* pLine = AcDbLine::cast(pEnt);
if(pLine != NULL){*/

pEnt->setColorIndex(1);
//}
}
pEnt->close();
}


delete pIter;
pBlkTblRcd->close();
}


你可能感兴趣的:(OBJECT ARX 绘制常用图形 总结)