- UID
- 45733
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2003-4-28
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
这个程序前前后后花乐我几天,主要是选择集的使用问题,包括选择、遍历,这次把程序贴出来是希望象我这般初学者在这个问题上少走弯路,希望高手指教。
程序在vc6.0 objectarx2000 下编译通过,在autocad2002中顺利运行。
/////////////////////////////////
// 选择cad中的数字进行累加 //
// gjf 2003.10 //
////////////////////////////////
#include <locale.h>
#include <math.h>
#include <string.h>
#include <aced.h>
#include <adscodes.h>
#include <dbents.h>
#include <dbsymtb.h>
#include <dbgroup.h>
#include <dbapserv.h>
//global variable
//declare
void initApp();
void unloadApp();
void usr_app();
AcDbObjectId createText(const AcGePoint3d&, const char*);// 在cad中创建数字
void adsPt_AcGePt(const ads_point&, AcGePoint3d&); // ads_point 与 AcGePoint3d的转换
bool isNumber(const char[]); // 用于判断字符串是否为数字
//define
void initApp()
{
acedRegCmds->addCommand("ADD_COMMAND",
"ADD",
"ADD",
ACRX_CMD_TRANSPARENT,
usr_app);
acutPrintf("执行命令:add ");
}
void unloadApp()
{
acedRegCmds->removeGroup("ADD_COMMAND");
}
extern "C" AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
switch (msg)
{
case AcRx::kInitAppMsg:
acrxDynamicLinker->unlockApplication(pkt);
acrxDynamicLinker->registerAppMDIAware(pkt);
initApp();
break;
case AcRx::kUnloadAppMsg:
unloadApp();
break;
default:
break;
}
return AcRx::kRetOK;
}
//main
void usr_app()
{
AcDbObjectIdArray idArr;
AcGePoint3d ptOutput(0.0, 0.0, 0.0);
ads_name ss, ss1;//记得用 acedSSFree释放
struct resbuf eb, *eb1, *eb2 = NULL;
char sbuf1[10], text1[20];
double sum1 = 0.0, temp1;
int total = 0;
eb.restype = 0; //检索实体名
strcpy(sbuf1, "TEXT");
eb.resval.rstring = sbuf1;
eb.rbnext = NULL; //无其它内容
//选择
if (acedSSGet(NULL, NULL, NULL, &eb, ss) != RTNORM)
{
acdbFail( "acedSSGet cancelled\n" );
}// 检索所有text
long length = 0;
acedSSLength(ss, &length);
acutPrintf("\n %d Selected.", length);
//处理选择集,把数字挑出来并且进行累加
for (long l = 0; l <= length; l++)
{
if (acedSSName(ss, l, ss1) == RTNORM) {
eb1 = acdbEntGet(ss1);
if (eb1 == NULL)
{
acdbFail("Fail to get entity.\n");
break;
}
for (eb2 = eb1; eb2 != NULL; eb2 = eb2 -> rbnext)
{
if (eb2 -> restype == 1)
{
strcpy(text1, eb2 -> resval.rstring);
// acutPrintf("\nText = %s", text1);
if (isNumber(text1)) //判断
{
temp1 = atof(text1);
sum1 += temp1;
total += 1;
}
}
}
acutRelRb(eb1);
}
}
if (acedSSFree(ss) != RTNORM || acedSSFree(ss1) != RTNORM)
{
acdbFail("error.\n");
exit(0);
}
//输出
ads_point ptTemp;
acutPrintf("\n Total Number = %d。", total);
acdbRToS(sum1, -1, -1, text1);
if (acedGetPoint(NULL, "\n请拾取要输出的点: ", ptTemp) == RTNORM)
{
adsPt_AcGePt(ptTemp, ptOutput);
idArr.append(createText(ptOutput, text1));
}
}
// 在图形数据库中写入字符
AcDbObjectId createText(const AcGePoint3d& insertionPt, const char* text)
{
AcDbText *pText = new AcDbText(insertionPt, text, AcDbObjectId::kNull, 0);
//get current entity's symbol table
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices() -> workingDatabase() -> getSymbolTable(pBlockTable , AcDb::kForRead);
//get pointer of record,use to add object
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE , pBlockTableRecord , AcDb::kForWrite);
pBlockTable->close();
//add text to record
AcDbObjectId textId;
pBlockTableRecord->appendAcDbEntity(textId , pText);
//close pointer
pBlockTableRecord->close();
pText->close();
//return text Id
return textId;
}
void adsPt_AcGePt(const ads_point& ptFrom, AcGePoint3d& ptTo)
{
ptTo[X] = ptFrom[X];
ptTo[Y] = ptFrom[Y];
ptTo[Z] = ptFrom[Z];
}
// 判断字符串是否为数字
bool isNumber(const char astring[])
{
bool dot = true;
int i = 1;
if (astring[0] == '-' || isdigit(astring[0]))
{
for (int i = 1; astring != 0; i++)
{
if (!isdigit(astring))
{
if (astring == '.' && dot)
dot = false;
else
return false;
}
}
}
else
return false;
return true;
} |
|