2021年1月1日 AutoCAD.Net/C#.Net QQ群:193522571获取任意路径dwg文件内所有块在控件中显示并能选择插入当前模型空间



主对话框CPP内代码:

#include "StdAfx.h"
#include "resource.h"
#include "BlockDialog.h"
#include "..\..\Common\我的类\file.h"
#include "..\..\Common\Document\DwgDatabaseUtil.h"
#include <dbents.h>
#include <dbmain.h>
#include "..\..\Chapter11\PreviewControl\GsPreviewCtrl.h"
#include "GsPreviewCtrl.h"
#include <gepnt3d.h>
#include <geassign.h>

CString filepatha;
CString filepath;
void CBlockDialog::OnButton1()
{

m_combo1.ResetContent();
m_combo2.ResetContent();
m_combo3.ResetContent();
m_list.ResetContent();
vector<CString> filename;
Cfile::getFiles(filepath,filename);
int i;
for (i=0;i<filename.size()-2;i++)
{
m_combo1.AddString(filename[i+2]);
}
if (m_combo1.GetCount()>0)
{
m_combo1.SetCurSel(0);

filepatha=filepath.Left(filepath.GetLength()-1);
CString ca;
m_combo1.GetWindowText(ca);
CString filepathaa=filepatha+ca;
vector<CString> filenamea;
Cfile::getFiles(filepathaa,filenamea);

for (i=0;i<filenamea.size()-2;i++)
{
m_combo2.AddString(filenamea[i+2]);
}
if (m_combo2.GetCount()>0)
{
m_combo2.SetCurSel(0);

CString filepathb=filepathaa.Left(filepathaa.GetLength()-1);
m_combo2.GetWindowText(ca);
CString filepathbb=filepathb+ca;
vector<CString> filenameb;
Cfile::getFiles(filepathbb,filenameb);

for (i=0;i<filenameb.size()-2;i++)
{
m_combo3.AddString(filenameb[i+2]);
}
if (m_combo3.GetCount()>0)
{
m_combo3.SetCurSel(0);
CString filepathc=filepathbb.Left(filepathbb.GetLength()-1);
m_combo3.GetWindowText(ca);
CString filepathcc=filepathc+ca;
AcDbDatabase *pDb=new AcDbDatabase(false);
pDb->readDwgFile(filepathcc,_SH_DENYWR);
AcDbObjectIdArray allEntIds = CDwgDatabaseUtil::GetAllEntityIds(NULL,pDb);
for (i=0;i<allEntIds.length();i++)
{
AcDbEntity *pEnt=NULL;
if (acdbOpenObject(pEnt,allEntIds[i],AcDb::kForRead)==Acad::eOk)
{
if (pEnt->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference *pBlkRef=NULL;
pBlkRef=AcDbBlockReference::cast(pEnt);
pEnt->close();
AcDbObjectId blkDefId=pBlkRef->blockTableRecord();
pBlkRef->close();
AcDbBlockTableRecord *pBlkDef=NULL;
char *blkDefName;
if (acdbOpenObject(pBlkDef,blkDefId,AcDb::kForRead)==Acad::eOk)
{
pBlkDef->getName(blkDefName);
m_list.AddString(blkDefName);
pBlkDef->close();
}
}
}
}
pDb->closeInput();
delete pDb;

}
}
}
filepath=filepath.Left(filepath.GetLength()-2);
}


