明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7136|回复: 16

共享一些代码(转自其他的论坛)

  [复制链接]
发表于 2008-10-23 13:58:00 | 显示全部楼层 |阅读模式

共享一些常用的代码

(此代码的出自是c3p论坛的,单纯为了知识,让知识更集中起来,如果有不妥,麻烦告诉我呀)

//获得所有的实体
Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
const AcArray& layerNameArr)
{
Acad::ErrorStatus es=Acad::eOk;
ASSERT(pDb);
if(pDb==NULL)
return Acad::eInvalidInput;
AcDbBlockTable *pBlkTable=NULL;
if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
{
acedAlert("打开块表失败");
return es;
}
AcDbBlockTableRecord *pBlkTableRecord=NULL;
if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
{
acedAlert("打开块表记录失败");
pBlkTable->close();
return es;
}
pBlkTable->close();//关闭块表

AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
{
pBlkTableRecord->close();
return es;
}

CAcModuleResourceOverride resoverride;
CProgressDlg progress;
progress.Create();
progress.SetPos(0);
progress.SetWindowText("正在检测图形中所有实体...");

for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
{
AcDbEntity *entity=NULL;
es=pIterator->getEntity(entity,AcDb::kForRead); //打开实体

if(es==Acad::eLockViolation)
{
acedAlert("内存锁定");
}
else if(es==Acad::eWasOpenForWrite)
{
acedAlert("实体以写方式打开");
}
else if(es==Acad::eWasOpenForRead)
{
acedAlert("实体以读方式打开");
}
else
{
if(layerNameArr.contains(entity->layer()))
IdArr.append(entity->objectId());
entity->close();
}
progress.StepIt();
}
delete pIterator;pIterator=NULL;
pBlkTableRecord->close();
acutPrintf("eend");
return es;
}

//得到指定层上指定颜色的所有实体
Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
const AcArray& layerNameArr,Adesk::UInt16 colorIndex)
{
Acad::ErrorStatus es=Acad::eOk;
ASSERT(pDb);
if(pDb==NULL)
return Acad::eInvalidInput;
AcDbBlockTable *pBlkTable=NULL;
if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
{
acedAlert("打开块表失败");
return es;
}
AcDbBlockTableRecord *pBlkTableRecord=NULL;
if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
{
acedAlert("打开块表记录失败");
pBlkTable->close();
return es;
}
pBlkTable->close();//关闭块表

AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
{
pBlkTableRecord->close();
return es;
}

CAcModuleResourceOverride resoverride;
CProgressDlg progress;
progress.Create();
progress.SetPos(0);
progress.SetWindowText("正在检测图形中所有实体...");

for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
{
AcDbEntity *entity=NULL;
if((es=pIterator->getEntity(entity,AcDb::kForRead))!=Acad::eOk)//打开实体
{
delete pIterator;pIterator=NULL;
pBlkTableRecord->close();
return es;
}
if(layerNameArr.contains(entity->layer())&&entity->colorIndex()==colorIndex)
IdArr.append(entity->objectId());
entity->close();
progress.StepIt();
}
delete pIterator;pIterator=NULL;
pBlkTableRecord->close();
return es;
}

//得到图形中所有的层
Acad::ErrorStatus CDrawFunction::getAllLayerName(AcDbDatabase *pDb,CStringArray& layerArray)
{
Acad::ErrorStatus es=Acad::eOk;
if(pDb==NULL)
return Acad::eInvalidInput;
layerArray.RemoveAll();
AcDbLayerTable *pLayerTable=NULL;
if((es=pDb->getSymbolTable(pLayerTable,AcDb::kForRead))!=Acad::eOk)
{
pLayerTable->close();
return es;
}
//创建一个层表迭代器
AcDbLayerTableIterator *pLayerTableIterator;
pLayerTable->newIterator(pLayerTableIterator);
pLayerTable->close();

char *pLayerName=NULL;
CString name;
for(int i=0;!pLayerTableIterator->done();pLayerTableIterator->step(),i++)
{
AcDbLayerTableRecord *pLayerTableRecord=NULL;
pLayerTableIterator->getRecord(pLayerTableRecord,AcDb::kForRead);
pLayerTableRecord->getName(pLayerName);
name.Format("%s",pLayerName);
pLayerTableRecord->close();
layerArray.Add(name);

}

if(pLayerName) acutDelString(pLayerName);
delete pLayerTableIterator;pLayerTableIterator=NULL;
return es;
}


