分享一个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,另外三个不会再拾取点,而是使用第一个已经拾取的点进行标注。