计算鼠标点位置的闭合pl线的面积.
C#开发|大少爷|2017-07-27 15:42
-
回答:
private void axMxDrawX1_MouseEvent(object sender, AxMxDrawXLib._DMxDrawXEvents_MouseEventEvent e) { if (e.lType == 4) { // 是鼠标左键双击 // 得到鼠标位置 MxDrawPoint point = new MxDrawPoint(); point.x = e.dX; point.y = e.dY; // 构造择集,得到鼠标所在闭合区域。 // 设置过滤条件 MxDrawResbuf filter = new MxDrawResbuf(); // 设置PL线为过滤条件 filter.AddStringEx("LWPOLYLINE", 5020); // 选择范围左下角 MxDrawPoint pt1 = new MxDrawPoint(); pt1.x = point.x - 100; pt1.y = point.y - 100; // 选择范围右上角 MxDrawPoint pt2 = new MxDrawPoint(); pt2.x = point.x + 100; pt2.y = point.y + 100; // 选择pt1,pt2所在范围内的对象. MxDrawSelectionSet ss = new MxDrawSelectionSet(); ss.Select(MCAD_McSelect.mcSelectionSetCrossing, pt1, pt2, filter); for (int i = 0; i < ss.Count; i++) { // 遍历所有对象,看当前点是在那个闭合区域内 MxDrawPolyline pl = (MxDrawPolyline)ss.Item(i); // 得到闭合区域的点坐标。 MxDrawPoints pts = new MxDrawPoints(); for (int j = 0; j < pl.NumVerts; j++) { MxDrawPoint pt = pl.GetPointAt(j); pts.Add(pt.x, pt.y, pt.z); } if (pts.Count < 3) continue; // 判断鼠标点,是不是在这个闭合的区域内. MxDrawUtility utility = new MxDrawUtility(); if (utility.PointInPolyline(pts, point.x, point.y)) { // 找到用户点击的闭合区域 double dArea = 0.0; pl.GetArea(out dArea); // 对选择的区域填充 for (int k = 0; k < pts.Count; k++) { MxDrawPoint tmp = pts.Item(k); if(k == 0) axMxDrawX1.PathMoveTo(tmp.x, tmp.y); else axMxDrawX1.PathLineTo(tmp.x, tmp.y); } axMxDrawX1.DrawCADColorIndex = 3; axMxDrawX1.DrawPathToHatch(0.0); axMxDrawX1.UpdateDisplay(); MessageBox.Show("面积为:" + dArea.ToString()); e.lRet = 1; // 取消后面的命令。 axMxDrawX1.SendStringToExecute(""); break; } } } }