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

时间:2018-01-30 14:48:22

标签: c# revit-api

我有一个带有面板和按钮的工作功能区选项卡,可用于调用Dialog框以进行测试。我现在正试图从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
}

1 个答案:

答案 0 :(得分:1)

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

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

最后,当您对文档进行更改时,需要在Transaction中包装对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的实现来将用户的选择限制为仅限于墙。我还想首先使用uidoc.Selection.GetElementIds()检查现有的选定对象,以便在提示用户选择墙之前查看是否已选择墙。建筑编码器博客应该有很多与这两点有关的例子。