C# 独立标记-在Revit中如何称呼它?

C# 独立标记-在Revit中如何称呼它?,c#,revit-api,C#,Revit Api,我有一个功能区选项卡,带有面板和按钮,用于调用对话框进行测试。我现在尝试从Autodesk的站点调用这段代码,该站点应创建一个新的独立标记,但它不起作用 [Transaction(TransactionMode.Manual)] public class Tagtest : IExternalCommand { #region Methods /// <summary> /// The CreateIndependentTag /// &

我有一个功能区选项卡,带有面板和按钮,用于调用对话框进行测试。我现在尝试从Autodesk的站点调用这段代码,该站点应创建一个新的独立标记,但它不起作用

[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
}
[事务(TransactionMode.Manual)]
公共类标记测试:IExternalCommand
{
#区域方法
/// 
///CreateIndependentTag
/// 
///
///
///
公共独立标记CreateIndependentTag(文档,墙)
{
显示(“创建独立标记方法”,“方法开始对话框”);
//确保活动视图不是三维视图
var view=document.ActiveView;
//定义新标记的标记模式和标记方向
var tagMode=tagMode.TM_ADDBY_CATEGORY;
var tagorn=TagOrientation.Horizontal;
//将标记添加到墙的中间
var wallLoc=墙位置,如位置曲线;
var wallStart=walloc.Curve.GetEndPoint(0);
var wallEnd=wallLoc.Curve.GetEndPoint(1);
var wallMid=wallLoc.曲线评估(0.5,正确);
var wallRef=新参考(墙);
var newTag=IndependentTag.Create(document、view.Id、wallRef、true、tagMode、tagorn、wallMid);
如果(null==newTag)抛出新异常(“创建独立标记失败”);
//TagText是只读的,因此我们将Type Mark Type参数更改为
//设置标记文本。标记族的标签参数决定
//用于标记文本的类型参数。
var type=wall.WallType;
var foundParameter=type.LookupParameter(“类型标记”);
var result=foundParameter.Set(“Hello”);
//释放引线模式
//否则,引线端点将与弯头点一起移动
newTag.leaderedcondition=leaderedcondition.Free;
var elbowPnt=wallMid+new XYZ(5.0,5.0,0.0);
newTag.leaderebow=elbowPnt;
var headerPnt=wallMid+new XYZ(10.0,10.0,0.0);
newTag.TagHeadPosition=headerPnt;
显示(“创建独立标记方法”、“方法结束对话框”);
返回新标签;
}
公共结果执行(ExternalCommandData commandData、ref字符串消息、ElementSet元素)
{
抛出新的NotImplementedException();
}
#端区
}

您需要从Execute方法调用CreateIndependentTag方法。 Execute方法是Revit实际调用的方法,当前您的方法只会引发一个异常

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

最后,在对文档进行更改时,需要在事务中包装对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()
检查现有的选定对象,以查看是否已选定墙,然后再提示用户选择墙。Building Coder博客应该有很多与这两点相关的例子。

好的!诚然,我刚刚开始这个开发者之旅,但看着我一年前提出的问题,我感到很尴尬!:)