求教过滤选择出错,检查了一上午
按照颜色过滤总是错误,以下是代码VS2008+CAD2008private List<Point3d> getinpoint(Line line)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Entity BlockEntity = null;
List<Point3d> retpoint = new List<Point3d>();
TypedValue[] filList = new TypedValue;
filList.SetValue(new TypedValue((int)DxfCode.Color, "8"),0);
SelectionFilter filter = new SelectionFilter(filList);
PromptSelectionResult ents = ed.SelectAll(filter);//每次都是到这一步报错,观察结果为ents的值为{error,null}
points.Clear();
if(ents.Value==null)
return retpoint;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (ObjectId BlockId in ents.Value.GetObjectIds())
{
//获取选择集里对象
BlockEntity = trans.GetObject(BlockId, OpenMode.ForRead) as Entity;
if (BlockEntity.GetType() == typeof(Line) || BlockEntity.GetType() == typeof(Polyline) || BlockEntity.GetType() == typeof(Spline))
{
line.IntersectWith(BlockEntity, Intersect.OnBothOperands, pts, 0, 0);
for (int k = 0; k < pts.Count; k++)
points.Add(pts);
}
}
trans.Commit();
}
return retpoint;
}
测试图中有5条颜色为8的线,可总是报错。
TypedValue[] filList ={ new TypedValue(0, "LINE"), new TypedValue(62, 8) };
SelectionFilter filter = new SelectionFilter(filList); 本帖最后由 xkaeli 于 2012-6-20 23:42 编辑
还是不行 网上太烂不能点回复,刚刚试了一下,还是不可以。我的这个功能是通过jig实现:画一条线,并在和颜色为8的对象的交点上各画一条水平线。一下是全部代码,还望高手再帮忙看一下。
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using DNA;
namespace ToCAD
{
public class DC_tongji_Draw_Jig: DrawJig
{
static Point3d startPoint, endPoint;
static List<Line> lines = new List<Line>();
static List<Point3d> points = new List<Point3d>();
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
foreach (Line line in lines)
{
draw.Geometry.Draw(line);
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions optJig = new JigPromptPointOptions("\n请指定第二点");
optJig.UseBasePoint = true;
optJig.BasePoint = startPoint;
optJig.UserInputControls = UserInputControls.NullResponseAccepted;
PromptPointResult resJigDis = prompts.AcquirePoint(optJig);
Point3d tempPt = resJigDis.Value;
if (resJigDis.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
if (endPoint != tempPt)
{
endPoint = tempPt;
//画水平线
lines.StartPoint = startPoint;
lines.EndPoint = endPoint;
points = getinpoint(lines);
for (int i = 1; i < points.Count; i++)
lines = new Line(startPoint, startPoint);
for (int i = 1; i < points.Count; i++)
{
Point3d pt1 = new Point3d(points.X - 2, points.Y, 0);
Point3d pt2 = new Point3d(points.X + 2, points.Y, 0);
lines.StartPoint = pt1;
lines.EndPoint = pt2;
}
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
private List<Point3d> getinpoint(Line line)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Entity BlockEntity=null;
Point3dCollection pts = new Point3dCollection();
List<Point3d> retpoint = new List<Point3d>();
TypedValue filterValue1 = new TypedValue((int)DxfCode.Color, "8");
TypedValue[] filterValues1 = {filterValue1};
SelectionFilter filter2 = new SelectionFilter(filterValues1);
PromptSelectionResult psr1 = ed.SelectAll(filter2);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
points.Clear();
foreach (ObjectId BlockId in psr1.Value.GetObjectIds())
{
//获取选择集里块里面的对象
BlockEntity = trans.GetObject(BlockId, OpenMode.ForRead) as Entity;
if (BlockEntity.GetType() == typeof(Line) || BlockEntity.GetType() == typeof(Polyline) || BlockEntity.GetType() == typeof(Spline))
{
line.IntersectWith(BlockEntity, Intersect.OnBothOperands, pts, 0, 0);
for (int k = 0; k < pts.Count; k++)
points.Add(pts);
}
}
trans.Commit();
}
return retpoint;
}
public static bool DrawJig()
{
DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument();//文档加锁
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
DC_tongji_Draw_Jig begin = new DC_tongji_Draw_Jig();
Database db = Application.DocumentManager.MdiActiveDocument.Database;
PromptPointResult pRes = ed.GetPoint("请选择第一点:");
startPoint = pRes.Value;
lines.Clear();
lines.Add(new Line(startPoint, startPoint));
points.Clear();
if (pRes.Status == PromptStatus.OK)
{
PromptResult resJig = ed.Drag(begin);
if (resJig.Status == PromptStatus.OK)
{
Tools.AddEntities(lines.ToArray());//Tools.AddEntities为DNA里面的函数(将对象添加到CAD文档中)
}
}
dl.Dispose();
return true;
}
}
} sieben 发表于 2012-6-20 15:45 static/image/common/back.gif
TypedValue[] filList ={ new TypedValue(0, "LINE"), new TypedValue(62, 8) };
Selecti ...
继续求教高手,更改以后还是不能搞定,今天下午网上太慢,不能回复,特在晚上抓紧请假,我要实现的具体内容见4楼,希望高手答疑解惑。 1,"更改以后还是不能搞定",你确认是因为过滤器问题?还是可能别的问题?
2,你的东西有点复杂,我帮不了你,只是建议最好不要在Jig的Sampler函数里面访问数据库 颜色似乎是过滤不到的 好像是有问题,在DrawJig里取不到选集,换成EntityJig吧。 如果图层的颜色是8但是图元随层 那么你这样是选不到的
先遍历图层,找出所有的颜色是8的图层
你的过滤条件应该是 选择
颜色8 or (图层颜色是8 and 颜色随层)
雪山飞狐_lzh 发表于 2012-6-21 23:46 static/image/common/back.gif
如果图层的颜色是8但是图元随层 那么你这样是选不到的
先遍历图层,找出所有的颜色是8的图层
留着有用!
页:
[1]