一、概述
最近体验许多外挂都有抽取实体的中心线,由于刚刚学习,我尝试看看能不能做出来,本博客代码没有封装函数,代码有待改进,但基本可以实现相应的功能。
二、案例实现的功能
1、采用包容体的思路通过UF_MODL_ask_bounding_box找到包围盒的X、Y、Z的最大之和最小值;
2、获得三对平行面的中点,利用每对中心点绘制直线;
3、更改直线的颜色、线型以及图层。
三、代码说明以及详细注解
//NXOpen_CreatebodyCentreLine
// Mandatory UF Includes
#include <uf.h>
#include <uf_object_types.h>
// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>
// Internal+External Includes
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx>
// Std C++ Includes
#include <iostream>
#include <sstream>
//用户定义
#include "uf_all.h"
using namespace NXOpen;
using std::string;
using std::exception;
using std::stringstream;
using std::endl;
using std::cout;
using std::cerr;
static int init_proc(UF_UI_selection_p_t select, void *user_data)
{
int errorCode = 0;
int num_triples = 1; //选择类型 数量
UF_UI_mask_t mask_triples[1] = { { UF_solid_type , 0,0 } //定义选择面类型
};
errorCode = UF_UI_set_sel_mask(select,
UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,
num_triples,
mask_triples);
if (errorCode == 0)
{
return UF_UI_SEL_SUCCESS;
}
else
{
return UF_UI_SEL_FAILURE;
}
}
//------------------------------------------------------------------------------
// NXOpen c++ test class
//------------------------------------------------------------------------------
class MyClass
{
// class members
public:
static Session *theSession;
static UI *theUI;
MyClass();
~MyClass();
void do_it();
void print(const NXString &);
void print(const string &);
void print(const char*);
private:
BasePart *workPart, *displayPart;
NXMessageBox *mb;
ListingWindow *lw;
LogFile *lf;
};
//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MyClass::theSession) = NULL;
UI *(MyClass::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
MyClass::MyClass()
{
// Initialize the NX Open C++ API environment
MyClass::theSession = NXOpen::Session::GetSession();
MyClass::theUI = UI::GetUI();
mb = theUI->NXMessageBox();
lw = theSession->ListingWindow();
lf = theSession->LogFile();
workPart = theSession->Parts()->BaseWork();
displayPart = theSession->Parts()->BaseDisplay();
}
//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
}
//------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const string &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const char * msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{
// TODO: add your code here
UF_initialize();
L10:
int response = 0;
tag_t object = NULL_TAG;
double cursor[3];
tag_t view = NULL_TAG;
UF_UI_select_with_single_dialog("提示:请选择实体", "选择实体", UF_UI_SEL_SCOPE_WORK_PART, init_proc, NULL, &response, &object, cursor, &view);
if (response == UF_UI_OK || response == UF_UI_OBJECT_SELECTED || response == UF_UI_OBJECT_SELECTED_BY_NAME)
{
UF_DISP_set_highlight(object, 0);
//判断体的类型
int bodyType = 0;
UF_MODL_ask_body_type(object, &bodyType);
double bounding_box[6];
if (bodyType == UF_MODL_SOLID_BODY)
{
//体找边界盒
UF_MODL_ask_bounding_box(object, bounding_box);
边界盒的八个顶点
//Point3d point1 = { bounding_box[3] ,bounding_box[1] ,bounding_box[2] };
//Point3d point2 = { bounding_box[3] ,bounding_box[4] ,bounding_box[2] };
//Point3d point3 = { bounding_box[0] ,bounding_box[4] ,bounding_box[2] };
//Point3d point4 = { bounding_box[0] ,bounding_box[1] ,bounding_box[2] };
//Point3d point5 = { bounding_box[3] ,bounding_box[1] ,bounding_box[5] };
//Point3d point6 = { bounding_box[3] ,bounding_box[4] ,bounding_box[5] };
//Point3d point7 = { bounding_box[0] ,bounding_box[4] ,bounding_box[5] };
//Point3d point8 = { bounding_box[0] ,bounding_box[1] ,bounding_box[5] };
//八个面的中点
//Point3d facepoint1 = { (bounding_box[3] - bounding_box[0] ) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,bounding_box[2] };
//Point3d facepoint2 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,bounding_box[5] };
//Point3d facepoint3 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,(bounding_box[5] - bounding_box[2]) / 2 };
//Point3d facepoint4 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,(bounding_box[5] - bounding_box[2]) / 2 };
//Point3d facepoint5 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,(bounding_box[5] - bounding_box[2]) / 2 };
//Point3d facepoint6 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,(bounding_box[5] - bounding_box[2]) / 2 };
//Point3d facepoint7 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,(bounding_box[5] - bounding_box[2]) / 2 };
//Point3d facepoint8 = { (bounding_box[3] - bounding_box[0]) / 2,(bounding_box[4] - bounding_box[1]) / 2 ,(bounding_box[5] - bounding_box[2]) / 2 };
//创建辅助线获得两条辅助线的中点
UF_CURVE_line_t Line_coords1,Line_coords2, Line_coords3;
Line_coords1.start_point[0] = (bounding_box[3] - bounding_box[0]) / 2 + bounding_box[0];
Line_coords1.start_point[1] = (bounding_box[4] - bounding_box[1]) / 2 + bounding_box[1];
Line_coords1.start_point[2] = bounding_box[2];
Line_coords1.end_point[0] = (bounding_box[3] - bounding_box[0]) / 2 + bounding_box[0];
Line_coords1.end_point[1] = (bounding_box[4] - bounding_box[1]) / 2 + bounding_box[1];
Line_coords1.end_point[2] = bounding_box[5]- bounding_box[2];
Line_coords2.start_point[0] = bounding_box[3];
Line_coords2.start_point[1] = (bounding_box[4] - bounding_box[1]) / 2 + bounding_box[1];
Line_coords2.start_point[2] = (bounding_box[5] - bounding_box[2]) / 2 + bounding_box[2];
Line_coords2.end_point[0] = bounding_box[0];
Line_coords2.end_point[1] = (bounding_box[4] - bounding_box[1]) / 2 + bounding_box[1];
Line_coords2.end_point[2] = (bounding_box[5] - bounding_box[2]) / 2 + bounding_box[2];
Line_coords3.start_point[0] = (bounding_box[3] - bounding_box[0]) / 2 + bounding_box[0];
Line_coords3.start_point[1] = bounding_box[1];
Line_coords3.start_point[2] = (bounding_box[5] - bounding_box[2]) / 2 + bounding_box[2];
Line_coords3.end_point[0] = (bounding_box[3] - bounding_box[0]) / 2 + bounding_box[0];
Line_coords3.end_point[1] = bounding_box[4];
Line_coords3.end_point[2] = (bounding_box[5] - bounding_box[2]) / 2 + bounding_box[2];
tag_t Line1 = NULL_TAG, Line2 = NULL_TAG, Line3 = NULL_TAG;
UF_CURVE_create_line(&Line_coords1, &Line1);
UF_CURVE_create_line(&Line_coords2, &Line2);
UF_CURVE_create_line(&Line_coords3, &Line3);
//染色、换线型、放置图层
UF_OBJ_set_color(Line1, 186);
UF_OBJ_set_font(Line1, UF_OBJ_FONT_CENTERLINE);
UF_OBJ_set_layer(Line1, 5);
UF_OBJ_set_color(Line2, 186);
UF_OBJ_set_font(Line2, UF_OBJ_FONT_CENTERLINE);
UF_OBJ_set_layer(Line2, 5);
UF_OBJ_set_color(Line3, 186);
UF_OBJ_set_font(Line3, UF_OBJ_FONT_CENTERLINE);
UF_OBJ_set_layer(Line3, 5);
}
else
{
uc1601("提示:请选择实体", 1);
}
goto L10;
}
UF_terminate();
}
//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
// Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
try
{
// Create NXOpen C++ class instance
MyClass *theMyClass;
theMyClass = new MyClass();
theMyClass->do_it();
delete theMyClass;
}
catch (const NXException& e1)
{
UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
}
catch (const exception& e2)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
}
catch (...)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
}
}
//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}
效果图如下所示: