一、创建文件夹结构

        1、在../qcad/scripts/路径下创建MyScripts文件夹

        2、在MyScripts文件夹中创建一个文本文件“MyScripts.js”;

        3、 在MyScripts文件夹中创建另外一个文件夹来提供一个命令Action,如命名为“MyAction”;

        4、在MyAction文件夹中创建一个文本文件MyAction.js;

        目录结构如下图所示

二、创建菜单和动作

        接下来分别复制代码到MyScripts.jsMyAction.js

         MyScripts.js

/**
 * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
 * 
 * This file is part of the QCAD project.
 *
 * QCAD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QCAD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QCAD.
 */
// MyScripts.js

// 所有的action都是从类EAction派生的,所以此处引入EAction:
include("scripts/EAction.js");

// 构造函数调用基类构造函数:
function MyScripts(guiAction) {
    EAction.call(this, guiAction);
}

// 从 EAction 类派生 MyScripts 类:
MyScripts.prototype = new EAction();

// 此静态函数返回一个新的或已有的 QMenu 对象.
MyScripts.getMenu = function () {
    // EAction.getMenu返回一个现有的或具有给定标题和对象名称的新 QMenu 对象
    // 对象名称(此处即 "MyScriptMenu")必须唯一
    return EAction.getMenu(MyScripts.getTitle(), "MyScriptsssMenu");
};

// 此静态函数返回一个新的或已有的 QToolBar 对象.
MyScripts.getToolBar = function () {
    // EAction.getToolBar 返回一个现有的或具有
    // 给定标题和对象名称的新 QToolBar 对象
    // 对象名称(此为“MyScriptsToolBar”)必须唯一。
    return EAction.getToolBar(MyScripts.getTitle(), "MyScriptsToolBar");
};

// 这个静态函数定义并返回菜单的标题和工具栏。
// qsTr 函数将标题标记为可翻译字符串.
MyScripts.getTitle = function () {
    return qsTr("My Scriptsssss");
};

// Init函数在开始时创建菜单和工具栏.
MyScripts.init = function () {
    MyScripts.getMenu();
    MyScripts.getToolBar();
};

        MyAction.js

/**
 * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
 * 
 * This file is part of the QCAD project.
 *
 * QCAD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QCAD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QCAD.
 */
// MyAction.js

// 引入母类定义:
include("../MyScripts.js");
 
// 构造函数调用基类构造函数:
function MyAction(guiAction) {
    MyScripts.call(this, guiAction);
}

// 从母类 MyScripts 派生类 MyAction:
MyAction.prototype = new MyScripts();

// 对于不需要任何用户输入的操作(如自动缩放),
// beginEvent 执行所有操作,然后终止
MyAction.prototype.beginEvent = function() {
    // 调用 beginEvent 的基类实现:
    MyScripts.prototype.beginEvent.call(this);

    // 获取主应用程序窗口:
    var appWin = EAction.getMainWindow();

    // 执行动作:在命令行窗口打印:
    appWin.handleUserMessage("MyAction() is running...");
    
    // 完成后终止:
    this.terminate();
};

// MyAction.init() 由 QCAD 调用初始化并创建
// 它的菜单/工具栏。
MyAction.init = function(basePath) {
    // 创建一个新的 RGuiAction(扩展 QAction):
    var action = new RGuiAction("&My Action", RMainWindowQt.getMainWindow());

    // 此操作需要打开文档。如果没有文件打开,菜单和工具按钮变灰:
    action.setRequiresDocument(true);

    // 定义此动作执行时,该脚本文件启动:
    action.setScriptFile(basePath + "/MyAction.js");

    // 设置icon:
    action.setIcon(basePath + "/MyAction.svg");

    // 该动作对应的命令行口令:
    action.setDefaultCommands(["myaction"]);

    // 定义此操作的排序顺序。菜单和工具按钮是
    // 按这些值排序:
    action.setGroupSortOrder(80100);
    action.setSortOrder(200);

    // 设置此操作添加到列表(菜单、工具栏、CAD工具栏面板):
    action.setWidgetNames(["MyScriptsMenu"]);
};

三、测试

        启动QCAD发现菜单栏上出现MyScripts及MyAction,点击执行后命令栏出现设置好的测试语句;

四、移除无用菜单

       同理,能加也能减,以帮助菜单为例,找到Help.js文件,屏蔽掉init初始化几句,如下

         运行,发现帮助菜单移除成功;(当然直接把整个Help.js文件移除更简单粗暴)

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