线段求和源代码(支持线段、多段线、圆、椭圆、样条曲线)
public void my1()
{
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;
PromptSelectionOpti** pso = new PromptSelectionOpti**();
PromptSelectionResult psr = ed.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
Selecti**et ss = psr.Value;
int lcount = 0;
int n = ss.Count;
double result = 0;
for (int i = 0; i < n; i++)
{
Entity ent = acTrans.GetObject(ss.ObjectId, OpenMode.ForWrite) as Entity;
if (ent is Line)
{
Line l = ent as Line;
result += l.Length;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + l.Length);
}
if (ent is Circle)
{
Circle c = ent as Circle;
result += c.Circumference;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + c.Circumference);
}
if (ent is Ellipse)
{
Ellipse e = ent as Ellipse;
double zc = Math.PI * (e.MajorRadius + e.MinorRadius);
result += zc;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + zc);
}
if (ent is Arc)
{
Arc arc = ent as Arc;
result += arc.Length;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + arc.Length);
}
if (ent is Spline)
{
Spline s = ent as Spline;
Curve c = s.ToPolyline();
Polyline pl = c as Polyline;
result += pl.Length;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + pl.Length);
}
if (ent is Polyline)
{
Polyline pl = ent as Polyline;
result += pl.Length;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + pl.Length);
}
// else
//{
// lcount++;
// ed.WriteMessage("\n实体" + lcount + "未计入总长!");
// }
}
ed.WriteMessage("\n线段的总长度为:{0}", result);
}
else ed.WriteMessage("\n未选中线段!"); ;
acTrans.Commit();
}
}
catch (System.Exception)
{
Application.ShowAlertDialog("error");
}
}
本帖最后由 sieben 于 2011-6-19 08:05 编辑
哦,不好意思!我的失误.
if (ent is Curve)
{
Curve cur = ent as Curve;
result += cur.GetDistanceAtParameter(cur.EndParam);
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + cur.GetDistanceAtParameter(cur.EndParam));
} if (ent is Curve)
{
Curve cur = ent as Curve;
result += cur.Length;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + cur.Length);
} 很不错的例子... 回复 sieben 的帖子
Curve类没有length属性啊? 学习了~~~不错~~~ 回复 sieben 的帖子
嘿嘿,谢谢指点哦....... if (ent is Ellipse)
{
Ellipse e = ent as Ellipse;
double zc = Math.PI * (e.MajorRadius + e.MinorRadius);
result += zc;
lcount++;
ed.WriteMessage("\n实体" + lcount + "的长度为:" + zc);
}
椭圆的弧长不是这样算的吧!!!!大哥 Using AcTrans As Transaction = AcCurDb.TransactionManager.StartTransaction()
Dim EntPro As PromptEntityOptions = New PromptEntityOptions("请选择第一条圆弧")
Dim EntRec As PromptEntityResult = AcDocEd.GetEntity(EntPro)
If EntRec.Status <> PromptStatus.OK Then Exit Sub
Dim OBJ As Object = AcTrans.GetObject(EntRec.ObjectId, OpenMode.ForRead)
Dim a As Ellipse = CType(OBJ, Ellipse)
b = a.Spline
MsgBox(a.GetDistAtPoint(a.EndPoint))
End Using
这样才是对的! 如果是VB的。我坚决相信把A的类型改成Object.就可以直接通用到所有的有长度的对象了!
页:
[1]