NX/UG二次开发—CAM—设置程序修剪边界Boundary

最近做了一个设置CAM程序边界的小工具,分享一下经验(官方的代码在文章末尾处),以下均以修剪边界曲线为例。

以下为官方提供的源码:

/***************************** Include Files **********************************/
#include <stdlib.h>

#include <uf.h>
#include <uf_obj.h>
#include <uf_object_types.h>
#include <uf_part.h>
#include <uf_ui.h>
#include <uf_ui_ont.h>
#include <uf_cambnd.h>
#include <uf_cam.h>
#include <uf_object_types.h>
#include <uf_disp.h>


#define     MAX_BND      20

/*******************************************************************/
static UF_CAMBND_boundary_data_p_t create_and_init_bnd_data
(
    UF_CAM_boundary_type_t   type,
    int                      plane_type,
    double                   *origin,
    double                   *matrix,
    UF_CAM_material_side_t   material_side,
    int                      ignore_holes,
    int                      ignore_islands,
    int                      ignore_chamfers,
    UF_CAMBND_app_data_p_t   app_data
);
static UF_CAMBND_app_data_p_t create_and_init_bnd_app_data(void);
static int init_proc1(UF_UI_selection_p_t select, void *user_data);
static int init_proc2(UF_UI_selection_p_t select, void *user_data);

