简体   繁体   English

Revit API-项目中未加载任何空间标签系列

[英]Revit API - No Space Tags family is loaded in the project

How can I add Space Tags family using only API? 如何仅使用API​​添加空间标签系列? I'm trying use 我正在尝试使用

doc.Create.NewSpaces2(level, phase, view);

( https://www.revitapidocs.com/2019/94b4e479-2301-2e78-6201-5bb5614ed796.htm ) https://www.revitapidocs.com/2019/94b4e479-2301-2e78-6201-5bb5614ed796.htm

in my project, but I'm getting error "No Space Tags family is loaded in the project". 在我的项目中,但出现错误“项目中未加载任何空间标签”。
When I added Annotations/Mechanical/Space Tag.rfa using Revit GUI everything is working. 当我使用Revit GUI添加Annotations / Mechanical / Space Tag.rfa时,一切正常。

You could embed the SpaceTag.rfa in your DLL as a resource, and then load it into Revit file programmatically when one is not present. 您可以将SpaceTag.rfa作为资源嵌入到DLL中,然后在不存在的情况下以编程方式将其加载到Revit文件中。

Here's an example: 这是一个例子:

1. Check if Family exists: 1.检查家庭是否存在:

var loadedFam = new FilteredElementCollector(Doc)
                .OfClass(typeof(Family))
                .Cast<Family>()
                .FirstOrDefault(x.Name == famName);

2. Load the Family if it doesn't exist: 2.加载族(如果不存在):

if (loadedFam == null)
{
    var resourcePath = GetFamilyFromResource(famName);
    if (string.IsNullOrWhiteSpace(resourcePath) ||
        !e.Document.LoadFamilySymbol(resourcePath,symbolName, out fs)) return null;
}

3. My utility methods here are: 3.我的实用方法是:

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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM