(Tekla Structures二次开发)分享官方示例DimensionCreator

分享一个Tekla官方的示例DimensionCreator,首先在vs中新建项目,添加引用文件,新建控件如下图所示:
窗口样式
控件名称如下代码所示:

        private System.Windows.Forms.Button angleDimensionButton;
        private System.Windows.Forms.Button radiusDimensionButton;
        private System.Windows.Forms.Button straightDimensionButton;
        private System.Windows.Forms.Button curvedRadialDimensionButton;
        private System.Windows.Forms.Button curvedOrthogonalDimensionButton;
        private System.Windows.Forms.CheckBox repeatCheckBox;
        private System.Windows.Forms.NumericUpDown distanceNumericUpDown;
        private System.Windows.Forms.Label distanceLabel;
        private System.Windows.Forms.NumericUpDown numberOfPointsToPickNumericUpDown;
        private System.Windows.Forms.Label numberOfPointsToPickLabel;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Button quitButton;

添加类,代码如下:

using System;
using Tekla.Structures.Drawing;
using Tekla.Structures.Geometry3d;
using Tekla.Structures.Drawing.UI;

namespace DimensionCreator
{
    class DimensionCreator
    {
        private static readonly PointList lastPoints = new PointList();
        private static ViewBase lastView;
        public static bool Repeat = false;
        public static int Points = 3;

        static void PickPoints(int numberToPick, ref PointList pointList, ref ViewBase view)
        {
            if(Repeat)
            {
                view = lastView;
                foreach (Point pointt in lastPoints)
                {
                    pointList.Add(new Point(pointt));
                }
                return;
            }

            var picker = new DrawingHandler().GetPicker();
            int ii = numberToPick;
            lastPoints.Clear();
            while(--ii!=-1)
            {
                Point point;
                picker.PickPoint("Pick point", out point, out view);
                pointList.Add(point);
                lastPoints.Add(new Point(point));
            }
            lastView = view;
        }

        public static void CreateAngleDimension()
        {
            PointList pointList = new PointList();
            ViewBase view = null;
            PickPoints(3, ref pointList, ref view);
            double distance = Math.Sqrt(new Vector(pointList[1] - pointList[0]).
                Dot(new Vector(pointList[1] - pointList[0])));
            double viewScale = 1.0;
            if (view is View)
                viewScale = (view as View).Attributes.Scale;
            distance = distance / viewScale;
            AngleDimension ad = new AngleDimension(view, pointList[0], pointList[1], pointList[2], distance);
            ad.Insert();

            new DrawingHandler().GetActiveDrawing().CommitChanges();
        }

        internal static void CreateRadiunDimension(double distance)
        {
            PointList pointList = new PointList();
            ViewBase view = null;
            PickPoints(3, ref pointList, ref view);
            RadiusDimension rd = new RadiusDimension(view, pointList[0], pointList[1], pointList[2], distance);
            rd.Insert();

            new DrawingHandler().GetActiveDrawing().CommitChanges();
        }

        internal static void CreateStraightDimension(double distance)
        {
            PointList pointList = new PointList();
            ViewBase view;
            Point firstPoint;
            Point secondPoint;

            Picker picker = new DrawingHandler().GetPicker();
            picker.PickTwoPoints("Pick first point", "Pick second point", out firstPoint, out secondPoint, out view);
            pointList.Add(firstPoint);
            pointList.Add(secondPoint);
            //获取与第一点,第二点连线方向垂直的向量
            Vector direction = new Vector(firstPoint.Y - secondPoint.Y, secondPoint.X - firstPoint.X, firstPoint.Z);

            StraightDimensionSet sds = new StraightDimensionSetHandler().CreateDimensionSet(view, pointList, direction, 
                distance);
            //至少一点与零件关联,否则标记不起作用
            sds.Attributes.LeftLowerTag.Add(new TextElement("LeftLow"));
            sds.Attributes.LeftMiddleTag.Add(new TextElement("LeftMiddle"));
            sds.Attributes.LeftUpperTag.Add(new TextElement("LeftUpper"));
            sds.Attributes.RightLowerTag.Add(new TextElement("RightLow"));
            sds.Attributes.RightMiddleTag.Add(new TextElement("RightMiddle"));
            sds.Attributes.RightUpperTag.Add(new TextElement("RightUpper"));
            bool isOk = sds.Modify();

            new DrawingHandler().GetActiveDrawing().CommitChanges();
        }

        //弯曲尺寸(增加直角参考线)
        internal static void CreateCurvedOrthoDimension(double distance)
        {
            PointList pointList = new PointList();
            ViewBase view = null;
            PickPoints(Points, ref pointList, ref view);
            new CurvedDimensionSetHandler().CreateCurvedDimensionSetOrthogonal(view, pointList[0], pointList[1],
                pointList[pointList.Count-1], pointList, distance);

            new DrawingHandler().GetActiveDrawing().CommitChanges();
        }

        //弯曲尺寸(增加半径参考线)
        internal static void CreateCurvedRadialDimension(double distance)
        {
            PointList pointList = new PointList();
            ViewBase view = null;
            PickPoints(3, ref pointList, ref view);
            new CurvedDimensionSetHandler().CreateCurvedDimensionSetRadial(view, pointList[0], pointList[1], pointList[2],
                pointList, distance);

            new DrawingHandler().GetActiveDrawing().CommitChanges();
        }
    }
}

窗体控件操作代码如下:

using System;
using System.Windows.Forms;

namespace DimensionCreator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void angleDimensionButton_Click(object sender, EventArgs e)
        {
            try { DimensionCreator.CreateAngleDimension(); }
            catch (Exception) { }
        }

        private void radiusDimensionButton_Click(object sender, EventArgs e)
        {
            try { DimensionCreator.CreateRadiunDimension((double)this.distanceNumericUpDown.Value); }
            catch (Exception) { }
        }

        private void straightDimensionButton_Click(object sender, EventArgs e)
        {
            try { DimensionCreator.CreateStraightDimension((double)this.distanceNumericUpDown.Value); }
            catch (Exception) { }
        }

        private void curvedRadialDimensionButton_Click(object sender, EventArgs e)
        {
            try { DimensionCreator.CreateCurvedRadialDimension((double)this.distanceNumericUpDown.Value); }
            catch { }
        }

        private void curvedOrthogonalDimensionButton_Click(object sender, EventArgs e)
        {
            try { DimensionCreator.CreateCurvedOrthoDimension((double)this.distanceNumericUpDown.Value); }
            catch (Exception) { }
        }

        private void repeatCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            DimensionCreator.Repeat = !DimensionCreator.Repeat;
        }

        private void numberOfPointsToPickNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            DimensionCreator.Points = (int)this.numberOfPointsToPickNumericUpDown.Value;
        }
    }
}

说明:
半径标注与value框中的值无关,其余四个会受到distance值的影响。
其中“弯曲尺寸(增加直角参考线)”拾取点的数量会受到NumberOfPoint影响。

关于Option中Repeat的作用:使用除“直尺寸标注”外的四个标注中任一标注完后,勾选Repeat,另外三个不会再拾取点,而是使用第一个已经拾取的点进行标注。

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值