/*******************************************************************/
static void do_cambnd_api(void)
{
    UF_CAMBND_boundary_data_p_t  bnd_data, bnd_data_list[MAX_BND];
    UF_CAMBND_app_data_p_t       app_data_list[MAX_BND]; 
    tag_t                        part_tag, *objects, entity_list[MAX_BND];
    int                          i, j, object_count, part_count, part_item_count;
    int                          type, subtype;
    UF_CAMBND_boundary_t         *part_bnd_list;
    UF_CAMBND_item_t             *part_item_list;

    /* Get the displayed part. */
    part_tag = UF_PART_ask_display_part( );
    
    if( part_tag != null_tag )
    {
        /* Get the highlighted/selected objects from Navigation Tool. */
        UF_UI_ONT_ask_selected_nodes( &object_count, &objects ); 

        if (object_count > 0)
        {
            tag_t     eid, view;
            double    cursor[3], origin[3], matrix[9];
            int       entity_count, response;

            char      cue_face[]   = "Select faces";
            char      cue_edge[]   = "Select edges";
            char      title[]      = "Boundary Selection";

            entity_count = 0;

            /* Allow the user to select faces. */
            while (TRUE)
            {
                UF_UI_select_with_single_dialog(cue_face, title,
                                                UF_UI_SEL_SCOPE_NO_CHANGE, init_proc1, NULL,
                                                &response, &eid, cursor, &view);

                if (response == UF_UI_OBJECT_SELECTED)
                {                    
                    entity_list[entity_count] = eid;

                    /* Allocate the memory for the boundary data of an item without app data.  */
                    bnd_data_list[entity_count] = create_and_init_bnd_data(UF_CAM_boundary_type_closed,
                                                                           1, origin, matrix, 
                                                                           UF_CAM_material_side_out_right,
                                                                           0, 0, 0, NULL);

                    entity_count++;
                }
                else if (response == UF_UI_OK || response == UF_UI_CANCEL || 
                         response == UF_UI_BACK)
                {
                    /* Geometry selection is finished. */
                    break;
                }
            }

            /* Append the face boundaries. */
            if (entity_count > 0)
            {
                for(i=0; i<object_count; i++)
                {
                    UF_OBJ_ask_type_and_subtype(objects[i], &type, &subtype);

                    /* The object type must be Operation or Geometry Group. */
                    if( type == UF_machining_operation_type || type == UF_machining_geometry_grp_type)
                    {
                        for (j=0; j<entity_count; j++)
                        {
                            /***********************************************************************/
                            /*
                             * UF_CAMBND_append_bnd_from_face
                             */
                            /***********************************************************************/
                            UF_CAMBND_append_bnd_from_face(objects[i], UF_CAM_part, 
                                                           entity_list[j], bnd_data_list[j]);
                            UF_CAMBND_append_bnd_from_face(objects[i], UF_CAM_blank, 
                                                           entity_list[j], bnd_data_list[j]);
                            UF_CAMBND_append_bnd_from_face(objects[i], UF_CAM_check, 
                                                           entity_list[j], bnd_data_list[j]);
                            UF_CAMBND_append_bnd_from_face(objects[i], UF_CAM_trim, 
                                                           entity_list[j], bnd_data_list[j]);
                        }
                    }
                }
            }

            /* Free the boundary data. */
            for (i=0; i<entity_count; i++)
            {
                UF_free(bnd_data_list[i]);
            }

            entity_count = 0;
            
            /* Allow the user to select edges. */
            while (TRUE)
            {
                UF_UI_select_with_single_dialog(cue_edge, title,
                                                UF_UI_SEL_SCOPE_NO_CHANGE, init_proc2, NULL,
                                                &response, &eid, cursor, &view);

                if (response == UF_UI_OBJECT_SELECTED)
                {                    
                    entity_list[entity_count] = eid;
                    app_data_list[entity_count] = create_and_init_bnd_app_data();
                    entity_count++;
                }
                else if (response == UF_UI_OK || response == UF_UI_CANCEL || 
                         response == UF_UI_BACK)
                {
                    /* Geometry selection is finished. */
                    break;
                }
            }

            /* Append curve boundaries. */
            if (entity_count > 0)
            {
                bnd_data = create_and_init_bnd_data(UF_CAM_boundary_type_open,
                                                    1, origin, matrix, 
                                                    UF_CAM_material_side_out_right,
                                                    0, 0, 0, NULL);
                for(i=0; i<object_count; i++)
                {
                    UF_OBJ_ask_type_and_subtype(objects[i], &type, &subtype);

                    if( type == UF_machining_operation_type || 
                        type == UF_machining_geometry_grp_type)
                    {
                        /***********************************************************************/
                        /*
                         * UF_CAMBND_append_bnd_from_curve
                         */
                        /***********************************************************************/
                        UF_CAMBND_append_bnd_from_curve(objects[i], UF_CAM_part, entity_count, 
                                                        entity_list, bnd_data, app_data_list);
                        UF_CAMBND_append_bnd_from_curve(objects[i], UF_CAM_blank, entity_count, 
                                                        entity_list, bnd_data, app_data_list);
                        UF_CAMBND_append_bnd_from_curve(objects[i], UF_CAM_check, entity_count, 
                                                        entity_list, bnd_data, app_data_list);
                        UF_CAMBND_append_bnd_from_curve(objects[i], UF_CAM_trim, entity_count, 
                                                        entity_list, bnd_data, app_data_list);
                    }
                }
            
                /* Free memory. */
                for (i=0; i<entity_count; i++)
                {
                    UF_free(app_data_list[i]);
                }
                
                UF_free (bnd_data);
            }

            /***********************************************************************/
            /*
             * UF_CAMBND_ask_boundaries
             */
            /***********************************************************************/
            UF_CAMBND_ask_boundaries(objects[0], UF_CAM_part, &part_count, &part_bnd_list);

            for (i=0; i<part_count; i++)
            {
                UF_CAMBND_boundary_data_t boundary_data;
                UF_CAMBND_app_data_t      app_data;

                /***********************************************************************/
                /*
                 * UF_CAMBND_ask_boundary_data
                 */
                /***********************************************************************/
                UF_CAMBND_ask_boundary_data(part_bnd_list[i], &boundary_data);

                /***********************************************************************/
                /*
                 * UF_CAMBND_ask_boundary_app_data
                 */
                /***********************************************************************/
                UF_CAMBND_ask_boundary_app_data(part_bnd_list[i], &app_data);

                /***********************************************************************/
                /*
                 * UF_CAMBND_set_boundary_app_data
                 */
                /***********************************************************************/
                UF_CAMBND_set_boundary_app_data(objects[0], UF_CAM_part, part_bnd_list[i], &app_data);

                /***********************************************************************/
                /*
                 * UF_CAMBND_ask_boundary_items
                 */
                /***********************************************************************/
                UF_CAMBND_ask_boundary_items(part_bnd_list[i], &part_item_count, &part_item_list);

                for (j=0; j<part_item_count; j++)
                {
                    tag_t                 entity;
                    UF_CAMBND_app_data_t  item_app_data;

                    /***********************************************************************/
                    /*
                     * UF_CAMBND_ask_item_entity
                     */
                    /***********************************************************************/
                    UF_CAMBND_ask_item_entity(part_item_list[j], &entity);

                    /***********************************************************************/
                    /*
                     * UF_CAMBND_ask_item_app_data
                     */
                    /***********************************************************************/
                    UF_CAMBND_ask_item_app_data(part_item_list[j], &item_app_data);


                    /***********************************************************************/
                    /*
                     * UF_CAMBND_set_item_app_data
                     */
                    /***********************************************************************/
                    UF_CAMBND_set_item_app_data(objects[0], UF_CAM_part, part_bnd_list[i],
                                                part_item_list[j], NULL);
                }

                /***********************************************************************/
                /*
                 * UF_CAMBND_delete_boundary
                 */
                /***********************************************************************/
                
                UF_CAMBND_delete_boundary(objects[0], UF_CAM_part, part_bnd_list[i]);
            }

            /***********************************************************************/
            /*
             * UF_CAMBND_delete_boundaries
             */
            /***********************************************************************/

            UF_CAMBND_delete_boundaries(objects[0], UF_CAM_blank);
            UF_CAMBND_delete_boundaries(objects[0], UF_CAM_check);
            UF_CAMBND_delete_boundaries(objects[0], UF_CAM_trim);

        }        
    }

    return;
}