//*************根据线形名得到线型ID********************//
//******************************************************//
BOOL CDrawFunction::getLinetypeIdFromString(const char* str, AcDbObjectId& id)
{
//----查找安装目录----//
CString AcadInstallPath;
FindAcadInstallPath(AcadInstallPath);
CString File=AcadInstallPath+"\\linetype\\user.lin";
//-----查找完毕--------//
ASSERT(str!=NULL);
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
Acad::ErrorStatus mess;
mess=pLinetypeTable->getAt(str,id);
if(mess==Acad::eKeyNotFound||mess==Acad::ePermanentlyErased)
{
pLinetypeTable->close();
Acad::ErrorStatus error;

error=acdbLoadLineTypeFile(str,File.GetBuffer(0),acdbHostApplicationServices()->workingDatabase());
if(error==Acad::eNullObjectPointer)
{
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
pLinetypeTable->getAt("CONTINUOUS", id);
pLinetypeTable->close();
return FALSE;
}
else if(error==Acad::eFileSystemErr)
{
AfxMessageBox("the specified file cannot be opened");
return FALSE;
}
else if(error==Acad::eUndefinedLineType)
{
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
AfxMessageBox("the linetype name specified by ltname is not found in the file");
pLinetypeTable->getAt("CONTINUOUS", id);
pLinetypeTable->close();
return TRUE;
}
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
pLinetypeTable->getAt(str,id);
pLinetypeTable->close();
return TRUE;
}
pLinetypeTable->close();
return TRUE;
}



void CDrawFunction::FindAcadInstallPath(CString &AcadInstallPath)
{
//查找样式目录安装路径
TCHAR AcadPath[255];
HKEY hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\线路处开发组\\RDS2002\\1.00"),0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
{
::AfxMessageBox("注册路径不对");
return ;
}
DWORD dwDataType=REG_SZ;
DWORD dwLength=255;
LONG lRet=RegQueryValueEx(hKey,TEXT("path"),NULL,NULL,(LPBYTE)AcadPath,&dwLength);
RegCloseKey(hKey);
if(lRet!=ERROR_SUCCESS)
{
acutPrintf("Read failed\n");
return ;
}
//-----查找完毕--------//
AcadInstallPath.Format("%s",AcadPath);
}

AcDbObjectId CDrawFunction::createTextStyle(CString fontName,CString bigFontName,CString textStyleName)
{
AcGiTextStyle *TextStyle=new AcGiTextStyle
(fontName,
bigFontName,
0,
0.67,
0,
0,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
textStyleName); //字体名
AcDbObjectId textStyleId;
toAcDbTextStyle(*TextStyle,textStyleId);
return textStyleId;
}



//*******************wuweifan2002.12.12*************************//
//********************插入多行文本***************************//
//************************************************************//
AcDbObjectId CDrawFunction::createMutiText(AcGePoint3d BasePoint,AcDb::TextHorzMode hMode,AcDb::TextVertMode vMode,CString Text,double texthight,double widthfactor,double angle,int color,CString smallFontName,CString bigFontName,CString layerName)
{
ASSERT(Text!=NULL);
AcDbMText *pMText=new AcDbMText();
if(pMText==NULL)
throw Acad::eOutOfMemory;
AcDbObjectId TextStyleId;
TextStyleId=createTextStyle(smallFontName,bigFontName,"xianlu");
pMText->setTextStyle(TextStyleId);
pMText->setContents(Text.GetBuffer(Text.GetLength()));
pMText->setTextHeight(texthight);
pMText->setRotation(angle);
pMText->setLineSpacingFactor(0.8);
pMText->setColorIndex(color);
if(layerName!="")
pMText->setLayer(layerName.GetBuffer(0));
AcDbObjectId MTextId;
addToModelSpace(MTextId, pMText);
pMText->close();
return MTextId;
}
本帖隐藏的内容需要回复才可以浏览

