Revit二次开发之“预先设置系统族类型”

像Wall和Duct要预先修改WallType或DuctType,不能用上篇的方法。
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

using  System.Windows.Forms;

using  Autodesk.Revit.UI;
using  Autodesk.Revit.DB;
using  Autodesk.Revit.Attributes;

using  Autodesk.Revit.DB.Mechanical;
using  Autodesk.Revit.UI.Selection;
using  RevitApp  =  Autodesk.Revit.ApplicationServices;

namespace  RevitCodes
{
    
public   class  Common
    {
        
// 毫米到英寸,单位换算
         public   static   double  mmToFeet( double  val) {  return  val  /   304.8 ; }
        
// 通过族名称,族类型,从过滤器中找到指定的类型。
        
// WallType fs = Common.FindSystemFamilyType(uiDoc.Document, typeof(WallType), "CW 102-50-140p", BuiltInCategory.OST_Walls) as WallType;
         public   static  Element FindSystemFamilyType(Document doc, Type targetType,  string  familyTypeName, BuiltInCategory catagory)
        {
            FilteredElementCollector collector 
=   new  FilteredElementCollector(doc);
            collector.OfClass(targetType);
            
if  (collector  !=   null )
                collector.OfCategory(catagory);
            IList
< Element >  elements  =  collector.ToElements();

            Element elem 
=   null ;
            
foreach  (Element e  in  elements)
            {
                
if  (e.Name.Equals(familyTypeName))
                {
                    elem 
=  e;
                    
break ;
                }
            }

            
return  elem;
        }
    }

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    
// [Journaling(JournalingMode.NoCommandData)]
     public   class  SetWallType : IExternalCommand
    {
        
public  Result Execute(ExternalCommandData commandData,  ref   string  message, ElementSet elements)
        {
            UIDocument uiDoc 
=  commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application app 
=  commandData.Application.Application;

            Transaction tr 
=   new  Transaction(uiDoc.Document,  " CreateWallWithDefType " );
            tr.Start();

            
string  type  =   " CW 102-50-140p " ; // 默认墙类型
             double  h  =  Common.mmToFeet( 3000 ); // 默认墙高

            WallType fs 
=  Common.FindSystemFamilyType(uiDoc.Document,  typeof (WallType), type, BuiltInCategory.OST_Walls)  as  WallType;
            XYZ pt1 
=  uiDoc.Selection.PickPoint( " 输入起点 " );
            XYZ pt2 
=  uiDoc.Selection.PickPoint( " 输入终点 " );
            XYZ first 
=   new  XYZ(pt1.X, pt1.Y,  0 );
            XYZ second 
=   new  XYZ(pt2.X, pt2.Y,  0 );
            XYZ third 
=   new  XYZ(pt2.X, pt2.Y, h);
            XYZ fourth 
=   new  XYZ(pt1.X, pt1.Y, h);

            CurveArray profile 
=   new  CurveArray();
            profile.Append(uiDoc.Document.Application.Create.NewLineBound(first, second));
            profile.Append(uiDoc.Document.Application.Create.NewLineBound(second, third));
            profile.Append(uiDoc.Document.Application.Create.NewLineBound(third, fourth));
            profile.Append(uiDoc.Document.Application.Create.NewLineBound(fourth, first));
            Wall wall 
=  uiDoc.Document.Create.NewWall(profile, fs, uiDoc.ActiveView.GenLevel,  false );
            Parameter para 
=  wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM);

            tr.Commit();

            
return  Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    
// [Journaling(JournalingMode.NoCommandData)]
     public   class  SetDuctType : IExternalCommand
    {
        
public  Result Execute(ExternalCommandData commandData,  ref   string  message, ElementSet elements)
        {
            UIDocument uiDoc 
=  commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application app 
=  commandData.Application.Application;
            Transaction tr 
=   new  Transaction(uiDoc.Document,  " CreateDuctWithDefType " );
            tr.Start();

            
string  type  =   " Taps " ; // 风管类型
            DuctType fs  =  Common.FindSystemFamilyType(uiDoc.Document,  typeof (DuctType), type, BuiltInCategory.OST_DuctCurves)  as  DuctType;
            XYZ pt1 
=  uiDoc.Selection.PickPoint( " 输入起点 " );
            XYZ pt2 
=  uiDoc.Selection.PickPoint( " 输入终点 " );
            uiDoc.Document.Create.NewDuct(pt1, pt2, fs);

            tr.Commit();

            
return  Result.Succeeded;
        }
    }
}
from: http://revit.5d6d.com/thread-1023-1-4.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值