当前位置:网站首页>Revit secondary development Hof method calls transaction

Revit secondary development Hof method calls transaction

2022-07-06 08:44:00 Imkc

Look again 《C# Functional programming 》 When I read a book HOF The concept of , I feel that this method can be used to sort out an expansion method in the process of daily development , Realize the rapid call of the program .

  1. Transaction Function inherited from IDisposable
  2. We usually need to use using Inside to Revit To operate . Realize automatic disassembly
  3. We need to create a generic function MTransaction And bound in IDisposable Interface
  4. Implement inside the above function using Keywords reduce repeated calls inside other functions , Reduce the amount of code and reduce coupling
  5. Create a function TransactionHelper This amplified reference encapsulates , Call intermediate functions through a third party to complete the call

Function structure

public static class MTransaction
{
    public static void Using<TDisp>(TDisp disposable, Action<TDisp> f) where TDisp : IDisposable
    {
        using (disposable)
        {
             f(disposable);
        }
    }
}

public static class TransactionHelper
{
    public static void Execute(Document doc, Action<IDisposable> f)
        => MTransaction.Using(new Transaction(doc, "create"), trans =>
        {
            trans.Start();
            f(trans);
            trans.Commit();
        });
}

Call this method

 TransactionHelper.Execute(doc, c =>
            {
                var line = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(2000 / 304.8, 2000 / 304.8, 0));
                Wall w = Wall.Create(doc, line, new ElementId(311), false);
            });
原网站

版权声明
本文为[Imkc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060837473850.html