Revit中创建TextNote

Revit二次开发 专栏收录该内容
18 篇文章 1 订阅

 创建TextNote时,不知Revit中它是以何种对齐方式创建,也很难放到我们想要的位置,固有如下工具类:

    public static class TextNoteSrv
    {
        public static TextNote CreateTextNote(
            this ViewPlan viewPlan,
            string text,
            ElementId textTypeId,
            XYZ location,
            TextNoteAlignment align = TextNoteAlignment.eCenterCenter,
            double angle = 0.0)
        {
            System.Diagnostics.Debug.Assert(viewPlan != null);
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(text));
            System.Diagnostics.Debug.Assert(textTypeId != null && textTypeId != ElementId.InvalidElementId);

            var document = viewPlan.Document;
            var scale = viewPlan.Scale;

            var opt = new TextNoteOptions()
            {
                HorizontalAlignment = HorizontalTextAlignment.Left,//左上角
                Rotation = 0,
                TypeId = textTypeId,
            };

            var textNote = TextNote.Create(document, viewPlan.Id, location, text, opt);
            document.Regenerate();

            #region 根据包围框算文本宽度

            var textNoteType = document.GetElement(textTypeId);
            // 引线/边界偏移量
            var space = textNoteType.GetDoubleValue(BuiltInParameter.LEADER_OFFSET_SHEET) * scale;
            var box = textNote.get_BoundingBox(viewPlan);
            var textHeight = (box.Max.Y - box.Min.Y) - space * 2;
            var textWidth = (box.Max.X - box.Min.X) - space * 2;

            #endregion

            var dic = new Dictionary<TextNoteAlignment, double[]>()
            {
                { TextNoteAlignment.eCenterCenter, new double[]{ -textWidth / 2.0, textHeight / 2.0 } },
                { TextNoteAlignment.eCenterDown, new double[]{ -textWidth / 2.0 , textHeight } },
                { TextNoteAlignment.eCenterUp, new double[]{ -textWidth / 2.0 , 0.0 } },
                { TextNoteAlignment.eLeftCenter, new double[]{ 0.0 , textHeight / 2.0 } },
                { TextNoteAlignment.eLeftDown, new double[]{ 0.0 , textHeight } },
                { TextNoteAlignment.eLeftUp, new double[]{ 0.0 , 0.0 } },
                { TextNoteAlignment.eRightCenter, new double[]{ -textWidth, textHeight / 2.0 } },
                { TextNoteAlignment.eRightDown, new double[]{ -textWidth, textHeight } },
                { TextNoteAlignment.eRightUp, new double[]{ -textWidth, 0.0 } },
            };

            var oldLocation = location;

            var xOffset = dic[align][0];//X偏移
            var yOffset = dic[align][1];//Y偏移
            location += XYZ.BasisX * xOffset;
            location += XYZ.BasisY * yOffset;
            ElementTransformUtils.MoveElement(document, textNote.Id, location - oldLocation);

            if (!DoubleEqualComparer.Instance.Equals(angle, 0))
            {
                var ratatePt = location + XYZ.BasisX * (textWidth / 2.0);
                ratatePt -= (XYZ.BasisY * (textHeight / 2.0));
                var axis = Line.CreateBound(ratatePt, ratatePt + XYZ.BasisZ * 5);
                ElementTransformUtils.RotateElement(document, textNote.Id, axis, angle);
            }

            return textNote;
        }
    }

    /// <summary>
    /// 文本位置
    /// </summary>
    public enum TextNoteAlignment
    {
        /// <summary>
        /// 左上位置
        /// </summary>
        eLeftUp = 10,
        /// <summary>
        /// 中上位置
        /// </summary>
        eCenterUp = 20,
        /// <summary>
        /// 右上位置
        /// </summary>
        eRightUp = 30,
        /// <summary>
        /// 左中位置
        /// </summary>
        eLeftCenter = 40,
        /// <summary>
        /// 中中位置
        /// </summary>
        eCenterCenter = 50,
        /// <summary>
        /// 右中位置
        /// </summary>
        eRightCenter = 60,
        /// <summary>
        /// 左下位置
        /// </summary>
        eLeftDown = 70,
        /// <summary>
        /// 中下位置
        /// </summary>
        eCenterDown = 80,
        /// <summary>
        /// 右下位置
        /// </summary>
        eRightDown = 90,
    }

 

  • 0
    点赞
  • 3
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值