自己写的:等高线(二维多线段polyline2d)转换为多线段(polyline)类 - 精华帖集合
www.dimcax.com
自己写的:等高线(二维多线段polyline2d)转换为多线段(polyline)类
我是搞测量的,经常处理地形图,经常是太大了,打开慢,最重要的原因就是地形图中等高线多为拟合过的二维多线段(polyline2d),占用大量内存。
经过多次实验,把“二维多线段(polyline2d)”转换为“多线段(polyline)”cad图形内存会小很多,因此写了这个类来进行批量转换,自己用过了,好像没有问题。
小弟也是刚刚学习cad开发,望大虾赐教,向你们学习,也帮我看看有没有问题。!
下面是代码:
using system;
using autodesk.autocad.applicationservices;
using autodesk.autocad.editorinput;
using autodesk.autocad.databaseservices;
using autodesk.autocad.geometry;
namespace classlibrary
{
class contourline
{
private database db;
private transaction trans;
private blocktable bt;
private blocktablerecord btr;
private objectid[] ids;//所以等高线id集合
public contourline()
{
db = hostapplicationservices.workingdatabase;
}
public void initialize()
{
trans = db.transactionmanager.starttransaction();
bt = (blocktable)trans.getobject(db.blocktableid, openmode.forread);
btr = (blocktablerecord)trans.getobject(bt[blocktablerecord.modelspace], openmode.forwrite);
}
public void disposeall()
{
bt.dispose();
btr.dispose();
trans.dispose();
}
/// <summary>
/// 得到polyline2d等高线id集合
/// </summary>
public void getpolyline2dids()
{
editor ed = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.editor;
typedvalue[] firstvalue = new typedvalue[]{
new typedvalue(0,"polyline"),
new typedvalue((int)dxfcode.layername,"dgx")
};
selectionfilter filter = new selectionfilter(firstvalue);
promptselectionresult resselectopt = ed.selectall(filter);
if (resselectopt.status != promptstatus.ok)
{
ids = null;
return;
}
selectionset ss = resselectopt.value;
ids = ss.getobjectids();
}
/// <summary>
/// 二维多线段转换
/// </summary>
public void convertpolyline2dtopolyline()
{
editor ed = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.editor;
getpolyline2dids();
if (ids == null)
{
ed.writemessage("\n没有需要转换的拟和多线段\n");
return;
}
initialize();
long polyline2dcount = 0;
foreach (objectid id in ids)
{
polyline2d pl2d = trans.getobject(id, openmode.forwrite) as polyline2d;
if (pl2d == null)
continue;
polyline2dcount++;
polyline pl = new polyline();
//poly2dtype.simplepoly 拟和=无
//poly2dtype.fitcurvepoly 曲线拟和
//poly2dtype.quadsplinepoly 二次拟和
//poly2dtype.cubicsplinepoly 三次拟和
switch (pl2d.polytype)
{
case poly2dtype.simplepoly:
case poly2dtype.fitcurvepoly:
pl.convertfrom(pl2d, false);
break;
case poly2dtype.quadsplinepoly:
case poly2dtype.cubicsplinepoly:
int index = 0;
foreach (objectid vid in pl2d)
{
vertex2d v2d = trans.getobject(vid, openmode.forread) as vertex2d;
if (v2d.vertextype != vertex2dtype.splinecontrolvertex)
pl.addvertexat(index++, new point2d(v2d.position.x, v2d.position.y), 0,0,0);
}
break;
}
pl.layer = pl2d.layer;
pl.color = pl2d.color;
pl.linetype = pl2d.linetype;
pl.constantwidth = pl2d.defaultstartwidth;
pl.closed = pl2d.closed;
pl.elevation = pl2d.elevation;
pl2d.erase(true); //删除原拟和二维多线段
btr.appendentity(pl);
trans.addnewlycreateddbobject(pl, true);
}
trans.commit();
disposeall();
ed.writemessage("\n处理了" + polyline2dcount.tostring() + "条拟和多线段\n");
}
}
}
复制代码
[ ]