object arx 编程实现 AutoCAD 的查找命令 部分源代码

// CArxDlg.cpp: 实现文件
//

#include "StdAfx.h"
#include "afxdialogex.h"
#include "resource.h"
#include <stdexcept>
#include "SelectSet.h"
#include "View.h"
#include "CArxDlg.h"
// CArxDlg 对话框

IMPLEMENT_DYNAMIC(CArxDlg, CAcUiDialog)

CArxDlg::CArxDlg(CWnd* pParent /*=nullptr*/)
	: CAcUiDialog(IDD_ARX_MFC, pParent)
	, mRadioSelect(0)
{
}

CArxDlg::~CArxDlg()
{
}

void CArxDlg::DoDataExchange(CDataExchange* pDX)
{
	CAcUiDialog::DoDataExchange(pDX);
	DDX_Radio(pDX, IDC_RADIO_BlockRef, mRadioSelect);
}

// CArxDlg 消息处理程序
BEGIN_MESSAGE_MAP(CArxDlg, CAcUiDialog)
	ON_BN_CLICKED(IDC_BTN_FIND, &CArxDlg::OnClickedBtnFind)
	ON_LBN_DBLCLK(IDC_LIST_NAME, &CArxDlg::OnLbnDblclkListName)
END_MESSAGE_MAP()


std::vector<AcGePoint3d> selectPoint;
//
//
void CArxDlg::OnClickedBtnFind()
{
	UpdateData(); // 关键
	CListBox* listbox = (CListBox *)GetDlgItem(IDC_LIST_NAME);
	listbox->ResetContent();  // 列表框清空
	selectPoint.clear();	  // 清空 查找到的 姓名 的 点 的坐标
	// 获得 编辑框 的输入内容
	CStringW name;
	CEdit* edit = (CEdit *)GetDlgItem(IDC_EDIT_NAME);
	edit->GetWindowTextW(name);
	
	switch (mRadioSelect)
	{
	case 0: 
		FindNameFromBlkRef(name, listbox);
		break;
	case 1: 
		FindNameFromText(name, listbox);
		break;
	case 2: // rb = acutBuildList(RTDXF0, _T("MTEXT"), 8, _T("注记"), RTNONE);
		break;
	default:
		break;
	}
}
//
//
//
void CArxDlg::FindNameFromBlkRef(CStringW &name, CListBox *listbox)
{
	struct resbuf *rb; CSelectSet ss;
	rb = acutBuildList(RTDXF0, _T("INSERT"), 8, _T("注记"), RTNONE);
	std::vector<AcDbBlockReference*> blkrefs = ss.GetAllEntitys<AcDbBlockReference>(rb);
	ss.GetAttribute(blkrefs);
	std::vector<AcGePoint3d>::vector::iterator itp = namePoints.points.begin();
	for (std::vector<std::wstring>::iterator itn = namePoints.names.begin(); itn != namePoints.names.end(); itn++, itp++)
	{
		if (!itn->compare(name.GetString()))
		{
			listbox->AddString(name.GetString());
			selectPoint.push_back(*itp);
		}
	}
}
//
//
//
void CArxDlg::FindNameFromText(CStringW &name, CListBox *listbox) {
	struct resbuf *rb; CSelectSet ss;
	rb = acutBuildList(RTDXF0, _T("TEXT"), 8, _T("汉字"), RTNONE);
	std::vector<AcDbText*> texts = ss.GetAllEntitys<AcDbText>(rb);
	for (std::vector<AcDbText*>::iterator it = texts.begin(); it != texts.end(); it++)
	{
		AcString text;
		(*it)->textString(text);
		// acutPrintf(_T("%s"), text.substr(5, -1).kACharPtr());
		if (!text.substr(4, -1).compare(name)) {  //  截取字符串 从第五个字符开始
			listbox->AddString(name.GetString());
			selectPoint.push_back((*it)->position());
			acutPrintf(_T("\n%f,%f"), (*it)->position().x, (*it)->position().y);
		}
		(*it)->close();  //  释放 对象
	}
}

