明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1923|回复: 9

求教过滤选择出错,检查了一上午

[复制链接]
发表于 2012-6-20 13:14 | 显示全部楼层 |阅读模式
按照颜色过滤总是错误,以下是代码VS2008+CAD2008

  1. private List<Point3d> getinpoint(Line line)
  2.         {
  3.             Document doc = Application.DocumentManager.MdiActiveDocument;
  4.             Editor ed = doc.Editor;
  5.             Database db = doc.Database;
  6.             Entity BlockEntity = null;
  7.             List<Point3d> retpoint = new List<Point3d>();
  8.             TypedValue[] filList = new TypedValue[1];
  9.             filList.SetValue(new TypedValue((int)DxfCode.Color, "8"),0);
  10.             SelectionFilter filter = new SelectionFilter(filList);
  11.             PromptSelectionResult ents = ed.SelectAll(filter);//每次都是到这一步报错,观察结果为ents的值为{error,null}
  12.             points.Clear();
  13.             if(ents.Value==null)
  14.                 return retpoint;
  15.             using (Transaction trans = db.TransactionManager.StartTransaction())
  16.             {
  17.                 foreach (ObjectId BlockId in ents.Value.GetObjectIds())
  18.                 {
  19.                     //获取选择集里对象
  20.                     BlockEntity = trans.GetObject(BlockId, OpenMode.ForRead) as Entity;
  21.                     if (BlockEntity.GetType() == typeof(Line) || BlockEntity.GetType() == typeof(Polyline) || BlockEntity.GetType() == typeof(Spline))
  22.                     {
  23.                         line.IntersectWith(BlockEntity, Intersect.OnBothOperands, pts, 0, 0);
  24.                         for (int k = 0; k < pts.Count; k++)
  25.                             points.Add(pts[k]);
  26.                     }
  27.                 }
  28.                 trans.Commit();
  29.             }
  30.             return retpoint;
  31.         }

测试图中有5条颜色为8的线,可总是报错。
发表于 2012-6-20 15:45 | 显示全部楼层
        TypedValue[] filList ={ new TypedValue(0, "LINE"), new TypedValue(62, 8) };
        SelectionFilter filter = new SelectionFilter(filList);
 楼主| 发表于 2012-6-20 17:20 | 显示全部楼层
本帖最后由 xkaeli 于 2012-6-20 23:42 编辑

还是不行
 楼主| 发表于 2012-6-20 17:21 | 显示全部楼层
