FreeCAD二次开发:集成二维CAD控件MxDraw

济南友泉软件有限公司

FreeCAD是一套基于OpenCASCADE/QT的三维全参数化建模开源代码,虽然提供了Draft、TechDraw等二维绘图功能,但是其二维建模能力仍旧比较弱。

Ref. from FreeCAD TechDraw-----------------------------------------------------------------------------------------------

If your primary goal is the production of complex 2D drawings and DXF files, and you don't need 3D modelling, FreeCAD may not be the right choice for you. You may wish to consider a dedicated software program for technical drafting instead, such as LibreCAD or QCad.

-----------------------------------------------------------------------------------------------Ref. from FreeCAD TechDraw

因此,需要研究借助于(非)商业软件包来强化FreeCAD的二维建模功能。本文探讨FreeCAD集成梦想CAD二维控件MxDraw 5.2的实现方法。

本文涉及的知识点:

  • FreeCAD文档/视图架构
  • 自定义视图窗体

一、MxDraw

梦想CAD是专业的CAD插件(控件),经过10年研发,可轻松在网页、手机及BS/CS程序中使用CAD和浏览编辑DWG文件,不需安装AutoCAD即可运行。与应用相对应,梦想CAD提供了电脑版、手机版、网页版等三种开发包。

梦想CAD电脑版包括2D、3D等多个版本,支持ObjectARX开发接口,C#,Delpi,VC,VB,JAVA,C++Builder、PHP,.NET,易语言,PowerBuild等。

1.1 安装MxDraw 5.2

下载梦想2D开发包MxDraw5.2TryVersion.exe,此开发包实际上是一个压缩文件,下载之后双击运行便会自动完成解压。

在解压目录中,运行MxDrawx64Setup(20210905).exe以完成MxDraw 5.2(64位)的安装以及注册MxDrawX.ocx。MxDraw默认安装在C:\Program Files\Mxkd\MxDrawx64。

1.2 测试

在Src/MxDraw5.2/Samples中,有一些测试项目,打开TestQtMxDraw.sln,编译链接之后,运行程序以确保MxDraw 5.2安装没有问题。

二、集成方法

在前面的FreeCAD源码分析篇章中,已经就FreeCAD文档\视图架构做了分析,故这里不赘述。

2.1 文档\视图对象

为了将MxDraw嵌入到FreeCAD文档\视图架构,首先需要创建一个MxDraw文档对象类型,

    /**
    * Embed the MxDraw control into FreeCAD's Document Object system.
    */
    class MxDraw : public App::DocumentObject
    {
        PROPERTY_HEADER(WaterBim::MxDraw);

    public:

        /// Constructor
        MxDraw();
        virtual ~MxDraw();

        /// returns the type name of the ViewProvider
        virtual const char* getViewProviderName(void) const {
            return "WaterBimGui::ViewProviderMxDraw";
        }
    };

与这个文档对象类型对应,需要定义一个视图对象,

/** The provider for MxDraw
    * Use MxDraw control to do actual work.
    */
    class ViewProviderMxDraw : public Gui::ViewProviderDocumentObject
    {
        PROPERTY_HEADER_WITH_OVERRIDE(WaterBimGui::ViewProviderMxDraw);

    public:
        /// constructor.
        ViewProviderMxDraw();

        /// destructor.
        ~ViewProviderMxDraw();

        void setDisplayMode(const char* ModeName) override;

        virtual bool doubleClicked(void) override;
        void setupContextMenu(QMenu* menu, QObject* receiver, const char* member) override;

        MxDraw* getMxDrawObject() const;

        QIcon getIcon() const override;

        virtual Gui::MDIView* getMDIView() override;

    protected:
        MxDrawView* showMxDrawView();

    private:
        QPointer<MxDrawView> view;
    };

在ViewProviderMxDraw中,通过成员函数getMDIView() 来自定义视图窗体,

