修剪穿过所有的实体
本帖最后由 chpmould 于 2010-12-12 08:27 编辑请教老师:
例如:我用程序画两条线的时候自动修剪所有穿过线...
谢谢狐哥,已通过测试... 本帖最后由 lzh741206 于 2010-12-10 20:10 编辑
public static void Test3()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
var resPt1 = ed.GetPoint("\n选择第一点");
if (resPt1.Status != PromptStatus.OK) return;
var pt1 = resPt1.Value;
var optPt2 = new PromptPointOptions("\n选择第二点");
optPt2.UseBasePoint = true;
optPt2.BasePoint = pt1;
var resPt2 = ed.GetPoint(optPt2);
if (resPt2.Status != PromptStatus.OK) return;
var pt2 = resPt2.Value;
//栏选
var resSel =
ed.SelectFence(
new Point3dCollection { pt1, pt2 },
new ResultList { { 0, "line" } });
if (resSel.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//按两点生成一条直线,并两侧偏移
Line line = new Line(pt1, pt2);
Line line1 = line.GetOffsetCurves(5) as Line;
Line line2 = line.GetOffsetCurves(-5) as Line;
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
//遍历选择集
SelectionSet ss = resSel.Value;
for (int i = 0; i < ss.Count; i++ )
{
FenceSelectedObject selObj = ss as FenceSelectedObject;
//获取被栏选的直线和栏选点
Line iline = tr.GetObject(selObj.ObjectId, OpenMode.ForWrite) as Line;
Point3d pt = selObj.GetIntersectionPoints().PointOnLine;
//获取直线和偏移界线的交点集合
Point3dCollection pts = new Point3dCollection();
iline.IntersectWith(line1, Intersect.ExtendArgument, pts, 0, 0);
iline.IntersectWith(line2, Intersect.ExtendArgument, pts, 0, 0);
if (pts.Count > 0)
{
//按交点在直线上的位置排序,并转换为参数集合
var pars =
pts.Cast<Point3d>()
.Select(pnt => iline.GetParameterAtPoint(iline.GetClosestPointTo(pnt, false)))
.OrderBy(par => par);
//按参数集合打断,并排除掉包含栏选点的直线
var lines =
from Line l in iline.GetSplitCurves(new DoubleCollection(pars.ToArray()))
let ls3d = new LineSegment3d(l.StartPoint, l.EndPoint)
where !ls3d.IsOn(pt, new Tolerance(1e-4, 1e-4))
select l;
AddEntity(tr, btr, lines);
iline.Erase();
}
}
AddEntity(tr, btr, line1);
AddEntity(tr, btr, line2);
tr.Commit();
}
}
public static ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
{
ObjectId id = btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
return id;
}
public static List<ObjectId> AddEntity<T>(Transaction tr, BlockTableRecord btr, IEnumerable<T> ents) where T : Entity
{
List<ObjectId> ids = new List<ObjectId>();
foreach (T ent in ents)
{
ids.Add(AddEntity(tr, btr, ent));
}
return ids;
}} 雪山飞狐_lzh 发表于 2010-12-10 20:06
}
谢谢大佬。打断直线已调试成功。。认真学习一下。。 雪山飞狐_lzh 发表于 2010-12-10 20:06
}
好东西,留名留存了!!!! Editor.SelectFence Method (Point3dCollection)
谢谢老师,这个"Editor.SelectFence Method (Point3dCollection)"怎么用,目前还不理解,能否再详细一点...
本帖最后由 chpmould 于 2010-12-10 20:37 编辑
回复 lzh741206 的帖子
测试的时候调试没有成功,请帮看一下我错在那里...(请看以下图片中的提示)
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=79849&extra=page%3D1 调试中还是这句where !ls3d.IsOn(pt, new Tolerance(1e-4, 1e-4))通不过...我错在那里 cad2008类库的问题,我一般是用2010/11的类库编译,再在2008里加载的
这句改成: //按参数集合打断,并排除掉包含栏选点的直线
var lines =
from Line l in iline.GetSplitCurves(pars)
let ls3d = new LineSegment3d(l.StartPoint, l.EndPoint) as Curve3d
where !ls3d.IsOn(pt, new Tolerance(1e-4, 1e-4))
select l; 谢谢狐哥! 现在已经调试通过了,只是这些代码的意思目前有很多没有理解,后续还要多花点时间慢慢的去理解。。。 雪山飞狐_lzh 发表于 2010-12-10 20:06 static/image/common/back.gif
}
貌似只能对直线进行裁剪 能否讲解下 怎么对其他图形进行裁剪
页:
[1]
2