Message 1 of 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am developing a revit plugin for the first time in C# for auto dimension setting between grid and duct/pipes... I got an error in which I cannot convert Autodesk.Revit.DB.Curve to Autodesk.Revit.DB.XYZ
The code below:
u
{
[Transaction(TransactionMode.Manual)]
public class Dim : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
View activeView = doc.ActiveView;
FilteredElementCollector collector = new FilteredElementCollector(doc, activeView.Id);
var ductAndChwElements = collector.OfCategory(BuiltInCategory.OST_DuctCurves).UnionWith(collector.OfCategory(BuiltInCategory.OST_PipeCurves)).WhereElementIsNotElementType().ToElements();
var coreWalls = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType(); coreWalls.Where(e => e.Name.ToLower().Contains("Core"));
var gridLines = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType();
using (Transaction t = new Transaction(doc, "set out Dimensions"))
{
t.Start();
foreach (Element element in ductAndChwElements)
{
LocationCurve curve = element.Location as LocationCurve;
if (curve != null)
{
Reference nearestRef = FindNearestReference(doc, curve.Curve, coreWalls, gridLines);
if (nearestRef != null)
{
XYZ elementPoint = curve.Curve.GetEndPoint(0);
XYZ refPoint = nearestRef.GlobalPoint;
Line dimensionLine = Line.CreateBound(elementPoint, refPoint);
ReferenceArray ra = new ReferenceArray();
ra.Append(nearestRef);
ra.Append(new Reference(element));
doc.Create.NewDimension(activeView, dimensionLine, ra);
}
}
}
t.Commit();
}
return Result.Succeeded;
}
private Reference FindNearestReference(Document doc, Curve curve1,FilteredElementCollector coreWalls, FilteredElementCollector gridLines) {
Reference nearestRef = null;
double minDistance = double.MaxValue;
foreach (Element Wall in coreWalls)
{
LocationCurve wallCurve = Wall.Location as LocationCurve;
if (wallCurve != null)
{
double distance = curve1.Distance(wallCurve.Curve);
if (distance < minDistance)
{
nearestRef = new Reference(Wall);
minDistance = distance;
}
}
}
foreach (Element grid in gridLines)
{
Curve gridCurve = (grid.Location as LocationCurve).Curve;
if (gridCurve != null)
{
double distance = curve1.Distance(gridCurve);
if (distance < minDistance)
{
nearestRef = new Reference(grid);
minDistance = distance;
}
}
}
return nearestRef;
}
Solved! Go to Solution.