Revit二次开发——选集

article/2025/9/28 17:00:44

选集

  • 选集
  • 用户选集
  • 过滤的用户选集


选集

选择图元后运行外部命令获取选择的内容

Revit API中定义了单选、多选、框选等方式的用户选集,用户可以十分方便的使用鼠标和键盘完成这三种方式的图元选择。Revit API根据三种用户选集各自的特点,封装了多种实现的重载。

using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;namespace HelloRevitSelection
{[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]public class SelectionElementSetCmd : IExternalCommand{public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){try{UIDocument uidoc = commandData.Application.ActiveUIDocument;Selection selection = uidoc.Selection;ICollection<ElementId> selectSet = selection.GetElementIds();if (0 == selectSet.Count){TaskDialog.Show("选集", "没有选择任何元素.");}else{string info = "当前文档的用户选集的元素Id为: ";foreach (ElementId id in selectSet){info += "\n\t" + id.IntegerValue;}TaskDialog.Show("选集", info);}return Autodesk.Revit.UI.Result.Succeeded;}catch(Exception ex){message = ex.Message;return Autodesk.Revit.UI.Result.Failed;}}}
}

用户选集

Selection类还有一些允许用户选择新对象,甚至屏幕上一个点的方法。这让用户可以使用光标选择一 个或多个图元(或其他对象,如边或面),然后将控制返回给应用程序。这些功能并不自动向活动选择集添加新的选择。
●PickObject( )方法提示用户选择一个Revit模型中的对象。
●PickObjects( )方法提示用户选择多个Revit模型中的对象。
●PickElementsByRectangle( )方法提示用户用矩形选择多个Revit模型中的对象。
●PickPoint( )方法提示用户在活动草图平面内拾取一个点。
PickBox( )方法调用一个通用的双击编辑器,让用户在屏幕上指定一个矩形区域。调用PickObject( )或PickObjcts( )时即指定了需选对象的类型。可指定的对象类型有图元和图元上的点、边或面。每个Pick函数都可被重载,重载时可以带一个字符串参数,该参数用于定制状态栏消息


public Reference PickObject(ObjectType objectType);
public Reference PickObject(ObjectType objectType, string statusPrompt);
public Reference PickObject(ObjectType objectType, ISelectionFilter selectionFilter);
public Reference PickObject(ObjectType objectType, ISelectionFilter selectionFilter, string statusPrompt);
public IList<Reference> PickObjects(ObjectType objectType, string statusPrompt);
public IList<Reference> PickObjects(ObjectType objectType, ISelectionFilter selectionFilter);
public IList<Reference> PickObjects(ObjectType objectType, ISelectionFilter selectionFilter, string statusPrompt);
public IList<Reference> PickObjects(ObjectType objectType, ISelectionFilter selectionFilter, string statusPrompt, IList<Reference> pPreSelected);
public IList<Reference> PickObjects(ObjectType objectType);
public XYZ PickPoint();
public XYZ PickPoint(ObjectSnapTypes snapSettings);
public XYZ PickPoint(string statusPrompt);
public XYZ PickPoint(ObjectSnapTypes snapSettings, string statusPrompt);

单选

