1、CAD二次开发学习笔记五(在ObjectARX中使用MFC) CAD二次开发学习笔记五(在ObjectARX中使用MFC) 编辑整理: 尊敬的读者朋友们: 这里是精品文档编辑中心,本文档内容是由我和我的同事精心编辑整理后发布的,发布之前我们对文中内容进行仔细校对,但是难免会有疏漏的地方,但是任然希望(CAD二次开发学习笔记五(在ObjectARX中使用MFC))的内容能够给您的工作和学习带来便利。同时也真诚的希望收到您的建议和反馈,这将是我们进步的源泉,前进的动力。 本文可编辑可修改,如果觉得对您有帮助请收藏以便随时查
2、阅,最后祝您生活愉快 业绩进步,以下为CAD二次开发学习笔记五(在ObjectARX中使用MFC)的全部内容。 CAD二次开发学习笔记五(在ObjectARX中使用MFC) 要实现的功能是: 执行ArxModal命令,弹出如图所示对话框, 选择点,则得到点坐标,选择角度则得到角度值。 步骤一: 新建基于MFC的ObjectArx项目, 参考:http://www。cnblogs。com/greatverve/archive/2010/05/31/ObjectARX—HelloWorld。html 打开资源视图添加一个对话框ID修改为IDD_ARX_MODAL (右
3、击资源视图中的对话框打开属性面板,可以修改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_MODAL Class name:CArxDialog 在类视图中右击CArxDialog类添加
4、变量 这样会在头文件中生成 源文件中生成 可以直接修改这两处代码添加其他变量. private: CAcUiPickButton m_btnAngle; CAcUiPickButton m_btnPoint; CAcUiNumericEdit m_editXpt; CAcUiNumericEdit m_editYpt; CAcUiNumericEdit m_editZpt; CAcUiAngleEdit m_editAngle; void CArxDlg::DoDataExchange (CDataExchange *pDX) { CAcU
5、iDialog::DoDataExchange (pDX) ; DDX_Control(pDX, IDC_BUTTON_POINT, m_btnPoint); DDX_Control(pDX, IDC_BUTTON_ANGLE, m_btnAngle); 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
6、 m_editAngle); } 步骤三: 为CArxDialog添加InitDialog消息响应。 方法是打开类视图,右击-〉属性 再添加OnClose()响应函数 在头文件中添加几个变量 public: CString m_strAngle; CString m_strZPt; CString m_strYPt; CString m_strXPt; 在头文件中定义两函数 void DisplayPoint(); void DisplayAngle(); 分别为两个按钮添加单击事件,为四个编辑框添加失去焦点事件。 步骤四:
7、打开acrxEntryPoint.cpp添加#include “ArxDialog.h” 运行结果如图 附源码: acrxEntryPoint。cpp // (C) Copyright 2002-2005 by Autodesk, Inc. // // 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 ab
8、ove 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 AN
9、Y 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
10、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 //—-——-—----—--——-——
11、———-——----——-—---——--—-—-———-—--————--—---—---—————--—-———— #include ”StdAfx。h" #include "resource。h" #include ”ArxDialog。h” //---—-—----—-—-—-———-——----—-—--—--—————————---———————-——-——————-———-—-----—-- #define szRDS _RXST(”") //——-———--—-—-—--—--——-—-—--—-------——--————-———--————-——-—-—
12、——-————-------—-—- //—-——— 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
13、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_kUnlo
14、adAppMsg (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 //防止资源冲突 CAcModuleR
15、esourceOverride resOverride; //显示ObjectARX的模态对话框 CArxDialog theDialog; theDialog.DoModal(); } } ; //--—-——----——--—-—--—---—-—--———----—-—-—--——————-——--——-—-----——-----—-—-—--- IMPLEMENT_ARX_ENTRYPOINT(CCADMFCApp) ACED_ARXCOMMAND_ENTRY_AUTO(CCADMFCApp, CADMFC, showDlg, MyCommand1
16、 ACRX_CMD_TRANSPARENT, NULL) ArxDialog.h // (C) Copyright 2002—2005 by Autodesk, Inc。 // // 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 co
17、pies 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 // MERCHANTABIL
18、ITY 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 // Sof
19、tware - 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 //—--—--—-——-——---—----—---—--—
20、—-—--——-—-—-—------—---—-—---—--————-—-—-----— #pragma once //————-—————---——----—-—--—-———-—-—-—------————————-—---——--———---—----—-——---- #include "acui.h” //-———-——-——-————--—-—-————--—-—--------————-—----—--—--——-——-—-—---——--—-——-—— class CArxDialog : public CAcUiDialog { DECLARE_D
21、YNAMIC (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: CAcUiPickButt
22、on 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
23、 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 OnEnKillfocusEdi
24、tAngle(); } ; ArxDialog。cpp // (C) Copyright 2002—2005 by Autodesk, Inc. // // 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 //
25、 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 FITNES
26、S 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 — Restr
27、icted Rights) and DFAR 252。227—7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable。 // //----——--—--—-—-----——--—--—-——--—-—-————--—-——---——-—----—--—-------—-—-—---— //-—--— ArxDialog.cpp : Implementation of CArxDialog //--—-—-—--—----—-—————-—---—-—-—----—-—-——
28、—-----————--——-——----—-—--——-———-- #include ”StdAfx。h” #include ”resource。h” #include ”ArxDialog。h” //—-—-——-—-—-—-———--—--—-—---—--——---————-------————--——---——---——--——---——-—-- IMPLEMENT_DYNAMIC (CArxDialog, CAcUiDialog) BEGIN_MESSAGE_MAP(CArxDialog, CAcUiDialog) ON_MESSAGE(WM_ACAD_
29、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::On
30、EnKillfocusEditYpt) ON_EN_KILLFOCUS(IDC_EDIT_ZPT, &CArxDialog::OnEnKillfocusEditZpt) ON_EN_KILLFOCUS(IDC_EDIT_ANGLE, &CArxDialog::OnEnKillfocusEditAngle) END_MESSAGE_MAP() //—--—--—--———-————-—-———--—-——--—-—---—---——---—-——-—----—-——--——-———-—-—-—--— CArxDialog::CArxDialog (CWnd *pParent /
31、*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (CArxDialog::IDD, pParent, hInstance) { } //-——-—--—--—---—-——---—-——--—-—---———-——---———-———-——--——---—-—---------—-———— void CArxDialog::DoDataExchange (CDataExchange *pDX) { CAcUiDialog::DoDataExchange (pDX) ; DDX_Control(pDX, IDC_
32、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); } //——-————————-—-———-——--————--———
33、————-———-————————--—---————-—--—---—--————-—-- //---—— 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() { CAcUiDial
34、og::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_st
35、rAngle = _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() { //在对话框中显示点的坐标
36、 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()
37、 } 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,
38、z); CAcUiDialog::OnClose(); } void CArxDialog::OnBnClickedButtonPoint() { // TODO: 在此添加控件通知处理程序代码 //隐藏对话框把控制权交给AutoCad BeginEditorCommand(); //提示用户输入一个点 ads_point pt; if(acedGetPoint(NULL,_T(”\n输入一个点:"),pt)==RTNORM) { //如果点有效,继续执行 CompleteEditorCommand(); m_strXPt
39、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 BeginEditorCom
40、mand(); //将当前选择的点的位置作为基点 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(acedGetAng
41、le(pt,_T(”\n输入角度:”),&angle)==RTNORM) { //如果正确获得角度,返回对话框 CompleteEditorCommand(); //将角度值转换为弧度值 m_strAngle。Format(_T(”%.2f”),angle*(180。0/PI)); //显示角度值 DisplayAngle(); } else { //否则取消命令(包括对话框) CancelEditorCommand(); } } void CArxDialog::OnEnKillfocusEditXpt() {
42、 // 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); }






