Revit二次开发之“通过新建族来修改族参数”

有些参数需要新建一个族,在新建的时候设置参数,而不能直接修改。
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
// [Journaling(JournalingMode.NoCommandData)]
public   class  NewFamily : IExternalCommand
{
    
// 毫米到英寸,单位换算
     public   double  mmToFeet( double  val) {  return  val  /   304.8 ; }
    
// 通过族名称,族类型,从过滤器中找到指定的类型。
     public  Element FindElementType(Document doc, Type targetType,  string  familyName,  string  typeName, BuiltInCategory catagory)
    {
        IList
< Element >  elements  =  FindSpecialElements(doc, targetType, catagory);

        Element elem 
=   null ;
        
foreach  (Element e  in  elements)
        {
            
if  (e.Name.Equals(typeName)  &&
                e.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM).AsString().Equals(familyName))
            {
                elem 
=  e;
                
break ;
            }
        }

        
return  elem;
    }
    
// 过滤
    
// FindSpecialElements(doc, typeof(FamilySymbol), BuiltInCategory.OST_Doors);
     public  IList < Element >  FindSpecialElements(Document doc, Type targetType, BuiltInCategory catagory)
    {
        FilteredElementCollector collector 
=   new  FilteredElementCollector(doc);
        collector.OfClass(targetType);
        
if  (collector  !=   null )
            collector.OfCategory(catagory);
        IList
< Element >  elements  =  collector.ToElements();

        
return  elements;
    }
    
public  Result Execute(ExternalCommandData commandData,  ref   string  message, ElementSet elements)
    {
        UIDocument uiDoc 
=  commandData.Application.ActiveUIDocument;
        Autodesk.Revit.ApplicationServices.Application app 
=  commandData.Application.Application;

        
//
        HyRevit.Key.Press.Keys( " DR " );
        
string  family  =   " 单开门 " ;
        
string  type  =   " M0721 " ;
        FamilySymbol fs 
=  FindElementType(uiDoc.Document,  typeof (FamilySymbol), family, type, BuiltInCategory.OST_Doors)  as  FamilySymbol;

        Family fy 
=  fs.Family;
        Document familyDoc 
=  uiDoc.Document.EditFamily(fy);
        
if  ( null   !=  familyDoc)
        {
            
if  (familyDoc.IsFamilyDocument)
            {
                FamilyManager familyManager 
=  familyDoc.FamilyManager;
                
if  ( null   !=  familyManager)
                {
                    Transaction tr 
=   new  Transaction(familyDoc,  " CreateDoorFamilyTransaction " );
                    tr.Start();
                    SubTransaction subTransaction 
=   new  SubTransaction(familyDoc);
                    subTransaction.Start();

                    FamilyType newFamilyType 
=  familyManager.NewType( " 新门1001 " );

                    FamilyParameter familyParam 
=  familyManager.get_Parameter( " Width " );
                    
if  ( null   !=  familyParam)
                    {
                        familyManager.Set(familyParam, mmToFeet(Convert.ToDouble(
" 500 " )));
                    }
                    familyParam 
=  familyManager.get_Parameter( " Height " );
                    
if  ( null   !=  familyParam)
                    {
                        familyManager.Set(familyParam, mmToFeet(Convert.ToDouble(
" 2000 " )));
                    }
                    familyDoc.LoadFamily(uiDoc.Document, 
new  FamilyOptions());

                    subTransaction.Commit();
                    tr.Commit();
                    familyDoc.Close(
false );
                    familyDoc.Dispose();
                }
            }
        }

        
// 进入命令,设置默认
        family  =   " 单开门 " ;
        type 
=   " 新门1001 " ;
        FamilySymbol fsNew 
=  FindElementType(uiDoc.Document,  typeof (FamilySymbol), family, type, BuiltInCategory.OST_Doors)  as  FamilySymbol;
        uiDoc.PromptForFamilyInstancePlacement(fsNew);

        
return  Result.Succeeded;
    }
}
from: http://revit.5d6d.com/thread-978-1-4.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值