/* Selection initialization procedures.*/
static int init_proc1
(
    UF_UI_selection_p_t select,
    void* user_data
)
{
    int num_triples = 1;
    UF_UI_mask_t mask_triples[] = {UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE};

    /* enable only lines and edges */
    if((UF_UI_set_sel_mask(select,
                           UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,
                           num_triples, mask_triples)) == 0)
    {
        return (UF_UI_SEL_SUCCESS);
    }
    else
    {
        return (UF_UI_SEL_FAILURE);
    }
}

static int init_proc2
(
    UF_UI_selection_p_t select,
    void* user_data
)
{
    int num_triples = 1;
    UF_UI_mask_t mask_triples[] = {UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_EDGE};

    /* enable only lines and edges */
    if((UF_UI_set_sel_mask(select,
                           UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,
                           num_triples, mask_triples)) == 0)
    {
        return (UF_UI_SEL_SUCCESS);
    }
    else
    {
        return (UF_UI_SEL_FAILURE);
    }
}

static UF_CAMBND_boundary_data_p_t create_and_init_bnd_data
(
    UF_CAM_boundary_type_t   type,
    int                      plane_type,
    double                   *origin,
    double                   *matrix,
    UF_CAM_material_side_t   material_side,
    int                      ignore_holes,
    int                      ignore_islands,
    int                      ignore_chamfers,
    UF_CAMBND_app_data_p_t   app_data
)
{
    UF_CAMBND_boundary_data_p_t bnd_data;
    int                         i, err_code;

    bnd_data = (UF_CAMBND_boundary_data_p_t) UF_allocate_memory(
                                             sizeof(UF_CAMBND_boundary_data_t), &err_code);

    bnd_data->boundary_type = type;
    bnd_data->plane_type = plane_type ;
    for (i=0; i<3; i++)
        bnd_data->origin[i] = origin[i] ;
    for (i=0; i<9; i++)
        bnd_data->matrix[i] = matrix[i] ;
    bnd_data->material_side = material_side;
    bnd_data->ignore_holes = ignore_holes ;
    bnd_data->ignore_islands = ignore_islands ; 
    bnd_data->ignore_chamfers = ignore_chamfers ;    
    bnd_data->app_data = app_data;

    return(bnd_data);
}

