时间:2019-03-08 标签:c#autocadsideloaddatabasebindingxrefs

c# autocad sideload database binding xrefs

我正在尝试在侧载绘图数据库中绑定外部参照。程序在“if(!xNode.Database.Filename.Equals(NewDb.Filename))”这一行停止。我也收到此错误 'System.NullReferenceException: Object reference not set to an instance of an object.at XBind.RecursiveFileProcessor.ProcessFile(String path).' 我做了一些研究并找到了 VB.NET 代码来附加外部参照并尝试推断它但没有成功。我很感激有人为我指出正确的方向。

                using (Database NewDb = new Database(false, true))
            {
                NewDb.ReadDwgFile(path, FileOpenMode.OpenForReadAndWriteNoShare, true, "");
                NewDb.CloseInput(true);
                using (Transaction tr = NewDb.TransactionManager.StartTransaction())
                {
                    ObjectIdCollection xrefCollection = new ObjectIdCollection();
                    XrefGraph xg = NewDb.GetHostDwgXrefGraph(false);
                    int numOfNodes = xg.NumNodes;
                    for (int cnt = 0; cnt < xg.NumNodes; cnt++)
                    {
                        XrefGraphNode xNode = xg.GetXrefNode(cnt) as XrefGraphNode;
                        if (!xNode.Database.Filename.Equals(NewDb.Filename))
                        {
                            if (xNode.XrefStatus == XrefStatus.Resolved)
                            {
                                xrefCollection.Add(xNode.BlockTableRecordId);
                            }
                        }
                    }
                    if (xrefCollection.Count != 0)
                    {
                        NewDb.BindXrefs(xrefCollection, true);
                    }
                    tr.Commit();
                }
                NewDb.SaveAs(path, DwgVersion.Current);
            }

不相信这对内存数据库有效,您可以尝试this approach,请看下面的图片:

[CommandMethod("CHX")]
public void ChangeXref()
{
  var doc = Application.DocumentManager.MdiActiveDocument;
  if (doc == null) return;

  var ed = doc.Editor;
  var db = doc.Database;

  // Get the database associated with each xref in the
  // drawing and change all of its circles to be dashed

  using (var tr = db.TransactionManager.StartTransaction())
  {
    var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

    // Loop through the contents of the modelspace
    foreach (var id in ms)
    {
      // We only care about BlockReferences
      var br = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
      if (br != null)
      {
        // Check whether the associated BlockTableRecord is
        // an external reference
        var bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
        if (bd.IsFromExternalReference)
        {
          // If so, get its Database and call the function
          // to change the linetype of its Circles

          var xdb = bd.GetXrefDatabase(false);
          if (xdb != null)
          {

            using (var xf = XrefFileLock.LockFile(xdb.XrefBlockId))
            {
              // Make sure the original symbols are loaded
              xdb.RestoreOriginalXrefSymbols();

              xdb.RestoreForwardingXrefSymbols();
            }

          }
        }
      }
    }
    tr.Commit();
  }
}

实际上,这将在内存中运行。 Winslow North 在 CloseInput() 之后缺少以下代码行...

NewDb.ResolveXrefs(true, false);

而且,您不需要为此进行交易。这不是必需的。我创建了自己的示例并对其进行了测试。有用。如果您需要我post,请告诉我。问题是由于外部参照未解析,xNode 有一个空数据库。您必须使用上面的行手动执行此操作。