自己写的一个首尾相连线段或多段线并成一条多段线的程序
经测试没有大问题,只是画完多段线后原有线段不能完全删除,希望高手给予修改意见,如有BUG请回复!public class mycommand
{
//本代码由<家在湾里>书写,仅供学习用,引用请注明出处.............
//联系本人请Q:584457142
//将首尾相连的线段或多段线连成多段线
public void my()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl,
OpenMode.ForWrite) as BlockTableRecord;
PromptSelectionOptions pso=new PromptSelectionOptions();
PromptSelectionResult psr = ed.GetSelection(pso);
Polyline pl = new Polyline();
if (psr.Status == PromptStatus.OK)
{
SelectionSet ss = psr.Value;
int n = ss.Count;
ObjectIdCollection oc = new ObjectIdCollection() ;
for (int i = 0; i < n; i++)
{
oc.Add(ss.ObjectId);
}
Entity ent2 = acTrans.GetObject(oc, OpenMode.ForWrite) as Entity;
for (int j = 0; j <n; j++)
{
for (int i = 1; i < oc.Count; i++)
{
Entity ent3 = acTrans.GetObject(oc, OpenMode.ForWrite) as Entity;
pl = mycommand.EntityUnion(ent2, ent3);
if (pl == null || pl.NumberOfVertices == 0) continue;
else
{
oc.RemoveAt(i);
ent2 = pl as Entity;
ent3.Erase();
break;
}
}
}
//ent2.Erase();
}
acBlkTblRec.AppendEntity(pl);
acTrans.AddNewlyCreatedDBObject(pl,true);
acTrans.Commit();
}
}
catch (System.Exception)
{
Application.ShowAlertDialog("error");
}
}
public static Polyline EntityUnion(Entity ent1, Entity ent2)
{
Polyline pl1 = new Polyline();
Polyline pl2 = new Polyline();
Polyline pl = new Polyline();
Point3d equalpoint = new Point3d();
Point3dCollection pc = new Point3dCollection();
Point3dCollection final = new Point3dCollection();
#region
if (ent1 is Polyline && ent2 is Polyline)
{
pl1 = ent1 as Polyline;
pl2 = ent2 as Polyline;
pc.Add(pl1.StartPoint);
pc.Add(pl1.EndPoint);
pc.Add(pl2.StartPoint);
pc.Add(pl2.EndPoint);
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (pc == pc)
{
equalpoint = pc;
break;
}
}
if (equalpoint != new Point3d(0, 0, 0)) break;
}
if (equalpoint == new Point3d(0, 0, 0)) return null;
if (pl1.EndPoint.Equals(equalpoint))
{
for (int i = 0; i < pl1.NumberOfVertices - 1; i++)
{
final.Add(pl1.GetPoint3dAt(i));
}
if (pl2.StartPoint.Equals(equalpoint))
{
for (int i = 0; i < pl2.NumberOfVertices; i++)
{
final.Add(pl2.GetPoint3dAt(i));
}
}
else
{
for (int i = pl2.NumberOfVertices - 1; i > -1; i--)
{
final.Add(pl2.GetPoint3dAt(i));
}
}
}
else if (pl1.StartPoint.Equals(equalpoint))
{
if (pl2.EndPoint.Equals(equalpoint))
{
for (int i = 0; i < pl2.NumberOfVertices; i++)
{
final.Add(pl2.GetPoint3dAt(i));
}
for (int i = 1; i < pl1.NumberOfVertices; i++)
{
final.Add(pl1.GetPoint3dAt(i));
}
}
else
{
for (int i = pl2.NumberOfVertices - 1; i >= 0; i--)
{
final.Add(pl2.GetPoint3dAt(i));
}
for (int i = 1; i < pl1.NumberOfVertices; i++)
{
final.Add(pl1.GetPoint3dAt(i));
}
}
}
for (int i = 0; i < final.Count; i++)
{
pl.AddVertexAt(i, new Point2d(final.X, final.Y), 0, 0, 0);
}
return pl;
}
#endregion
#region
if (ent1 is Line && ent2 is Line)
{
Line l1 = ent1 as Line;
Line l2 = ent2 as Line;
pc.Add(l1.StartPoint);
pc.Add(l1.EndPoint);
pc.Add(l2.StartPoint);
pc.Add(l2.EndPoint);
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (pc == pc)
{
equalpoint = pc;
pc.Remove(pc);
pc.Remove(pc);
break;
}
}
if (equalpoint != new Point3d(0, 0, 0)) break;
}
if (equalpoint == new Point3d(0, 0, 0)) return null;
final.Add(pc);
Point3d p1 = pc;
Point3d p2 = pc;
final.Add(equalpoint);
final.Add(pc);
for (int i = 0; i < final.Count; i++)
{
pl.AddVertexAt(i, new Point2d(final.X, final.Y), 0, 0, 0);
}
return pl;
}
#endregion
#region
if (ent1 is Line && ent2 is Polyline)
{
Line l = ent1 as Line;
pl1 = ent2 as Polyline;
pc.Add(l.StartPoint);
pc.Add(l.EndPoint);
pc.Add(pl1.StartPoint);
pc.Add(pl1.EndPoint);
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (pc == pc)
{
equalpoint = pc;
break;
}
}
if (equalpoint != new Point3d(0, 0, 0)) break;
}
if (equalpoint == new Point3d(0, 0, 0)) return null;
if (equalpoint.Equals(pl1.EndPoint))
{
for (int i = 0; i < pl1.NumberOfVertices; i++)
{
final.Add(pl1.GetPoint3dAt(i));
}
if (equalpoint.Equals(l.StartPoint))
final.Add(l.EndPoint);
else final.Add(l.StartPoint);
}
else if (equalpoint.Equals(pl1.StartPoint))
{
for (int i = pl1.NumberOfVertices - 1; i >= 0; i--)
{
final.Add(pl1.GetPoint3dAt(i));
}
if (equalpoint.Equals(l.StartPoint))
final.Add(l.EndPoint);
else final.Add(l.StartPoint);
}
for (int i = 0; i < final.Count; i++)
{
pl.AddVertexAt(i, new Point2d(final.X, final.Y), 0, 0, 0);
}
return pl;
}
#endregion
#region
if (ent1 is Polyline && ent2 is Line)
{
Line l = ent2 as Line;
pl1 = ent1 as Polyline;
pc.Add(l.StartPoint);
pc.Add(l.EndPoint);
pc.Add(pl1.StartPoint);
pc.Add(pl1.EndPoint);
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (pc == pc)
{
equalpoint = pc;
break;
}
}
if (equalpoint != new Point3d(0, 0, 0)) break;
}
if (equalpoint == new Point3d(0, 0, 0)) return null;
if (equalpoint.Equals(pl1.EndPoint))
{
for (int i = 0; i < pl1.NumberOfVertices; i++)
{
final.Add(pl1.GetPoint3dAt(i));
}
if (equalpoint.Equals(l.StartPoint))
final.Add(l.EndPoint);
else final.Add(l.StartPoint);
}
else if (equalpoint.Equals(pl1.StartPoint))
{
for (int i = pl1.NumberOfVertices - 1; i >= 0; i--)
{
final.Add(pl1.GetPoint3dAt(i));
}
if (equalpoint.Equals(l.StartPoint))
final.Add(l.EndPoint);
else final.Add(l.StartPoint);
}
for (int i = 0; i < final.Count; i++)
{
pl.AddVertexAt(i, new Point2d(final.X, final.Y), 0, 0, 0);
}
return pl;
}
#endregion
else return null;
}
应该有这种方法了吧。 回复 yxr_MJTD 的帖子
别人肯定是写过了,你是API里面有这个方法吗?不妨写出方法名来一看 多谢分享,来看看 谢谢楼主分享,很不错的程度, 多谢楼主,正愁着如何实现呢。。
页:
[1]