1)未加载到项目中的族文件,使用路径
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
FamilyListBoxMember member = (FamilyListBoxMember)listBox1.SelectedItem;
List<string> data = new List<string>();
data.Add(member.FullPath);
UIApplication.DoDragDrop(data);
}
}
其中FamilyListBoxMember的定义如下:
public class FamilyListBoxMember
{
public string FullPath;
public string Name;
public FamilyListBoxMember(string fullPath, string name)
{
FullPath = fullPath;
Name = name;
}
public override string ToString()
{
return Name;
}
}
2)使用族类型创建实例
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
ListViewItem selectedItem = listView1.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
if (selectedItem != null)
{
LoadedFamilyDropHandler myhandler = new LoadedFamilyDropHandler();
UIApplication.DoDragDrop(selectedItem.Tag,myhandler);
}
}
}
public class LoadedFamilyDropHandler : IDropHandler
{
public void Execute(UIDocument document, object data)
{
ElementId familySymbolId = (ElementId)data;
FamilySymbol symbol = document.Document.GetElement(familySymbolId) as FamilySymbol;
if (symbol != null)
{
document.PromptForFamilyInstancePlacement(symbol);
}
}
}
//赋值
FilteredElementCollector collector = new FilteredElementCollector(m_document).OfCategory(BuiltInCategory.OST_Furniture).OfClass(typeof(FamilySymbol));
foreach (FamilySymbol familySymbol in collector.Cast<FamilySymbol>())
{
ListViewItem item = new ListViewItem();
item.Tag = familySymbol.Id;
item.Text = familySymbol.Family.Name + "::" + familySymbol.Name;
Bitmap bitmap = familySymbol.GetPreviewImage(size);
if (bitmap != null)
{
imageList.Images.Add(bitmap);
int index = imageList.Images.Count - 1;
item.ImageIndex = index;
}
collection.Add(item);
}
通过拖拽创建实例的方法有以下两种:
// Drag action from list view
private void listView_MouseMove(object sender, MouseEventArgs e)
{
if (System.Windows.Forms.Control.MouseButtons == MouseButtons.Left)
{
ListViewItem selectedItem = this.listView1.SelectedItems.Cast<ListViewItem>().FirstOrDefault<ListViewItem>();
if (selectedItem != null)
{
// Use custom Revit drag and drop behavior
LoadedFamilyDropHandler myhanlder = new LoadedFamilyDropHandler();
UIApplication.DoDragDrop(selectedItem.Tag, myhanlder);
}
}
}
// Drag action from list box
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (System.Windows.Forms.Control.MouseButtons == MouseButtons.Left)
{
FamilyListBoxMember member = (FamilyListBoxMember)listBox1.SelectedItem;
// Use standard Revit drag and drop behavior
List<String> data = new List<String>();
data.Add(member.FullPath);
UIApplication.DoDragDrop(data);
}
}
public class LoadedFamilyDropHandler : IDropHandler
{
public void Execute(UIDocument document, object data)
{
ElementId familySymbolId = (ElementId)data;
FamilySymbol symbol = document.Document.GetElement(familySymbolId) as FamilySymbol;
if (symbol != null)
{
document.PromptForFamilyInstancePlacement(symbol);
}
}
}
详细的介绍可参照:UIAPI
https://blog.csdn.net/flower4wine/article/details/7554773