CloudCompare2.11.0插件开发

  • 一、UI文件生成
  • 二、代码文件配置
  • 三、资源文件配置
  • 四、CMake文件配置
  • 五、相关代码
  • 六、编译安装效果


一、UI文件生成

cloudcompare 生成模型 cloudcompare开发_json 1、选择Qt自带的界面设计软件Designer,因为后面编译CloudCompare插件所使用的编译器是VS2019,故选择的是MSVC 2019 64位版的

cloudcompare 生成模型 cloudcompare开发_#include_02


cloudcompare 生成模型 cloudcompare开发_json 2、新建一个UI对话框界面,即Dialog对话框

cloudcompare 生成模型 cloudcompare开发_json_04


cloudcompare 生成模型 cloudcompare开发_json 3、窗口添加组件

cloudcompare 生成模型 cloudcompare开发_json 创建好的界面如下,接下来给这个窗口添加一些我们需要的组件

cloudcompare 生成模型 cloudcompare开发_json_07


cloudcompare 生成模型 cloudcompare开发_json 添加好组件后的窗口如下,将该UI文件保存到一个文件路径下,后面需要用到这个文件

cloudcompare 生成模型 cloudcompare开发_#include_09

二、代码文件配置

cloudcompare 生成模型 cloudcompare开发_json 1、将CloudCompare源码中自带的插件例子复制一份,然后改成UIPlugin(也可改成其他名字)

cloudcompare 生成模型 cloudcompare开发_ui_11


cloudcompare 生成模型 cloudcompare开发_json 2、在UIPlugin文件夹中新建一个src文件夹和ui文件夹,其中src文件夹放的是源代码文件,ui文件夹下放的是前面保存的UI文件,文件结构图如下

cloudcompare 生成模型 cloudcompare开发_qt_13


cloudcompare 生成模型 cloudcompare开发_#include_14


cloudcompare 生成模型 cloudcompare开发_ui_15


cloudcompare 生成模型 cloudcompare开发_json 3、在src文件夹下新建一个UIPluginParameters.cpp文件和UIPluginParameters.h文件

cloudcompare 生成模型 cloudcompare开发_#include_17

三、资源文件配置

cloudcompare 生成模型 cloudcompare开发_json 1、UIPlugin.qrc文件配置

cloudcompare 生成模型 cloudcompare开发_#include_19


cloudcompare 生成模型 cloudcompare开发_json 2、info.json文件配置

cloudcompare 生成模型 cloudcompare开发_cloudcompare 生成模型_21

四、CMake文件配置

cloudcompare 生成模型 cloudcompare开发_json 1、在src文件夹下新建一个CMakeLists.txt文件,内容配置如下

cloudcompare 生成模型 cloudcompare开发_qt_23


cloudcompare 生成模型 cloudcompare开发_json 2、在ui文件夹下新建一个CMakeLists.txt文件,内容配置如下

cloudcompare 生成模型 cloudcompare开发_#include_25


cloudcompare 生成模型 cloudcompare开发_json 3、UIPlugin文件夹下的CMakeLists.txt文件配置

cloudcompare 生成模型 cloudcompare开发_json_27


cloudcompare 生成模型 cloudcompare开发_json 4、在UIPlugin文件夹所在路径下的CMakeLists.txt文件中添加如下内容

cloudcompare 生成模型 cloudcompare开发_ui_29


cloudcompare 生成模型 cloudcompare开发_#include_30


cloudcompare 生成模型 cloudcompare开发_json 5、在CMake中先点击Configure配置文件,生成UIPlugin选项,然后在Ungrouped Entries中勾选中UIPlugin,如果在Ungrouped Entries中未找到自己写的插件的话,在PLUGIN中或许能找到,勾选后点击Configure

cloudcompare 生成模型 cloudcompare开发_qt_32


cloudcompare 生成模型 cloudcompare开发_json 6、点击Generate生成解决方案,点击Open Project打开解决方案

cloudcompare 生成模型 cloudcompare开发_#include_34

五、相关代码

cloudcompare 生成模型 cloudcompare开发_json 1、打开解决方案后,文件结构如下

cloudcompare 生成模型 cloudcompare开发_#include_36


cloudcompare 生成模型 cloudcompare开发_json 2、先对UIPlugin.hUIPlugin.cpp文件进行如下修改

cloudcompare 生成模型 cloudcompare开发_cloudcompare 生成模型_38


cloudcompare 生成模型 cloudcompare开发_cloudcompare 生成模型_39


cloudcompare 生成模型 cloudcompare开发_qt_40


cloudcompare 生成模型 cloudcompare开发_json 3、右键UIPlugin.ui文件,点击编译,生成ui_UIPlugin.h文件

cloudcompare 生成模型 cloudcompare开发_#include_42


cloudcompare 生成模型 cloudcompare开发_json 4、在UIPluginParameters.hUIPluginParameters.cpp文件中写入以下内容

UIPluginParameters.h文件

#ifndef UIPLUGIN_PARAMETERS_HEADER
#define UIPLUGIN_PARAMETERS_HEADER

#include <ui_UIPlugin.h>

class UIPluginParameters :public QDialog
{
	Q_OBJECT

public:
	explicit UIPluginParameters(QWidget* parent = nullptr);
	~UIPluginParameters();

private:
	Ui::Dialog* ui;

private slots:
	void doConfirmBtn();
	void doCancelBtn();

};

#endif

注意:如果需要使用信号槽功能的话,请一定要加上Q_OBJECT,否则编译会报错!!!
UIPluginParameters.cpp文件

#include "UIPluginParameters.h"
#include <QMessageBox>

UIPluginParameters::UIPluginParameters(QWidget* parent) :
	QDialog(parent),
	ui(new Ui::Dialog)
{
	ui->setupUi(this);
	connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(doConfirmBtn()));
	connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(doCancelBtn()));
}

UIPluginParameters::~UIPluginParameters()
{
	delete ui;
}

void UIPluginParameters::doConfirmBtn()
{
	QMessageBox::information(this, "Tips", QString("The parameter value entered is %1").arg(ui->lineEdit->text()), QMessageBox::Ok);
	this->close();
}

void UIPluginParameters::doCancelBtn()
{
	QMessageBox::information(this, "Tips", QString("The cancel button has been clicked."), QMessageBox::Ok);
	this->close();
}

注意:① connect中的信号函数和槽函数一定要加括号,否则按钮点击将会没有任何反应

cloudcompare 生成模型 cloudcompare开发_json cloudcompare 生成模型 cloudcompare开发_json ② QString中的 %1是占位符,后面的arg中的变量为参数,若直接在QString中使用参数,则弹窗无法正确显示内容

cloudcompare 生成模型 cloudcompare开发_json 5、在UIPlugin.cpp中添加一个头文件,同时作以下修改

cloudcompare 生成模型 cloudcompare开发_json_47


cloudcompare 生成模型 cloudcompare开发_json_48


cloudcompare 生成模型 cloudcompare开发_cloudcompare 生成模型_49

//##########################################################################
//#                                                                        #
//#                CLOUDCOMPARE PLUGIN: ExamplePlugin                      #
//#                                                                        #
//#  This program 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; version 2 of the License.               #
//#                                                                        #
//#  This program 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.                          #
//#                                                                        #
//#                             COPYRIGHT: XXX                             #
//#                                                                        #
//##########################################################################

// First:
//	Replace all occurrences of 'ExamplePlugin' by your own plugin class name in this file.
//	This includes the resource path to info.json in the constructor.

// Second:
//	Open ExamplePlugin.qrc, change the "prefix" and the icon filename for your plugin.
//	Change the name of the file to <yourPluginName>.qrc

