- UID
- 112821
- 积分
- 66
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2004-3-13
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
我发这个程序的主要目的是:
这个程序从技术上说没有任何难度,但从结构上看还有很多问题,虽然完全实现了CAD的直线功能,但结构很复杂,如果不带撤销功能,程序应该能精简许多.但如果就是带撤销功能,程序还能不能更加优化,程序虽然是写出来了,但至少目前我认为它还是垃圾.
平时我们有时就是这样,往往认为不屑一顾的东西,真正动起手来却不是那么回事.
请高手发表评论,如能从程序的结构或实现的手段上提供更好的方法,本人将不胜感激!
[php]
#include "geassign.h"
#include "dbsymtb.h"
#include "dbapserv.h"
#include "dbtrans.h"
#include "actrans.h"
#include "acdb.h"
#include "rxregsvc.h"
#include "aced.h"
#include "adslib.h"
#include "dbents.h"
//命令执行函数
void windo()
{
AcGePoint3d startPt, endPt;
AcGePoint3d pt;
AcGePoint3dArray ptArray;
char kw[10];
int rc;
Acad::ErrorStatus es;
Adesk::Boolean keepPicking = Adesk::kTrue; // 是否结束循环
Adesk::Boolean keepcloseing = Adesk::kFalse; // 是否闭合
int i = 0;
int y = 0;
while(keepPicking)
{
if(i == 0)
{
rc = acedGetPoint(NULL, "\n指定第一点: ", asDblArray(pt));
if(rc != RTNORM)
{
keepPicking = Adesk::kFalse;
break;
}
if(i == y)
{
ptArray.append(pt);
i++;
y++;
}
else
{
ptArray.setAt(i, pt);
i++;
}
rc = RTNONE;
}
else if(i == 1)
{
pt = ptArray.at(i-1);
acedInitGet(0, "Undo");
rc = acedGetPoint(asDblArray(pt), "\n指定下一点或[撤销U]: ", asDblArray(pt));
switch (rc)
{
case RTCAN:
case RTERROR:
case RTNONE:
keepPicking = Adesk::kFalse;
break;
case RTKWORD:
if(acedGetInput(kw) != RTNORM)
{
keepPicking = Adesk::kFalse;
break;
}
if(strcmp(kw, "Undo") == 0)
{
//actrTransactionManager->abortTransaction();
i--;
break;
}
break;
case RTNORM:
if(i == y)
{
ptArray.append(pt);
i++;
y++;
}
else
{
ptArray.setAt(i, pt);
i++;
}
} //switch
} //if
else if(i == 2)
{
pt = ptArray.at(i-1);
acedInitGet(0, "Undo");
rc = acedGetPoint(asDblArray(pt), "\n指定下一点或[撤销U]: ", asDblArray(pt));
switch (rc)
{
case RTCAN:
case RTERROR:
case RTNONE:
keepPicking = Adesk::kFalse;
break;
case RTKWORD:
if(acedGetInput(kw) != RTNORM)
{
keepPicking = Adesk::kFalse;
break;
}
if(strcmp(kw, "Undo") == 0)
{
actrTransactionManager->abortTransaction();
i--;
break;
}
break;
case RTNORM:
if(i == y)
{
ptArray.append(pt);
i++;
y++;
}
else
{
ptArray.setAt(i, pt);
i++;
}
} //switch
} //if
else if(i > 2)
{
pt = ptArray.at(i-1);
acedInitGet(0, "Close Undo");
rc = acedGetPoint(asDblArray(pt), "\n指定下一点或[闭合C/撤销U]: ", asDblArray(pt));
switch (rc)
{
case RTCAN:
case RTERROR:
case RTNONE:
keepPicking = Adesk::kFalse;
break;
case RTKWORD:
if(acedGetInput(kw) != RTNORM)
{
keepPicking = Adesk::kFalse;
break;
}
if(strcmp(kw, "Close") == 0)
{
keepcloseing = Adesk::kTrue;
keepPicking = Adesk::kFalse;
rc = RTNORM;
break;
}
else if(strcmp(kw, "Undo") == 0)
{
actrTransactionManager->abortTransaction();
i--;
break;
}
break;
case RTNORM:
if(i == y)
{
ptArray.append(pt);
i++;
y++;
}
else
{
ptArray.setAt(i, pt);
i++;
}
} //switch
} //if
if(keepcloseing)
{
AcTransaction *pTrans;
pTrans = actrTransactionManager->startTransaction();
if (pTrans == NULL)
{
keepPicking = Adesk::kFalse;
break;
}
startPt = ptArray.at(i-1);
endPt = ptArray.at(0);
AcDbLine *pLine = new AcDbLine(startPt, endPt);
AcDbObjectId windId;
es = appendToBlock(pLine, windId);
if(es != Acad::eOk)
{
if(pLine)
{
delete pLine;
}
actrTransactionManager->abortTransaction();
keepPicking = Adesk::kFalse;
return;
}
es = actrTransactionManager->addNewlyCreatedDBRObject(pLine);
if(es != Acad::eOk)
{
if(pLine)
{
delete pLine;
}
actrTransactionManager->abortTransaction();
keepPicking = Adesk::kFalse;
return;
}
pLine->close();
pLine->draw();
}
else
if(rc == RTNORM)
{
AcTransaction *pTrans;
pTrans = actrTransactionManager->startTransaction();
if (pTrans == NULL)
{
keepPicking = Adesk::kFalse;
break;
}
startPt = ptArray.at(i-2);
endPt = ptArray.at(i-1);
AcDbLine *pLine = new AcDbLine(startPt, endPt);
AcDbObjectId windId;
es = appendToBlock(pLine, windId);
if(es != Acad::eOk)
{
if(pLine)
{
delete pLine;
}
actrTransactionManager->abortTransaction();
keepPicking = Adesk::kFalse;
return;
}
es = actrTransactionManager->addNewlyCreatedDBRObject(pLine);
if(es != Acad::eOk)
{
if(pLine)
{
delete pLine;
}
actrTransactionManager->abortTransaction();
keepPicking = Adesk::kFalse;
return;
}
pLine->close();
pLine->draw();
}
} //while
actrTransactionManager->endTransaction();
} //函数结束
//通用函数,把实体加入块表记录,但不关闭实体的指针
Acad::ErrorStatus appendToBlock(AcDbEntity *pEnt, AcDbObjectId& idEnt)
{
Acad::ErrorStatus es;
AcDbBlockTable *pBlockTable = NULL;
AcDbBlockTableRecord *pBlockTableRecord = NULL;
if((es = acdbHostApplicationServices()->workingDatabase()
->getBlockTable( pBlockTable, AcDb::kForRead )) != Acad::eOk)
return es;
if((es = pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite )) != Acad::eOk)
{
pBlockTable->close();
return es;
}
es = pBlockTableRecord->appendAcDbEntity( idEnt, pEnt );
pBlockTableRecord->close();
pBlockTable->close();
return es;
}
[/php]
下面是用VC++6.0编译好的程序,大家运行看看:
命令名WINDO |
|