c# autocad旁载数据库绑定xref
本文关键字:绑定 xref 数据库 autocad | 更新日期: 2023-09-27 18:12:01
我正在尝试绑定xref在一个侧负载绘图数据库。程序在这行' if(!xNode.Database.Filename.Equals(NewDb.Filename))'处停止。我也收到这个错误的系统。NullReferenceException:对象引用没有设置为对象的实例。在XBind.RecursiveFileProcessor。ProcessFile (String路径)。"我做了一些研究,发现了VB。. NET代码附加一个xref,并尝试推断,但没有成功。我希望有人能给我指出正确的方向。
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);
}
实际上,这将在内存中工作。Winslow North缺少CloseInput()之后的一行代码…
NewDb.ResolveXrefs(true, false);
但是,您也不需要为此使用Transaction。没有必要。我制作了自己的样本并进行了测试。它的工作原理。如果你需要我发布,请告诉我。问题是,由于没有解析Xref, xNode有一个空数据库。您必须手动完成上面的行。
不相信这将在内存数据库工作,你可以尝试这种方法,看看下面的一部分:
[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();
}
}