找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 865|回复: 0

.NET - System.NullReferenceException if draw method is called after layer is ...

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-8 23:47:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
.NET - System.NullReferenceException if draw method is called after layer is created

问题:

I need to add a layer and then add an entity to a drawing that will be on the new layer that is also created. After the entity is appended to modelspace and added to the transaction, but before the transaction is commited I need to call the entities' draw method. When I do this a System.NullReferenceException error occurs. Is there a work around for this problem?

解答:
The work around is to change a property of the layer after it has been added to the layer table. For example if the IsFrozen property of the new layer is changed then the error is resolved. The example below creates a new line and a new layer. Adding a new layer to the layer table should invalidate the layer cache. The problem is that the layer cache is not getting updated because the layer table is not "dirtied". (logged change request)

Note: The same problem will occur using the unmanaged C++ APIs. Also this problem may appear with this error:

"An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll"

  1. using System;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.EditorInput;
  6. using Autodesk.AutoCAD.Runtime;
  7. using System.Windows.Forms;


  8. namespace ClassLibrary2
  9. {
  10. public class Class1
  11. {
  12.   [Autodesk.AutoCAD.Runtime.CommandMethod("testLine")]
  13.   public static void testLine()
  14.   {

  15.    Database pDb = AcadApp.DocumentManager.MdiActiveDocument.Database;

  16.    using (Transaction tr = pDb.TransactionManager.StartTransaction())
  17.    {
  18.     AcadApp.DocumentManager.MdiActiveDocument.TransactionManager.EnableGraphicsFlush(true);

  19.     using (Line line = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0)))
  20.     {
  21.      try
  22.      {
  23.       ObjectId objId = getLayer("TestLayerName");
  24.       line.LayerId = objId;

  25.       BlockTable blkTable = (BlockTable)tr.GetObject(pDb.BlockTableId, OpenMode.ForRead);
  26.       BlockTableRecord btrMSpace = (BlockTableRecord)tr.GetObject(blkTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

  27.       btrMSpace.AppendEntity(line);
  28.       tr.AddNewlyCreatedDBObject(line, true);

  29.       AcadApp.DocumentManager.MdiActiveDocument.TransactionManager.FlushGraphics();
  30.       line.Draw();
  31.      }
  32.      catch(System.Exception ex)
  33.      {
  34.       MessageBox.Show(ex.ToString());
  35.      }
  36.     }
  37.     tr.Commit();
  38.    }
  39.   }


  40.   private static ObjectId getLayer(string LayerName)
  41.   {
  42.    Database DB = AcadApp.DocumentManager.MdiActiveDocument.Database;
  43.    ObjectId tRetVal;
  44.    using (Transaction tr = DB.TransactionManager.StartTransaction())
  45.    {
  46.     LayerTable tLtb = (LayerTable)tr.GetObject(DB.LayerTableId, OpenMode.ForWrite);
  47.     if (tLtb.Has(LayerName))
  48.     {
  49.      tRetVal = tLtb[LayerName];
  50.     }
  51.     else
  52.     {
  53.      using (LayerTableRecord tLtbRec = new LayerTableRecord())
  54.      {
  55.       tLtbRec.Name = LayerName;
  56.       tLtbRec.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, Convert.ToInt16(6));

  57.       tRetVal = tLtb.Add(tLtbRec);
  58.       tr.AddNewlyCreatedDBObject(tLtbRec, true);
  59.       //WORKAROUND: this will mark the layer table dirty so that FlushGraphics() will updated the layer cache
  60.       //and thus line.Draw above works. It doesn't matter what value we are setting to IsFrozen: simply touching
  61.       //the property will do the trick.
  62.       tLtbRec.IsFrozen = false;
  63.      }
  64.     }
  65.     tr.Commit();
  66.    }

  67.    return tRetVal;
  68.   }
  69. }
  70. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-21 17:37 , Processed in 0.328501 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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