MxDrawView* ViewProviderMxDraw::showMxDrawView()
{
    if (!view) {
        Gui::Document* doc = Gui::Application::Instance->getDocument
        (this->pcObject->getDocument());
        view = new MxDrawView(doc, this->pcObject, Gui::getMainWindow());
        view->setWindowIcon(Gui::BitmapFactory().pixmap(":icons/mxdraw.png"));
        view->setWindowTitle(QString::fromUtf8(pcObject->Label.getValue()) + QString::fromLatin1("[*]"));
        Gui::getMainWindow()->addWindow(view);
        startEditing();
    }

    return view;
}

Gui::MDIView* ViewProviderMxDraw::getMDIView() {
    return showMxDrawView();
}

2.2 自定义视图窗体

参考MxDraw Samples中示例TestQtMxDraw,可以知道MxDraw实际上是通过QAxWidget实现的二维CAD窗体的创建,仿照TestQtMxDraw,定义MxDrawView视图窗体类,

    class MxDrawView : public Gui::MDIView
    {
        Q_OBJECT

        TYPESYSTEM_HEADER();

    public:
        MxDrawView(Gui::Document* pcDocument, App::DocumentObject* docObj, QWidget* parent);
        ~MxDrawView();

    protected Q_SLOTS:

    private slots:
        void on_pushButton_clicked();

        void on_axWidget_ImplementCommandEvent(int iCommandId);

    protected:
        Ui::MxDrawView* ui;

    };

参考资料

FreeCAD TechDrawhttps://wiki.freecad.org/TechDraw_Workbench

梦想CAD控件https://www.mxdraw.com/

FreeCAD源码分析:FreeCADGui模块https://blog.csdn.net/qq_26221775/article/details/113898970

Module developer’s guide to FreeCAD source code by Qingfeng Xia http://www.iesensor.com • 2015-09-18 version 0.1 for FreeCAD version 0.16-dev • 2016-09-18 version 0.2 for FreeCAD version 0.17-dev License of this book This ebook is licensed the same as FreeCAD document license CC-BY 3.0 http://creativecommons.org/licenses/by/3.0/Contents 1 FreeCAD overview and architecture 7 1.1 Introduction to FreeCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2 Key features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3 Software architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.1 Key software libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.2 Mixed python and c++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.3 GPL code will not be included into installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4 How 3D model are renderred . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4.1 Selection of 3D visualization libarary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4.2 Discussion of 3D rendering library selection on FreeCAD Forum . . . . . . . . . . . . . . . . . . . . . 8 1.5 Roadmap of FreeCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5.1 Keep updated with main components: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5.2 C++11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5.3 Pyside 2 project for Qt 5.x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2 Organisation of FreeCAD source code 11 2.1 Build system for FreeCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.1.1 Analysis of src/cMake/SMesh.cMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 List of files and folders in FreeCAD source folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 List of modules in FreeCAD Mod folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4 Learning path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5 Learning OpenInventor/Coin3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5.1 OpenInventor in FreeCAD’s ViewProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5.2 Important classes in OpenInventor/Coin3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.5.3 Window System integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5.4 Pivy: Coin3D ’s Python wrapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3 Base, App and Main module 19 3.1 List of header files in Base folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.1.1 Frequently included headers files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1.2 Correct way of using Sequencer in try-catch block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1.3 String enconding utf8 and conversion into wchar_t QString . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2 Type, BaseClass, PyObjectBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.1 Type system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.2 src/Base/BaseClass.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2.3 src/Base/PyObjectBase.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.4 src/Base/Persistence.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.5 GeoFeature: Base class of all geometric document objects . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.3 Unit scheme for physial quantity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.3.1 src/Base/Unit.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.3.2 src/Base/Quantity.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.4 List of header files in App folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.5 Property framewrok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.5.1 Naming of property and PropertyEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.5.2 src/App/PropertyStandard.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5.3 PropertyEnumeration, see src/App/Enumeration.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5.4 Geometry related property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5.5 File related property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值