當導入或者鏈接CAD文件之後,一般我們在Revit裏面選中該CAD,點擊"導入實例”的“查詢”,選中某個圖元,就會彈出圖層信息
那麼API如何實現此功能呢?
本人剛開始並沒有找到如何做到,後來偶然機會發現Line/GeometryObject類有個屬性叫GraphicsStyleId,試了一下,答案就出來了。
那就是,
- 使用Selection.PickObject(s)來選擇鏈接文件裏的對象獲取到Reference
- 然後使用Element.GetGeometryObjectFromReference(Reference)獲取幾何信息,該幾何信息是GeometryObject類,於是通過GraphicsStyleId就可以獲取到GraphicsStyle,
- 最後通過GraphicsStyleId的GraphicsStyleCategory屬性獲得類別Category,Category.Name即是圖層的名稱
var doc = commandData.Application.ActiveUIDocument.Document;
var uiSel = commandData.Application.ActiveUIDocument.Selection;
try
{
var reference = uiSel.PickObject(ObjectType.PointOnElement, "statusPrompt");
var element = doc.GetElement(reference);
var geoObj = element.GetGeometryObjectFromReference(reference);
if (geoObj.GraphicsStyleId != ElementId.InvalidElementId)
{
GraphicsStyle gs = doc.GetElement(geoObj.GraphicsStyleId) as GraphicsStyle;
if (gs != null)
{
var name = gs.GraphicsStyleCategory.Name;
}
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
}