本帖最后由 lzh741206 于 2010-12-7 21:02 编辑
- static int _divNumber = 2;
- [CommandMethod("mdiv")]
- public static void MyDiv()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- PromptEntityOptions optsEnt = new PromptEntityOptions("\n请选择一个曲线:");
- optsEnt.SetRejectMessage("\n你选择的不是曲线!");
- optsEnt.AddAllowedClass(typeof(Curve), false);
- PromptEntityResult resEnt = ed.GetEntity(optsEnt);
- if (resEnt.Status != PromptStatus.OK) return;
- PromptIntegerOptions optsInt = new PromptIntegerOptions("\n请输入段数:");
- optsInt.DefaultValue = _divNumber;
- PromptIntegerResult resInt = ed.GetInteger(optsInt);
- if (resInt.Status != PromptStatus.OK) return;
- optsInt.Message = "段数应大于1,请重新输入:";
- while (resInt.Value < 2)
- {
- resInt = ed.GetInteger(optsInt);
- if (resInt.Status != PromptStatus.OK) return;
- }
- int n = _divNumber = resInt.Value;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Curve c = tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite) as Curve;
- double len = c.GetDistanceAtParameter(c.EndParam);
- double ilen = len / n;
- DoubleCollection pars = new DoubleCollection();
- if (c.Closed) pars.Add(0);
- for (int i = 1; i < n; i++)
- {
- pars.Add(c.GetParameterAtDistance(ilen * i));
- }
- DBObjectCollection objs = c.GetSplitCurves(pars);
- BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- foreach (Entity ent in objs)
- {
- btr.AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(ent, true);
- }
- c.Erase();
- tr.Commit();
- }
- }
|