【已解决】指定点打断线段
本帖最后由 白糖 于 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.
//
}
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();
}
}
}
}
}
}
1,你倒是说一下可能哪里出错,有什么提示?
2, if (line.GetClosestPointTo(OriginPoint, false).DistanceTo(OriginPoint) == 0) 这一句或许与你的预期不一样,0.000000001 与0.0是不等的
3, oldLine.UpgradeOpen();
oldLine.Erase();
trans.Commit();
oldLine 并非是下方的事务中打开的,你在这里修改应该是有问题的 本帖最后由 白糖 于 2012-9-29 23:00 编辑
sieben 发表于 2012-9-29 22:51 http://bbs.mjtd.com/static/image/common/back.gif
1,你倒是说一下可能哪里出错,有什么提示?
2, if (line.GetClosestPointTo(OriginPoint, false).Distan ...
修改了下代码,确实是求最近距离那里出了问题,求出来的距离非预期,请问怎么解决呢?
页:
[1]