【发布时间】:2021-05-09 13:22:24
【问题描述】:
我正在尝试在我的代码中启动事务,但是正如您在标题中看到的那样,它会产生错误。我已经阅读了很多关于外部应用程序的帖子,但不知道它是如何工作的。
这是我用来启动交易的代码: 在我的表格中:
private void Loading_FA_only()//Lance l'import en boucle suivant les items dans la file d'attente
{
foreach(FileInfo fi in selected_rfas_donnees)
{
Loading_Family.famille = fi;
Loading_Family lf = new Loading_Family();
lf.Execute(Command.uiapplication);
Listing_succes(Loading_Family.succes, fi);
}
selected_rfas.Clear();
selected_rfas_donnees.Clear();
Update_Compteur();
}
以及我尝试执行的操作:
public class Loading_Family : IExternalEventHandler
{
public static FileInfo famille;
public static bool succes;
public void Execute(UIApplication uiapp)
{
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Access current selection
Selection sel = uidoc.Selection;
// Retrieve elements from database
FilteredElementCollector col
= new FilteredElementCollector(doc)
.WhereElementIsNotElementType()
.OfCategory(BuiltInCategory.INVALID)
.OfClass(typeof(Wall));
// Filtered element collector is iterable
foreach (Element e in col)
{
Debug.Print(e.Name);
}
// Modify document within a transaction
succes = false;
string nom_famille = famille.Name.Remove(famille.Name.Length - 4, 4);
FilteredElementCollector familles_doc = new FilteredElementCollector(doc).OfClass(typeof(Family));
foreach (Element elmt in familles_doc)
{
if (elmt.Name == nom_famille)
{
var result = TaskDialog.Show("CPF - Importation", "Il semblerait que " + nom_famille + " soit déjà présent dans votre projet.\nVoullez-vous le remplacer ?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
if (result == TaskDialogResult.Yes)
{
using (Transaction tr = new Transaction(doc, "Importer la famille"))
{
tr.Start();
doc.LoadFamily(famille.FullName);
tr.Commit();
tr.Dispose();
}
}
}
else
{
using (Transaction tr = new Transaction(doc, "Importer la famille"))
{
tr.Start();
doc.LoadFamily(famille.FullName);
tr.Commit();
tr.Dispose();
}
}
}
familles_doc = new FilteredElementCollector(doc);
foreach (Element elmt in familles_doc)
{
if (elmt.Name == nom_famille) { succes = true; }
else { succes = false; }
}
using (Transaction tx = new Transaction(doc))
{
tx.Start("Transaction Name");
tx.Commit();
}
}
public string GetName()
{
return "my event";
}
}
我对此感到绝望。我绝对不知道他们的“ExternalEventHandler”或“ExternalApplication”是如何工作的。 感谢您的帮助:)
【问题讨论】: