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

  • Post author:
  • Post category:其他


济南友泉软件有限公司

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 TechDraw



https://wiki.freecad.org/TechDraw_Workbench





梦想CAD控件



https://www.mxdraw.com/





FreeCAD源码分析:FreeCADGui模块



https://blog.csdn.net/qq_26221775/article/details/113898970