如何仅使用API添加空间标签系列?我正在尝试使用
doc.Create.NewSpaces2(level, phase, view);
(https://www.revitapidocs.com/2019/94b4e479-2301-2e78-6201-5bb5614ed796.htm)
在我的项目中,但出现错误“项目中未加载任何空间标签”。
当我使用Revit GUI添加Annotations / Mechanical / Space Tag.rfa时,一切正常。
答案 0 :(得分:2)
您可以将SpaceTag.rfa
作为资源嵌入DLL中,然后在不存在的情况下以编程方式将其加载到Revit文件中。
这里是一个示例:
var loadedFam = new FilteredElementCollector(Doc)
.OfClass(typeof(Family))
.Cast<Family>()
.FirstOrDefault(x.Name == famName);
if (loadedFam == null)
{
var resourcePath = GetFamilyFromResource(famName);
if (string.IsNullOrWhiteSpace(resourcePath) ||
!e.Document.LoadFamilySymbol(resourcePath,symbolName, out fs)) return null;
}
private static string GetFamilyFromResource(string resourceName)
{
var contents = Resources.StreamBinaryEmbeddedResource(AppCommand.AppInstance.Assembly,
$"FullPathToResource.{resourceName}.rfa");
if (!contents.Any()) return string.Empty;
var filePath = Path.Combine(Path.GetTempPath(), $"{resourceName}.rfa");
using (var fileStream = File.Open(filePath, FileMode.Create))
using (var writer = new BinaryWriter(fileStream))
{
writer.Write(contents);
}
if (!File.Exists(filePath) || new FileInfo(filePath).Length <= 0) return string.Empty;
return filePath;
}
public static byte[] StreamBinaryEmbeddedResource(Assembly assembly, string fileName)
{
using (var stream = assembly.GetManifestResourceStream(fileName))
{
if (stream == null) return new byte[] { };
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Dispose();
return buffer;
}
}