public class LoadClanInform : IExternalEventHandler
{
public string ClanPath { get; set; }
public FormGouJianWu mainForm { get; set; }
public void Execute(UIApplication app)
{
//File.AppendAllText(@"H:\abc.txt", "IExternalEventHandler " + DateTime.Now.ToString() + "\n");
Document doc = app.ActiveUIDocument.Document;
UIDocument uiDoc = app.ActiveUIDocument;
Family family = null;
// 过滤当前文档是否存在该族
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(Family));
IList<Element> CollectorList = collector.ToElements();
foreach (var elem in CollectorList)
{
var familyItem = elem as Family;
// str是要载入族的名称
if (familyItem.Name == System.IO.Path.GetFileNameWithoutExtension(ClanPath))
{
family = familyItem;
break;
}
}
// 如果未载入过,则载入该族
if (family == null)
{
using (Transaction tran = new Transaction(doc, "载入族"))
{
tran.Start();
// File.AppendAllText(@"H:\abc.txt", "family " + DateTime.Now.ToString() + "\n");
doc.LoadFamily(ClanPath, out family);
tran.Commit();
}
}
try
{
// 交互式放置
// File.AppendAllText(@"H:\abc.txt", "交互式放置 " + DateTime.Now.ToString() + "\n");
uiDoc.PromptForFamilyInstancePlacement(doc.GetElement(family.GetFamilySymbolIds().FirstOrDefault()) as FamilySymbol);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
{
mainForm.Show();
//Console.WriteLine(ex);
//File.AppendAllText(@"H:\abc.txt", ex + DateTime.Now.ToString() + "\n");
}
}
public string GetName()
{
return "";
}
}
下载源码