revit创建门(一定要激活familysymbol,而且一定要放在事务里面才算激活成功)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB.Structure;

 

namespace 创建门

{

[Transaction(TransactionMode.Manual)]

class 创建门 : IExternalCommand

{

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

{

UIDocument uidoc = commandData.Application.ActiveUIDocument;

Document doc = uidoc.Document;//获取活动文档

 

Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "请选择墙");

Element elem = doc.GetElement(refer);

Wall awall = elem as Wall;//把选择的元素转换为墙

 

 

Line line = (awall.Location as LocationCurve).Curve as Line;//获取墙的线

 

XYZ midPoint = (line.GetEndPoint(0) + line.GetEndPoint(1)) / 2;//获取中点

 

Level wallLevel = doc.GetElement(awall.LevelId) as Level;//获取标高,元素转换为标高

 

//创建过滤器

FamilySymbol familySymbol = null;

FilteredElementCollector doorCollector = new FilteredElementCollector(doc);

//创建过滤收集器

ElementCategoryFilter doorFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);

//过滤出门类别

ElementClassFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol));

//过滤出门

LogicalAndFilter doorAndFilter = new LogicalAndFilter(doorFilter, familySymbolFilter);

//逻辑过滤器

doorCollector.WherePasses(doorAndFilter);

 

foreach (FamilySymbol symbol in doorCollector)

{

if (symbol.Name == "600 x 1800mm")

{

familySymbol = symbol;

break;

}

}

 

 

 

TaskDialog.Show("revit", familySymbol.Name);

 

Transaction trans = new Transaction(doc);

trans.Start("创建门");

 

if (!familySymbol.IsActive)//激活familysymbol,这个要放在事务里面才算激活(重点)

{

familySymbol.Activate();

}

 

doc.Create.NewFamilyInstance(midPoint, familySymbol, elem, wallLevel, StructuralType.NonStructural);

trans.Commit();

 

 

 

 

 

return Result.Succeeded;

}

}

}

 

 

菜鸟一枚,请多包涵

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值