//**********************扩展记录操作(2002.12.12)***************//
//****************打开扩展记录为写******************************//
//**************************************************************//
AcDbDictionary* CDrawFunction:penDictionaryForWrite(LPCTSTR dictName,
bool createIfNotFound,AcDbDictionary* parentDict)
{
ASSERT(dictName != NULL);
ASSERT(parentDict != NULL);
ASSERT(parentDict->isWriteEnabled());
AcDbDictionary* dict = NULL;
AcDbObject* obj;
Acad::ErrorStatus es;
es = parentDict->getAt(dictName, obj, AcDb::kForWrite);
if (es == Acad::eOk)
{
dict = AcDbDictionary::cast(obj);
}
else if (es == Acad::eKeyNotFound)
{
if (createIfNotFound)
{
dict = new AcDbDictionary;
AcDbObjectId dictId;
es = parentDict->setAt(dictName, dict, dictId);
if (es != Acad::eOk)
{
delete dict;dict = NULL;
}
}
}
return dict;
}

 楼主| 发表于 2008-10-24 19:12:00 | 显示全部楼层
虽然很简单,但是也需要大家捧捧呀。。太打击新人信心了。。。有时候你不经意的一句话,一个微小的举动,或许是对别人来说是很大的鼓舞和鼓励,尤其是对于我们这些新人。。。比如说:你很平常的一个字,好,不错,再接再厉。。那样才至于不让我们有:班门弄斧的感觉。。。
发表于 2008-10-24 23:47:00 | 显示全部楼层
支持一下!
发表于 2008-11-5 16:15:00 | 显示全部楼层

不错,对新手很有帮助

发表于 2008-11-10 08:35:00 | 显示全部楼层
好,继续
发表于 2008-11-13 11:57:00 | 显示全部楼层

请教高手一个问题呀!我想用OBJECTARX2008在VS2005平台上把我原来(ADS程序)移植过来(很多年前做的一些小功能),但是现在编译时总是提示这样的问题:

error C2664: 'Acad::ErrorStatus AcDbSymbolTableRecord::getName(ACHAR *&) const' : cannot convert parameter 1 from 'char *' to 'ACHAR *&'

原代码为:

if ( acdbOpenObject ((AcDbObject *&)pCurEntBlock, blockId, AcDb::kForRead) == Acad::eOk )
   {
         pCurEntBlock->getName(blockName);。。。。

而且后面还有很多这样的问题,请高手指点一下,鲜花献上!

发表于 2008-11-13 13:12:00 | 显示全部楼层

if ( acdbOpenObject ((AcDbObject *&)pCurEntBlock, blockId, AcDb::kForRead) == Acad::eOk )
   {

         //TCHAR* blockName;blockName 类型的问题。在2005下用的是unicode
         pCurEntBlock->getName(blockName);

发表于 2008-11-23 14:25:00 | 显示全部楼层

/得到ARX当前位置

g_strCurPath = acedGetAppName();

int nPos = g_strCurPath.ReverseFind('\\');

CString strDbxPath = g_strCurPath.Left(nPos+1);

#include "acdocman.h"
//得到文档位置

AcApDocument* pDoc = acDocManager->curDocument();

CString path=pDoc->fileName();

获取当前AutoCAD的路径

string filename = Process.GetCurrentProcess().MainModule.FileName;
得到AutoCAD的完整路径,包含acad.exe文件名,如C:\Program Files\AutoCAD 2008\acad.exe
string path = Path.GetDirectoryName(filename);
得到AutoCAD的路径,如C:\Program Files\AutoCAD 2008

发表于 2008-12-28 10:17:00 | 显示全部楼层
发表于 2009-4-1 19:38:00 | 显示全部楼层
太好了,顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 07:18 , Processed in 0.204499 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表