void CBlockDialog::OnSelchangeCombo1()
{
m_combo2.ResetContent();
CString ca;
m_combo1.GetLBText(m_combo1.GetCurSel(),ca);
CString filepa=filepatha+ca;
vector<CString>filename;
Cfile::getFiles(filepa,filename);
int i;
for (i=0;i<filename.size()-2;i++)
{
m_combo2.AddString(filename[i+2]);
}
if (m_combo2.GetCount()>0)
{
m_combo2.SetCurSel(0);

m_combo3.ResetContent();
m_combo2.GetWindowText(ca);
filepa=filepa.Left(filepa.GetLength()-1);
filepa+=ca;
vector<CString>filenamea;
Cfile::getFiles(filepa,filenamea);

for (i=0;i<filenamea.size()-2;i++)
{
m_combo3.AddString(filenamea[i+2]);
}
if (m_combo3.GetCount()>0)
{
m_combo3.SetCurSel(0);

m_list.ResetContent();
m_combo3.GetWindowText(ca);
filepa=filepa.Left(filepa.GetLength()-1);
filepa+=ca;
AcDbDatabase *pDb=new AcDbDatabase(false);
pDb->readDwgFile(filepa,_SH_DENYWR);
AcDbObjectIdArray allEntIds = CDwgDatabaseUtil::GetAllEntityIds(NULL,pDb);
for (int i=0;i<allEntIds.length();i++)
{
AcDbEntity *pEnt=NULL;
if (acdbOpenObject(pEnt,allEntIds[i],AcDb::kForRead)==Acad::eOk)
{
if (pEnt->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference *pBlkRef=NULL;
pBlkRef=AcDbBlockReference::cast(pEnt);
pEnt->close();
AcDbObjectId blkDefId=pBlkRef->blockTableRecord();
pBlkRef->close();
AcDbBlockTableRecord *pBlkDef=NULL;
char *blkDefName;
if (acdbOpenObject(pBlkDef,blkDefId,AcDb::kForRead)==Acad::eOk)
{
pBlkDef->getName(blkDefName);
m_list.AddString(blkDefName);
pBlkDef->close();
}
}
}
}
delete pDb;
filepa="";

}

}

}

void CBlockDialog::OnSelchangeCombo2()
{
m_combo3.ResetContent();
CString ca;
CString cb;
m_combo2.GetLBText(m_combo2.GetCurSel(),ca);
m_combo1.GetWindowText(cb);
CString filepa=filepatha+cb;
filepa+="\\";
filepa=filepa+ca;
vector<CString>filename;
Cfile::getFiles(filepa,filename);

for (int i=0;i<filename.size()-2;i++)
{
m_combo3.AddString(filename[i+2]);
}
if (m_combo3.GetCount()>0)
{
m_combo3.SetCurSel(0);

m_list.ResetContent();
m_combo3.GetWindowText(ca);
filepa=filepa.Left(filepa.GetLength()-1);
filepa+=ca;

AcDbDatabase *pDb=new AcDbDatabase();
pDb->readDwgFile(filepa,_SH_DENYWR);
AcDbObjectIdArray allEntIds = CDwgDatabaseUtil::GetAllEntityIds(NULL,pDb);
for (i=0;i<allEntIds.length();i++)
{
AcDbEntity *pEnt=NULL;
if (acdbOpenObject(pEnt,allEntIds[i],AcDb::kForRead)==Acad::eOk)
{
if (pEnt->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference *pBlkRef=NULL;
pBlkRef=AcDbBlockReference::cast(pEnt);
pEnt->close();
AcDbObjectId blkDefId=pBlkRef->blockTableRecord();
pBlkRef->close();
AcDbBlockTableRecord *pBlkDef=NULL;
char *blkDefName;
if (acdbOpenObject(pBlkDef,blkDefId,AcDb::kForRead)==Acad::eOk)
{
pBlkDef->getName(blkDefName);
m_list.AddString(blkDefName);
pBlkDef->close();
}
}
}
}
delete pDb;
filepa="";
}

}

