Revit 2011二次开发之得到选择的对象

start
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Document_Selection : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        
try
        {
            UIDocument uidoc 
= commandData.Application.ActiveUIDocument;
            
//得到选择的对象
            Selection selection = uidoc.Selection;
            ElementSet collection 
= selection.Elements;
            
if (0 == collection.Size)
            {
                TaskDialog.Show(
"Revit""You haven't selected any elements");
            }
            
else
            {
                
string info = "Ids of selected elements in the document are:";
                
foreach (Element elem in collection)
                {
                    info 
+= "\n\t" + elem.Id.IntegerValue;
                }
                TaskDialog.Show(
"Revit", info);
            }

            
//得到当前文档中门的ID
            ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
            ElementCategoryFilter doorsCategoryfilter 
= new ElementCategoryFilter(BuiltInCategory.OST_Doors);
            LogicalAndFilter doorsInstancefilter 
= new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter);
            FilteredElementCollector collector 
= new FilteredElementCollector(uidoc.Document);
            ICollection
<ElementId> doors = collector.WherePasses(doorsInstancefilter).ToElementIds();
            
string prompt = "the ids of the doors in the current document are:";
            
foreach (ElementId id in doors)
            {
                prompt 
+= "\n\t" + id.IntegerValue;
            }
            TaskDialog.Show(
"Revit", prompt);
        }
        
catch (Exception e)
        {
            messages 
= e.Message;
            
return Result.Failed;
        }
        
return Result.Succeeded;
    }
}
end
posted @ 2011-03-19 22:05  大气象  阅读(1436)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com