[日积月累]:Arx函数集,网友自制
自愿发布,版权放弃,免除低水平的重复劳动请勿发布有损贵公司(单位)核心竞争力的代码.
视质量酌情加积分
欢迎跟贴发布!
我先来抛砖
**** Hidden Message ***** 我也跟一帖,大家还可以修改,重载。。。
//************************************************************************
//函数名称:selectEntityInLayer
//函数类型:Acad::ErrorStatus
//返回值:正常:Acad::eOk
//功能描述:选择指定图层上的所有实体!
//函数作者:Darcy
//创建日期:200X-XX-XX
//参数列表:
//变量名:nLayerName 变量类型:const char* 变量说明:(输入)图层名
//变量名:nIDs 变量类型:AcDbObjectIdArray& 变量说明:(输出)图层中实体的对象标识符集合
//************************************************************************
Acad::ErrorStatus selectEntityInLayer(const char* nLayerName,AcDbObjectIdArray& nIDs)
{
Acad::ErrorStatus es = Acad::eOk;
ads_name ents;
struct resbuf *rb;
rb=acutNewRb(AcDb::kDxfLayerName);
rb->restype=8;
rb->resval.rstring=(char*)nLayerName;
rb->rbnext=NULL;
acedSSGet("X",NULL,NULL,rb,ents);
long entNums=0;
acedSSLength(ents,&entNums);
if (entNums == 0)
es = Acad::eInvalidInput;
else
{
for (long a = 0; a < entNums ; a ++)
{
AcDbObjectIdobjId;
ads_name ent;
acedSSName(ents,a,ent);
acdbGetObjectId(objId, ent);
nIDs.append(objId);
}
}
acedSSFree(ents);
acutRelRb(rb);
return es;
} 受到版主鼓励,再贴两个,自己写的,莫要见笑
//==========================================================
功能:新建一个图层
参数:LayerName -- 图层名,LayerColor -- 颜色名
返回:图层ID
//==========================================================
AcDbObjectId CreateNewLayer(CString LayerName, AcCmColor LayerColor)
{
// 获得当前图形数据库的符号表
AcDbLayerTable *pLayerTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTable,
AcDb::kForWrite);
// 生成新的图层表记录
AcDbLayerTableRecord *pLayerTableRecord = new AcDbLayerTableRecord;
pLayerTableRecord->setName(LayerName); // 设置图层名
pLayerTableRecord->setColor(LayerColor); // 设置图层颜色
AcDbObjectId layerId;
pLayerTable->add(layerId,pLayerTableRecord);
// 关闭图层表和图层表记录
pLayerTable->close();
pLayerTableRecord->close();
return layerId;
}
//==========================================================
功能:在指定图层上新建一条直线
参数:见注释
返回:直线ID
//==========================================================
AcDbObjectId CreateLine( double x1,double y1,double z1,// 起点坐标
double x2,double y2,double z2,// 终点坐标
AcDbObjectId layer) // 直线所在图层
{
AcGePoint3d StartPt(x1,y1,z1); // 起点
AcGePoint3d EndPt(x2,y2,z2); // 终点
AcDbLine *pLine = new AcDbLine(StartPt,EndPt);
// 获得当前图形数据库的符号表
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,
AcDb::kForRead);
// 获得符号表中的模型空间块表记录指针,用于添加对象
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
pBlockTable->close();
// 将直线添加到模型空间块表记录中
AcDbObjectId lineId;
pLine->setLayer(layer,Adesk::kTrue); // 设置直线所在图层
pBlockTableRecord->appendAcDbEntity(lineId,pLine);
// 关闭块表记录指针和直线指针
pBlockTableRecord->close();
pLine->close();
// 返回直线ID号
return lineId;
}
void CMyDatabase::rotationGroup(CString strGroupName ,CReiPoint ptRotation,double rotationAngle)
{
AcGePoint3d pt;
AcDbDictionary *pGroupDict;
acdbCurDwg()->getGroupDictionary(pGroupDict,AcDb::kForWrite);
AcDbObjectId groupId;
AcDbGroup *pGroup;
pt.x=ptRotation.x;
pt.y=ptRotation.y;
pt.z=ptRotation.z;
if(pGroupDict->getAt(strGroupName,groupId)==Acad::eOk)
acdbOpenObject(pGroup,groupId,AcDb::kForWrite);
else
{
pGroupDict->close();
return;
}
pGroupDict->close();
AcDbGroupIterator* pIter=pGroup->newIterator();
AcDbEntity* pEnt;
AcDbObjectId objId;
pIter=pGroup->newIterator();
for(;!pIter->done();pIter->next())
{
objId=pIter->objectId();
acdbOpenAcDbEntity(pEnt,objId,AcDb::kForWrite);
rotationEntity(pEnt,pt,rotationAngle);
pEnt->close();
}
delete pIter;
pGroup->close();
}
BOOL Utils::ReadTxtFile(CString sFile, CStringArray &arComponent)
{
CString sText = _T("");
CStdioFile file;
CFileFind findfile;
if (!findfile.FindFile(sFile))return FALSE;
if(file.Open(sFile,CFile::modeRead))
{
BOOL bSuccess = TRUE;
while(bSuccess)
{
bSuccess = file.ReadString(sText);
if(bSuccess)
{
arComponent.Add(sText);
}
}
file.Close();
}
return TRUE;
}
//创建字体类型
void DrawAndEdit::createTextStyle(char *styleName, char *fontName, char *bigFontName, double textSize, double xScale, double obliqueAngle, double trPercent)
{
AcDbTextStyleTable *pTextStyleTable;
acdbHostApplicationServices()->workingDatabase()
->getSymbolTable(pTextStyleTable, AcDb::kForRead);
AcDbTextStyleTableRecord *pRecord;
pTextStyleTable->getAt(ACDB_MODEL_SPACE,pRecord,
AcDb::kForWrite);
if (!pTextStyleTable->has(styleName)){
AcDbObjectId recId;
pTextStyleTable->close();
acdbHostApplicationServices()->workingDatabase()
->getSymbolTable(pTextStyleTable, AcDb::kForWrite);
pRecord = new AcDbTextStyleTableRecord();
pRecord->setName(styleName);
pRecord->setFileName(fontName);
pRecord->setBigFontFileName(bigFontName);
pRecord->setTextSize(textSize);
pRecord->setXScale(xScale);
pRecord->setObliquingAngle(obliqueAngle);
pRecord->setPriorSize(trPercent);
pTextStyleTable->add(recId,pRecord);
pRecord->close();
}
pTextStyleTable->close();
return;
} //
//生成新组(sGroupName)
//追加数组中所有实体到该组中
//组名字 , Id数组
int createGroup(CString sGroupName,
const AcDbObjectIdArray *idArr)
{
AcDbGroup *pGroup = new AcDbGroup((LPSTR)(LPCTSTR)sGroupName);
AcDbObjectId groupObjectId;
AcDbDictionary*pGroupDict = NULL;
acdbHostApplicationServices()->workingDatabase()
->getGroupDictionary(pGroupDict, AcDb::kForWrite);
pGroupDict->setAt(sGroupName, pGroup, groupObjectId);
pGroupDict->close();
pGroup->close();
acdbOpenObject(pGroup, groupObjectId, AcDb::kForWrite);
for (int i = 0; i < idArr->length(); i++)
{
groupObjectId = idArr->at(i);
pGroup->append(groupObjectId);
}
pGroup->close();
return TRUE;
}
一样好用的文本格式表建立函数
AcDbObjectId CreateNewTextStyle(){
AcDbTextStyleTable *pTextStyleTable;
AcDbTextStyleTableRecord *pTextStyleTableRcd
AcDbObjectId textId;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable,AcDb::kForWrite);
if (!pTextStyleTable->has(StyleName)
{
AcDbTextStyleTableRecord *pTSTblRcd= new AcDbTextStyleTableRecord;
pTSTblRcd->setName(StyleName);
pTSTblRcd->setFileName(fontName);
pTSTblRcd->setBigFontFileName(bigfontName);
pTSTblRcd->setTextSize(textSize);
pTSTblRcd->setXScale(xScale);
pTSTblRcd->setObliquingAngle(obliqueAngle);
pTSTblRcd->setPriorSize(trPercent);
pTextStyleTable->add(textId,pTextStyleTableRcd);
pTSTblRcd->close();
}
pTextStyleTable->close();
return textId;
}
is Region1 in Region2?
// Function name : RgnInRgn// Description : is Region1 in Region2?
// Return type : bool
// Argument : const AcDbRegion* pRegion1
// Argument : const AcDbRegion* pRegion2
**** Hidden Message *****
遍历数据库版!
//************************************************************************
//函数名称:selectEntityInLayer
//函数类型:Acad::ErrorStatus
//返回值:
//功能描述:选择指定层上的实体,得到其对象属性标识符!
//函数作者:Darcy
//创建日期:200X-XX-XX
//参数列表:
//变量名:nLayerName 变量类型:CString 变量说明:
//变量名:nIDs 变量类型:AcDbObjectIdArray& 变量说明:
//变量名:nModelSpace 变量类型:bool 变量说明:
//************************************************************************
Acad::ErrorStatus selectEntityInLayer(
CString nLayerName,
AcDbObjectIdArray& nIDs,
bool nModelSpace
)
{
Acad::ErrorStatus es=Acad::eOk;
AcDbBlockTable* pBlockTable=NULL;
AcDbBlockTableRecord*pSpaceRecord=NULL;
if (acdbHostApplicationServices()->workingDatabase()==NULL)
return Acad::eNoDatabase;
if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead))==Acad::eOk)
{
char entryName;
if (nModelSpace)
strcpy(entryName,ACDB_MODEL_SPACE);
else
strcpy(entryName,ACDB_PAPER_SPACE);
//Get the Model or Paper Space record and open it for read:
if ((es = pBlockTable->getAt((const char*)entryName, pSpaceRecord, AcDb::kForRead))==Acad::eOk)
{
AcDbBlockTableRecordIterator* pIter;
if (pSpaceRecord->newIterator(pIter)==Acad::eOk)
{
for (pIter->start();!pIter->done();pIter->step())
{
char *name=NULL;
AcDbEntity* pEntity;
if (pIter->getEntity(pEntity,AcDb::kForRead)==Acad::eOk)
{
name=pEntity->layer();
if (nLayerName.CompareNoCase(name)==0)
nIDs.append(pEntity->objectId());
pEntity->close();
acutDelString(name);
}
}
delete pIter;
}
pSpaceRecord->close();
}
pBlockTable->close();
}
return es;
}
//=========================================================
功能: 判断两点是否相同(误差在指定范围内)
参数: p1、p2为欲判定的两点
返回值: 0 -- 不相同;1 -- 相同
//=========================================================
int Equal_Points (const ads_point p1, const ads_point p2)
{
// 指定误差范围
const ads_real Equality_Margin = (ads_real)0.00000001;
int c ;
for (c = X ; c <= Z ; c++) {
if (fabs(p1 - p2) > Equality_Margin) {
return (0) ;
}
}
return (1) ;
}
/*
时间:2003.06.26
功能:判断点 pt 是否在区域 ptArr 内
实现:根据射线法求交点个数,偶数-区域外,奇数-区域内
变量:pt 指定点 ptArr 判断区域
返回:在区域 TRUE 不在 FALSE
修改:
*/
BOOL BaseHandle::PointIsInPolygon(AcGePoint3d pt, AcGePoint3dArray ptArr)
{
int ptNum,i,interNum;
AcGePoint3d ptA,ptB;
ads_point pt0,pt1,pt2,ptIns,ptX;
interNum = 0;
pt0 = 0.0;
pt0 = 0.0;
pt0 = 0.0;
ptX = pt.x;
ptX = pt.y;
ptX = pt.z;
ptNum = ptArr.length();
for (i = 0;i < ptNum - 1;i++){
ptA = ptArr.at(i);
ptB = ptArr.at(i + 1);
pt1 = ptA.x;
pt1 = ptA.y;
pt1 = 0.0;
pt2 = ptB.x;
pt2 = ptB.y;
pt2 = 0.0;
if (acdbInters(ptX,pt0,pt1,pt2,1,ptIns) == RTNORM){
interNum++;
}
}
//上述交点没有判断是否与某一端点重合,如果有interNum-1,有
//兴趣的朋友可以修改完成。
if (interNum % 2 == 0){
return false;
}else{
return true;
}
}
如何交互生成面域对象
AcDbObjectId eId;
AcDbEntity * ent;
ads_name en;
int rc = acdbEntLast(en);//获取数据库中任意一个实体
if (rc != RTNORM)
{
acutPrintf("the database has nothing.\n", rc);
}
if(!acedCommand(RTSTR,"-boundary",RTSTR,PAUSE,RTSTR,"",RTNONE))
return; //调用命令生成边界
for(;;) //边界可能是嵌套型的,也就是有孤岛,所以进行循环
{
int rc = acdbEntNext(en,en); //获取生成的边界
if (rc != RTNORM)
break; //已经没有边界实体,退出循环
acdbGetObjectId(eId, en);
acdbOpenObject(ent, eId,AcDb::kForRead);
ent->close();
if(ent->isKindOf(AcDbPolyline::desc())) //生成的应是AcDbPolyline实体
//..................
//create region object //这时就可以根据AcDbPolyline
}; //的点的信息生成面域对象 已知一段弧的起点和终点以及其凸度,求其圆心
int getCenter(ads_point startPoint,ads_point endPoint,double bulge,ads_point& center)
{
if (bulge==0.0)
{
ads_point_set(startPoint,center);
return 0;
}
ads_point startPt,endPt;
if (bulge<0.0)
{
ads_point_set(endPoint,startPt);
ads_point_set(startPoint,endPt);
bulge=bulge*(-1.0);
}
else
{
ads_point_set(startPoint,startPt);
ads_point_set(endPoint,endPt);
}
double distStartToEnd,distX,distY,radius;
distStartToEnd=ads_distance(startPt,endPt);
distX=distStartToEnd/2.0;
distY=bulge*distX;
radius=((distX*distX)+(distY*distY))/(2.0*distY);
double tmpAng;
ads_point tmpPt;
tmpAng=ads_angle(startPt,endPt);
ads_polar(startPt,tmpAng,distX,tmpPt);
ads_polar(tmpPt,(tmpAng+(_PI/2.0)),(radius-distY),center);
return 1;
};
取得选择集包围盒
取得选择集包围盒本来是放在下面的帖子里面的,现放在这里以便收集整理
BOOL GetSSExtents(ads_name ss, ads_point &maxpt, ads_point &minpt)
{
long len;
int ret=ads_sslength(ss,&len);
if(ret!=RTNORM||len<1) return FALSE;
AcDbObjectId objId;
ads_name ename;
AcDbEntity *pEnt;
AcDbExtents extent;
AcGePoint3d pt1,pt2;
ads_ssname (ss, 0, ename);
acdbGetObjectId(objId,ename);
acdbOpenObject(pEnt,objId,AcDb::kForRead);
pEnt->getGeomExtents(extent);
pt1=extent.maxPoint();
pt2=extent.minPoint();
pEnt->close();
for(int i=0;igetGeomExtents(extent);
if(pt1extent.minPoint()||pt2>extent.minPoint())
pt2=extent.minPoint();
pEnt->close();
}
maxpt=pt1;
maxpt=pt1;
maxpt=pt1;
minpt=pt2;
minpt=pt2;
minpt=pt2;
return TRUE;
}