明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1366|回复: 3

[选集] System.NullReferenceException: 未将对象引用设置到对象的实例

[复制链接]
发表于 2014-7-7 22:05 | 显示全部楼层 |阅读模式
  1. [code=csharp]namespace Select
  2. {
  3.     public class ssa
  4.     {
  5.         [CommandMethod("ccm")]
  6.         public void Command1_Method()
  7.         {
  8.             Database db = HostApplicationServices.WorkingDatabase;
  9.             Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
  10.             
  11.             try
  12.             {
  13.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  14.                 {
  15.                     PromptEntityOptions peo = new PromptEntityOptions("\n请选择直线");
  16.                     PromptEntityResult per = ed.GetEntity(peo);

  17.                     ObjectId id = per.ObjectId;
  18.                     Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
  19.                     Line line = (Line)ent;

  20.                     PromptSelectionResult psr = ed.SelectOnLine(line);
  21.                     SelectionSet ss = psr.Value;
  22.                     
  23.                      using (Transaction tr2 = db.TransactionManager.StartTransaction())
  24.                         {
  25.                          ed.WriteMessage("实体个数为:{0}",ss.Count);
  26.                             foreach (ObjectId idi in ss.GetObjectIds())
  27.                             {
  28.                              //   Entity enti = (Entity)idi.GetObject(OpenMode.ForWrite);
  29.                                 Entity enti = (Entity)tr2.GetObject(idi, OpenMode.ForWrite);
  30.                                 enti.ColorIndex = 1;
  31.                             }               
  32.                             tr2.Commit();
  33.                         }
  34.                     tr.Commit();
  35.                 }
  36.             }
  37.             catch (System.Exception ex)
  38.             {
  39.                 Debug.WriteLine(ex.ToString());
  40.                 ed.WriteMessage(ex.ToString());
  41.             }
  42.         }

  43.     }
  44. }
[/code]SelectOnLine(line) 函数原型为:
public static PromptSelectionResult SelectOnLine(this Editor ed, Line line);
在CAD里运行后,老是提示:“请选择直线: System.NullReferenceException: 未将对象引用设置到对象的实例。
   在 Select.ssa.Command1_Method() 位置 D:\CS文件夹\选择集\Select\ssa.cs:行号 59”,
指明的行号内容为:“ed.WriteMessage("实体个数为:{0}",ss.Count);
请各位大侠看看到底是哪里出错了,谢谢大家!



发表于 2014-7-7 23:38 来自手机 | 显示全部楼层
看 看ss 是否为 null
 楼主| 发表于 2014-7-8 11:02 | 显示全部楼层
雪山飞狐_lzh 发表于 2014-7-7 23:38
看 看ss 是否为 null

可能是我的 SelectOnLine函数有问题,但是我也看不出来哪里有问题,麻烦您给看看
  1. public static PromptSelectionResult SelectOnLine(this Editor ed, Lined line)
  2.         {
  3.             Point3d p1 = line.StartPoint;
  4.             Point3d p2 = line.EndPoint;
  5.             Point3d[] ps = { p1, p1, p2, p2 };
  6.             Point3dCollection pts = new Point3dCollection(ps);
  7.             return ed.SelectCrossingPolygon(pts);

  8.         }

==========================================
发表于 2014-7-8 11:34 | 显示全部楼层
用selectfence试试 还要保证选择区域可见
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-19 16:14 , Processed in 0.626690 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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