using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;namespace HelloRevitSelection
{[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]public class SinglePickCmd : IExternalCommand{public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){try{UIDocument uidoc = commandData.Application.ActiveUIDocument;Document doc = uidoc.Document;Selection selection = uidoc.Selection;//在revit当前文档下,进行选择元素的操作Reference eleRef = selection.PickObject(ObjectType.Element);// 对用户单选有效性进行验证if (null != eleRef && ElementId.InvalidElementId != eleRef.ElementId){//获取直接选择的这个元素Element element = doc.GetElement(eleRef.ElementId);TaskDialog.Show("单选", string.Format("图元:{0}\nID:{1}\n 坐标:{2}", element.Name, element.Id, eleRef.GlobalPoint));}return Autodesk.Revit.UI.Result.Succeeded;}catch(Exception ex){message = ex.Message;return Autodesk.Revit.UI.Result.Failed;}}}}

点选获取坐标

public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){try{UIDocument uidoc = commandData.Application.ActiveUIDocument;Document doc = uidoc.Document;Selection selection = uidoc.Selection;string msg = string.Empty;//点选获取坐标XYZ xyz = selection.PickPoint();msg = xyz.ToString();TaskDialog.Show(this.GetType().Name, msg);//AnnotationSymbolreturn Result.Succeeded;}catch (Exception ex){message = ex.Message;return Result.Failed;}}

ObjectType
在这里插入图片描述

过滤的用户选集

PickObject()、PickObjects( )和PickElementsByRectangle( )都有一一 个以ISelectionFilter作为参数的重载。ISelectionFilter是一一个接口, 在选集操作期间,可用此接口实现过滤对象。它有两个可以重载的方法: AllowElement()用 于指定是否允许选择某个图元,AllowReference( )用于指定是否允许选择对某个几何体的参照。

ISelectionFilter接口

using Autodesk.Revit.DB;namespace Autodesk.Revit.UI.Selection
{public interface ISelectionFilter{bool AllowElement(DB.Element elem);bool AllowReference(Reference reference, XYZ position);}
}

一个例子·

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;namespace DemoFilterUserSelections
{[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]public class DemoFilterSelection : IExternalCommand{public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){try{//句柄UIDocument uidoc = commandData.Application.ActiveUIDocument;//文档Document doc = uidoc.Document;//获取选择的图元集合Selection selection = uidoc.Selection;string msg = string.Empty;ISelectionFilter wallFilter = new WallSelectionFilter();//获取用户选集IList<Reference> eleRefs = selection.PickObjects(ObjectType.Element,wallFilter);if (0 == eleRefs.Count){TaskDialog.Show("过滤的用户选集", "用户没有选择图元");}else{foreach (Reference eleRef in eleRefs){//获取这个元素Element element = doc.GetElement(eleRef.ElementId);msg += "图元:" + element.Name + "ID:" + element.Id + "坐标:" + eleRef.GlobalPoint+"\n";}TaskDialog.Show("过滤的用户选集", msg);}return Autodesk.Revit.UI.Result.Succeeded;}catch (Exception ex){message = ex.Message;return Autodesk.Revit.UI.Result.Failed;}}}//新建一个WallSelectionFilter类,实现ISelectionFilter接口public class WallSelectionFilter : ISelectionFilter{//方法:允许什么样的元素被选择public bool AllowElement(Element elem){return elem is Wall;}//方法:否允许选择对某个几何体的参照,即是否允许引用public bool AllowReference(Reference reference, XYZ position){return false;  //设置为不允许}}}

SDK的例子

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using Autodesk.Revit;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;namespace Revit.SDK.Samples.Selections.CS
{/// <summary>/// A default filter./// All objects are allowed to be picked./// </summary>public class DefaultElementsFilter : ISelectionFilter{/// <summary>/// Allow all the element to be selected/// </summary>/// <param name="element">A candidate element in selection operation.</param>/// <returns>Return true to allow the user to select this candidate element.</returns>public bool AllowElement(Element element){return true;}/// <summary>/// Allow all the reference to be selected/// </summary>/// <param name="refer">A candidate reference in selection operation.</param>/// <param name="point">The 3D position of the mouse on the candidate reference.</param>/// <returns>Return true to allow the user to select this candidate reference.</returns>public bool AllowReference(Reference refer, XYZ point){return true;}}/// <summary>/// A Filter for Wall Face./// Only wall faces are allowed to be picked./// </summary>public class WallFaceFilter : ISelectionFilter{// Revit document.Document m_doc = null;/// <summary>/// Constructor the filter and initialize the document./// </summary>/// <param name="doc">The document.</param>public WallFaceFilter(Document doc){m_doc = doc;}/// <summary>/// Allow wall to be selected/// </summary>/// <param name="element">A candidate element in selection operation.</param>/// <returns>Return true for wall. Return false for non wall element.</returns>public bool AllowElement(Element element){return element is Wall;}/// <summary>/// Allow face reference to be selected/// </summary>/// <param name="refer">A candidate reference in selection operation.</param>/// <param name="point">The 3D position of the mouse on the candidate reference.</param>/// <returns>Return true for face reference. Return false for non face reference.</returns>public bool AllowReference(Reference refer, XYZ point){GeometryObject geoObject = m_doc.GetElement(refer).GetGeometryObjectFromReference(refer);return geoObject != null && geoObject is Face;}}/// <summary>/// A Filter for planar face./// Only planar faces are allowed to be picked./// </summary>public class PlanarFaceFilter : ISelectionFilter{// Revit document.Document m_doc = null;/// <summary>/// Constructor the filter and initialize the document./// </summary>/// <param name="doc">The document.</param>public PlanarFaceFilter(Document doc){m_doc = doc;}/// <summary>/// Allow all the element to be selected/// </summary>/// <param name="element">A candidate element in selection operation.</param>/// <returns>Return true to allow the user to select this candidate element.</returns>public bool AllowElement(Element element){return true;}/// <summary>/// Allow planar face reference to be selected/// </summary>/// <param name="refer">A candidate reference in selection operation.</param>/// <param name="point">The 3D position of the mouse on the candidate reference.</param>/// <returns>Return true for planar face reference. Return false for non planar face reference.</returns>public bool AllowReference(Reference refer, XYZ point){GeometryObject geoObject = m_doc.GetElement(refer).GetGeometryObjectFromReference(refer);return geoObject != null && geoObject is PlanarFace;}}
}
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using Autodesk.Revit;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Reference = Autodesk.Revit.DB.Reference;
using Exceptions = Autodesk.Revit.Exceptions;namespace Revit.SDK.Samples.Selections.CS
{/// <summary>/// A enum class for specific selection type./// </summary>public enum SelectionType{/// <summary>/// type for select element./// </summary>Element,/// <summary>/// type for select face./// </summary>Face,/// <summary>/// type for select edge./// </summary>Edge,/// <summary>/// type for select point./// </summary>Point}/// <summary>/// A class for object selection and storage./// </summary>public class SelectionManager{/// <summary>/// To store a reference to the commandData./// </summary>ExternalCommandData m_commandData;/// <summary>/// store the application/// </summary>UIApplication m_application;/// <summary>/// store the document/// </summary>UIDocument m_document;/// <summary>/// For basic creation./// </summary>Autodesk.Revit.Creation.ItemFactoryBase m_CreationBase;/// <summary>/// The picked point of element./// </summary>XYZ m_elemPickedPoint;SelectionType m_selectionType = SelectionType.Element;/// <summary>/// For specific selection type./// </summary>public SelectionType SelectionType{get { return m_selectionType; }set { m_selectionType = value; }}Element m_selectedElement;/// <summary>/// Store the selected element./// </summary>public Element SelectedElement{get{ return m_selectedElement;}set { m_selectedElement = value; }}XYZ m_selectedPoint;/// <summary>/// Store the selected point. /// When the point is picked, move the element to the point./// </summary>public XYZ SelectedPoint{get { return m_selectedPoint; }set { m_selectedPoint = value; if (m_selectedElement != null && m_selectedPoint != null){MoveElement(m_selectedElement, m_selectedPoint);}}}       /// <summary>/// constructor of SelectionManager/// </summary>/// <param name="commandData"></param>public SelectionManager(ExternalCommandData commandData){m_commandData = commandData;m_application = m_commandData.Application;m_document = m_application.ActiveUIDocument;if (m_document.Document.IsFamilyDocument){m_CreationBase = m_document.Document.FamilyCreate;}else{m_CreationBase = m_document.Document.Create;}}/// <summary>/// Select objects according to the selection type./// </summary>public void SelectObjects(){switch (m_selectionType){case SelectionType.Element:PickElement(); // pick elementbreak;case SelectionType.Face:break;case SelectionType.Edge:break;case SelectionType.Point:PickPoint(); // pick pointbreak;}}/// <summary>/// Pick the element from UI./// </summary>internal void PickElement(){try{// Pick an element.Reference eRef = m_document.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Please pick an element.");if (eRef != null && eRef.ElementId != ElementId.InvalidElementId){SelectedElement = m_document.Document.GetElement(eRef);m_elemPickedPoint = eRef.GlobalPoint;}}catch (Exceptions.OperationCanceledException){// Element selection cancelled.SelectedElement = null;}}/// <summary>/// Pick the point from UI./// </summary>internal void PickPoint(){try{// Pick a point.XYZ targetPoint = m_document.Selection.PickPoint("Please pick a point.");SelectedPoint = targetPoint;}catch (Exceptions.OperationCanceledException){// Point selection cancelled.SelectedPoint = null;}}/// <summary>/// Move an element to the point./// </summary>/// <param name="elem">The element to be moved.</param>/// <param name="targetPoint">The location element to be moved.</param>internal void MoveElement(Element elem, XYZ targetPoint){XYZ vecToMove = targetPoint - m_elemPickedPoint;m_elemPickedPoint = targetPoint;ElementTransformUtils.MoveElement(m_document.Document,elem.Id, vecToMove);}}
}

http://chatgpt.dhexx.cn/article/34YReSwy.shtml

相关文章

Revit二次开发——族的基础

族 族族编辑族实例 族 Revit将族分为系统族、内建族、标准构件族。 系统族&#xff1a;系统族是在 Autodesk Revit 中预定义的族&#xff0c;包含基本建筑构件&#xff0c;例如墙、窗和门。例如&#xff1a;基本墙系统族包含定义内墙、外墙、基础墙、常规墙和隔断墙样式的墙类…

Revit二次开发-CurveLoop的闭合处理

在Revit二次开发中&#xff0c;生成solid体块的时候&#xff0c;截面一般使用的CurveLoop这个数据结构。 笔者在使用CurveLoop的使用&#xff0c;给它Append某个几何的时候&#xff0c;老出异常捕获&#xff1a; XXXXXX. 英文不打了&#xff0c;就是不连续的意思。 查阅api发…

【Revit二次开发】族

族 族族实例族文档Revit API中族文档相关类族参数和族类型 族 Revit将族分为系统族、内建族、标准构件族。 Autodesk revit中的所有图元都是基于族的。 系统族&#xff1a;系统族是在 Autodesk Revit 中预定义的族&#xff0c;包含基本建筑构件&#xff0c;例如墙、窗和门。例…

Revit二次开发放样融合CreateSweptBlendGeometry

生成solid的放样融合接口为CreateSweptBlendGeometry&#xff0c;参数描述如下&#xff1a; 1&#xff09;Curve pathCurve&#xff1a;路径只要一个线 2&#xff09; IList<double> pathParams&#xff1a;数组大小和截面数组一样&#xff0c;作用是定位截面数组每个截…

Revit二次开发入门

Revit二次开发入门 订购并安装Autodesk Revit安装Visual Studio安装Revit AddIn-Manager 安装步骤&#xff1a; 找到安装Revit解压后的文件夹&#xff0c;找到\Utilities\SDK目录&#xff0c;并运行RevitSDK.exe&#xff1b; 指定安装目录&#xff08;安装步骤实际上只是把一个…

Revit二次开发01-开发环境的配置(Revit2018+Visual Studio 2015)

1.软件简介 1.1 Revit 2018 SDK Revit SDK包含RevitAPI的帮助文档以及带源码的案例&#xff0c;Revit SDK需要与Revit的版本保持一致。Revit SDK中的Add-In Manager是Revit的官方插件&#xff0c;用来加载revit的插件&#xff0c;RevitAPI.chm是RevitAPI的帮助文档。 1.2 Revi…

Revit二次开发有几种方式?做Revit二次开发的必看!

Revit二次开发有三种方式&#xff1a;外部命令&#xff08;IExternalCommand&#xff09;、外部应用(IExternalApplication)和宏(Macro)。下面将简要说明外部应用、宏的开发过程&#xff0c;并着重说明外部命令的开发过程。 1.宏&#xff08;Macro&#xff09; 宏是基于RevitAP…

Revit二次开发案例一(第1讲)

这期起&#xff0c;将为大家带来二次开发的小案例讲解&#xff0c;通过借助Revit建模与二次开发&#xff0c;帮助大家快速提高开发水平&#xff0c;快快参加吧&#xff01;&#xff01;&#xff01; 族实例是通过族类型创建的&#xff0c;而创建族实例是Revit二次开发尤其重要…

【REVIT二次开发教程】01-“你好,Revit”

本教程全程采用VS2022&#xff0c;Revit2022。 1.新建项目 打开VS2022&#xff0c;创建新项目&#xff0c;选择项目类型为C#&#xff0c;Windows&#xff0c;然后点击下一步。 2.添加引用 选择右上角【解决方案管理器】中的【依赖项】&#xff0c;右键点击&#xff0c;选择【…

运放的差分放大电路

该放大器的传递函数为&#xff1a;    若R1 R3 且R2 R4&#xff0c;则公式 1 简化为&#xff1a; 应用电路&#xff1a; 电路一&#xff1a; 用运放做电流采样&#xff0c;再用单片机AD采集处理。 注&#xff1a; 1、Rp10、Rp11、Cp8、Cp9&#xff0c;是对输入做的RC…

模电(十四)差分放大电路

目录 差分放大电路 双端输入双端输出 Q点分析 抑制共模信号 放大差模信号 双端输入单端输出 Q点分析 差模信号分析 共模信号分析 单端输入双端输出 单端输入单端输出 四种接法比较 具有恒流源的差分放大电路 差分放大电路的改进 差分放大电路 采用引入支流负反馈&a…

差分放大电路基础

该放大器的传递函数为&#xff1a; 若R1 R3 且R2 R4&#xff0c;则公式 1 简化为&#xff1a; 应用电路&#xff1a; 电路一&#xff1a; 用运放做电流采样&#xff0c;再用单片机AD采集处理。 注&#xff1a; 1、Rp10、Rp11、Cp8、Cp9&#xff0c;是对输入做的RC滤波&…

6.深入浅出:差分放大电路——参考《模拟电子技术基础》清华大学华成英主讲

如果你想理解差分电路共模信号和差模信号&#xff0c;共模放大倍数&#xff0c;差模放大倍数&#xff0c;共模抑制比等&#xff0c;本文或许能给你比较系统的认知&#xff0c;主要包括&#xff1a; 什么是零点漂移&#xff1f; 引入差分电路 两个共射极放大电路对称布置&…

差分放大电路的构成(零点漂移、差分放大电路是怎么构成的、共模信号、差模信号)

差分放大电路的构成 直接耦合放大电路 零点漂移现象 输入短接&#xff0c;输入为零的时候&#xff0c;输出不是一条直线&#xff0c;就是交流量不是零。 一、产生原因 温漂&#xff0c;温度变化使静态工作点发生变化&#xff0c;产生零点漂移 二、抑制温漂的方法 加射极电阻…

干货 | 教你轻松掌握差分放大电路,看完这篇不踩坑

要想掌握差分放大电路&#xff0c;首先就要知道什么是差分放大电路以及它的作用。 差分放大电路是模拟集成运算放大器输入级所采用的的电路形式&#xff0c;差分放大电路是由对称的两个基本放大电路&#xff0c;通过射极公共电阻耦合构成的&#xff0c;对称的意思就是说两个三极…

全差分运算放大器浅析

全差分放大器(Fully-Differential)是一种应用在将单端信号转换为差分信号&#xff0c;或者将差分信号转换为差分信号的芯片。 全差分放大器的配置特点&#xff0c;就是全对称匹配。即两侧输入阻抗配置完全一致&#xff08;阻抗包括源内阻&#xff09;&#xff0c;反馈配置完全…

运算放大器——4种基本运放电路(同相放大、反相放大、加法器、差分放大电路)实际设计中需要考虑的实际问题

运算放大器——4种基本运放电路(同相放大、反相放大、加法器、差分放大电路)在实际设计中需要考虑的实际问题 前言 第一篇博客就从运放入手吧&#xff0c;话不多说。正文开始&#xff1a;想必大家对运放电路都熟悉的再不能熟悉了。可是这里为什么又再拿出来写呢&#xff1f;肯…

高压电压采样之差分放大电路

在高压电压采样方案研究一文中提到&#xff0c;可以使用差分放大电路进行高压电压采样。如下图&#xff1a;   该方案有缺陷&#xff0c;如下。整车车身与12V电池负极连接到一起&#xff0c;同属于低压电路&#xff0c;我们可以称之为大地。没有绝对的绝缘体&#xff0c;电池…

电子电路:差分放大器分析

1. 差分放大器与共模抑制比 1.1差分放大器 差分放大器有两个输入端和一个输出端&#xff0c;它可以获取两个输入电压之间的差值&#xff0c;并将这个差值放大后送到输出端&#xff0c;差分放大器的模型如下&#xff1a; 输入电压 v i 1 , v i 2 v_{i1}, v_{i2} vi1​,vi2​…

常规放大电路和差分放大电路

常规放大电路和差分放大电路 0、小叙闲言 有一个两相四线的步进电机&#xff0c;需测量其A、B两相的电流大小&#xff0c;电机线圈的电阻为0.6Ω&#xff0c;电感为2.2mH。打算在A、B相各串接一个0.1Ω的采样电阻&#xff0c;然后通过放大电路&#xff0c;送到单片机采样&…