- UID
- 76992
- 积分
- 54
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2003-9-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
程序功能:
能通过框选出一个圆的选择集,对集内的圆进行编号,把编号以文本实体形式存放到新建的层内!
程序运行命令: zhb
本程序所碰到的问题:
A): 本程序经过N次修改,个人觉得这个程序还是写得很乱,烦琐,望有人高人能指点或修改一下,能简化程序.使程序的可读性加强些.
B):这个程序在连续运行到第三次时,即连续编三次号时,出现问题(按默认值进行操作),在求指定层上的文本最大编号时,求出来的值是第二次编号时的最大值,原因不明!!!-------重点要解决的问题!
C) 附上已经调试编译出来的ARX文件!者支持CAD2002,其它版本未知!
- void corillazhb()
- {
- AcDbDatabase *pDb = NULL; //数据库指针
- AcGePoint3dArray arrCenter; //取放圆心坐标的数组
- char newLayerName[20]; //存放编号的层名
- char kw[10]; //关键字
- int cmpMax=0, cmpText=0; //文本比较的过渡变量(取文本的最大值MAX)
- AcDbObjectId LayerId; //层ID号
- AcDbLayerTable *pLayerTable; //层记录指针
- AcDbLayerTableRecord *pLayerTableRecord;//层表记录指针
- int rc; //函数返回码
-
-
- if((pDb = acdbHostApplicationServices()->workingDatabase()) == NULL) //取得当前数据库指针
- {
- acdbFail("\n错误::当前数据库为空!");
- return ;
- }
-
- if(pDb->getSymbolTable(pLayerTable, AcDb::kForWrite) != Acad::eOk) //取得层表
- {
- acdbFail("\n错误::获取层表指针失败.");
- pLayerTable->close();
- return ;
- }
- rc = acedGetString(1,"\n提示::请输入一个新的层名用来存储编号文本,默认值为<zbh>: ", newLayerName); //输入层名
- switch(rc) {
- case RTCAN:
- acutPrintf("\n提示::用户取消操作.");
- pLayerTable->close();
- return ;
- break;
- case RTERROR:
- acdbFail("\n错误::取得新层名失败.");
- pLayerTable->close();
- return ;
- break;
- case RTNORM:
- if(stricmp(newLayerName,"\0") == 0) //如果用户输入为空,则取默认值
- {
- strcpy(newLayerName, "zbh");
- }
- break;
- }
-
- if(pLayerTable->has(newLayerName))//如果输入层存在
- {
- acedInitGet(NULL, "Yes No");
- rc = acedGetKword("\n注意::输入层名已经存在,是否继续 - [Yes/No]<Yes>: ", kw);//用户决定是否继续
- switch(rc) {
- case RTCAN:
- acutPrintf("\n提示::用户按下取消键.");
- pLayerTable->close();
- return ;
- break;
- case RTNONE:
- strcpy(kw,"Yes");
- break;
-
- case RTNORM:
- break;
-
- case RTERROR:
- acdbFail("\n错误::取得关键字失败.");
- pLayerTable->close();
- return ;
- break;
- }
-
- if (stricmp(kw,"Yes") == 0) { //用户选择继续
- pLayerTable->close();
- AcDbBlockTable *pBlockTable;
- if(pDb->getSymbolTable(pBlockTable, AcDb::kForRead)!=Acad::eOk) //取得块表
- {
- acdbFail("\n错误::取得块表指针失败.");
- pBlockTable->close();
- return ;
- }
-
- AcDbBlockTableRecord *pBlockTableRecord;
- if(pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite)!=Acad::eOk)//取得块表记录
- {
- acdbFail("\n错误::打开块表记录失败.");
- pBlockTableRecord->close();
- pBlockTable->close();
- return ;
- }
- pBlockTable->close();
- AcDbBlockTableRecordIterator *pIterator;
- if(pBlockTableRecord->newIterator(pIterator) != Acad::eOk) //创建一个遍历器
- {
- delete pIterator;
- pBlockTableRecord->close();
- return ;
- }
-
- AcDbText *pTextTmp = NULL;
-
- for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
- {
- AcDbEntity *entity=NULL;
- if(pIterator->getEntity(entity,AcDb::kForRead)!=Acad::eOk)//打开实体为可读
- {
- delete pIterator;pIterator=NULL;
- pBlockTableRecord->close();
- pTextTmp->close();
- return ;
- }
-
- if(stricmp(entity->layer(),newLayerName) == 0) //获取指定层上的实体
- {
- if(entity->isKindOf(AcDbText::desc())) //过滤为文本实体
- {
- pTextTmp = AcDbText::cast(entity);
- cmpText = atoi(pTextTmp->textString());//把字符串转为整数
-
- if (cmpText > cmpMax) //取出最大值
- {
- cmpMax = cmpText;
- }
- }
- }
- entity->close();
- }
- pTextTmp->close();
- delete pIterator;
- pBlockTableRecord->close();
-
- }
- else //用户选择NO放弃
- {
- acutPrintf("\n提示::用户放弃操作.");
- pLayerTable->close();
- return ;
- }
- }
- else//如果输入层不存在
- {
- pLayerTableRecord = new AcDbLayerTableRecord;
- if(pLayerTableRecord->setName(newLayerName)!=Acad::eOk) //往记录中加入层名
- {
- acdbFail("\n错误::添加层表记录失败.");
- pLayerTableRecord->close();
- pLayerTable->close();
- return ;
- }
- if(pLayerTable->add(LayerId, pLayerTableRecord)!=Acad::eOk)//在层表中加入记录
- {
- acdbFail("\n错误::追加层表记录失失败.");
- pLayerTableRecord->close();
- pLayerTable->close();
- return ;
- }
- pLayerTable->close();
- pLayerTableRecord->close();
- }
- //==============================================================
- struct resbuf *filter; //过滤器变量
- ads_name getCircle,temEnt; //实体名
- ads_real inputRadius = 0; //过滤圆的半径
- ads_point startPoint,endPoint; //框选的矩形对角点
- acedInitGet(RSG_NONULL|RSG_NOZERO|RSG_NONEG,NULL);
- rc = acedGetReal("\n提示::请输入要编号的圆的半径(R): ", &inputRadius);//输入过滤的圆半径
- switch(rc)
- {
- case RTCAN:
- acutPrintf("\n提示::用户取消操作.");
- return ;
- break;
- case RTERROR:
- acdbFail("\n错误::取得圆半径失败.");
- return ;
- break;
- default:
- break;
- }
- filter = acutBuildList(RTDXF0, "CIRCLE", 40, inputRadius, 0); //创建过滤器
- if(acedGetPoint(NULL,"\n提示::拖出一个矩形框选出要进行编号的圆集: ",startPoint) != RTNORM)//框选矩形始角点
- {
- acdbFail("\n错误::取得起始对角点失败.");
- acutRelRb(filter);
- return ;
- }
- acedInitGet(RSG_DASH,NULL);//虚线
- if(acedGetCorner(startPoint,NULL,endPoint) != RTNORM)//框选矩形终角点
- {
- acdbFail("\n错误::取得终止对角点失败.");
- acutRelRb(filter);
- return ;
- }
- if(acedSSGet("W",startPoint,endPoint,filter,/*&eb1,*/getCircle) != RTNORM) //创建框选内实体的选择集
- {
- acdbFail("\n错误::创建选择集失败或当前没有符合条件的圆实体.");
- acutRelRb(filter);
- return ;
- }
- acutRelRb(filter); //释放过滤器
-
- //=====================================================
-
- long lenLast; //存储选择集长度(实体个数)的变量
- AcDbEntity *pEnt; //实体指针变量
- AcDbCircle *pCirc; //圆指针变量
-
- if(acedSSLength(getCircle, &lenLast) != RTNORM) //取得选择集长度
- {
- acdbFail("\n错误::取得选择集的长度失败.");
- acedSSFree(getCircle);
- return ;
- }
- for(int k=0; k < lenLast; k++) //循环
- {
- AcDbObjectId pEntId; //对象ID
- if(acedSSName(getCircle, k, temEnt) != RTNORM)
- {
- acdbFail("\n错误::取得实体选择集失败.");
- acedSSFree(getCircle);
- return ;
- }
- if(acdbGetObjectId(pEntId, temEnt) != Acad::eOk)
- {
- acdbFail("\n错误::取得实体ID失败.");
- acedSSFree(getCircle);
- return ;
- }
- if(acdbOpenObject(pEnt, pEntId, AcDb::kForRead) != Acad::eOk)
- {
- acdbFail("\n错误::打开实体失败.");
- acedSSFree(getCircle);
- return ;
- }
- pCirc = AcDbCircle::cast(pEnt); //给圆指针赋值
- arrCenter.append(pCirc->center()); //追加圆心到圆心数组变量
-
- }
-
- pCirc->close();
- pEnt->close();
- acedSSFree(getCircle); //释放选择集
- //=================================================================
- int startNum = 1; //编号的起始数值
- char TextString[10]; //转变int编号为string变量
-
- AcDbBlockTable *pBlockTable;
- if(pDb->getSymbolTable(pBlockTable, AcDb::kForRead)!=Acad::eOk) //取得块表
- {
- acdbFail("\n错误::取得块表指针失败.");
- pBlockTable->close();
- return ;
- }
- AcDbBlockTableRecord *pBlockTableRecord;
- if(pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite)!=Acad::eOk) //取得块表记录
- {
- acdbFail("\n错误::打开块表记录失败.");
- pBlockTable->close();
- return ;
- }
- pBlockTable->close();
- if (cmpMax != 0)//判断桩编号层的最大文本编号是否为零
- {
-
- acutPrintf("\n注意::当前桩编号层上的最大编号是 %d!",cmpMax);
- acedInitGet(NULL, "Yes No");
- rc=acedGetKword("\n提示::是否要续写上次最大编号? - [Yes/No]<Yes>: ", kw);//用户决定是否继续
- switch(rc) {
- case RTCAN:
- acutPrintf("\n提示::用户取消操作.");
- pBlockTableRecord->close();
- return ;
- break;
-
- case RTNONE: //直接回车
- strcpy(kw,"Yes");
- startNum = cmpMax+1;
- break;
- case RTNORM:
- if (stricmp(kw,"Yes") == 0)//用户输入YES
- {
- startNum = cmpMax+1;
- }
- else
- {
- acedGetInt("\n提示::请输入一个新的起始号<1>: ",&startNum);//NO
- }
-
- break;
- }
- }
- else//为0
- {
- acedGetInt("\n提示::请输入一个新的起始号<1>: ",&startNum);
- }
-
-
- for(int i=0; i< arrCenter.length(); i++) //循环
- {
-
- if(acdbRToS(startNum,-1,-1,TextString) != RTNORM) //整型转为字符串
- {
- acdbFail("\n错误::整型数敏转化为字符串时失败.");
- pBlockTableRecord->close();
- return ;
- }
-
- AcDbObjectId TextId = NULL;
- AcDbText *pText = new AcDbText(arrCenter[i], TextString, TextId, 20.0, 0.0);
- pText->setLayer(LayerId,Adesk::kTrue);
- if(pBlockTableRecord->appendAcDbEntity(TextId, pText)!=Acad::eOk)
- {
- acdbFail("\n错误::添加文本到新建层表记录失败.");
- pBlockTableRecord->close();
- return ;
- }
- startNum++;
- pText->close();
- }
- pBlockTableRecord->close();
- }
复制代码 |
|