如何实现pe命令,连接多段线
如何实现cad自带的pe命令(连接多段线),字节写的代码如下ids是小短线,可是老是连接不起来,是怎么回事??
//link the little lines
public DBObjectCollection polylink(DBObjectCollection ids)
{
DBObjectCollection polyIds = new DBObjectCollection();
bool IsEmpty = false;
while (!IsEmpty)
{
if (ids.Count < 3)
{
IsEmpty = true;
}
Polyline pl0 = ids as Polyline;
Polyline pl = new Polyline();
Point2d plst = pl0.GetPoint2dAt(0);
Point2d plet = pl0.GetPoint2dAt(1);
pl.AddVertexAt(pl.NumberOfVertices, plst, 0, 0, 0);
pl.AddVertexAt(pl.NumberOfVertices, plet, 0, 0, 0);
ids.RemoveAt(0);
foreach (DBObject polyid in ids)
{
Polyline poly = polyid as Polyline;
if (poly.Length==0)
{
ids.Remove(polyid);
}
if (pl.EndPoint == poly.StartPoint)
{
pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.EndPoint.X, poly.EndPoint.Y), 0, 0, 0);
ids.Remove(polyid);
}
else if (pl.EndPoint == poly.EndPoint)
{
pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.StartPoint.X, poly.StartPoint.Y), 0, 0, 0);
ids.Remove(polyid);
}
}
if (pl.EndParam >= 2)
{
pl.Closed = true;
polyIds.Add(pl);
}
}
return polyIds;
}
看了很多都是用command实现的,net高版本Curve对象提供了 JionLine
方法,低版本没有办法唉
1, foreach (DBObject polyid in ids)
{
Polyline poly = polyid as Polyline;
if (poly.Length==0)
{
ids.Remove(polyid);
}
这里 在 foreach (DBObject polyid in ids) 里面 ids.Remove(polyid); 这是大忌 2, if (pl.EndPoint == poly.StartPoint)
{
这样的判断很不确定 可惜是VB编的,谁有选择第一条线段而连接起多段线的lisp程序吗? public DBObjectCollection polylink(DBObjectCollection ids)
{
DBObjectCollection polyIds = new DBObjectCollection();
bool IsEmpty = false;
if (ids.Count < 1)
{
IsEmpty = true;
return polyIds;
}
while(!IsEmpty)
{
Polyline pl0 = ids as Polyline;
Polyline pl = new Polyline();
Point2d plst = pl0.GetPoint2dAt(0);
Point2d plet = pl0.GetPoint2dAt(1);
pl.AddVertexAt(pl.NumberOfVertices, plst, 0, 0, 0);
pl.AddVertexAt(pl.NumberOfVertices, plet, 0, 0, 0);
ids.RemoveAt(0);
bool close=false;
while (!close)
{
foreach (DBObject polyid in ids)
{
Polyline poly = polyid as Polyline;
if (poly.Length == 0)
{
ids.Remove(polyid);
}
if (pl.EndPoint == poly.StartPoint)
{
pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.EndPoint.X, poly.EndPoint.Y), 0, 0, 0);
ids.Remove(polyid);
}
else if (pl.EndPoint == poly.EndPoint)
{
pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.StartPoint.X, poly.StartPoint.Y), 0, 0, 0);
ids.Remove(polyid);
}
if (pl.EndPoint == pl.StartPoint)
{
close=true;
pl.Closed=true;
}
}
if (ids.Count<1)
{
close = true;
}
}
if (pl.EndParam >= 2)
{
pl.ColorIndex = 2;
polyIds.Add(pl);
}
if (ids.Count<1)
{
IsEmpty = true;
}
}
return polyIds;
}
public void linkTst()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] fillist = new TypedValue[] { new TypedValue((int)DxfCode.Start, "Lwpolyline") };
SelectionFilter filter = new SelectionFilter(fillist);
PromptSelectionResult polyRes = ed.GetSelection(filter);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
if (polyRes.Status == PromptStatus.Cancel)
{
return;
}
else if (polyRes.Status==PromptStatus.OK||polyRes.Status==PromptStatus.None)
{
SelectionSet polyset = polyRes.Value;
DBObjectCollection ids = new DBObjectCollection();
for (int i = 0; i < polyset.Count;i++ )
{
Polyline pl = trans.GetObject(polyset.ObjectId, OpenMode.ForWrite) as Polyline;
ids.Add(pl);
}
DBObjectCollection polyids=polylink(ids);
jinhe.ObjectARX.Tools.AddEntities(polyids);
}
trans.Commit();
}
}
这样测试就是正确滴喽,可能有错误没有测试出来吧!!!
回复 李毛毛 的帖子
为什么,我都是这么用滴??? 回复 sieben 的帖子
因为我看到了,它们的起始点确实是一摸一样,所以没有用误差了。 回复 sieben 的帖子
呃,this is C#,not VB 这样测试就是正确滴喽
---------------------
是 测试结果正确 还是 测试方法正确?
呵呵,好吧! 你说正确就正确吧! 回复 teykmcqh 的帖子
高手!估计是你看穿了C#的外表!看到了VB的本质!
页:
[1]
2