IndependentTag - 我如何在 Revit 中调用它?

IndependentTag - How do i call this in Revit?

我有一个功能区选项卡,其中包含面板和按钮,可用于调用仅用于测试的对话框。我现在正尝试从 Autodesk 的站点调用这段代码,它应该创建一个新的 IndependentTag,但是它不起作用。

[Transaction(TransactionMode.Manual)]
public class Tagtest : IExternalCommand
{
    #region Methods

    /// <summary>
    ///       The CreateIndependentTag
    /// </summary>
    /// <param name="document">The <see cref="Document" /></param>
    /// <param name="wall">The <see cref="Wall" /></param>
    /// <returns>The <see cref="IndependentTag" /></returns>
    public IndependentTag CreateIndependentTag(Document document, Wall wall)
    {
        TaskDialog.Show("Create Independent Tag Method", "Start Of Method Dialog");
        // make sure active view is not a 3D view
        var view = document.ActiveView;

        // define tag mode and tag orientation for new tag
        var tagMode = TagMode.TM_ADDBY_CATEGORY;
        var tagorn = TagOrientation.Horizontal;

        // Add the tag to the middle of the wall
        var wallLoc = wall.Location as LocationCurve;
        var wallStart = wallLoc.Curve.GetEndPoint(0);
        var wallEnd = wallLoc.Curve.GetEndPoint(1);
        var wallMid = wallLoc.Curve.Evaluate(0.5, true);
        var wallRef = new Reference(wall);

        var newTag = IndependentTag.Create(document, view.Id, wallRef, true, tagMode, tagorn, wallMid);
        if (null == newTag) throw new Exception("Create IndependentTag Failed.");

        // newTag.TagText is read-only, so we change the Type Mark type parameter to 
        // set the tag text.  The label parameter for the tag family determines
        // what type parameter is used for the tag text.

        var type = wall.WallType;

        var foundParameter = type.LookupParameter("Type Mark");
        var result = foundParameter.Set("Hello");

        // set leader mode free
        // otherwise leader end point move with elbow point

        newTag.LeaderEndCondition = LeaderEndCondition.Free;
        var elbowPnt = wallMid + new XYZ(5.0, 5.0, 0.0);
        newTag.LeaderElbow = elbowPnt;
        var headerPnt = wallMid + new XYZ(10.0, 10.0, 0.0);
        newTag.TagHeadPosition = headerPnt;

        TaskDialog.Show("Create Independent Tag Method", "End Of Method Dialog");

        return newTag;
    }

    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        throw new NotImplementedException();
    }

    #endregion
}

您需要从 Execute 方法中调用 CreateIndependentTag 方法。 Execute方法是Revit实际调用的方法,目前你的只是抛出一个异常。

此外,CreateIndependentTag 方法需要一面墙以及文档作为参数。文档可以从ExternalCommandData获取。
这堵墙可以通过提示用户 select 一堵墙或通过预先 select 获得一堵墙来获得。在这种情况下,我们将提示用户 select 墙并随后验证 selection。

最后,您需要在对文档进行更改时将对 CreateIndependentTag 的调用包装在事务中。

将它们放在一起看起来像这样:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIDocument uidoc = commandData.Application.ActiveUIDocument;
        Document doc = uidoc.Document;

        Reference reference;
        try
        {
            reference = uidoc.Selection.PickObject(ObjectType.Element, "Pick a wall");
        }
        catch
        {
            return Result.Cancelled;
        }

        var element = doc.GetElement(reference);

        if (element == null || !(element is Wall wall))
        {
            TaskDialog.Show("Error", "Selected element was not a wall");
            return Result.Failed;
        }

        using (Transaction trans = new Transaction(doc, "Creating tag"))
        {
            trans.Start();

            CreateIndependentTag(doc, wall);

            trans.Commit();
        }
    }

一些注意事项:最好创建一个 ISelectionFilter 的实现以将用户的 selection 限制为仅墙。我还喜欢先使用 uidoc.Selection.GetElementIds() 检查现有的 selected 对象,以查看在提示用户 select 之前是否已经 selected 墙。 Building Coder 博客应该有很多与这两点相关的示例。