获取选择的程序刀轨和当前加工坐标系,获取刀轨数据点转换到加工坐标系,然后创建样条,创建样条是用UG录制的代码,有更好的方法欢迎交流学习一下。
void CreateCurve()
{
tag_t setup_tag;
UF_SETUP_ask_setup(&setup_tag);
int count = 0;
tag_t* objects;
UF_UI_ONT_ask_selected_nodes(&count, &objects);
if (count != 1)
{
uc1601("选择一个程序或程序组", 1);
return;
}
tag_t initial_wcs;
tag_t wcs_value;
logical is_group = TRUE;
tag_p_t oprea_list = NULL_TAG;
UF_CSYS_ask_wcs(&initial_wcs);
UF_NCGROUP_is_group(objects[0], &is_group);
if (is_group == TRUE)
{
if (count == 0)
{
uc1601("程序组为空", 1);
return;
}
UF_NCGROUP_ask_member_list(objects[0], &count, &oprea_list);
UF_PARAM_ask_tag_value(oprea_list[0], UF_PARAM_MCS, &wcs_value);
}
else
{
UF_PARAM_ask_tag_value(objects[0], UF_PARAM_MCS, &wcs_value);
}
tag_t matrix_tag;
double origin[3];
UF_CSYS_ask_csys_info(wcs_value, &matrix_tag, origin);
UF_CSYS_ask_matrix_values(matrix_tag,Matrix);
UF_CSYS_set_wcs(wcs_value);
UF_CAM_preferences_t number;
UF_CAM_ask_cam_preferences(&number);
number.clsf_decimal_place = 5;
UF_CAM_set_cam_preferences(&number);
char path_char[133] = "D:\\tmpcls.cls";
UF_SETUP_generate_clsf(setup_tag, objects[0], "CLSF_STANDARD", path_char, UF_SETUP_OUTPUT_UNITS_OUTPUT_DEFINED);
FILE* fp = fopen(path_char, "r");
char fchar[256];
if (fp == 0)
{
uc1601("转换失败", 1);
return;
}
string fbuf;
int k1 = -1, k2 = -1, k3 = -1, k4 = -1,k5=-1,k6=-1,k7=-1,k8=-1;
bool isStart = false;
char msg[256];
UF_OBJ_ask_name(objects[0],msg);
vector<Point3d>points;
while (1)
{
if (fgets(fchar, 256, fp) == NULL)
{
break;
}
fbuf = fchar;
if (strstr(fbuf.c_str(), msg))
{
isStart = true;
points.clear();
continue;
}
if (!isStart)
{
continue;
}
if (fbuf.compare(0, 4, "GOTO") == 0)
{
k1 = k2 = k3 = k4 =k5=k6=k7=k8=-1;
k1 = fbuf.find('/', 0);
k2 = fbuf.find(',', k1 + 1);
k3 = fbuf.find(',', k2 + 1);
k4 = fbuf.find(',', k3 + 1);
k5 = fbuf.find(',', k4 + 1);
k6 = fbuf.find(',', k5 + 1);
k7 = fbuf.find(',', k6 + 1);
k8 = fbuf.find(',', k7 + 1);
double point1[3] = { 0,0,0 };
double point2[3] = { 0,0,0 };
point1[0] = atof(fbuf.substr(k1 + 1, k2 - k1 - 1).c_str());
point1[1] = atof(fbuf.substr(k2 + 1, k3 - k2 - 1).c_str());
if (k4 == string::npos)
{
point1[2] = atof(fbuf.substr(k3 + 1, fbuf.length()).c_str());
}
else
{
point1[2] = atof(fbuf.substr(k3 + 1, k4 - k3 - 1).c_str());
}
UF_CSYS_map_point(UF_CSYS_ROOT_WCS_COORDS, point1, UF_CSYS_ROOT_COORDS, point1);
UF_CSYS_map_point(UF_CSYS_ROOT_COORDS, point1, UF_CSYS_WORK_COORDS, point1);
Point3d point_3d1(point1[0],point1[1],point1[2]);
points.push_back(point_3d1);
}
}
UF_CSYS_set_wcs(initial_wcs);
CreateSpline(points);
}
tag_t CreateSpline(vector<Point3d>points)
{
if (Spline_tag!=NULL)
{
UF_OBJ_delete_object(Spline_tag);
Spline_tag = NULL;
}
if (points.size() == 0)
{
return Spline_tag;
}
if (points.size() ==1)
{
double point[3] = { points[0].X,points[0].Y,points[1].Z };
UF_CURVE_create_point(point, &Spline_tag);
return Spline_tag;
}
try
{
Session* theSession = Session::GetSession();
Part* workPart(theSession->Parts()->Work());
Part* displayPart(theSession->Parts()->Display());
NXObject* nullNXObject(NULL);
Features::StudioSplineBuilderEx* studioSplineBuilderEx1;
studioSplineBuilderEx1 = workPart->Features()->CreateStudioSplineBuilderEx(nullNXObject);
Point3d origin1(0.0, 0.0, 0.0);
Vector3d normal1(0.0, 0.0, 1.0);
Plane* plane1;
plane1 = workPart->Planes()->CreatePlane(origin1, normal1, SmartObject::UpdateOptionWithinModeling);
studioSplineBuilderEx1->SetDrawingPlane(plane1);
Point3d origin2(0.0, 0.0, 0.0);
Vector3d normal2(0.0, 0.0, 1.0);
Plane* plane2;
plane2 = workPart->Planes()->CreatePlane(origin2, normal2, SmartObject::UpdateOptionWithinModeling);
studioSplineBuilderEx1->SetMovementPlane(plane2);
studioSplineBuilderEx1->OrientExpress()->SetReferenceOption(GeometricUtilities::OrientXpressBuilder::ReferenceWcsDisplayPart);
studioSplineBuilderEx1->SetMatchKnotsType(Features::StudioSplineBuilderEx::MatchKnotsTypesNone);
studioSplineBuilderEx1->OrientExpress()->SetAxisOption(GeometricUtilities::OrientXpressBuilder::AxisPassive);
studioSplineBuilderEx1->OrientExpress()->SetPlaneOption(GeometricUtilities::OrientXpressBuilder::PlanePassive);
studioSplineBuilderEx1->SetDegree(1);
for (int i = 0; i < points.size(); i++)
{
Point* point1;
point1 = workPart->Points()->CreatePoint(points[i]);
Features::GeometricConstraintData* geometricConstraintData1;
geometricConstraintData1 = studioSplineBuilderEx1->ConstraintManager()->CreateGeometricConstraintData();
geometricConstraintData1->SetPoint(point1);
studioSplineBuilderEx1->ConstraintManager()->Append(geometricConstraintData1);
}
NXObject* nXObject1;
nXObject1 = studioSplineBuilderEx1->Commit();
studioSplineBuilderEx1->Destroy();
if (nXObject1)
{
Features::Feature* feature1 = dynamic_cast<Features::Feature*>(nXObject1);
vector<NXObject*>vec = feature1->GetEntities();
if (!vec.empty())
{
Spline_tag = vec[0]->Tag();
}
}
return Spline_tag;
}
catch (const std::exception&)
{
return Spline_tag;
}
}