C#Revit API,CreateIndependentTag方法无法向天花板添加标记,线位置点处出现异常

C#Revit API,CreateIndependentTag方法无法向天花板添加标记,线位置点处出现异常,c#,tags,revit,C#,Tags,Revit,此代码尝试向“天花板视图”列表中的所有天花板添加标记。天花板视图列表将填充,我可以从文档中获取天花板元素,但在尝试获取天花板元素的中心点时,它似乎失败了。我在谷歌上搜索了所有的博客,也找不到在revit中标记楼板的参考,因为这可能是类似的情况 public IndependentTag CreateIndependentTag(Document doc) { List<View> viewList = ceilingViewCollector(doc);

此代码尝试向“天花板视图”列表中的所有天花板添加标记。天花板视图列表将填充,我可以从文档中获取天花板元素,但在尝试获取天花板元素的中心点时,它似乎失败了。我在谷歌上搜索了所有的博客,也找不到在revit中标记楼板的参考,因为这可能是类似的情况

public IndependentTag CreateIndependentTag(Document doc)
    {
        List<View> viewList = ceilingViewCollector(doc);

        foreach (View view in viewList)
        {

            // Find all ceiling elements in the document by using category filter
            ElementCategoryFilter filter = new    ElementCategoryFilter(BuiltInCategory.OST_Ceilings);

            // Use shortcut WhereElementIsNotElementType() to find ceiling instances only
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            IList<Element> CeilingList = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();


            foreach (Ceiling ceiling in CeilingList)
            {
                TaskDialog.Show("Ceiling", ceiling.Name);


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


                //add tag to centre of ceiling?
                LocationPoint p = ceiling.Location as LocationPoint;
                p.Point = new XYZ(0.0, p.Point.Y, p.Point.Z);
                ceilingCentre = p.Point;

                string coords = "point = " + ceilingCentre.ToString();
                TaskDialog.Show("Source room Center", coords);




                IndependentTag newTag = doc.Create.NewTag(view, ceiling, true, tagMode, tagorn, ceilingCentre);
                if (null == newTag)
                {
                    throw new Exception("Create IndependentTag Failed.");
                }
                // set leader mode free
                // otherwise leader end point move with elbow point
                newTag.LeaderEndCondition = LeaderEndCondition.Free;
                XYZ elbowPnt = ceilingCentre + new XYZ(5.0, 5.0, 0.0);
                newTag.LeaderElbow = elbowPnt;
                XYZ headerPnt = ceilingCentre + new XYZ(10.0, 10.0, 0.0);
                newTag.TagHeadPosition = headerPnt;


                return newTag;
            }
        }
        return null;
    }
公共独立标签CreateIndependentTag(文档文档)
{
列表视图列表=ceilingViewCollector(文档);
foreach(视图列表中的视图)
{
//使用类别过滤器查找文档中的所有天花板图元
ElementCategoryFilter过滤器=新的ElementCategoryFilter(BuiltInCategory.OST_天花板);
//使用快捷方式WhereElementIsNotElementType()仅查找天花板实例
FilteredElementCollector=新的FilteredElementCollector(单据);
IList CeilingList=collector.wherePass(filter.WhereElementIsNotElementType().ToElements();
foreach(天花板列表中的天花板)
{
TaskDialog.Show(“天花板”,天花板.名称);
//定义新标记的标记模式和标记方向
TagMode TagMode=TagMode.TM_ADDBY_CATEGORY;
TagOrientation tagorn=TagOrientation.Horizontal;
//是否将标签添加到天花板中心?
位置点p=天花板。位置为位置点;
p、 点=新的XYZ(0.0,p.Point.Y,p.Point.Z);
天花板中心=p点;
string coords=“point=“+ceilingcenter.ToString();
TaskDialog.Show(“源文件室中心”,coords);
独立标记newTag=doc.Create.newTag(视图、天花板、true、tagMode、tagorn、天花板中心);
if(null==newTag)
{
抛出新异常(“创建独立标记失败”);
}
//释放引线模式
//否则,引线端点将与弯头点一起移动
newTag.leaderedcondition=leaderedcondition.Free;
XYZ elbowPnt=天花板中心+新XYZ(5.0,5.0,0.0);
newTag.leaderebow=elbowPnt;
XYZ headerPnt=天花板中心+新XYZ(10.0,10.0,0.0);
newTag.TagHeadPosition=headerPnt;
返回新标签;
}
}
返回null;
}

我不是专家,但你忘了你的X组件了吗

            //add tag to centre of ceiling?
            LocationPoint p = ceiling.Location as LocationPoint;
            p.Point = new XYZ(p.Point.X / 2, p.Point.Y / 2, p.Point.Z);
            ceilingCentre = p.Point;

看起来你想把这个
p放在X和Y的中心,并保留Z分量。

有点晚了,但我刚刚遇到了这个。Revit图元可以通过点(大多数族实例)、线(墙、基于线的构件)或草图(天花板、楼板等)定位。您的代码将无法工作,因为您正在将位置强制转换为位置点,而它不是位置点


使用草图时,您无法将位置投射到任何有用的位置,因此您需要自己确定天花板中心的方式。简单的答案是找到天花板的边界框,并按照mtumminello的建议计算其中心。这将适用于大多数天花板,除非您有一个大的L形或其他形状,边界框中心可能根本不在天花板上方。如果您需要了解这一点,您必须想出一些其他算法来找到中心点。

Thanx对于Think@Gutblender,代码仍然在以下行中出现:LocationPoint p=天花板。Location as LocationPoint;我在定位线和墙元素上尝试了相同的代码,效果很好。还有其他想法吗?谢谢@Gutblender,他做了最初的编辑。我在审查队列中审查建议的编辑,出现了这个:有一个选项可以改进建议的编辑,它允许我修复Gutblender遗漏的一个小缩进错误。您好,我有两个想法。第一个是过滤元素收集器可能需要活动视图viewId。例如:FilteredElementCollector=newfilteredelementcollector(doc,doc.ActiveView.Id);第二,您可能需要获取天花板的BoundingBoxXYZ以计算天花板的中点……第三,在每个语句的“view”之后,您可能需要添加一个检查以确保view.ViewType!=ViewType.ThreeD