- 积分
- 577
- 明经币
- 个
- 注册时间
- 2012-3-15
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2012-12-7 22:19:30
|
显示全部楼层
看看我的
- [CommandMethod("middleline")]
- public void MiddleLine()
- {
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
-
- PromptSelectionOptions selOpts = new PromptSelectionOptions();
- selOpts.MessageForAdding = "选择平行直线段线段";
- SelectionFilter sf = new SelectionFilter(new TypedValue[] {new TypedValue(0,"Line")});
- PromptSelectionResult selRes = ed.GetSelection(selOpts,sf);
- if (selRes.Status != PromptStatus.OK)
- {
- // throw new System.Exception("错误或用户取消");
- return;
- }
- //需要处理的直线段集合
- List<ObjectId> lineObjectIds = new List<ObjectId>(selRes.Value.GetObjectIds());
- using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
- {
- Database db = HostApplicationServices.WorkingDatabase;
- BlockTable bt = trans.GetObject(db.BlockTableId,OpenMode.ForRead) as BlockTable;
- BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- //选取选集第一条线段作为起始线段
- ObjectId fLineId = lineObjectIds[0];
- ObjectId sLineId = lineObjectIds[1];
- Line firstLine = trans.GetObject(fLineId, OpenMode.ForRead) as Line;
- Line secondLine = trans.GetObject(sLineId, OpenMode.ForRead) as Line;
- Line middleLine;
- //判断两条线段的方向做出不同处理
- if (firstLine.Angle == secondLine.Angle)
- {
- middleLine = new Line(MiddlePoint(firstLine.StartPoint,secondLine.StartPoint), MiddlePoint(firstLine.EndPoint,secondLine.EndPoint));
- ed.WriteMessage(firstLine.Angle+"\r\n"+secondLine.Angle);
- }else if ((firstLine.Angle < secondLine.Angle) ? (firstLine.Angle + 180 == secondLine.Angle) : (firstLine.Angle == 180 + secondLine.Angle))
- {
- middleLine = new Line(MiddlePoint(firstLine.StartPoint,secondLine.EndPoint), MiddlePoint(firstLine.EndPoint,secondLine.StartPoint));
- ed.WriteMessage(firstLine.Angle+"\r\n"+secondLine.Angle);
- }else
- {
- MessageBox.Show("所选择线段为非平行线段,请重新选择平行线段!");
- return;
- }
- middleLine.ColorIndex = 210;
- btr.AppendEntity(middleLine);
- trans.AddNewlyCreatedDBObject(middleLine,true);
- trans.Commit();
- }
- }
- public Point3d MiddlePoint(Point3d p1, Point3d p2)
- {
- return new Point3d((p1.X + p2.X)/2.0,(p1.Y + p2.Y)/2.0,(p1.Z + p2.Z)/2.0);
- }
|
|