- 积分
- 577
- 明经币
- 个
- 注册时间
- 2012-3-15
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 白糖 于 2012-9-30 01:06 编辑
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.Windows.Forms;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- namespace CADtest
- {
- ///
- /// Description of UserControl1.
- ///
- public partial class UserControl1 : UserControl
- {
- public UserControl1()
- {
- //
- // The InitializeComponent() call is required for Windows Forms designer support.
- //
- InitializeComponent();
-
- //
- // TODO: Add constructor code after the InitializeComponent() call.
- //
- }
- [CommandMethod("test")]
- public void Test()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptSelectionOptions selOpts = new PromptSelectionOptions();
- selOpts.MessageForAdding = "\n请选取线段";
- SelectionFilter sf = new SelectionFilter(new TypedValue[]{new TypedValue(0,"Line")});
- PromptSelectionResult selRes = ed.GetSelection(selOpts,sf);
- if (selRes.Status != PromptStatus.OK) return;
- List<ObjectId> ids = new List<ObjectId>(selRes.Value.GetObjectIds());
-
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- List<Line> lstLines = new List<Line>();
- foreach (ObjectId id in ids)
- {
- Entity ent = trans.GetObject(id,OpenMode.ForRead) as Entity;
- if (ent.GetType() == typeof(Line))
- {
- Line line = ent as Line;
- lstLines.Add(line);
- }
- }
- BreakLine(lstLines);
- trans.Commit();
- }
- }
- //从原点打断中线
- public void BreakLine(List<Line> lines)
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
-
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- //原点
- Point3d OriginPoint = new Point3d(0,0,0);
- foreach(Line line in lines)
- {
- MessageBox.Show(line.ObjectId.ToString()+":"+line.GetClosestPointTo(OriginPoint, false).DistanceTo(OriginPoint)+"\n");
- if (line.GetClosestPointTo(OriginPoint,false).DistanceTo(OriginPoint) < Tolerance.Global.EqualPoint)//取得原点所在线段(设置容差)
- {
- List<double> pars = new List<double>();
- pars.Add(line.GetParameterAtPoint(OriginPoint));
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
- DBObjectCollection objs;
- //按原点打断
- objs = line.GetSplitCurves(new DoubleCollection(pars.ToArray()));
- foreach (Line newline in objs)
- {
- btr.AppendEntity(newline);
- trans.AddNewlyCreatedDBObject(newline, true);
- }
- line.UpgradeOpen();
- line.Erase();
- trans.Commit();
- }
- }
- }
- }
- }
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|