Revit 二次开发之“交互操作-得到选择的对象”

进入选择对象状态使用Selection.PickObject();函数。
异常处理有固定格式。
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class GetSelect : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        
try
        {
            
if (null == commandData)
            {
                
throw new ArgumentNullException("commandData");
            }

            UIApplication uiApp 
= commandData.Application;
            Document doc 
= uiApp.ActiveUIDocument.Document;
            Selection sel 
= uiApp.ActiveUIDocument.Selection;

            Reference refelem 
= null;//类似C#中的Object基类
            IList<Duct> ducts = new List<Duct>();
            IList
<XYZ> xyzs = new List<XYZ>();

            
for (int i = 1; i < 5; i++)
            {
                
//没有提示文字
                refelem = sel.PickObject(ObjectType.Element, "请选择第 " + i.ToString() + " 个对象");
                
if (refelem.Element is Duct)
                {
                    ducts.Add(refelem.Element 
as Duct);
                    xyzs.Add(refelem.GlobalPoint);
                    MessageBox.Show(
"选择了一个风管");
                }
                
else
                {
                    MessageBox.Show(
"请选择风管");
                }
            }
            MessageBox.Show(
"你选择了" + ducts.Count + "个风管");

        }
        
catch (Exception e)
        {
            messages 
= e.Message;
            
return Result.Failed;
        }

        
return Result.Succeeded;
    }
}
end
posted @ 2011-03-18 08:46  大气象  阅读(2157)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com