void CArxDlg::OnLbnDblclkListName()
{
	UpdateData();
	CListBox* listbox = (CListBox *)GetDlgItem(IDC_LIST_NAME);
	int index = listbox->GetCurSel();
	AcGePoint3d point = selectPoint.at(index);
	switch (mRadioSelect)
	{
	case 0:CMyView::setViewOnPointBlockRef(point);
		break;
	case 1:CMyView::setViewOnPointText(point);
		break;
	case 2:
	default:
		break;
	}
	
}
#pragma once
#include <gepnt2d.h>
#include <gepnt3d.h>
#include <dbsymtb.h>
#include <aced.h>
class CMyView
{
public:
	CMyView();
	~CMyView();
	static void setViewOnPointBlockRef(const AcGePoint3d& point);
	static void setViewOnPointText(const AcGePoint3d &point);
	static AcGePoint3d WcsToDcs(const AcGePoint3d& point);

private:

};

CMyView::CMyView()
{
}

CMyView::~CMyView()
{
}
//
// 根据 查找到内容的 坐标点 缩放视图 并且 查找内容属于 AcDbBlockReference
//
void CMyView::setViewOnPointBlockRef(const AcGePoint3d & point)
{
	// acutPrintf(_T("\nsetView %f,%f"), CMyView::WcsToDcs(point).x, CMyView::WcsToDcs(point).y);
	// acutPrintf(_T("\n 2d %f,%f"), point.convert2d(AcGePlane()).x, point.convert2d(AcGePlane()).y);
	AcGePoint2d pnt2D = CMyView::WcsToDcs(point).convert2d(AcGePlane());
	AcDbViewTableRecord view;
	view.setCenterPoint(AcGePoint2d(pnt2D.x, pnt2D.y-15));
	view.setHeight(35);
	view.setWidth(35);
	acedSetCurrentView(&view, NULL);
	view.close();
}

//
//  根据 查找到内容的 坐标点 缩放视图 并且 查找内容属于 AcDbText
//
void CMyView::setViewOnPointText(const AcGePoint3d &point) {
	AcDbViewTableRecord view;
	// AcGePoint2d pnt2d = point.convert2d(AcGePlane());
	view.setCenterPoint(AcGePoint2d(point.x, point.y - 15));
	view.setHeight(35);
	view.setWidth(35);
	acedSetCurrentView(&view, NULL);
	view.close();
}
//
//  世界坐标系 转 用户坐标系
//
AcGePoint3d CMyView::WcsToDcs(const AcGePoint3d & point)
{
	AcGePoint3d pnt;
	struct resbuf rbFrom, rbTo;
	rbFrom.restype = RTSHORT;
	rbFrom.resval.rint = 0;
	rbTo.restype = RTSHORT;
	rbTo.resval.rint = 2;
	acedTrans(asDblArray(point), &rbFrom, &rbTo, Adesk::kFalse, asDblArray(pnt));
	return pnt;
}
#pragma once
#include <ads.h>
#include <adsdef.h>
#include <acedads.h>
#include <vector>
#include <adscodes.h>  // RTDXF0
#include <tchar.h>
#include <winnt.h>
#include <gepnt2d.h>
#include <gepnt3d.h>
#include <dbents.h>
#include <string>
// #define RESBUF(classname) \
//	struct resbuf *rb = acutBuildList(RTDXF0, _T(#classname), /*8, _T(""),*/ RTNONE)
struct Namepoint {
	std::vector<std::wstring> names;
	std::vector<AcGePoint3d> points;
} namePoints;

//
// 选择集类
//
class CSelectSet
{
public:
	int GetSelectSetMousePick(ads_name);
	int GetSelectSetResBuf(ads_name, resbuf*);
	template<class T>
	std::vector<T*> GetAllEntitysMousePick();
	template<class T>
	std::vector<T*> GetAllEntitys(resbuf*);