网上太烂不能点回复,刚刚试了一下,还是不可以。我的这个功能是通过jig实现:画一条线,并在和颜色为8的对象的交点上各画一条水平线。一下是全部代码,还望高手再帮忙看一下。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9. using DNA;

  10. namespace ToCAD
  11. {
  12.     public class DC_tongji_Draw_Jig: DrawJig
  13.     {
  14.         static Point3d startPoint, endPoint;
  15.         static List<Line> lines = new List<Line>();
  16.         static List<Point3d> points = new List<Point3d>();
  17.         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  18.         {
  19.             foreach (Line line in lines)
  20.             {
  21.                 draw.Geometry.Draw(line);
  22.             }
  23.             return true;
  24.         }
  25.         protected override SamplerStatus Sampler(JigPrompts prompts)
  26.         {
  27.             JigPromptPointOptions optJig = new JigPromptPointOptions("\n请指定第二点");
  28.             optJig.UseBasePoint = true;
  29.             optJig.BasePoint = startPoint;
  30.             optJig.UserInputControls = UserInputControls.NullResponseAccepted;
  31.             PromptPointResult resJigDis = prompts.AcquirePoint(optJig);
  32.             Point3d tempPt = resJigDis.Value;
  33.             if (resJigDis.Status == PromptStatus.Cancel)
  34.             {
  35.                 return SamplerStatus.Cancel;
  36.             }
  37.             if (endPoint != tempPt)
  38.             {
  39.                 endPoint = tempPt;
  40.                 //画水平线
  41.                 lines[0].StartPoint = startPoint;
  42.                 lines[0].EndPoint = endPoint;
  43.                 points = getinpoint(lines[0]);
  44.                 for (int i = 1; i < points.Count; i++)
  45.                     lines[i] = new Line(startPoint, startPoint);
  46.                 for (int i = 1; i < points.Count; i++)
  47.                 {
  48.                     Point3d pt1 = new Point3d(points[i - 1].X - 2, points[i - 1].Y, 0);
  49.                     Point3d pt2 = new Point3d(points[i - 1].X + 2, points[i - 1].Y, 0);
  50.                     lines[i].StartPoint = pt1;
  51.                     lines[i].EndPoint = pt2;
  52.                 }
  53.                 return SamplerStatus.OK;
  54.             }
  55.             else
  56.             {
  57.                 return SamplerStatus.NoChange;
  58.             }
  59.         }
  60.         private List<Point3d> getinpoint(Line line)
  61.         {
  62.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  63.             Editor ed = doc.Editor;
  64.             Database db = doc.Database;
  65.             Entity BlockEntity=null;
  66.             Point3dCollection pts = new Point3dCollection();
  67.             List<Point3d> retpoint = new List<Point3d>();
  68.             TypedValue filterValue1 = new TypedValue((int)DxfCode.Color, "8");
  69.             TypedValue[] filterValues1 = {filterValue1};
  70.             SelectionFilter filter2 = new SelectionFilter(filterValues1);
  71.             PromptSelectionResult psr1 = ed.SelectAll(filter2);
  72.             using (Transaction trans = db.TransactionManager.StartTransaction())
  73.             {
  74.                  points.Clear();
  75.                 foreach (ObjectId BlockId in psr1.Value.GetObjectIds())
  76.                 {
  77.                     //获取选择集里块里面的对象
  78.                     BlockEntity = trans.GetObject(BlockId, OpenMode.ForRead) as Entity;
  79.                     if (BlockEntity.GetType() == typeof(Line) || BlockEntity.GetType() == typeof(Polyline) || BlockEntity.GetType() == typeof(Spline))
  80.                     {
  81.                         line.IntersectWith(BlockEntity, Intersect.OnBothOperands, pts, 0, 0);
  82.                         for (int k = 0; k < pts.Count; k++)
  83.                             points.Add(pts[k]);
  84.                     }
  85.                 }
  86.                 trans.Commit();
  87.             }
  88.             return retpoint;
  89.         }
  90.         public static bool DrawJig()
  91.         {
  92.             DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument();//文档加锁
  93.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  94.             DC_tongji_Draw_Jig begin = new DC_tongji_Draw_Jig();
  95.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  96.             PromptPointResult pRes = ed.GetPoint("请选择第一点:");
  97.             startPoint = pRes.Value;
  98.             lines.Clear();
  99.             lines.Add(new Line(startPoint, startPoint));
  100.             points.Clear();
  101.             if (pRes.Status == PromptStatus.OK)
  102.             {
  103.                 PromptResult resJig = ed.Drag(begin);
  104.                 if (resJig.Status == PromptStatus.OK)
  105.                 {
  106.                     Tools.AddEntities(lines.ToArray());//Tools.AddEntities为DNA里面的函数(将对象添加到CAD文档中)
  107.                 }
  108.             }
  109.             dl.Dispose();
  110.             return true;
  111.         }
  112.     }
  113. }
 楼主| 发表于 2012-6-20 23:47 | 显示全部楼层
sieben 发表于 2012-6-20 15:45
TypedValue[] filList ={ new TypedValue(0, "LINE"), new TypedValue(62, 8) };
        Selecti ...

继续求教高手,更改以后还是不能搞定,今天下午网上太慢,不能回复,特在晚上抓紧请假,我要实现的具体内容见4楼,希望高手答疑解惑。
发表于 2012-6-21 08:39 | 显示全部楼层
1,"更改以后还是不能搞定",你确认是因为过滤器问题?还是可能别的问题?
2,你的东西有点复杂,我帮不了你,只是建议最好不要在Jig的Sampler函数里面访问数据库
发表于 2012-6-21 20:20 | 显示全部楼层
颜色似乎是过滤不到的
发表于 2012-6-21 22:29 | 显示全部楼层
好像是有问题,在DrawJig里取不到选集,换成EntityJig吧。
发表于 2012-6-21 23:46 | 显示全部楼层
如果图层的颜色是8  但是图元随层 那么你这样是选不到的

先遍历图层,找出所有的颜色是8的图层
你的过滤条件应该是 选择
颜色8 or (图层颜色是8 and 颜色随层)

评分

参与人数 2金钱 +26 收起 理由
7.兮♂贝 + 6
GZZ + 20 赞一个!

查看全部评分

发表于 2014-7-5 13:48 | 显示全部楼层
雪山飞狐_lzh 发表于 2012-6-21 23:46
如果图层的颜色是8  但是图元随层 那么你这样是选不到的

先遍历图层,找出所有的颜色是8的图层

留着有用!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-17 19:31 , Processed in 0.235332 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表