public class KickingLine:IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
Reference refer = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new roomFilter(), "");//过滤出房间
Family family=CreateProfileFamily(uiapp);
Material mate = new FilteredElementCollector(doc).OfClass(typeof(Material)).Where(o => o.Name == "面层 - 白色").First() as Material;
ElementType wallSweepType = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Cornices).WhereElementIsElementType().First() as ElementType;
//ElementType wallSweepType = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Cornices).WhereElementIsElementType().Cast().First();
using (Transaction transaction = new Transaction(doc, "BIM"))
{
transaction.Start();
//复制一个墙饰条类型
ElementType type = wallSweepType.Duplicate("踢脚线");
Room room = doc.GetElement(refer) as Room;
SpatialElementBoundaryOptions options = new SpatialElementBoundaryOptions();
options.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;
IList<IList<Autodesk.Revit.DB.BoundarySegment>> boundList = room.GetBoundarySegments(options);
foreach (IList loop in boundList)
{
foreach (BoundarySegment seg in loop)
{
Element elem = doc.GetElement(seg.ElementId);
if (elem is Wall)
{
Wall wall = elem as Wall;
WallSweepInfo wallSweepInfo = new WallSweepInfo(WallSweepType.Sweep, false);//false水平布置,true垂直布置
//墙的正反或里外面
wallSweepInfo.WallSide = WallSide.Interior;
//距墙底高度
wallSweepInfo.Distance = 0;
//偏离墙距离
wallSweepInfo.WallOffset = 0;
foreach (Parameter o in type.Parameters)
{
if (o.Definition.Name == "轮廓")
{
// o.Set(FamilyUtils.GetProfileSymbols(doc, ProfileFamilyUsage.WallSweep, false).ElementAt(0));
o.Set(family.Id);
}
if (o.Definition.Name == "材质")
{
o.Set(mate.Id);
}
}
WallSweep ws = WallSweep.Create(wall, type.Id, wallSweepInfo);//创建函数
}
}
transaction.Commit();
}
}
return Result.Succeeded;
}
private Family CreateProfileFamily(UIApplication uiapp)
{
Document familyDoc=uiapp.Application.NewFamilyDocument(@"C:\Users\018193\Desktop\公制轮廓.rft");
Family family=null;
using (Transaction transFamily=new Transaction(familyDoc))
{
//找到默认的平面,为详图线服务
View view=new FilteredElementCollector(familyDoc).OfClass(typeof(View)).Where(o=>o.Name=="参照标高").First() as View;
transFamily.Start("familyCreation");
//创建四条首尾相连的曲线,为详图线服务
Curve curve1=Line.CreateBound(new XYZ(0,0,0),new XYZ(0,150/304.8,0));
Curve curve2=Line.CreateBound(new XYZ(0,150/304.8,0),new XYZ(25/304.8,150/304.8,0));
Curve curve3=Line.CreateBound(new XYZ(25/304.8,150/304.8,0),new XYZ(25/304.8,0,0));
Curve curve4=Line.CreateBound(new XYZ(25/304.8,0,0),new XYZ(0,0,0));
//创建四条首尾相连的详图线也就是真正的墙饰条轮廓
DetailCurve dc1=familyDoc.FamilyCreate.NewDetailCurve(view,curve1);
DetailCurve dc2=familyDoc.FamilyCreate.NewDetailCurve(view,curve2);
DetailCurve dc3=familyDoc.FamilyCreate.NewDetailCurve(view,curve3);
DetailCurve dc4=familyDoc.FamilyCreate.NewDetailCurve(view,curve4);
transFamily.Commit();
//族另存选项
SaveAsOptions option=new SaveAsOptions();
//覆盖已有同名文件
option.OverwriteExistingFile=true;
//保存在本地桌面上,可以删除也可以保留为其他项目使用
familyDoc.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop,Environment.SpecialFolderOption.None)+@"\踢脚线.rfa",option );
//保存后族就有名字了可以直接使用familyDoc导入到目标项目中了
family=familyDoc.LoadFamily(uiapp.ActiveUIDocument.Document,new LoadOptions());
//记得关闭 要不然在桌面上临时生成的族文件无法顺利删除也就是要资源释放
familyDoc.Close();
}
return family;
}
public class roomFilter:ISelectionFilter
{
public bool AllowElement(Element elem)
{
return elem is Room;
}
public bool AllowReference(Reference reference,XYZ position)
{
return false;
}
}
public class LoadOptions : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
{
source = FamilySource.Project;
overwriteParameterValues = true;
return true;
}
}
}
revit 二次开发 利用墙饰条创建踢脚线
最新推荐文章于 2022-02-11 15:57:32 发布