NX9+VS2012 #include <uf.h> #include <uf_sket.h> #include <uf_modl.h> #include <uf_curve.h> UF_initialize(); //初始化草图 char SketchName[UF_OBJ_NAME_BUFSIZE] = "AAA"; tag_t SketchTag = NULL_TAG; UF_SKET_initialize_sketch(SketchName, &SketchTag); ////创建草图(按坐标系矩阵方式创建) //int option = 2; //double matrix[9] = {1,0,0,0,1,0,1,0,0}; //tag_t object[2]; //int reference[2]; //int plane_dir = 2; //UF_SKET_create_sketch(SketchName, option, matrix, object, reference, plane_dir, &SketchTag); //创建基准平面 double origin_point[3] = {0,0,100}; double plane_normal[3] = {0,0,1}; tag_t plane_tag = NULL_TAG; UF_MODL_create_fixed_dplane(origin_point, plane_normal, &plane_tag); //创建基准轴(轴的方向就是草图摆放的水平方向) double point1[3] = {0,0,0}; double point2[3] = {10,0,0}; tag_t daxis_tag = NULL_TAG; UF_MODL_create_fixed_daxis(point1, point2, &daxis_tag); //创建草图(在基准平面上创建) int option = 1; double matrix[9]; tag_t object[2] = {plane_tag, daxis_tag}; int reference[2] = {1, 1}; int plane_dir = 2; UF_SKET_create_sketch(SketchName, option, matrix, object, reference, plane_dir, &SketchTag); //创建四条直线(做矩形) UF_CURVE_line_t Line_coords1; Line_coords1.start_point[0] = 0; Line_coords1.start_point[1] = 0; Line_coords1.start_point[2] = 100; Line_coords1.end_point[0] = 100; Line_coords1.end_point[1] = 0; Line_coords1.end_point[2] = 100; tag_t Line1Tag = NULL_TAG; UF_CURVE_create_line(&Line_coords1, &Line1Tag); UF_CURVE_line_t Line_coords2; Line_coords2.start_point[0] = 100; Line_coords2.start_point[1] = 0; Line_coords2.start_point[2] = 100; Line_coords2.end_point[0] = 100; Line_coords2.end_point[1] = -100; Line_coords2.end_point[2] = 100; tag_t Line2Tag = NULL_TAG; UF_CURVE_create_line(&Line_coords2, &Line2Tag); UF_CURVE_line_t Line_coords3; Line_coords3.start_point[0] = 100; Line_coords3.start_point[1] = -100; Line_coords3.start_point[2] = 100; Line_coords3.end_point[0] = 0; Line_coords3.end_point[1] = -100; Line_coords3.end_point[2] = 100; tag_t Line3Tag = NULL_TAG; UF_CURVE_create_line(&Line_coords3, &Line3Tag); UF_CURVE_line_t Line_coords4; Line_coords4.start_point[0] = 0; Line_coords4.start_point[1] = -100; Line_coords4.start_point[2] = 100; Line_coords4.end_point[0] = 0; Line_coords4.end_point[1] = 0; Line_coords4.end_point[2] = 100; tag_t Line4Tag = NULL_TAG; UF_CURVE_create_line(&Line_coords4, &Line4Tag); //添加到草图里 tag_t SketchLine[4] = {Line1Tag,Line2Tag,Line3Tag,Line4Tag}; UF_SKET_add_objects(SketchTag, 4, SketchLine); //创建水平几何约束 UF_SKET_con_geom_t con_geoms1[1];//定义数组结构体 con_geoms1[0].geom_tag = Line1Tag;//直线对象 con_geoms1[0].vertex_type = UF_SKET_no_vertex;//不需要点 tag_t con_tag1 = NULL_TAG; UF_SKET_create_geometric_constraint(SketchTag, UF_SKET_horizontal, 1,con_geoms1, &con_tag1);//设置为水平约束 UF_SKET_con_geom_t con_geoms2[1]; con_geoms2[0].geom_tag = Line3Tag; con_geoms2[0].vertex_type = UF_SKET_no_vertex; tag_t con_tag2 = NULL_TAG; UF_SKET_create_geometric_constraint(SketchTag, UF_SKET_horizontal, 1,con_geoms2, &con_tag2); //创建竖直几何约束 UF_SKET_con_geom_t con_geoms3[1]; con_geoms3[0].geom_tag = Line2Tag; con_geoms3[0].vertex_type = UF_SKET_no_vertex; tag_t con_tag3 = NULL_TAG; UF_SKET_create_geometric_constraint(SketchTag, UF_SKET_vertical, 1,con_geoms3, &con_tag3); UF_SKET_con_geom_t con_geoms4[1]; con_geoms4[0].geom_tag = Line4Tag; con_geoms4[0].vertex_type = UF_SKET_no_vertex; tag_t con_tag4 = NULL_TAG; UF_SKET_create_geometric_constraint(SketchTag, UF_SKET_vertical, 1,con_geoms4, &con_tag4); //创建点 double PointCoords[3] = {0,0,100}; tag_t PointTag = NULL_TAG; UF_CURVE_create_point(PointCoords, &PointTag); //创建点到点几何约束 UF_SKET_con_geom_t con_geoms5[2]; con_geoms5[0].geom_tag = Line1Tag;//直线对象 con_geoms5[0].vertex_type = UF_SKET_start_vertex;//起点 con_geoms5[1].geom_tag = PointTag;//点对象 con_geoms5[1].vertex_type = UF_SKET_no_vertex;//不需要点 tag_t con_tag5 = NULL_TAG; UF_SKET_create_geometric_constraint(SketchTag, UF_SKET_coincident , 2,con_geoms5, &con_tag5); ////标注尺寸 //UF_SKET_dim_object_t Line1DimObject1;//定义结构体 //Line1DimObject1.object_tag = Line1Tag;//直线对象 //Line1DimObject1.object_assoc_type = UF_SKET_end_point;//点类型 //Line1DimObject1.object_assoc_mod_value = UF_SKET_first_end_point;//起点 //UF_SKET_dim_object_t Line1DimObject2;//定义结构体 //Line1DimObject2.object_tag = Line1Tag;//直线类型 //Line1DimObject2.object_assoc_type = UF_SKET_end_point;//点类型 //Line1DimObject2.object_assoc_mod_value = UF_SKET_last_end_point ;//终点 //double Line1DimOrigin[3] = {50,10,100};//尺寸位置放置点 //tag_t Line1Dim1Tag = NULL_TAG; //UF_SKET_create_dimension(SketchTag, UF_SKET_horizontal_dim, &Line1DimObject1, &Line1DimObject2, Line1DimOrigin, &Line1Dim1Tag); ////标注尺寸 //UF_SKET_dim_object_t Line2DimObject1;//定义结构体 //Line2DimObject1.object_tag = Line2Tag;//直线对象 //Line2DimObject1.object_assoc_type = UF_SKET_end_point;//点类型 //Line2DimObject1.object_assoc_mod_value = UF_SKET_first_end_point;//起点 //UF_SKET_dim_object_t Line2DimObject2;//定义结构体 //Line2DimObject2.object_tag = Line2Tag;//直线类型 //Line2DimObject2.object_assoc_type = UF_SKET_end_point;//点类型 //Line2DimObject2.object_assoc_mod_value = UF_SKET_last_end_point ;//终点 //double Line2DimOrigin[3] = {120,-50,100};//尺寸位置放置点 //tag_t Line2Dim1Tag = NULL_TAG; //UF_SKET_create_dimension(SketchTag, UF_SKET_vertical_dim, &Line2DimObject1, &Line2DimObject2, Line2DimOrigin, &Line2Dim1Tag); //标注尺寸约束(直线1) UF_SKET_dim_object_t Line1DimObject1[2];//定义数组结构体 Line1DimObject1[0].object_tag = Line1Tag;//直线对象 Line1DimObject1[0].object_assoc_type = UF_SKET_end_point;//点类型 Line1DimObject1[0].object_assoc_mod_value = UF_SKET_first_end_point;//起点 Line1DimObject1[1].object_tag = Line1Tag;//直线对象 Line1DimObject1[1].object_assoc_type = UF_SKET_end_point;//点类型 Line1DimObject1[1].object_assoc_mod_value = UF_SKET_last_end_point;//终点 double Line1DimOrigin[3] = {50,10,100};//尺寸位置放置点 tag_t Line1Con1Tag = NULL_TAG; UF_SKET_create_dimensional_constraint(SketchTag, UF_SKET_horizontal_dim,2, Line1DimObject1, Line1DimOrigin, &Line1Con1Tag); //标注尺寸约束(直线2) UF_SKET_dim_object_t Line2DimObject1[2];//定义数组结构体 Line2DimObject1[0].object_tag = Line2Tag;//直线对象 Line2DimObject1[0].object_assoc_type = UF_SKET_end_point;//点类型 Line2DimObject1[0].object_assoc_mod_value = UF_SKET_first_end_point;//起点 Line2DimObject1[1].object_tag = Line2Tag;//直线对象 Line2DimObject1[1].object_assoc_type = UF_SKET_end_point;//点类型 Line2DimObject1[1].object_assoc_mod_value = UF_SKET_last_end_point;//终点 double Line2DimOrigin[3] = {120,-50,100};//尺寸位置放置点 tag_t Line2Con1Tag = NULL_TAG; UF_SKET_create_dimensional_constraint(SketchTag, UF_SKET_vertical_dim,2, Line2DimObject1, Line2DimOrigin, &Line2Con1Tag); //更新草图 UF_SKET_update_sketch(SketchTag); //终止草图 UF_SKET_terminate_sketch(); UF_terminate(); Caesar卢尚宇 2020年8月15日
NX二次开发-NXOPEN C++方式创建草图,添加约束,标注尺寸 https://www.cnblogs.com/nxopen2018/p/13510239.html