使用 .NET 插入块不使用文件搜索路径来定位文件

Inserting a block with .NET is not using the files search paths to locate the files

我在此文件夹中定义了两个块:

D:\我的Documents\MyAutoCAD\Blocks

块是:

在 Autocad 中,文件搜索路径设置如下:

如果我开始绘制新图形并通过键入它们的名称来插入这些块中的一个,它就会被找到并插入。

但是,我发现对于 .NET,我必须在路径前加上前缀。例如:

if (!acBlkTbl.Has("COORD2D"))
{
    _AcDb.Database blkDb = new _AcDb.Database(false, true);
    blkDb.ReadDwgFile("D:\My Documents\My AutoCAD\Blocks\COORD2D" + ".DWG", 
                     System.IO.FileShare.Read, true, "");
    acCurDb.Insert("COORD2D", blkDb, true);
}
blkRecId = acBlkTbl["COORD2D"];

我不想指定路径。我希望它能找到它,因为它在受支持的搜索路径中。那么我错过了哪一步?

我发现这很有用 link。这是为了 VB 但我能够采用原则:

if (!acBlkTbl.Has("COORD2D"))
{
    _AcDb.Database blkDb = new _AcDb.Database(false, true);
    string blockPath = _AcDb.HostApplicationServices.Current.FindFile("COORD2D.DWG",
                            acCurDb, _AcDb.FindFileHint.Default);
    blkDb.ReadDwgFile(blockPath, System.IO.FileShare.Read, true, "");
    acCurDb.Insert("COORD2D", blkDb, true);
}
blkRecId = acBlkTbl["COORD2D"];

我不得不使用 FindFile 命令。


旁注

其中一个答案 here 指出:

AcDbDatabase::readDwgFile() expects the filename argument to be a full path. It does not search along any search paths to find the file.

Use AcDbHostApplicationServices::findFile() to find the file and get a full path before calling readDwgFile().