应用程序:Selections
Revit 平台:全部
Revit 版本:2011.0
首次发布于:2011.0
编程语言:C#
技能水平:初学者
类别:选择
类型:外部命令
主题:Selections
摘要:本示例将演示如何执行选择操作。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.Document
Autodesk.Revit.DB.Element
Autodesk.Revit.DB.Reference
Autodesk.Revit.UI.Selection.Selection
Autodesk.Revit.UI.Selection.ObjectType
Autodesk.Revit.UI.Selection.ISelectionFilter
Autodesk.Revit.UI.Selection.ObjectSnapTypes
Autodesk.Revit.Exceptions.OperationCanceledException
项目文件:
Command.cs
该文件包含四个类,它们实现了 IExternalCommand 接口。
PickforDeletion 命令允许选择一些元素,然后从文档中删除这些元素。
PlaceAtPointOnWallFace 命令允许在墙面上选择一个点,然后在该点上放置一个固定尺寸为 36" x 48" 的窗户。
PlaceAtPickedFaceWorkplane 命令允许选择一个面,并在其上设置工作平面,然后在工作平面上选择一个点作为圆的中心。
SelectionDialog 命令允许从对话框中选择一个元素和一个点。选择点后,该元素将移动到所选择的点上。
SelectionFilters.cs
该文件包含三个类,它们实现了 ISelectionFilter 接口。
DefaultElementsFilter 是允许选择所有对象的过滤器。
WallFaceFilter 是一个过滤器,允许仅选择墙面。
PlanarFaceFilter 是一个过滤器,允许仅选择平面面。
SelectionManager.cs
该文件包含两个类。
SelectionType 用于从对话框中选择特定的选择类型。
SelectionManager 执行对象选择和存储选项卡的工作。
SelectionForm.cs
该文件包含一个 SelectionForm 类,该类提供了一个选择对象的对话框。
描述:
该示例利用 Selection 类的 PickObject 和 PickObjects 方法来选择一个和多个对象。它还利用 Selection 类的 PickPoint 方法来选择点。
实现接口 ISelectionFilter 对元素和对象引用进行过滤,并在选择时使用。
说明:
1.启动 Revit。
2.为了使用这些命令,用户应该打开目录中的“SelectionsSample.rvt”文件,或者手动创建一个已加载 36" x 48" 窗户类型的项目文档。如果已打开的文档不包含指定类型的窗户,则 PlaceAtPointOnWallFace 命令会失败。
3.从外部命令菜单启动“选择要删除的项”。选择要在文档中删除的一些元素,这些选定的元素将被删除。
4.从外部命令菜单启动“在墙面点放置”。在墙面上选择一个点,然后会在所选择的位置放置一个 36" x 48" 的窗户。
5.从外部命令菜单启动“在选择的面工作平面上放置”。选择一个柱子的面;在所选择的面上创建一个新的工作平面,然后在该平面上选择一个点,将创建一个以该点为中心的圆。
6.从外部命令菜单启动“选择对话框”。一个名为“选择”的对话框显示。点击“选择元素”按钮。选择文档中的一个元素。对话框再次显示。点击“移动到”按钮。选择文档中的一个点,先前选定的元素将移动到所选位置。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
SelectionFilters.cs
//
// (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;
}
}
}
SelectionManager.cs
//
// (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 element
break;
case SelectionType.Face:
break;
case SelectionType.Edge:
break;
case SelectionType.Point:
PickPoint(); // pick point
break;
}
}
/// <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);
}
}
}