Revit二次开发之“族”操作

时间:2021-10-14 17:36:50
风管 - 类别Category
圆形风管 - 族Family
T形三通 - 族型号FamilySymbol
画出来 - 族实例FamilyInstance

加载族

Revit二次开发之“族”操作[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.NoCommandData)]
public class LoadFamily : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        Document doc 
= commandData.Application.ActiveUIDocument.Document;
        doc.LoadFamily(
@"C:\Documents and Settings\All Users\Application Data\Autodesk\RME 2011\Metric Library\窗\M_带贴面窗.rfa");
        
return Result.Succeeded;
    }
}
Revit二次开发之“族”操作
end Revit二次开发之“族”操作    [Transaction(TransactionMode.Automatic)]
    [Regeneration(RegenerationOption.Manual)]
    [Journaling(JournalingMode.NoCommandData)]
    
public class LoadFamily : IExternalCommand
    {
        
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            Document doc 
= commandData.Application.ActiveUIDocument.Document;
            
//加载族
            
//doc.LoadFamily(@"C:\Documents and Settings\All Users\Application Data\Autodesk\RME 2011\Metric Library\窗\M_带贴面窗.rfa");
            
//加载族中的一个Symbol
            
//doc.LoadFamilySymbol(@"C:\Documents and Settings\All Users\Application Data\Autodesk\RME 2011\Metric Library\窗\M_带贴面窗.rfa"
            
//, "0406 x 0610 mm");
            
//遍历族
            TraversqlFamily(doc);

            
return Result.Succeeded;
        }
        
public Result TraversqlFamily(Document doc)
        {
            Family family 
= null;
            FilteredElementCollector collector 
= new FilteredElementCollector(doc);
            ICollection
<Element> collection = collector.OfClass(typeof(Family)).ToElements();
            
string sMsg = "Standard families already loaded in this model:";
            
foreach (Element e in collection)
            {
                family 
= e as Family;
                
if (null != family)
                {
                    
/*
                    //获得族的类别名称
                    string famCatName = (null == family.FamilyCategory) ? "?" : family.FamilyCategory.Name;
                    sMsg += "\r\n Name=" + family.Name + ";Category=" + famCatName;

                    if (family.Name == "M_圆形四通")//M_风管尺寸标记//M_圆形软风管标记
                        MessageBox.Show(family.Name);
                    if (family.Name == "风管")
                        MessageBox.Show(family.Name);
                    if (family.Name == "圆形风管")
                        MessageBox.Show(family.Name);
                    if (family.Name == "接头")
                        MessageBox.Show(family.Name);

                    if (family.Name.Contains("风管"))
                        MessageBox.Show(family.Name);
*/
                    
if (family.FamilyCategory.Name == "圆形风管")
                    
//if (family.FamilyCategory.Name == "风管")
                        sMsg += "\r\n Name=" + family.Name + ";Category=" + family.FamilyCategory.Name;
                }
            }
            MessageBox.Show(sMsg);

            
return Result.Succeeded;
        }
Revit二次开发之“族”操作
end 
创建族实例 Revit二次开发之“族”操作[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Automatic)]
public class CreateFamilyInstance : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIApplication uiApp 
= commandData.Application;
        Document doc 
= uiApp.ActiveUIDocument.Document;
        Selection sel 
= uiApp.ActiveUIDocument.Selection;

        FamilySymbol familySymbol 
= null;
        FilteredElementCollector collector 
= new FilteredElementCollector(doc);//过滤元素
        ICollection<Element> collection = collector.OfClass(typeof(FamilySymbol)).ToElements();

        
foreach (Element e in collection)
        {
            familySymbol 
= e as FamilySymbol;
            
if (null != familySymbol.Category)
            {
                
if ("风管管件" == familySymbol.Category.Name)//这里不知道怎样用内参BuiltInCategory.o
                {
                    
if (familySymbol.Family.Name == "矩形弯头 - 法兰")
                    {
                        
//遍历族符号的方法
                        FamilySymbolSetIterator symbolItor = familySymbol.Family.Symbols.ForwardIterator();
                        FamilySymbol fs 
= null;
                        
while (symbolItor.MoveNext())
                        {
                            fs 
= symbolItor.Current as FamilySymbol;
                        }
                        
break;
                    }
                }
            }
        }

        XYZ point 
= new XYZ(101010);

        
//创建族实例
        FamilyInstance fi = doc.Create.NewFamilyInstance(point, familySymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);

        
//修改族实例
        FamilySymbol newFamilySymbol = null;
        
foreach (Element e in collection)
        {
            newFamilySymbol 
= e as FamilySymbol;
            
if (null != newFamilySymbol.Category)
            {
                
if ("风管管件" == newFamilySymbol.Category.Name)
                {
                    
if (newFamilySymbol.Family.Name == "矩形弯头 - 平滑半径 - 法兰")
                    {
                        FamilySymbolSetIterator symbolItor 
= newFamilySymbol.Family.Symbols.ForwardIterator();
                        FamilySymbol fs 
= null;
                        
while (symbolItor.MoveNext())
                        {
                            fs 
= symbolItor.Current as FamilySymbol;
                            
//MessageBox.Show(fs.Name);
                        }
                        
break;
                    }
                }
            }
        }
        fi.Symbol 
= newFamilySymbol;

        
return Result.Succeeded;
    }
}
Revit二次开发之“族”操作
end