代码之家  ›  专栏  ›  技术社区  ›  Winslow North

c#autocad侧载数据库绑定外部参照

  •  0
  • Winslow North  · 技术社区  · 10 年前

    我正在尝试在侧载图形数据库中绑定外部参照。程序在“if(!xNode.Database.Filename.Equals(NewDb.Filename)”行停止。我也收到了这个错误“系统”。NullReferenceException:对象引用未设置为对象的实例。在XBind.RecursiveFileProcessor上。ProcessFile(字符串路径)。'我做了一些研究,找到了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);
                }
    
    2 回复  |  直到 10 年前
        1
  •  2
  •   bjhuffine    10 年前

    事实上,这将在记忆中发挥作用。Winslow North在CloseInput()之后缺少以下代码行。。。

    NewDb.ResolveXrefs(true, false);
    

    但是,您不需要为此进行交易。这没有必要。我创建了自己的样本并进行了测试。它起作用了。如果你需要我发布,请告诉我。问题是,由于未解析外部参照,xNode的数据库为空。您必须使用上面的行手动执行此操作。

        2
  •  0
  •   Augusto Goncalves    10 年前

    不相信这对内存数据库有效,您可以尝试 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();
      }
    }