static UF_CAMBND_app_data_p_t create_and_init_bnd_app_data(void)
{
    UF_CAMBND_app_data_p_t  app_data; 
    int                     err_code;

    app_data = (UF_CAMBND_app_data_p_t) UF_allocate_memory(
                                          sizeof(UF_CAMBND_app_data_t), &err_code);

    if (err_code)
return(NULL);

    /* Set flags. */
    app_data->has_stock = 1;
    app_data->has_tolerances = 0 ;
    app_data->has_feedrate = 0 ;
    app_data->has_blank_distance = 0;
    app_data->has_tool_position = 0;
    
    /* Set values. */
    app_data->stock = 0.1 ;
    app_data->tolerances[0] = 0.1 ;
    app_data->tolerances[1] = 0.1 ;
    app_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;
    app_data->feedrate_value = 0.1 ;
    app_data->blank_distance = 0.0 ;
    app_data->tool_position = UF_CAM_tool_position_tanto;

    return(app_data);
}

/*ARGSUSED*/
void ufusr(char *param, int *retcode, int param_len)
{
    UF_initialize();

    do_cambnd_api();

    UF_terminate();
}

int ufusr_ask_unload(void)
{
 return (UF_UNLOAD_IMMEDIATELY);
}

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
NX二次开发中获取边界类型可以使用以下代码: ``` #include <NXOpen/Part.hxx> #include <NXOpen/Body.hxx> #include <NXOpen/BodyCollection.hxx> #include <NXOpen/Edge.hxx> #include <NXOpen/EdgeCollection.hxx> #include <NXOpen/Features_Feature.hxx> #include <NXOpen/Features_FeatureCollection.hxx> #include <NXOpen/Features_FeatureType.hxx> #include <NXOpen/TopologicalRegion.hxx> #include <NXOpen/TopologicalRegionCollection.hxx> #include <NXOpen/TopologyQuery.hxx> using namespace NXOpen; void GetBoundaryType() { Part *part = NXOpen::Session::GetSession()->Parts()->Work(); BodyCollection *bodies = part->Bodies(); Features::FeatureCollection *features = part->Features(); // Loop through all the bodies in the part for (int i = 0; i < bodies->Count(); i++) { Body *body = bodies->GetItem(i); // Loop through all the edges in the body EdgeCollection *edges = body->GetEdges(); for (int j = 0; j < edges->Count(); j++) { Edge *edge = edges->GetItem(j); // Check if the edge is a boundary edge TopologicalRegionCollection *regions = edge->GetTopology()->GetRegions(); if (regions->Count() == 1) { TopologicalRegion *region = regions->GetItem(0); if (region->IsSolidRegion()) { // The edge is a boundary edge of a solid body // Do something } else if (region->IsSheetRegion()) { // The edge is a boundary edge of a sheet body // Do something } else if (region->IsWireframeRegion()) { // The edge is a boundary edge of a wireframe body // Do something } } } // Loop through all the features in the body for (int j = 0; j < features->Count(); j++) { Features::Feature *feature = features->GetItem(j); // Check if the feature is a boundary feature if (feature->Type() == Features::FeatureType::Boundary) { // The feature is a boundary feature // Do something } } } } ``` 这段代码会遍历当前工作部件中所有的边界,然后通过`GetTopology()`方法获取边界的拓扑结构信息,再通过`GetRegions()`方法获取边界的区域信息,最后根据区域的类型确定边界的类型。另外,还可以通过遍历部件中的所有特征,判断特征类型是否为边界,来获取所有的边界类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恩·艾克斯·红

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值