revit二次开发-创建族

通过文件路径载入族

并点击任意一点创建实例

revit二次开发-创建族_第1张图片

 代码如下:

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace loadingFamily
{
    [TransactionAttribute(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        UIDocument uIDocument = null;
        UIApplication uIApplication = null;
        Document document = null;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIApplication uiApp = commandData.Application;
            uIDocument = uiApp.ActiveUIDocument;
            uIApplication = uIDocument.Application;
            document = uIDocument.Document;


            using (Transaction transaction = new Transaction(document, "创建族"))
            {

                transaction.Start();

                FamilyInstance familyInstance = CreateFamilyInstance();

                transaction.Commit();
                TransactionStatus status = transaction.GetStatus();
                if (status.Equals(TransactionStatus.Committed))
                {
                    TaskDialog.Show("提示", "创建成功!");             
                }
            }
            return Result.Succeeded;

        }
        /// 
        /// 载入族
        /// 
        /// 文件路径
        /// 
        public Family LoadFamily(string path)
        {

            bool result = document.LoadFamily(path, out Family family);

            if (result == false)
            {
                //获取文件信息
                FileInfo fileInfo = new FileInfo(path);
                //获取文件名  集装箱式宿舍.rfa
                String fileName = fileInfo.Name;
                //取到.的位置
                int num = fileName.IndexOf(".");
                //取到  集装箱式宿舍
                fileName = fileName.Substring(0, num);


                //过滤族类型
                FilteredElementCollector collector = new FilteredElementCollector(document);
                //FamilySymbol symbol = collector.OfClass(typeof(FamilySymbol)).Where(x => (x as FamilySymbol).FamilyName.Equals(fileName)).FirstOrDefault() as FamilySymbol;
                IList elements = collector.OfClass(typeof(FamilySymbol)).ToElements();
                FamilySymbol symbol = null;
                //过滤出  集装箱式宿舍
                foreach (var item in elements)
                {
                    symbol = item as FamilySymbol;
                    if (symbol.FamilyName.Equals(fileName))
                    {
                        break;
                    }
                }

                family = symbol.Family;

            }
            return family;
        }
        /// 
        /// 创建实例
        /// 
        /// 
        public FamilyInstance CreateFamilyInstance()
        {
            //选择点
            XYZ xyz = uIDocument.Selection.PickPoint();
            //载入族
            Family family = LoadFamily(@"D:\Guo\revit\构件坞\集装箱式宿舍.rfa");
            //获取族类型
            ISet ids = family.GetFamilySymbolIds();
            ElementId id = ids.FirstOrDefault();
            FamilySymbol familySymbol = document.GetElement(id) as FamilySymbol;
            //symbol 是否激活
            if (!familySymbol.IsActive)
            {
                familySymbol.Activate();
            }
            //获取标高
            View view = uIDocument.ActiveView;
            Level level = view.GenLevel;

            //创建实例
            FamilyInstance familyInstance = document.Create.NewFamilyInstance(xyz, familySymbol, level, StructuralType.NonStructural);

            return familyInstance;
        }

    }
}

你可能感兴趣的:(Revit二次开发,c#,开发语言)