// Third:
//	Open the info.json file and fill in the information about the plugin.
//	 "type" should be one of: "Standard", "GL", or "I/O" (required)
//	 "name" is the name of the plugin (required)
//	 "icon" is the Qt resource path to the plugin's icon (from the .qrc file)
//	 "description" is used as a tootip if the plugin has actions and is displayed in the plugin dialog
//	 "authors", "maintainers", and "references" show up in the plugin dialog as well

#include <QtGui>

#include "UIPlugin.h"
#include "UIPluginParameters.h"

// Default constructor:
//	- pass the Qt resource path to the info.json file (from <yourPluginName>.qrc file) 
//  - constructor should mainly be used to initialize actions and other members
ExamplePlugin::ExamplePlugin( QObject *parent )
	: QObject( parent )
	, ccStdPluginInterface( ":/CC/plugin/UIPlugin/info.json" )
	, m_action( nullptr )
{
}

// This method should enable or disable your plugin actions
// depending on the currently selected entities ('selectedEntities').
void ExamplePlugin::onNewSelection( const ccHObject::Container &selectedEntities )
{
	if ( m_action == nullptr )
	{
		return;
	}
	
	// If you need to check for a specific type of object, you can use the methods
	// in ccHObjectCaster.h or loop and check the objects' classIDs like this:
	//
	//	for ( ccHObject *object : selectedEntities )
	//	{
	//		if ( object->getClassID() == CC_TYPES::VIEWPORT_2D_OBJECT )
	//		{
	//			// ... do something with the viewports
	//		}
	//	}
	
	// For example - only enable our action if something is selected.
	//m_action->setEnabled( !selectedEntities.empty() );
}

// This method returns all the 'actions' your plugin can perform.
// getActions() will be called only once, when plugin is loaded.
QList<QAction *> ExamplePlugin::getActions()
{
	// default action (if it has not been already created, this is the moment to do it)
	if ( !m_action )
	{
		// Here we use the default plugin name, description, and icon,
		// but each action should have its own.
		m_action = new QAction( getName(), this );
		m_action->setToolTip( getDescription() );
		m_action->setIcon( getIcon() );
		
		// Connect appropriate signal
		connect( m_action, &QAction::triggered, this, &ExamplePlugin::doAction );
	}

	return { m_action };
}

// This is an example of an action's method called when the corresponding action
// is triggered (i.e. the corresponding icon or menu entry is clicked in CC's
// main interface). You can access most of CC's components (database,
// 3D views, console, etc.) via the 'm_app' variable (see the ccMainAppInterface
// class in ccMainAppInterface.h).
void ExamplePlugin::doAction()
{	
	if ( m_app == nullptr )
	{
		// m_app should have already been initialized by CC when plugin is loaded
		Q_ASSERT( false );
		
		return;
	}

	/*** HERE STARTS THE ACTION ***/

	// Put your code here
	// --> you may want to start by asking for parameters (with a custom dialog, etc.)
	UIPluginParameters uipp;
	if (!uipp.exec())
	{
		return;
	}

	// This is how you can output messages
	// Display a standard message in the console
	//m_app->dispToConsole( "[ExamplePlugin] Hello world!", ccMainAppInterface::STD_CONSOLE_MESSAGE );
	
	// Display a warning message in the console
	//m_app->dispToConsole( "[ExamplePlugin] Warning: example plugin shouldn't be used as is", ccMainAppInterface::WRN_CONSOLE_MESSAGE );
	
	// Display an error message in the console AND pop-up an error box
	//m_app->dispToConsole( "Example plugin shouldn't be used - it doesn't do anything!", ccMainAppInterface::ERR_CONSOLE_MESSAGE );

	/*** HERE ENDS THE ACTION ***/
}

cloudcompare 生成模型 cloudcompare开发_json 6、右键ALL_BUILD解决方案,点击生成,对所有项目进行编译,之后右键INSTALL解决方案,点击生成,安装CloudCompare

六、编译安装效果

cloudcompare 生成模型 cloudcompare开发_ui_51


cloudcompare 生成模型 cloudcompare开发_json_52


cloudcompare 生成模型 cloudcompare开发_json