先不修改代码,加入Cloudcompare后使用vs编辑
在 \plugins\example\CMakeLists.txt 添加自己的插件源码目录
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/ExampleGLPlugin )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/ExampleIOPlugin )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/ExamplePlugin )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/MyPlugin )
在 \plugins\example\MyPlugin\CMakeLists.txt 修改项目名称
cmake_minimum_required( VERSION 3.0 )
option( PLUGIN_EXAMPLE_STANDARD "Check to install example plugin" OFF )
if ( PLUGIN_EXAMPLE_STANDARD )
project( MyPlugin )
include( ../../CMakePluginTpl.cmake )
endif()
直接复制例程ExamplePlugin,修改文件名字,如下图所示
使用CMake重新生成项目,注意勾选 PLUGIN_EXAMPLE_STANDARD
打开以后就能看到MyPlugin
直接看例程的注释就知道怎么修改,我这里为了减少篇幅就删掉注释了,直接放核心源码
#ifndef MyPlugin_PLUGIN_HEADER
#define MyPlugin_PLUGIN_HEADER
#include "ccStdPluginInterface.h"
class MyPlugin : public QObject, public ccStdPluginInterface
{
Q_OBJECT
Q_INTERFACES(ccStdPluginInterface)
Q_PLUGIN_METADATA(IID "cccorp.cloudcompare.plugin.MyPlugin" FILE "info.json") // 改成自己的
public:
explicit MyPlugin( QObject *parent = nullptr );
~MyPlugin() override = default;
void onNewSelection( const ccHObject::Container &selectedEntities ) override;
QList<QAction *> getActions() override;
private:
void doAction();
QAction* m_action;
};
#endif
#include
#include "MyPlugin.h"
#include
MyPlugin::MyPlugin( QObject *parent )
: QObject( parent )
, ccStdPluginInterface( ":/CC/plugin/MyPlugin/info.json" )
, m_action( nullptr )
{
}
void MyPlugin::onNewSelection( const ccHObject::Container &selectedEntities )
{
if ( m_action == nullptr )
{
return;
}
m_action->setEnabled( !selectedEntities.empty() );
}
QList<QAction *> MyPlugin::getActions()
{
if ( !m_action )
{
m_action = new QAction( getName(), this );
m_action->setToolTip( getDescription() );
m_action->setIcon( getIcon() );
connect( m_action, &QAction::triggered, this, &MyPlugin::doAction);
}
return { m_action };
}
void MyPlugin::doAction()
{
if ( m_app == nullptr )
{
Q_ASSERT( false );
return;
}
// 1.获取所选点云
ccPointCloud* cloud;
const ccHObject::Container& selectedEntities = m_app->getSelectedEntities();
if (selectedEntities.size() != 1)
{
m_app->dispToConsole("[MyPlugin] selectedEntities.size() != 1", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
}
ccHObject *entity = selectedEntities[0];
if (entity->isA(CC_TYPES::POINT_CLOUD))
{
cloud = static_cast<ccPointCloud*>(entity);
}
else {
m_app->dispToConsole("[MyPlugin] not a CC_TYPES::POINT_CLOUD", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
}
// 2.遍历点云坐标
CCLib::ReferenceCloud sampledCloud(cloud);
QElapsedTimer eTimer; // 统计时间
eTimer.start();
CCVector3 bbmin, bbmax;
cloud->getBoundingBox(bbmin, bbmax); // 获取边界框
float th = (bbmin[2] + bbmax[2]) / 2; // 设定阈值
for (unsigned int i = 0; i < cloud->size(); ++i) {
if (cloud->getPoint(i)->z > th) {
sampledCloud.addPointIndex(i); // 取大于阈值的坐标
}
}
ccLog::Print("[MyPlugin] Timing: %3.3f s.", eTimer.elapsed() / 1000.0);
// 3.生成新的点云
int warnings = 0;
ccPointCloud* newPointCloud = cloud->partialClone(&sampledCloud, &warnings);
if (warnings){
ccLog::Warning("[MyPlugin] Not enough memory: colors, normals or scalar fields may be missing!");
}
if (newPointCloud)
{
newPointCloud->setName(cloud->getName() + QString(".MyPlugin"));
newPointCloud->setGlobalShift(cloud->getGlobalShift());
newPointCloud->setGlobalScale(cloud->getGlobalScale());
newPointCloud->setDisplay(cloud->getDisplay());
newPointCloud->prepareDisplayForRefresh();
if (cloud->getParent())
cloud->getParent()->addChild(newPointCloud);
cloud->setEnabled(false);
m_app->addToDB(newPointCloud);
newPointCloud->prepareDisplayForRefresh();
}
// 4.结尾 Hello world!
m_app->dispToConsole( "[MyPlugin] Hello world!", ccMainAppInterface::STD_CONSOLE_MESSAGE );
}
{
"type": "Standard",
"name": "MyPlugin",
"icon": ":/CC/plugin/MyPlugin/images/rabbit.ico",
"description": "This is my plugin.",
"authors": [
{
"name": "dasbai",
"email": "https://blog.csdn.net/qq_38204686"
}
],
"maintainers": [
{
"name": "dasbai",
"email": "https://blog.csdn.net/qq_38204686"
}
]
}
<RCC>
<qresource prefix="/CC/plugin/MyPlugin">
<file>info.json</file>
<file>images/rabbit.ico</file>
</qresource>
</RCC>
鼠标移到MyPlugin然后右键,点击生成
如果设定好安装路径可以右键INSTALL,点生成
或者手动将dll复制到exe的\plugins文件夹中