【NX二次开发】创建偏置的边或曲线UF_CURVE_create_offset_curve()
创建偏置的边或曲线例子
源码:(来自胡君大佬)
//创建偏置的边或曲线, direction:1=右或外,2=左或内, 原对象可以是单条、相连接的边或线、封闭的 void CreatOffsetCurves(vector<tag_t>& vCurves, double distance, int direction, vector<tag_t>& vNewCurves) { if (vCurves.size() <= 0) return; if (distance == 0) return; int n_curves = vCurves.size(); UF_CURVE_offset_data_t offset_data; offset_data.approximation_tolerance = 0.001; UF_MODL_ask_distance_tolerance(&offset_data.string_tolerance); offset_data.offset_type = UF_CURVE_OFFSET_DISTANCE_TANGENT; if (direction == 2) distance = -distance; char dist_str[21]; sprintf(dist_str, "%f", distance); if (true) { int i = 0; if (strchr(dist_str, '.')) { for (i = strlen(dist_str) - 1; dist_str[i] == '0'; i--) dist_str[i] = '\0'; if (dist_str[i] == '.') dist_str[i] = '\0'; } } UF_CURVE_offset_distance_data_t distance_data; distance_data.distance = dist_str; offset_data.offset_def.distance_type1 = &distance_data; double tol = 0.001; UF_MODL_ask_distance_tolerance(&tol); UF_STRING_t stringCurvs; //对线排序 if (true) { int n_segs[100] = { 0 }, dirs[100] = { 0 }; int n = 0; n_segs[n] = 1; dirs[n] = UF_MODL_CURVE_START_FROM_BEGIN; double free_start[3] = { 0.0 }, free_end[3] = { 0.0 }; if (true) { double limits[2] = { 0.0 }; UF_EVAL_p_t evaluator; UF_EVAL_initialize(vCurves[0], &evaluator); UF_EVAL_ask_limits(evaluator, limits); UF_EVAL_evaluate(evaluator, 0, limits[0], free_start, NULL); UF_EVAL_evaluate(evaluator, 0, limits[1], free_end, NULL); UF_EVAL_free(evaluator); } int start = 0; for (int i = 0; i < n_curves; i++) { bool found = FALSE; for (int j = i+1; j < n_curves; j++) { double this_start[3] = { 0.0 }, this_end[3] = { 0.0 }; if (true) { double limits[2] = { 0.0 }; UF_EVAL_p_t evaluator; UF_EVAL_initialize(vCurves[j], &evaluator); UF_EVAL_ask_limits(evaluator, limits); UF_EVAL_evaluate(evaluator, 0, limits[0], this_start, NULL); UF_EVAL_evaluate(evaluator, 0, limits[1], this_end, NULL); UF_EVAL_free(evaluator); } double gap = 0.0; UF_VEC3_distance(this_start, free_start, &gap); if (gap <= tol) { tag_t temp = vCurves[j]; for (int k = j; k > start; k--) vCurves[k] = vCurves[k - 1]; vCurves[start] = temp; n_segs[n]++; dirs[n] = UF_MODL_CURVE_START_FROM_END; UF_VEC3_copy(this_end, free_start); found = TRUE; break; } UF_VEC3_distance(this_end, free_start, &gap); if (gap <= tol) { tag_t temp = vCurves[j]; for (int k = j; k > start; k--) vCurves[k] = vCurves[k - 1]; vCurves[start] = temp; n_segs[n]++; dirs[n] = UF_MODL_CURVE_START_FROM_BEGIN; UF_VEC3_copy(this_start, free_start); found = TRUE; break; } UF_VEC3_distance(this_start, free_end, &gap); if (gap <= tol) { tag_t temp = vCurves[j]; vCurves[j] = vCurves[i + 1]; vCurves[i + 1] = temp; n_segs[n]++; UF_VEC3_copy(this_end, free_end); found = TRUE; break; } UF_VEC3_distance(this_end, free_end, &gap); if (gap <= tol) { tag_t temp = vCurves[j]; vCurves[j] = vCurves[i + 1]; vCurves[i + 1] = temp; n_segs[n]++; UF_VEC3_copy(this_start, free_end); found = TRUE; break; } } if (found == FALSE) { n++; n_segs[n] = 1; dirs[n] = UF_MODL_CURVE_START_FROM_BEGIN; start = i + 1; if (i + 1 < n_curves) { double limits[2] = { 0.0 }; UF_EVAL_p_t evaluator; UF_EVAL_initialize(vCurves[i+1], &evaluator); UF_EVAL_ask_limits(evaluator, limits); UF_EVAL_evaluate(evaluator, 0, limits[0], free_start, NULL); UF_EVAL_evaluate(evaluator, 0, limits[1], free_end, NULL); UF_EVAL_free(evaluator); } } } UF_MODL_init_string_list(&stringCurvs); UF_MODL_create_string_list(n, n_curves, &stringCurvs); for (int i = 0; i < n; i++) { stringCurvs.string[i] = n_segs[i]; stringCurvs.dir[i] = dirs[i]; } for (int i = 0; i < n_curves; i++) stringCurvs.id[i] = vCurves[i]; } offset_data.input_curves = &stringCurvs; int num_curves=0; tag_t * offset_curves=NULL; UF_CURVE_create_offset_curve(&offset_data, &num_curves, &offset_curves); //无参偏置 for (int i = 0; i < num_curves; i++) vNewCurves.push_back(offset_curves[i]); UF_DISP_refresh(); UF_free(offset_curves); UF_MODL_free_string_list(&stringCurvs); }