【Revit】使用PickObect拾取CAD的线的层信息
1、第一步使用PickObject以及筛选器CADPickObjectFilter 获取拾取的线的Reference
Reference r = selection.PickObject(ObjectType.PointOnElement, new CADPickObjectFilter(_doc));
筛选器代码:
class CADPickObjectFilter : ISelectionFilter { private Document _doc; public CADPickObjectFilter(Document doc) { _doc = doc; } public bool AllowElement(Element elem) { if (elem is ImportInstance) return true; else return false; } public bool AllowReference(Reference reference, XYZ position) { var elem = _doc.GetElement(reference); if (elem is ImportInstance) { var geo = elem.GetGeometryObjectFromReference(reference); if (geo != null && geo is GeometryObject && geo.GraphicsStyleId.IntegerValue != -1) { return true; } else { return false; } } else return false; } }
第二步,通过Reference获取信息
protected GeometryElement _geoelem = null; protected GeometryObject _geoobj = null; protected Transform _transform; protected ImportInstance _import; protected string _filename; var elem = _doc.GetElement(r); var import = elem as ImportInstance; if (import != null) { var cadlinkType = _doc.GetElement(elem.GetTypeId()); if (!cadlinkType.IsExternalFileReference()) { MessageBox.Show("请将导入CAD改为链接CAD方式。"); return; } _filename = ModelPathUtils.ConvertModelPathToUserVisiblePath(cadlinkType.GetExternalFileReference().GetAbsolutePath()); _import = import; _transform = _import.GetTransform(); } _geoelem = elem.get_Geometry(new Options()); _geoobj = elem.GetGeometryObjectFromReference(r); if (_geoobj.GraphicsStyleId != ElementId.InvalidElementId) { var graphicsStyleId = _geoobj.GraphicsStyleId; GraphicsStyle gs = _doc.GetElement(graphicsStyleId) as GraphicsStyle; if (gs != null) { var category = gs.GraphicsStyleCategory;//CAD的层 } }