	void
		GetAttribute(std::vector<AcDbBlockReference*> blkrefs);

};
//
// 选择全部实体
// 鼠标选取
//
int CSelectSet::GetSelectSetMousePick(ads_name outSS)
{
	int rc;
	rc = acedSSGet(NULL, NULL, NULL, NULL, outSS);
	return 0;
}
//
// 选择全部实体
// 根据选择集
//
int CSelectSet::GetSelectSetResBuf(ads_name outSS, resbuf *rb) {
	int rc;
	rc = acedSSGet(_T("X"), NULL, NULL, rb, outSS);  //  根据选择集选取
	return 0;
}
// 获得全部某种类型实体
// 鼠标选取
//
template<class T>
std::vector<T*> CSelectSet::GetAllEntitysMousePick() {

	int rc;  ads_name ssEntName, outSS;  int len;
	GetSelectSetMousePick(outSS);
	std::vector<T*> rets;
	acedSSLength(outSS, &len);
	for (size_t i = 0; i < len; i++)
	{
		acedSSName(outSS, i, ssEntName);
		AcDbObjectId objId;
		acdbGetObjectId(objId, ssEntName);
		AcDbEntity* pEnt;
		acdbOpenObject(pEnt, objId, AcDb::kForRead);

		if (pEnt->isKindOf(T::desc())) {
			rets.push_back(T::cast(pEnt));
		}
		else {
			pEnt->close();
		}
	}
	acedSSFree(outSS);  // 选择集释放
	return rets;
}

//
// 获得所有某种类型实体
// 过滤器全部选中
//
template<class T>
std::vector<T*> CSelectSet::GetAllEntitys(resbuf *rb) {
	int rc, len;
	ads_name outSS, ssEntName;
	std::vector<T*> rets;
	// struct resbuf *rb = acutBuildList(RTDXF0, _T("INSERT"), /*8, _T(""),*/ RTNONE); // 过滤属性块
	// RESBUF(insert);
	GetSelectSetResBuf(outSS, rb);
	acedSSLength(outSS, &len);
	for (size_t i = 0; i < len; i++)
	{
		acedSSName(outSS, i, ssEntName);
		AcDbObjectId objId;
		acdbGetObjectId(objId, ssEntName);
		AcDbEntity* pEnt;
		acdbOpenObject(pEnt, objId, AcDb::kForRead);

		if (pEnt->isKindOf(T::desc())) {
			rets.push_back(T::cast(pEnt));
		}
		else {
			pEnt->close();
		}
	}
	acedSSFree(outSS); acutRelRb(rb);
	return rets;
}
//
// 获得块参照各个属性及坐标
// 产权人
void
CSelectSet::GetAttribute(std::vector<AcDbBlockReference*> blkrefs)
{
	namePoints.names.clear(); namePoints.points.clear();
	for (std::vector<AcDbBlockReference*>::iterator it = blkrefs.begin(); it != blkrefs.end(); it++)
	{
		// 为块参照新建一个属性迭代器
		AcDbObjectIterator* pAttIterator = (*it)->attributeIterator();
		for (pAttIterator->start(); !pAttIterator->done(); pAttIterator->step())
		{
			AcDbObjectId attId = pAttIterator->objectId();

			AcDbAttribute *pAtt = NULL;
			(*it)->openAttribute(pAtt, attId, AcDb::kForRead);

			// 获得标签
			AcString tag;
			pAtt->tag(tag);

			// 获得指定标签的属性
			if (!tag.compare(_T("产权人"))) { 
				namePoints.names.push_back(pAtt->textString()); 
				namePoints.points.push_back(pAtt->position());
			}
				
			/*	else if (tag == "编号")
					no = pAtt->textString();
				else if (tag == "坐落门牌")
					address = pAtt->textString();*/

			pAtt->close();   // 属性
		}
		(*it)->close();      // 块参照
		delete pAttIterator; // 块参照迭代器
		pAttIterator = NULL;
	}
	// return namePoints;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值