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
posted @ 2011-08-12 08:08  大气象  阅读(3405)  评论(0)    收藏  举报
http://www.tianqiweiqi.com