public class ZhiPai
{
[CommandMethod("SHL")]
public void ShortenLines()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
SelectionSet ss =
SelectLines(ed, "\nSelect lines to shorten: ");
if (ss != null)
{
Transaction tr =
db.TransactionManager.StartOpenCloseTransaction();
using (tr)
{
foreach (SelectedObject so in ss)
{
Curve cur =
(Curve)tr.GetObject(
so.ObjectId,
OpenMode.ForWrite
);
double sp = cur.StartParam,
ep = cur.EndParam,
delta = (ep - sp) * 0.25;
DoubleCollection dc = new DoubleCollection();
dc.Add(sp + delta);
dc.Add(ep - delta);
DBObjectCollection objs = cur.GetSplitCurves(dc);
if (objs.Count == 3)
{
Entity ent = (Entity)objs[1];
cur.HandOverTo(ent, true, true);
tr.AddNewlyCreatedDBObject(ent, true);
cur.Dispose();
objs[0].Dispose();
objs[2].Dispose();
}
}
tr.Commit();
}
}
}
private SelectionSet SelectLines(Editor ed, string msg)
{
PromptSelectionOptions pso =
new PromptSelectionOptions();
pso.MessageForAdding =
(string.IsNullOrEmpty(msg) ? "\nSelect lines: " : msg);
TypedValue[] tvs =
new TypedValue[1] {
new TypedValue(
(int)DxfCode.Start,
"LINE"
)
};
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.GetSelection(pso, sf);
if (psr.Status != PromptStatus.OK || psr.Value.Count <= 0)
return null;
return psr.Value;
}
}
在cur.HandOverTo(ent, true, true);显示ZwSoft.ZwCAD.Runtime.Exception: eInvalidContext异常

















