几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量  


返回   几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 » 仿射空间:CAX软件开发(三)二次开发与程序设计 » CAD二次开发 » AutoCAD二次开发 » ObjectARX(VB.NET/C#)
用户名
密码
注册 帮助 会员 日历 银行 搜索 今日新帖 标记论坛为已读


 
 
主题工具 搜索本主题 显示模式
旧 2009-04-20, 04:31 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】[求助]如何用.net获取单行文本的精确边界框

[求助]如何用.net获取单行文本的精确边界框
www.dimcax.com
[求助]如何用.net获取单行文本的精确边界框
不是那个geometricextents,主要是对于文本斜放时的边框求解,给个思路也行。 用文字高度和转角自己 计算么??
弄得有点复杂,试试看吧: //取得文本(dbtext、mtext)的边界矩形框 polyline m_gettextincludebox(entity m_textent) { polyline m_pl = new polyline(); if (m_textent is dbtext) { #region dbtext dbtext m_dbtextent = m_textent as dbtext; point3dcollection m_bbox = m_getboundingboxnet(m_dbtextent);//文字范围框 point3d m_centertpt = m_bbox[0].add(m_bbox[1].getvectorto(m_bbox[0]) * 0.5);//文字中心点 dbtext m_newtextent = (dbtext)m_dbtextent.clone();//复制新文字 m_newtextent.rotation = 0;//不旋转(水平) point3dcollection m_newbbox = m_getboundingboxnet(m_newtextent);//文字范围框 point3d m_newcenterpt = m_newbbox[0].add(m_newbbox[1].getvectorto(m_newbbox[0]) * 0.5);//文字中心点 m_bbox.clear(); m_bbox = new point3dcollection(new point3d[]{ m_newbbox[0], new point3d(m_newbbox[0].x,m_newbbox[1].y,m_newbbox[0].z), m_newbbox[1], new point3d(m_newbbox[1].x,m_newbbox[0].y,m_newbbox[0].z) }); m_pl = m_createlwpolyline(m_bbox, true);//新文字范围框 m_pl.transformby(matrix3d.displacement(m_centertpt.getvectorto(m_newcenterpt)));//移动 m_pl.transformby(matrix3d.rotation(m_dbtextent.rotation, new vector3d(0, 0, 1), m_centertpt));//旋转 #endregion } else { #region mtext mtext m_mtextent = (mtext)m_textent.clone() as mtext; m_mtextent.width = m_mtextent.actualwidth; point3dcollection m_bpts = m_mtextent.getboundingpoints(); double m_height = m_bpts[0].distanceto(m_bpts[2]); vector3d m_vect1 = m_mtextent.direction; vector3d m_vect2 = m_vect1.rotateby(math.pi * 0.5, new vector3d(0, 0, 1)); point3d m_pt = new point3d(); switch (m_mtextent.attachment) { case attachmentpoint.middlealign: case attachmentpoint.middlecenter: case attachmentpoint.middlefit: case attachmentpoint.middleleft: case attachmentpoint.middlemid: case attachmentpoint.middleright: m_pt = m_bpts[2].add(m_vect2 * (m_height - m_mtextent.actualheight) * 0.5); break; case attachmentpoint.topalign: case attachmentpoint.topcenter: case attachmentpoint.topfit: case attachmentpoint.topleft: case attachmentpoint.topmid: case attachmentpoint.topright: m_pt = m_bpts[2].add(m_vect2 * (m_height - m_mtextent.actualheight)); break; default: m_pt = m_bpts[2]; break; } point3dcollection m_pts = new point3dcollection(); m_pts.add(m_pt); m_pt = m_pt.add(m_vect2 * m_mtextent.actualheight); m_pts.add(m_pt); m_pt = m_pt.add(m_vect1 * m_mtextent.actualwidth); m_pts.add(m_pt); m_pt = m_pt.add(m_vect2.negate() * m_mtextent.actualheight); m_pts.add(m_pt); m_pl = m_createlwpolyline(m_pts, true); #endregion } return m_pl; } 需要的相关过程: //取得实体的包围矩形框 point3dcollection m_getboundingboxnet(entity m_ent) { point3dcollection m_pts = new point3dcollection(new point3d[] { m_ent.geomextents.minpoint, m_ent.geomextents.maxpoint }); if (m_ent is dbtext || m_ent is mtext || m_ent is polyline3d) { point3dcollection m_interpts = new point3dcollection(); polyline m_pl = m_createrectangle(new point3d(0, 0, 0), new point3d(m_pts[1].x, m_pts[1].y, 0)); m_ent.boundingboxintersectwith(m_pl, intersect.onbothoperands, new plane(), m_interpts, 0, 0); m_pl.dispose(); if (m_ent is polyline3d) { double min_x, min_y, min_z, max_x, max_y, max_z; m_pointcollectionsort(ref m_interpts, 0, true); min_x = m_interpts[0].x; max_x = m_interpts[m_interpts.count - 1].x; m_pointcollectionsort(ref m_interpts, 1, true); min_y = m_interpts[0].y; max_y = m_interpts[m_interpts.count - 1].y; m_pointcollectionsort(ref m_interpts, 2, true); min_z = m_interpts[0].z; max_z = m_interpts[m_interpts.count - 1].z; m_pts = new point3dcollection(new point3d[] { new point3d(min_x, min_y, min_z),new point3d(max_x,max_y,max_z)}); } else m_pts = new point3dcollection(new point3d[] {new point3d(m_interpts[1].x,m_interpts[0].y,m_interpts[0].z),m_pts[1] }); } return m_pts; } //绘制矩形 polyline m_createrectangle(point2d m_pt1, point2d m_pt2) { //求出左上角点和右下角点 point2d m_pt3,m_pt4; if (m_pt1.x > m_pt2.x) { m_pt3 = m_pt1; m_pt1 = new point2d(m_pt2.x, m_pt2.y); m_pt2 = new point2d(m_pt3.x, m_pt3.y); } if (m_pt1.y < m_pt2.y) { m_pt3 = m_pt1; m_pt4 = m_pt2; m_pt1 = new point2d(m_pt3.x, m_pt4.y); m_pt2 = new point2d(m_pt4.x, m_pt3.y); } polyline m_lwpline = new polyline(); m_lwpline.addvertexat(0, m_pt1, 0, 0, 0); m_lwpline.addvertexat(1, new point2d(m_pt2.x, m_pt1.y), 0, 0, 0); m_lwpline.addvertexat(2, m_pt2, 0, 0, 0); m_lwpline.addvertexat(3, new point2d(m_pt1.x, m_pt2.y), 0, 0, 0); m_lwpline.closed = true; return m_lwpline; } //绘制轻量多段线 polyline m_createlwpolyline(point3dcollection m_ptarr, bool m_bclose) { point2dcollection m_ptarr = new point2dcollection(); foreach (point3d m_pt in m_ptarr) m_ptarr.add(new point2d(m_pt.x, m_pt.y)); if (m_ptarr.count == 0) { m_ptarr.add(new point3d(0, 0, 0)); m_ptarr.add(new point3d(0, 0, 0)); } else if (m_ptarr.count < 2) m_ptarr.add(m_ptarr[0]); polyline m_lwpline = new polyline(); for (int i = 0; i < m_ptarr.count; i++) { m_lwpline.addvertexat(i, m_ptarr, 0.0, 0, 0); } m_lwpline.closed = m_bclose; return m_lwpline; } [
多谢楼上提供的思路,参考楼上的代码已经实现。
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
 


主题工具 搜索本主题
搜索本主题:

高级搜索
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

vB 代码开启
[IMG]代码开启
HTML代码关闭



所有的时间均为北京时间。 现在的时间是 12:51 AM.


于2004年创办,几何尺寸与公差论坛"致力于产品几何量公差标准GD&T | GPS研究/CAD设计/CAM加工/CMM测量"。免责声明:论坛严禁发布色情反动言论及有关违反国家法律法规内容!情节严重者提供其IP,并配合相关部门进行严厉查处,若內容有涉及侵权,请立即联系我们QQ:44671734。注:此论坛须管理员验证方可发帖。
沪ICP备06057009号-2
更多