Pro/TOOLKIT示例程序(十四)特征元素树

Pro/TOOLKIT创建特征时需要用到特征元素树。
特征元素树是用树状的结构形式来表达定义某个特征时所需的全部信息。
基于参照面偏移一定距离创建基准面的代码如下:

static ProError UserElemtreeElementAdd(
    ProElement parent_element,
    ProElement child_element,
    ProValueData value_data )
{
    ProValue value;
    ProError status;
    status = ProValueAlloc(&value);
    status = ProValueDataSet(value, &value_data);
    status = ProElementValueSet(child_element, value);
    status = ProElemtreeElementAdd(parent_element, NULL, child_element);
    return status;
}
//-----------------------------------------------------------------------------
// 基于参照面偏移一定距离创建基准面
// offset_surface --- 参照面
// offset_dist    --- 偏移距离
// dtm_level      --- DTM_LEVEL_ASM  创建在顶层组立
//                    DTM_LEVEL_PART 创建在参照面所在的模型
//-----------------------------------------------------------------------------
int CreateOffsetDatumPlane(ProSelection offset_surface,
                           double       offset_dist,
                           int          dtm_level)
{
    ProError         status;
    ProElement       elem_tree, elem_ftype, elem_consts, elem_offset;
    ProElement       elem_const_type, elem_offset_ref, elem_offset_dist;
    ProValueData     value_data;
    ProModelitem     surf_modelitem, model_modelitem;
    ProSelection     mdl_sel;
    ProFeature       feature;
    ProErrorlist     errors;
    ProAsmcomppath   p_cmp_path;

    /*----------------------------------------------------------------*\
    Allocate the root element of the tree
    \*----------------------------------------------------------------*/
    status = ProElementAlloc(PRO_E_FEATURE_TREE, &elem_tree);

    /*----------------------------------------------------------------*\
    Allocate the feature type element. Add it to the element tree.
    \*----------------------------------------------------------------*/
    status = ProElementAlloc(PRO_E_FEATURE_TYPE, &elem_ftype);
    value_data.type = PRO_VALUE_TYPE_INT;
    value_data.v.i = PRO_FEAT_DATUM;
    status = UserElemtreeElementAdd(elem_tree, elem_ftype, value_data);

    /*----------------------------------------------------------------*\
    Allocate the constraints element. Add it to the element tree.
    \*----------------------------------------------------------------*/
    status = ProElementAlloc(PRO_E_DTMPLN_CONSTRAINTS, &elem_consts);
    status = ProElemtreeElementAdd(elem_tree, NULL, elem_consts);

    /*----------------------------------------------------------------*\
    Allocate the constraint element. Add it under constraints element.
    \*----------------------------------------------------------------*/
    status = ProElementAlloc(PRO_E_DTMPLN_CONSTRAINT, &elem_offset);
    status = ProElemtreeElementAdd(elem_consts, NULL, elem_offset);

    /*----------------------------------------------------------------*\
    Allocate the constraint type element. Add it under the
    constraint element.
    \*----------------------------------------------------------------*/
    status = ProElementAlloc(PRO_E_DTMPLN_CONSTR_TYPE, &elem_const_type);
    value_data.type = PRO_VALUE_TYPE_INT;
    value_data.v.i = PRO_DTMPLN_OFFS;   
    status = UserElemtreeElementAdd(elem_offset, elem_const_type, value_data);

    /*----------------------------------------------------------------*\
    Allocate the constraint reference element. Add it under the
    constraint element.
    \*----------------------------------------------------------------*/
    status = ProElementAlloc(PRO_E_DTMPLN_CONSTR_REF, &elem_offset_ref);
    value_data.type = PRO_VALUE_TYPE_SELECTION;
    value_data.v.r = offset_surface;
    status = UserElemtreeElementAdd(elem_offset, elem_offset_ref, value_data);

    /*----------------------------------------------------------------*\
    Allocate the reference offset value element. Add it under the
    constraint element.
    \*----------------------------------------------------------------*/
    status = ProElementAlloc (PRO_E_DTMPLN_CONSTR_REF_OFFSET, &elem_offset_dist);
    value_data.type = PRO_VALUE_TYPE_DOUBLE;
    value_data.v.d = offset_dist;
    status = UserElemtreeElementAdd(elem_offset, elem_offset_dist, value_data);

    /*----------------------------------------------------------------*\
    Get the assembly component path to the part that contains the
    offset surface.
    \*----------------------------------------------------------------*/
    status = ProSelectionAsmcomppathGet(offset_surface, &p_cmp_path);

    switch (dtm_level)
    {
    case DTM_LEVEL_ASM:
        /*----------------------------------------------------------*\
        Get a ProModelitem handle to the root assembly 
        \*----------------------------------------------------------*/
        status = ProMdlToModelitem(p_cmp_path.owner, &model_modelitem);
        /*----------------------------------------------------------*\
        Allocate a ProSection object for the root assembly.
        \*----------------------------------------------------------*/
        status = ProSelectionAlloc(NULL, &model_modelitem, &mdl_sel);
        break;

    case DTM_LEVEL_PART:
        /*----------------------------------------------------------*\
        Get a ProModelitem handle to the selected surface.
        \*----------------------------------------------------------*/
        status = ProSelectionModelitemGet(offset_surface, &surf_modelitem);
        /*----------------------------------------------------------*\
        Get a ProModelitem to the owner of the selected surface.
        \*----------------------------------------------------------*/
        status = ProMdlToModelitem(surf_modelitem.owner, &model_modelitem);
        /*----------------------------------------------------------*\
        Allocate a ProSection object for the part to which the
        selected surface belongs.
        \*----------------------------------------------------------*/
        status = ProSelectionAlloc(&p_cmp_path, &model_modelitem, &mdl_sel);
        break;

    default:
        return 0;
    }

    /*----------------------------------------------------------------*\
    Create the datum plane.
    \*----------------------------------------------------------------*/
    status = ProFeatureCreate(mdl_sel, elem_tree, NULL, 0, &feature, &errors);

    /*----------------------------------------------------------------*\
    Free data
    \*----------------------------------------------------------------*/
    status = ProElementFree(&elem_tree);
    status = ProSelectionFree(&mdl_sel);

    return status;
}

你可能感兴趣的:(Pro/E二次开发)