思路:擷取目前文檔doc,1.先拿到目前模型中的項目原點坐标;2.再去那連結模型中項目原點坐标
using Autodesk.Revit.Attributes;
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestCoordinates
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiDoc = commandData.Application.ActiveUIDocument;
var doc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);//拿到檔案doc
//string mainModelName = doc.Title;//
//var collectormain =collector.OfCategory(BuiltInCategory.OST_ProjectBasePoint).OfClass(typeof(BasePoint));
//List<Element> MainProjectBasePoints = collectormain.ToElements().ToList();
//BasePoint MainProjectBasePoint = MainProjectBasePoints.FirstOrDefault() as BasePoint;
ProjectPosition projectPosition = MainProjectBasePoint.get_Parameter(BuiltInParameter position);
//foreach (var e in MainProjectBasePoints)
//{
// BasePoint basePoint = e as BasePoint;
// XYZ p = basePoint.Position;
// double x = p.X * 304.8;
// string x1 = string.Format("{0:F2}", x);
// double newX = Convert.ToDouble(x1);
// double y = p.Y * 304.8;
// string y1 = string.Format("{0:F2}", y);
// double newY = Convert.ToDouble(y1);
// double z = p.Z * 304.8;
// string z1 = string.Format("{0:F2}", z);
// double newZ = Convert.ToDouble(z1);
// XYZ newP = new XYZ(newX, newY, newZ);
//}
//過濾出含連結執行個體的收集器
var collectorlink =collector.OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(typeof(RevitLinkInstance));
List<Element> RevitLinkInstances = collectorlink.ToElements().ToList();
foreach (var e in RevitLinkInstances)
{
//拿連結模型的文檔Document
RevitLinkInstance revitLinkInstance = (RevitLinkInstance)e;
Document linkdoc = revitLinkInstance.GetLinkDocument();
string linkdocname = linkdoc.Title;
//拿項目基點(這個隻有一個)
FilteredElementCollector BPcollector = new FilteredElementCollector(linkdoc);
BPcollector.OfCategory(BuiltInCategory.OST_ProjectBasePoint).OfClass(typeof(BasePoint));
List<Element> ProjectBasePoints = BPcollector.ToElements().ToList();
foreach (var item in ProjectBasePoints)
{
BasePoint linkbasePoint = item as BasePoint;
XYZ linkp = linkbasePoint.Position;
double x = linkp.X * 304.8;
string x1 = string.Format("{0:F2}", x);
double newX = Convert.ToDouble(x1);
double y = linkp.Y * 304.8;
string y1 = string.Format("{0:F2}", y);
double newY = Convert.ToDouble(y1);
double z = linkp.Z * 304.8;
string z1 = string.Format("{0:F2}", z);
double newZ = Convert.ToDouble(z1);
TaskDialog.Show("測試", $"選擇的連結項目基點是({x1},{y1},{z1}),項目名稱{linkdocname}");
}
//BasePoint ProjectBasePoint = ProjectBasePoints.FirstOrDefault() as BasePoint;
//Coordinates = ProjectBasePoint.ToString();
}
return Result.Succeeded;
}
}
}