// thanks Tony Tanzillo !!!
// the total caode as follow
[CommandMethod("SSG")]
public void GeometryTrans()
{
//**************begin here
Line line1 = new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0));
adb.DrawLine(line1); //Transaction commit entity to WorkingDatabase
using (Transaction ctrans = mmc.db.TransactionManager.StartTransaction())
{
Line line2 = (Line)ctrans.GetObject(line1.ObjectId, OpenMode.ForWrite);
//***************
BreakCurve(line2, new Point3d(20, 0, 0), new Point3d(50, 0, 0));
//****************
ctrans.Commit();
}
}
public bool BreakCurve(Curve curve, Point3d p1, Point3d p2)
{
try
{
Point3d p11 = curve.GetClosestPointTo(p1, false);
double param1 = curve.GetParameterAtPoint(p11);
Point3d p21 = curve.GetClosestPointTo(p2, false);
double param2 = curve.GetParameterAtPoint(p21);
DoubleCollection dColl = new DoubleCollection(2);
if (param1 < param2)
{
dColl.Add(param1);
dColl.Add(param2);
}
else
{
dColl.Add(param2);
dColl.Add(param1);
}
DBObjectCollection objColl = curve.GetSplitCurves(dColl);
Entity ent;
if (objColl.Count == 2)
{
ent = (Entity)objColl[1];
adb.DrawEntity(ent);
}
else if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
adb.DrawEntity(ent);
//*****************************
//ent = (Entity)objColl[1]; // if not note this line code ,all will be OK,
//adb.DrawEntity(ent);
ent = (Entity)objColl[2]; //here catch System.Exception ex
//ex.Message = Insertion index was out of range. Must be non-negative and
//less than or equal to size.Parameter name: index
//***********************
adb.DrawEntity(ent);
}
curve.Erase();
return true;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
catch (System.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
}