仍然是丰富绘图草绘功能,实现Creo在草绘直线可以沿原直线延长和缩短的功能。

CREO Toolkit二次开发-草绘直线修改垂直水平一文修改草绘的操作一致,通过while循环使用ProMouseTrack获取鼠标坐标,计算鼠标坐标在直线或直线延长线上的投影坐标,并计算出直线延长线或直线的投影坐标,最后根据投影坐标更新直线,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (1)
{
status = ProMouseTrack(options, &button_pressed, positionmouse);
// 按任意鼠标键退出,注意PRO_ANY_BUTTON是个坑
if (button_pressed == PRO_LEFT_BUTTON || button_pressed == PRO_RIGHT_BUTTON || button_pressed == PRO_MIDDLE_BUTTON)
{
status = ProWindowRepaint(PRO_VALUE_UNUSED);
break;
}
// 这里要计算了
calculate_and_update(curvedata.line.end1, curvedata.line.end2, positionmouse, bidirectional);
// 实时更改线段
status = ProDtlentitydataCurveSet(entdata, &curvedata);
status = ProDtlentityModify(&modelitem, NULL, entdata);
status = ProWindowCurrentGet(&wid);
status = ProWindowRefresh(wid);
}

calculate_and_update函数主要用于计算鼠标坐标在直线或直线延长线上的投影坐标,简单的几何计算,也没有什么需要特殊说明的,给出代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void calculate_and_update(ProPoint3d start, ProPoint3d end, ProPoint3d third, int bidirectional)
{
ProPoint3d point_on_x;
ProPoint3d point_on_y;
double dist_x;
double dist_y;
ProPoint3d closest_point;
double dist_to_start;
double dist_to_end;
ProPoint3d mid;
ProPoint3d mirror;
int moved_point = 0;

calculate_projection_x(start, end, third, point_on_x);
calculate_projection_y(start, end, third, point_on_y);

// 计算距离
dist_x = distance(third, point_on_x);
dist_y = distance(third, point_on_y);

if (dist_x < dist_y)
{
closest_point[0] = point_on_x[0];
closest_point[1] = point_on_x[1];
closest_point[2] = 0.0; // 保留 z 坐标
}
else
{
closest_point[0] = point_on_y[0];
closest_point[1] = point_on_y[1];
closest_point[2] = 0.0; // 保留 z 坐标
}

// 判断更短的点距离起点还是终点更近
dist_to_start = distance(closest_point, start);
dist_to_end = distance(closest_point, end);
midpoint(start, end, mid);

if (dist_to_start < dist_to_end)
{
moved_point = 1;
start[0] = closest_point[0];
start[1] = closest_point[1];
start[2] = 0.0; // 保留 z 坐标
if (bidirectional)
{
mirror_point(closest_point, mid, mirror);
end[0] = mirror[0];
end[1] = mirror[1];
end[2] = 0.0; // 保留 z 坐标
}
}
else
{
moved_point = 2;
end[0] = closest_point[0];
end[1] = closest_point[1];
end[2] = 0.0; // 保留 z 坐标
if (bidirectional)
{
mirror_point(closest_point, mid, mirror);
start[0] = mirror[0];
start[1] = mirror[1];
start[2] = 0.0; // 保留 z 坐标
}
}
}

代码公开,需要的人可以随便根据自己的环境修改编译。完整代码可在Github.com下载。代码在VS2010,Creo 2.0 M060 X64下编译通过。