void CBlockDialog::OnSelchangeCombo3()
{
CString ca;
CString cb;
CString cc;
m_list.ResetContent();
m_combo1.GetWindowText(ca);
m_combo2.GetWindowText(cb);
m_combo3.GetLBText(m_combo3.GetCurSel(),cc);
CString filepa=filepatha+ca;
filepa+="\\";
filepa=filepa+cb;
filepa+="\\";
filepa=filepa+cc;

AcDbDatabase *pDb=new AcDbDatabase();
pDb->readDwgFile(filepa,_SH_DENYWR);
AcDbObjectIdArray allEntIds = CDwgDatabaseUtil::GetAllEntityIds(NULL,pDb);
for (int i=0;i<allEntIds.length();i++)
{
AcDbEntity *pEnt=NULL;
if (acdbOpenObject(pEnt,allEntIds[i],AcDb::kForRead)==Acad::eOk)
{
if (pEnt->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference *pBlkRef=NULL;
pBlkRef=AcDbBlockReference::cast(pEnt);
pEnt->close();
AcDbObjectId blkDefId=pBlkRef->blockTableRecord();
pBlkRef->close();
AcDbBlockTableRecord *pBlkDef=NULL;
char *blkDefName;
if (acdbOpenObject(pBlkDef,blkDefId,AcDb::kForRead)==Acad::eOk)
{
pBlkDef->getName(blkDefName);
m_list.AddString(blkDefName);
pBlkDef->close();
}
}
}
}
delete pDb;
filepa="";

}

BOOL CBlockDialog::OnInitDialog()
{
CAcUiDialog::OnInitDialog();

m_edit.SetWindowText("E:\\ARX\\资源");
m_edit.GetWindowText(filepath);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CBlockDialog::OnKillfocusEdit1()
{
m_edit.Convert();
m_edit.GetWindowText(filepath);

}

void CBlockDialog::OnSelchangeList3()
{
// TODO: Add your control notification handler code here

CString blockName;
m_list.GetText(m_list.GetCurSel(),blockName);
CString ca,cb,cc;
CString fileName;
m_combo1.GetWindowText(ca);
m_combo2.GetWindowText(cb);
m_combo3.GetWindowText(cc);

fileName=filepatha+ca;
fileName+="\\";
fileName=fileName+cb;
fileName+="\\";
fileName=fileName+cc;
AcDbDatabase *m_pDb=new AcDbDatabase();
m_pDb->readDwgFile(fileName,_SH_DENYWR);

AcDbBlockTable *pBlkTbl=NULL;
m_pDb->getSymbolTable(pBlkTbl,AcDb::kForRead);
AcDbObjectId blkDefId;
pBlkTbl->getAt(blockName,blkDefId);
pBlkTbl->close();
AcDbBlockReference *pBlkRef=new AcDbBlockReference(AcGePoint3d::kOrigin,blkDefId);

AcDbDatabase *pDb=new AcDbDatabase();
CDwgDatabaseUtil::PostToModelSpace(pBlkRef,pDb);

m_gsPreviewCtrl.SubclassDlgItem(IDC_PREVIEW,this);
m_gsPreviewCtrl.Init(PreviewControlDLL.ModuleResourceInstance(),true);
m_gsPreviewCtrl.SetDatabase(pDb);

}

void CBlockDialog::OnButton()
{

CString blockName;
m_list.GetText(m_list.GetCurSel(),blockName);
CString ca,cb,cc;
CString fileName;
m_combo1.GetWindowText(ca);
m_combo2.GetWindowText(cb);
m_combo3.GetWindowText(cc);

fileName=filepatha+ca;
fileName+="\\";
fileName=fileName+cb;
fileName+="\\";
fileName=fileName+cc;
AcDbDatabase *m_pDb=new AcDbDatabase();
m_pDb->readDwgFile(fileName,_SH_DENYWR);

AcDbBlockTable *pBlkTbl=NULL;
m_pDb->getSymbolTable(pBlkTbl,AcDb::kForRead);
AcDbObjectId blkDefId;
pBlkTbl->getAt(blockName,blkDefId);
pBlkTbl->close();

BeginEditorCommand();
ads_point pt;
if (acedGetPoint(NULL,"\n输入一个点:",pt)!=RTNORM)
{
CompleteEditorCommand();

}
else
{
AcGePoint3d pt3d=asPnt3d(pt);
AcDbBlockReference *pBlkRef=new AcDbBlockReference(pt3d,blkDefId);
AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
CDwgDatabaseUtil::PostToModelSpace(pBlkRef,pDb);
pBlkRef->close();
CompleteEditorCommand();
}

}

原文地址:https://www.cnblogs.com/NewAutoMan/p/5125381.html