CAD二次开发学习笔记五(在ObjectARX中使用MFC)
要实现的功能是:
执行ArxModal命令,弹出如图所示对话框
选择点,则得到点坐标,选择角度则得到角度值。
步骤一:
新建基于MFC的ObjectArx项目,
参考:http://www.cnblogs.com/greatverve/archive/2010/05/31/ObjectARX-HelloWorld.html
打开资源视图添加一个对话框ID修改为IDD_ARX_MODAL
(右击资源视图中的对话框打开属性面板,可以修改ID)
设计如图界面,ID如下:
IDC_BUTTON_POINT
IDC_BUTTON_ANGLE
IDC_EDIT_XPT
IDC_EDIT_YPT
IDC_EDIT_ZPT
IDC_EDIT_ANGLE
选择两个Button把Owner Draw设置为True
完成界面。
步骤二:
打开类视图,右击项目->添加类(这里不是右击对话框添加类)
这张图有点小错误,这里Dialog ID:IDD_ARX_MODALClass name:CArxDialog
在类视图中右击CArxDialog类添加变量
这样会在头文件中生成
源文件中生成
根据这个规律添加其他变量
CAcUiPickButton m_btnAngle;
CAcUiPickButton m_btnPoint;
CAcUiNumericEdit m_editXpt;
CAcUiNumericEdit m_editYpt;
CAcUiNumericEdit m_editZpt;
CAcUiAngleEdit m_editAngle;
void CArxDialog::DoDataExchange (CDataExchange *pDX) {
CAcUiDialog::DoDataExchange (pDX) ;
DDX_Control(pDX, IDC_BUTTON_ANGLE, m_btnAngle);
DDX_Control(pDX, IDC_BUTTON_POINT, m_btnPoint);
DDX_Control(pDX, IDC_EDIT_XPT, m_editXpt);
DDX_Control(pDX, IDC_EDIT_YPT, m_editYpt);
DDX_Control(pDX, IDC_EDIT_ZPT, m_editZpt);
DDX_Control(pDX, IDC_EDIT_ANGLE, m_editAngle);
}
步骤三:
为CArxDialog添加InitDialog消息响应。
方法是打开类视图,右击->属性
再添加OnClose()响应函数
在头文件中添加几个变量
public:
CString m_strAngle;
CString m_strZPt;
CString m_strYPt;
CString m_strXPt;
在头文件中定义两函数
void DisplayPoint();
void DisplayAngle();
分别为两个按钮添加单击事件,为四个编辑框添加失去焦点事件。
步骤四:
打开acrxEntryPoint.cpp添加#include “ArxDialog.h”
运行结果如图
源码如下
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//-----------------------------------------------------------------------------
//----- acrxEntryPoint.h
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "ArxDialog.h"
//-----------------------------------------------------------------------------
#define szRDS _RXST("")
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CCADMFCApp : public AcRxArxApp {
public:
CCADMFCApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
// TODO: Load dependencies here
// You *must* call On_kInitAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
// TODO: Add your initialization code here
return (retCode) ;
}
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
// TODO: Add your code here
// You *must* call On_kUnloadAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;
// TODO: Unload dependencies here
return (retCode) ;
}
virtual void RegisterServerComponents () {
}
// - CADMFC.showDlg command (do not rename)
static void CADMFCshowDlg(void)
{
// Add your code for command CADMFC.showDlg here
//防止资源冲突
CAcModuleResourceOverride resOverride;
//显示ObjectARX的模态对话框
CArxDialog theDialog;
theDialog.DoModal();
}
} ;
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CCADMFCApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CCADMFCApp, CADMFC, showDlg, MyCommand1, ACRX_CMD_TRANSPARENT, NULL)
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//-----------------------------------------------------------------------------
//----- ArxDialog.h : Declaration of the CArxDialog
//-----------------------------------------------------------------------------
#pragma once
//-----------------------------------------------------------------------------
#include "acui.h"
//-----------------------------------------------------------------------------
class CArxDialog : public CAcUiDialog {
DECLARE_DYNAMIC (CArxDialog)
public:
CArxDialog (CWnd *pParent =NULL, HINSTANCE hInstance =NULL) ;
enum { IDD = IDD_ARX_MODAL} ;
protected:
virtual void DoDataExchange (CDataExchange *pDX) ;
afx_msg LRESULT OnAcadKeepFocus (WPARAM, LPARAM) ;
DECLARE_MESSAGE_MAP()
private:
CAcUiPickButton m_btnAngle;
CAcUiPickButton m_btnPoint;
CAcUiNumericEdit m_editXpt;
CAcUiNumericEdit m_editYpt;
CAcUiNumericEdit m_editZpt;
CAcUiAngleEdit m_editAngle;
public:
virtual BOOL OnInitDialog();
afx_msg void OnClose();
public:
CString m_strAngle;
CString m_strZPt;
CString m_strYPt;
CString m_strXPt;
void DisplayPoint();
void DisplayAngle();
afx_msg void OnBnClickedButtonPoint();
afx_msg void OnBnClickedButtonAngle();
afx_msg void OnEnKillfocusEditXpt();
afx_msg void OnEnKillfocusEditYpt();
afx_msg void OnEnKillfocusEditZpt();
afx_msg void OnEnKillfocusEditAngle();
} ;
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//-----------------------------------------------------------------------------
//----- ArxDialog.cpp : Implementation of CArxDialog
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "ArxDialog.h"
//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC (CArxDialog, CAcUiDialog)
BEGIN_MESSAGE_MAP(CArxDialog, CAcUiDialog)
ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_POINT, &CArxDialog::OnBnClickedButtonPoint)
ON_BN_CLICKED(IDC_BUTTON_ANGLE, &CArxDialog::OnBnClickedButtonAngle)
ON_EN_KILLFOCUS(IDC_EDIT_XPT, &CArxDialog::OnEnKillfocusEditXpt)
ON_EN_KILLFOCUS(IDC_EDIT_YPT, &CArxDialog::OnEnKillfocusEditYpt)
ON_EN_KILLFOCUS(IDC_EDIT_ZPT, &CArxDialog::OnEnKillfocusEditZpt)
ON_EN_KILLFOCUS(IDC_EDIT_ANGLE, &CArxDialog::OnEnKillfocusEditAngle)
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
CArxDialog::CArxDialog (CWnd *pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (CArxDialog::IDD, pParent, hInstance) {
}
//-----------------------------------------------------------------------------
void CArxDialog::DoDataExchange (CDataExchange *pDX) {
CAcUiDialog::DoDataExchange (pDX) ;
DDX_Control(pDX, IDC_BUTTON_ANGLE, m_btnAngle);
DDX_Control(pDX, IDC_BUTTON_POINT, m_btnPoint);
DDX_Control(pDX, IDC_EDIT_XPT, m_editXpt);
DDX_Control(pDX, IDC_EDIT_YPT, m_editYpt);
DDX_Control(pDX, IDC_EDIT_ZPT, m_editZpt);
DDX_Control(pDX, IDC_EDIT_ANGLE, m_editAngle);
}
//-----------------------------------------------------------------------------
//----- Needed for modeless dialogs to keep focus.
//----- Return FALSE to not keep the focus, return TRUE to keep the focus
LRESULT CArxDialog::OnAcadKeepFocus (WPARAM, LPARAM) {
return (TRUE) ;
}
BOOL CArxDialog::OnInitDialog()
{
CAcUiDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
//设置点的范围
m_editXpt.SetRange(-100.0,100.0);
m_editYpt.SetRange(-100.0,100.0);
m_editZpt.SetRange(-100.0,100.0);
//设置角度的输入范围
m_editAngle.SetRange(0.0,90.0);
//加载默认的位图
m_btnPoint.AutoLoad();
m_btnAngle.AutoLoad();
//设置文本框的默认值
m_strAngle = _T("0.0");
m_strXPt = _T("0.0");
m_strYPt = _T("0.0");
m_strZPt = _T("0.0");
//显示初始点的坐标和角度值
DisplayPoint();
DisplayAngle();
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CArxDialog::DisplayPoint()
{
//在对话框中显示点的坐标
m_editXpt.SetWindowText(m_strXPt);
m_editXpt.Convert();//更新控件和其关联的成员变量
m_editYpt.SetWindowText(m_strYPt);
m_editYpt.Convert();
m_editZpt.SetWindowText(m_strZPt);
m_editZpt.Convert();
}
void CArxDialog::DisplayAngle()
{
m_editAngle.SetWindowText(m_strAngle);
m_editAngle.Convert();
}
void CArxDialog::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//double x=atof(m_strXPt);//错误 1 error C2664: “atof”: 不能将参数 1 从“CString”转换为“const char *”
double x=_tstof(m_strXPt);
double y=_tstof(m_strYPt);
double z=_tstof(m_strZPt);
acutPrintf(_T("\n选择的点坐标为(%.2f,%.2f,%.2f)."),x,y,z);
CAcUiDialog::OnClose();
}
void CArxDialog::OnBnClickedButtonPoint()
{
// TODO: 在此添加控件通知处理程序代码
//隐藏对话框把控制权交给AutoCad
BeginEditorCommand();
//提示用户输入一个点
ads_point pt;
if(acedGetPoint(NULL,_T("\n输入一个点:"),pt)==RTNORM)
{
//如果点有效,继续执行
CompleteEditorCommand();
m_strXPt.Format(_T("%.2f"),pt[X]);
m_strYPt.Format(_T("%.2f"),pt[Y]);
m_strZPt.Format(_T("%.2f"),pt[Z]);
//显示点的坐标
DisplayPoint();
}
else
{
CancelEditorCommand();
}
}
void CArxDialog::OnBnClickedButtonAngle()
{
// TODO: 在此添加控件通知处理程序代码
//把隐藏对话框把控制权交给autocad
BeginEditorCommand();
//将当前选择的点的位置作为基点
ads_point pt;
acdbDisToF(m_strXPt,-1,&pt[X]);
acdbDisToF(m_strYPt,-1,&pt[Y]);
acdbDisToF(m_strZPt,-1,&pt[Z]);
//提示用户输入一点
double angle;
const double PI = 4*atan(1.0);//错误 1 error C2668: “atan”: 对重载函数的调用不明确 d:\codes\cpp\dlg\dlg\arxdlg.cpp 167
if(acedGetAngle(pt,_T("\n输入角度:"),&angle)==RTNORM)
{
//如果正确获得角度,返回对话框
CompleteEditorCommand();
//将角度值转换为弧度值
m_strAngle.Format(_T("%.2f"),angle*(180.0/PI));
//显示角度值
DisplayAngle();
}
else
{
//否则取消命令(包括对话框)
CancelEditorCommand();
}
}
void CArxDialog::OnEnKillfocusEditXpt()
{
// TODO: 在此添加控件通知处理程序代码
m_editXpt.Convert();
m_editXpt.GetWindowText(m_strXPt);
}
void CArxDialog::OnEnKillfocusEditYpt()
{
// TODO: 在此添加控件通知处理程序代码
m_editYpt.Convert();
m_editYpt.GetWindowText(m_strYPt);
}
void CArxDialog::OnEnKillfocusEditZpt()
{
// TODO: 在此添加控件通知处理程序代码
//获得并更新用户输入的值
m_editZpt.Convert();
m_editZpt.GetWindowText(m_strZPt);
}
void CArxDialog::OnEnKillfocusEditAngle()
{
// TODO: 在此添加控件通知处理程序代码
m_editAngle.Convert();
m_editAngle.GetWindowText(m_strAngle);
}
参考:
本文word:http://files.cnblogs.com/greatverve/cad-mfc.rar
CAD二次开发学习笔记五(在ObjectARX中使用MFC)相关推荐
- 基于C#的中望CAD二次开发学习笔记(1)环境测试
目录 前言 一.ZRXSDK的安装使用 二.创建项目 三.编写环境测试代码 四.在ZWCAD中测试 参考资料 总结 前言 作为一个设计院搬砖人,和各种CAD打交道是必不可少的.当然,其中最为正统的是A ...
- 引用:基于C#的中望CAD二次开发学习笔记
目录 前言 一.ZRXSDK的安装使用 二.创建项目 三.编写环境测试代码 四.在ZWCAD中测试 参考资料 总结 前言 作为一个设计院搬砖人,和各种CAD打交道是必不可少的.当然,其中最为正统的是A ...
- C#进行CAD二次开发学习笔记--02
目录 Editor 拖动类EntityJig 选择集 Editor 在C#进行CAD二次开发时,Editor类是一个特别有用的类.它提供了大量常用的接口函数,比如: // 拖动相关接口 public ...
- CAD二次开发学习笔记四(得到选中的实体,修改实体,如等分线段)
AcGeVector3d是点阵的集合,通过等分点的差集得到. 新的点可以通过点与点阵相差得到. 大气象 public: // - ArxProject2.partLine command (do no ...
- C#进行CAD二次开发学习笔记-01
一些基础知识 需要引用CAD的库文件 常用接口和类 与C++ ---- ObjectArx库的一些区别 需要引用CAD的库文件 accoremad.dll acdbmgd.dll acmgd.dll ...
- CAD二次开发学习笔记二(创建一个对话框)
打开资源视图->右击->添加资源->Dialog 双击对话框,弹出MFC类向导,输入类名FirstClass, 确定,创建对话框类.FirstClass.h与FirstClass.c ...
- cad二次开发 java_基于.NET的CAD二次开发学习笔记一:CAD开发入门
1.AutoCAD .NET API由不同的DLL文件组成,它们提供用于访问图形文件或AutoCAD应用程序的包含丰富的类.结构.方法和事件.每一个DLL文件都定义不同的使用基于功能的库组织组件的命名 ...
- Revit二次开发学习笔记
Revit二次开发学习笔记1 20220314: 概念:Application与Document 接口函数:IExternalCommand.ActiveView与Selection 20220316 ...
- Polyworks脚本开发学习笔记(五)-变量使用基本语法
Polyworks脚本开发学习笔记(五)-变量使用基本语法 定义变量及赋值 定义各种类型的变量 定义变量时,只需要使用DECLARE 关键字即可定义,为了区别变量和脚本中的其它字符,建议都以小写v开头 ...
最新文章
- datagrid的正反双向排序
- R语言使用ggplot2包的快速可视化函数qplot绘制分组散点图(添加平滑曲线与标准差带)实战
- 《Excel与VBA开发》一书上市时间
- ubuntu14.04 访问windows目录的方法 mount.cifs方式 取代smbfs方式
- vb php mysql_VB连接MYSQL数据的方法
- 前端学习(757):预解析
- SharePoint Pages(1)之SharePoint页面体系架构
- 如何用区块链保障数据安全和承载数据确权
- android 协程,Android 上的 Kotlin 协程
- 京东物流:将连续第10年春节也送货 为坚守岗位一线员工补贴近4亿元
- call and apply
- RN和React路由详解及对比
- linux系统iso文件详解,ISO镜像文件解析
- 关于杭州电子科技大学毕业论文格式设置
- Latex论文用bibtex实现期刊/会议缩写
- ESP-BOX LVGL ask_wdt: Task watchdog got triggered问题 物联网
- flink的内存管理器MemoryManager
- Activity-的-36-大难点,你会几个?,android游戏开发实践指南
- 独上高楼 望尽天涯路
- apache atlas 官方安装