原文链接:
Remove Imported JPG and BMP Images
这个主题可以帮助我们更好地理解Revit是如何处理导入的文件,以及如何通过API删除导入的文件。
提问:
我希望在Revit模型中删除导入的JPG和BMP图片。我尝试使用 filtered emelent collector,应用 WhereElementIsNotElementType 方法,找到所有名称以 ".jpg" 结尾的元素,
最后在一个独立的循环中删除它们。但是我的代码始终无法实现目标。
回答:
你的方法确实无法工作。我做了一些测试,发现症结在于你必须首先删除 non-ElementType 元素,然后才能删除对应的 ElementType 元素。
以下是我的完成代码:
为了测试这段代码,我新建了一个Revit模型并且插入一个图片:jeremy_philippe_partha_jim.jpg。
测试日志如下:
这表明:Revit 为每个插入的图片创建两个 ElementType 对象和一个 non-ElementType 对象。而且这些对象的 ElementId 是连续的整数。
包含这个新例程的 Building Coder Samples 可以在此下载: version 2012.0.97.0
这个主题可以帮助我们更好地理解Revit是如何处理导入的文件,以及如何通过API删除导入的文件。
提问:
我希望在Revit模型中删除导入的JPG和BMP图片。我尝试使用 filtered emelent collector,应用 WhereElementIsNotElementType 方法,找到所有名称以 ".jpg" 结尾的元素,
最后在一个独立的循环中删除它们。但是我的代码始终无法实现目标。
回答:
你的方法确实无法工作。我做了一些测试,发现症结在于你必须首先删除 non-ElementType 元素,然后才能删除对应的 ElementType 元素。
以下是我的完成代码:
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector col = new FilteredElementCollector( doc ).WhereElementIsNotElementType();
List<ElementId> ids = new List<ElementId>();
foreach( Element e in col )
{
if( ElementNameEndsWithJpg( e ) )
{
Debug.Print( Util.ElementDescription( e ) );
ids.Add( e.Id );
}
}
ICollection<ElementId> idsDeleted = null;
Transaction t;
int n = ids.Count;
if( 0 < n )
{
using( t = new Transaction( doc ) )
{
t.Start( "Delete non-ElementType '.jpg' elements" );
idsDeleted = doc.Delete( ids );
t.Commit();
}
}
int m = ( null == idsDeleted ) ? 0 : idsDeleted.Count;
Debug.Print( string.Format("Selected {0} non-ElementType element{1}, " + "{2} successfully deleted.", n, Util.PluralSuffix( n ), m ) );
col = new FilteredElementCollector( doc ).WhereElementIsElementType();
ids.Clear();
foreach( Element e in col )
{
if( ElementNameEndsWithJpg( e ) )
{
Debug.Print( Util.ElementDescription( e ) );
ids.Add( e.Id );
}
}
n = ids.Count;
if( 0 < n )
{
using( t = new Transaction( doc ) )
{
t.Start( "Delete element type '.jpg' elements" );
idsDeleted = doc.Delete( ids );
t.Commit();
}
}
m = ( null == idsDeleted ) ? 0 : idsDeleted.Count;
Debug.Print( string.Format("Selected {0} element type{1}, " + "{2} successfully deleted.", n, Util.PluralSuffix( n ), m ) );
return Result.Succeeded;
为了测试这段代码,我新建了一个Revit模型并且插入一个图片:jeremy_philippe_partha_jim.jpg。
测试日志如下:
Raster Images <161178 jeremy_philippe_partha_jim.jpg>
Selected 1 non-ElementType element, 1 successfully deleted.
ElementType <161176 jeremy_philippe_partha_jim.jpg>
Raster Images <161177 jeremy_philippe_partha_jim.jpg>
Selected 2 element types, 2 successfully deleted.
这表明:Revit 为每个插入的图片创建两个 ElementType 对象和一个 non-ElementType 对象。而且这些对象的 ElementId 是连续的整数。
包含这个新例程的 Building Coder Samples 可以在此下载: version 2012.0.97.0