1. 程式人生 > >Revit開發:獲取在Revit上拾取圖紙圖塊名稱

Revit開發:獲取在Revit上拾取圖紙圖塊名稱

直接獲取沒法得,通過間接臨時事務生成模型得到

public static string GetBlockReferenceName(ImportInstance importInstance, Reference reference)
        {
            var doc = RvtApp.Document;
            string name = null;
            GeometryObject go = importInstance.GetGeometryObjectFromReference(reference);
            using (TransactionGroup tg = new TransactionGroup(doc))
            {
                tg.Start("Temp Trans");
                DirectShape ds = null;
                using (Transaction trans = new Transaction(doc))
                {
                    trans.Start("Temp DirectShape");
                    ds = DirectShape.CreateElement(doc,
                                          new ElementId(BuiltInCategory.OST_GenericModel), Guid.NewGuid().ToString(),
                                          Guid.NewGuid().ToString());
                    ds.AppendShape(new List<GeometryObject>() { go });
                    trans.Commit();
                }
                Options options = new Options
                {
                    ComputeReferences = true,
                    View = doc.ActiveView
                };
                var gi = ds.get_Geometry(options).FirstOrDefault(i => i is GeometryInstance) as GeometryInstance;
                name = gi?.Symbol?.Name;
                tg.RollBack();
            }
            return name;
        }