当前位置: 首页 > news >正文

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

选择两个ButtonOwner Draw设置为True

完成界面。

 

步骤二:
打开类视图,右击项目->添加类(这里不是右击对话框添加类)



这张图有点小错误,这里Dialog ID:IDD_ARX_MODAL      Class name:CArxDialog

在类视图中右击CArxDialog类添加变量

这样会在头文件中生成


源文件中生成

根据这个规律添加其他变量

ExpandedBlockStart.gif 大气象
private :
    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

运行结果如图



源码如下

ExpandedBlockStart.gif 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 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)

 

 

ExpandedBlockStart.gif 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 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();
} ;

 

 

ExpandedBlockStart.gif 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 
//  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


相关文章:

  • VIM配置文件
  • 修复xcode6.2 插件不能使用问题
  • Gartner亚太地区MSS市场观察
  • 北京兆维数据中心
  • Android网络编程之Http通信
  • 工控博客精华链接
  • Microsoft dotnetConf 2015 一些整理
  • 身高后天因素
  • python ConfigParser模块详解
  • jquery技巧总结 学习
  • [20150321]索引空块的问题.txt
  • C# 多线程之同步输出奇偶数
  • 自已写的框架拿出来等人来拍转 (序)
  • 电商做促销活动的要考虑到的
  • IIS6升级到IIS7后,UrlRewriting提示404,AJAX会提示未声明SYS错误
  • 5、React组件事件详解
  • CSS中外联样式表代表的含义
  • Django 博客开发教程 16 - 统计文章阅读量
  • JavaScript学习总结——原型
  • JDK9: 集成 Jshell 和 Maven 项目.
  • Laravel核心解读--Facades
  • Magento 1.x 中文订单打印乱码
  • mysql中InnoDB引擎中页的概念
  • oldjun 检测网站的经验
  • quasar-framework cnodejs社区
  • WePY 在小程序性能调优上做出的探究
  • zookeeper系列(七)实战分布式命名服务
  • 关于 Cirru Editor 存储格式
  • 区块链共识机制优缺点对比都是什么
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 删除表内多余的重复数据
  • 设计模式 开闭原则
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 首页查询功能的一次实现过程
  • 思维导图—你不知道的JavaScript中卷
  • 以太坊客户端Geth命令参数详解
  • 《码出高效》学习笔记与书中错误记录
  • #HarmonyOS:Web组件的使用
  • #QT(TCP网络编程-服务端)
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • $.ajax,axios,fetch三种ajax请求的区别
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (第27天)Oracle 数据泵转换分区表
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (转)3D模板阴影原理
  • (转)memcache、redis缓存
  • (转)mysql使用Navicat 导出和导入数据库
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .gitignore文件—git忽略文件
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .net mvc 获取url中controller和action