喷头属于系统族,但不能通过NewDuct();类似这样的方法布置。
必须使用NewFamilyInstance()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = 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 Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Architecture;
using System.Xml;
namespace RevitCodes
{
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdSprinklers : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Selection sel = uiApp.ActiveUIDocument.Selection;
Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
ts.Start();
//从系统族中过滤出喷头,是作为一个FamilySymbol存在的
FilteredElementCollector collector = new FilteredElementCollector(doc);
//此法可以只过滤系统类型
//collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_Sprinklers);
collector.OfCategory(BuiltInCategory.OST_Sprinklers);
FamilyInstance fi = null;//如果已经布置了,就是族实例
int iFi = 0;
FamilySymbol symbol = null;//系统族是族符号
int iFs = 0;
foreach (Element el in collector.ToElements())
{
fi = el as FamilyInstance;
if (fi != null)
iFi += 1;
symbol = el as FamilySymbol;
if (symbol != null)
iFs += 1;
}
TaskDialog.Show("count", iFi + "," + iFs);
if (fi != null)
doc.Create.NewFamilyInstance(new XYZ(10, 10, 10), fi.Symbol, StructuralType.Beam);
if (symbol != null)
doc.Create.NewFamilyInstance(new XYZ(10, 10, 10), symbol, StructuralType.Beam);
ts.Commit();
return Result.Succeeded;
}
XYZ PointOnFace(PlanarFace face)
{
XYZ p = new XYZ(0, 0, 0);
Mesh mesh = face.Triangulate();
for (int i = 0; i < mesh.NumTriangles; ++i)
{
MeshTriangle triangle = mesh.get_Triangle(i);
p += triangle.get_Vertex(0);
p += triangle.get_Vertex(1);
p += triangle.get_Vertex(2);
p *= 0.3333333333333333;
break;
}
return p;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = 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 Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Architecture;
using System.Xml;
namespace RevitCodes
{
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdSprinklers : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Selection sel = uiApp.ActiveUIDocument.Selection;
Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
ts.Start();
//从系统族中过滤出喷头,是作为一个FamilySymbol存在的
FilteredElementCollector collector = new FilteredElementCollector(doc);
//此法可以只过滤系统类型
//collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_Sprinklers);
collector.OfCategory(BuiltInCategory.OST_Sprinklers);
FamilyInstance fi = null;//如果已经布置了,就是族实例
int iFi = 0;
FamilySymbol symbol = null;//系统族是族符号
int iFs = 0;
foreach (Element el in collector.ToElements())
{
fi = el as FamilyInstance;
if (fi != null)
iFi += 1;
symbol = el as FamilySymbol;
if (symbol != null)
iFs += 1;
}
TaskDialog.Show("count", iFi + "," + iFs);
if (fi != null)
doc.Create.NewFamilyInstance(new XYZ(10, 10, 10), fi.Symbol, StructuralType.Beam);
if (symbol != null)
doc.Create.NewFamilyInstance(new XYZ(10, 10, 10), symbol, StructuralType.Beam);
ts.Commit();
return Result.Succeeded;
}
XYZ PointOnFace(PlanarFace face)
{
XYZ p = new XYZ(0, 0, 0);
Mesh mesh = face.Triangulate();
for (int i = 0; i < mesh.NumTriangles; ++i)
{
MeshTriangle triangle = mesh.get_Triangle(i);
p += triangle.get_Vertex(0);
p += triangle.get_Vertex(1);
p += triangle.get_Vertex(2);
p *= 0.3333333333333333;
break;
}
return p;
}
}
}