尺寸标注和公差(.NET)

标注将测量值添加到图形中。公差指定尺寸可以变化的程度。使用AutoCAD .NET API,可以使用标注样式和重写来管理标注。

本节中的主题

  • 尺寸标注概念(.NET)
  • 创建标注(.NET)
  • 编辑标注(.NET)
  • 使用标注样式(.NET)
  • 模型空间和图纸空间中的尺寸标注(.NET)
  • 创建引线和注释(.NET)
  • 使用几何公差(.NET)

尺寸标注概念(.NET)

尺寸显示对象的几何测量值、对象之间的距离或角度,或要素的XY坐标。AutoCAD®提供三种基本的尺寸标注类型:线性、径向和角度。线性标注包括对齐标注、旋转标注和坐标标注。

img

可以为直线、多线、圆弧、圆和圆弧段创建标注,也可以创建独立的标注。

AutoCAD在当前图层上绘制尺寸标注。每个标注都有一个与之关联的标注样式,无论是默认样式还是您定义的样式。样式控制诸如颜色、文字样式、箭头和标注中元素的比例等特性。尺寸标注不支持对象厚度。样式族允许对不同类型标注的基础样式进行细微修改。替代允许对特定标注进行样式修改。

本节中的主题

  • 标注的组成部分(.NET)
  • 定义标注的系统变量(.NET)
  • 设置标注文字样式(.NET)
  • 理解引线(.NET)
  • 了解关联标注(.NET)

标注的组成部分(.NET)

尺寸标注由许多不同的对象组成,如直线、文字、实体填充和块。虽然每种尺寸类型看起来可能略有不同,但它们确实有几个共同点。

img

  • 尺寸线。指示尺寸方向和范围的直线。对于角度标注,尺寸线为圆弧。
  • 延长线。从被标注的特征延伸到尺寸线的直线。延长线也称为投影线或尺寸界线。
  • 箭头用于指示尺寸线端点的符号。箭头也被称为终止符号或只是终止。
  • 尺寸文本。一个text字符串,通常表示正在测量的距离或角度的实际测量值。文本还可以包括前缀、后缀和公差。
  • 引线从某个注释到参照特征的实线。

img

  • 中间标记。十字标记圆或弧的中心的小十字
  • 中线。一组虚线,标记圆或弧的中心

定义标注的系统变量(.NET)

标注系统变量控制标注的外观。尺寸标注系统变量包括DIMAUNIT、DIMUPT、DIMTOFL、DIMFIT、DIMTIH、DIMTOH、DIMJUST和DIMJUST。您可以使用 SetSystemVariable 方法设置这些变量,该方法可以从 Application 对象访问。例如,以下代码行将DIMAUNIT系统变量(角度标注的单位格式)设置为弧度(3):

1
Application.SetSystemVariable("DIMAUNIT", 3);

设置标注文字样式(.NET)

尺寸文本是指与尺寸相关的任何类型的文本,包括测量、公差(横向和几何)、前缀、后缀以及单行或段落形式的文本注释。您可以使用AutoCAD计算的默认测量值作为文本,提供自己的文本或完全隐藏文本。可以使用标注文字添加信息,如特殊制造步骤或装配说明。标注文字使用由DIMTXSTY系统变量指定的文字样式。

img

理解引线(.NET)

默认引线是一条带箭头的直线,指向图形中的特征。通常,引线的功能是将注释与特征连接起来。在这种情况下,注释意味着段落文本、块或特征控制框架。此类引线不同于AutoCAD为半径标注、直径标注和线性标注自动创建的简单引线,这些标注的文字不适合尺寸界线之间。

img

引线对象与注释相关联,因此编辑注释时,引线也会相应更新。可以复制图形中其他位置使用的注释并将其附加到引线,也可以创建新注释。也可以创建不附加注释的引线。

了解关联标注(.NET)

修改关联标注关联的几何对象时,关联标注会自动调整其位置、方向和测量值。DIMASSOC系统变量控制关联标注。将DIMASSOC设置为2以启用关联标注。

创建标注(.NET)

可以创建线性、径向、角度和纵坐标标注。

创建标注时,将使用激活的标注样式。创建尺寸界线后,可以修改尺寸界线原点、标注文字位置、标注文字内容及其相对于尺寸线的角度。也可以修改标注使用的标注样式。

本节中的主题

  • 创建线性标注(.NET)
  • 创建径向标注(.NET)
  • 创建角度标注(.NET)
  • 创建折弯半径标注(.NET)
  • 创建弧长标注(.NET)
  • 创建坐标标注(.NET)

创建线性标注(.NET)

可以对齐或旋转线性标注。对齐标注的尺寸线平行于尺寸界线原点所在的线沿着。旋转尺寸的尺寸线与尺寸界线原点成一定角度放置。

可以通过创建 AlignedDimension 和 RotatedDimension 对象的实例来创建线性标注。创建线性标注的实例后,可以修改文字、文字的角度或尺寸线的角度。下图显示了线性标注的类型和尺寸界线原点的放置如何影响尺寸线和文字的角度。

img

创建 AlignedDimension 对象的实例时,可以选择指定尺寸界线原点、尺寸线位置、标注文字和要应用的标注样式。如果没有向 AlignedDimension 对象构造函数传递任何参数,则对象将被分配一组默认属性值。

RotatedDimension 对象构造函数提供了与 AlignedDimension 对象构造函数相同的选项,但有一个例外。 RotatedDimension 对象构造函数接受一个附加参数,该参数指定尺寸线旋转的角度。

标注折弯线

线性标注上的纵行不是通过一组特性添加的,而是通过扩展数据(扩展数据)添加的。负责标注折弯线的应用程序名称为ACAD_DSTYLE_DIMJAG_POSITION。下面是需要附加到线性维度的Xdata结构的示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Open the Registered Application table for read
RegAppTable acRegAppTbl;
acRegAppTbl = <transaction>.GetObject(<current_database>.RegAppTableId,
OpenMode.ForRead) as RegAppTable;

// Check to see if the app "ACAD_DSTYLE_DIMJAG_POSITION" is
// registered and if not add it to the RegApp table
if (acRegAppTbl.Has("ACAD_DSTYLE_DIMJAG_POSITION") == false)
{
using (RegAppTableRecord acRegAppTblRec = new RegAppTableRecord())
{
acRegAppTblRec.Name = "ACAD_DSTYLE_DIMJAG_POSITION";

<transaction>.GetObject(<current_database>.RegAppTableId, OpenMode.ForWrite);

acRegAppTbl.Add(acRegAppTblRec);
<transaction>.AddNewlyCreatedDBObject(acRegAppTblRec, true);
}
}

// Create a result buffer to define the Xdata
ResultBuffer acResBuf = new ResultBuffer();
acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName,
"ACAD_DSTYLE_DIMJAG_POSITION"));
acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 387));
acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 3));
acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 389));
acResBuf.Add(new TypedValue((int)DxfCode.ExtendedDataXCoordinate,
new Point3d(-1.26985, 3.91514, 0)));

// Attach the Xdata to the dimension
<dimension_object>.XData = acResBuf;

创建旋转线性标注

本示例在模型空间中创建旋转标注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[CommandMethod("CreateRotatedDimension")]
public static void CreateRotatedDimension()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the rotated dimension
using (RotatedDimension acRotDim = new RotatedDimension())
{
acRotDim.XLine1Point = new Point3d(0, 0, 0);
acRotDim.XLine2Point = new Point3d(6, 3, 0);
acRotDim.Rotation = 0.707;
acRotDim.DimLinePoint = new Point3d(0, 5, 0);
acRotDim.DimensionStyle = acCurDb.Dimstyle;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acRotDim);
acTrans.AddNewlyCreatedDBObject(acRotDim, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

创建径向标注(.NET)

径向尺寸测量圆弧和圆的半径和直径。通过创建 RadialDimension 和 DiametricDimension 对象的实例来创建径向和直径尺寸。

根据圆或圆弧的大小、标注文字的位置以及DIMUPT、DIMTOFL、DIMFIT、DIMTIH、DIMTOH、DIMJUST和DIMJUST标注系统变量中的值,可以创建不同类型的径向标注。(可以使用 GetSystemVariable 和 SetSystemVariable 方法查询或设置系统变量。)

对于水平标注文字,如果尺寸线与水平线的角度大于15度,并且位于圆或圆弧之外,则AutoCAD将绘制一条弯钩线,也称为基线或折线。钩线放置在标注文字的旁边或下方,如下图所示:

img

img

创建 RadialDimension 对象的实例时,可以选择指定中心点和弦点、引线长度、标注文字和要应用的标注样式。创建 DiametricDimension 对象与创建 RadialDimension 对象类似,不同之处在于指定弦和远弦点,而不是中心和弦点。

LeaderLength 属性指定从 ChordPoint 到注释文字的距离(如果不需要钩线,则为停止)。

创建径向尺寸标注

本示例在模型空间中创建径向标注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[CommandMethod("CreateRadialDimension")]
public static void CreateRadialDimension()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the radial dimension
using (RadialDimension acRadDim = new RadialDimension())
{
acRadDim.Center = new Point3d(0, 0, 0);
acRadDim.ChordPoint = new Point3d(5, 5, 0);
acRadDim.LeaderLength = 5;
acRadDim.DimensionStyle = acCurDb.Dimstyle;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acRadDim);
acTrans.AddNewlyCreatedDBObject(acRadDim, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

创建角度标注(.NET)

角度标注测量两条线或三点之间的角度。例如,您可以使用它们来测量圆的两个半径之间的角度。尺寸线形成圆弧。角度尺寸是通过创建 LineAngularDimension2 或 Point3AngularDimension 对象的实例来创建的。

  • LineAngularDimension2. 表示由两条直线定义的角度标注。
  • Point3AngularDimension. 表示由三个点定义的角度标注。

当您创建 LineAngularDimension2 或 Point3AngularDimension 对象的实例时,构造函数可以选择接受一组参数。创建新的 LineAngularDimension2 对象时,可以提供以下参数:

  • 延长线1起点( XLine1Start 属性)
  • 延长线1终点( XLine1End 属性)
  • 延长线2起点( XLine2Start 属性)
  • 延长线1终点( XLine2End 属性)
  • 弧点( ArcPoint 属性)
  • 标注文字( DimensionText 属性)
  • 标注样式( DimensionStyleName 或 DimensionStyle 属性)

创建新的 Point3AngularDimension 对象时,可以提供以下参数:

  • 中心点( CenterPoint 属性)
  • 延长线1点( XLine1Point 属性)
  • 延长线2点( XLine2Point 属性)
  • 弧点( ArcPoint 属性)
  • 标注文字( DimensionText 属性)
  • 标注样式( DimensionStyleName 或 DimensionStyle 属性)

创建角度标注

本示例在模型空间中创建角度标注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[CommandMethod("CreateAngularDimension")]
public static void CreateAngularDimension()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create an angular dimension
using (LineAngularDimension2 acLinAngDim = new LineAngularDimension2())
{
acLinAngDim.XLine1Start = new Point3d(0, 5, 0);
acLinAngDim.XLine1End = new Point3d(1, 7, 0);
acLinAngDim.XLine2Start = new Point3d(0, 5, 0);
acLinAngDim.XLine2End = new Point3d(1, 3, 0);
acLinAngDim.ArcPoint = new Point3d(3, 5, 0);
acLinAngDim.DimensionStyle = acCurDb.Dimstyle;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acLinAngDim);
acTrans.AddNewlyCreatedDBObject(acLinAngDim, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

创建折弯半径标注(.NET)

折弯半径尺寸测量对象的半径,并显示前面带有半径符号的尺寸文本。在以下情况下,您可能会在径向尺寸对象上使用折弯尺寸:

  • 对象的中心点位于布局之外,或者位于模型中没有足够空间放置径向尺寸标注的区域上
  • 物体的半径很大

通过创建 RadialDimensionLarge 对象的实例来创建慢跑半径尺寸。当你创建一个 RadialDimensionLarge 对象的实例时,它的构造函数可以有选择地接受一组参数。创建新的 RadialDimensionLarge 对象时,可以提供以下参数:

  • 中心点( Center 属性)
  • 弦点( ChordPoint 属性)
  • 覆写中间点( OverrideCenter 属性)
  • 折弯线的位置( JogPoint 属性)
  • 折弯线的角度( JogAngle 属性)
  • 标注文字( DimensionText 属性)
  • 标注样式( DimensionStyleName 或 DimensionStyle 属性)

创建折弯半径标注

本示例在模型空间中创建折弯半径标注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[CommandMethod("CreateJoggedDimension")]
public static void CreateJoggedDimension()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create a large radius dimension
using (RadialDimensionLarge acRadDimLrg = new RadialDimensionLarge())
{
acRadDimLrg.Center = new Point3d(-3, -4, 0);
acRadDimLrg.ChordPoint = new Point3d(2, 7, 0);
acRadDimLrg.OverrideCenter = new Point3d(0, 2, 0);
acRadDimLrg.JogPoint = new Point3d(1, 4.5, 0);
acRadDimLrg.JogAngle = 0.707;
acRadDimLrg.DimensionStyle = acCurDb.Dimstyle;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acRadDimLrg);
acTrans.AddNewlyCreatedDBObject(acRadDimLrg, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

创建弧长标注(.NET)

弧长标注测量沿弧的沿着长度,并显示标注文字,标注文字带有位于弧上方或弧前方的弧符号。当需要标注弧的实际长度而不仅仅是其起点和终点之间的距离时,可以使用弧长标注。

可以通过创建 ArcDimension 对象的实例来创建弧长半径标注。创建 ArcDimension 对象的实例时,需要一组定义尺寸的参数。创建新的 ArcDimension 对象时,必须提供以下参数:

  • 中心点( Center 属性)
  • 延长线1点( XLine1Point 物业)
  • 延长线2点( XLine2Point 属性)
  • 弧点( ArcPoint 属性)
  • 标注文字( DimensionText 属性)
  • 标注样式( DimensionStyleName 或 DimensionStyle 属性)

注意:DIMARCSYM系统变量控制是否显示圆弧符号以及该符号相对于标注文字的显示位置。

创建弧长标注

本示例在模型空间中创建弧长标注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[CommandMethod("CreateArcLengthDimension")]
public static void CreateArcLengthDimension()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create an arc length dimension
using (ArcDimension acArcDim = new ArcDimension(new Point3d(4.5, 1.5, 0),
new Point3d(8, 4.25, 0),
new Point3d(0, 2, 0),
new Point3d(5, 7, 0),
"<>",
acCurDb.Dimstyle))
{

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acArcDim);
acTrans.AddNewlyCreatedDBObject(acArcDim, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

创建坐标尺寸(.NET)

坐标或基准尺寸测量从原点(称为基准)到标注特征(如零件中的孔)的垂直距离。这些尺寸通过保持特征与基准的精确偏移来防止误差的增加。

img

坐标尺寸由带有引线的XY纵坐标组成。X基准坐标尺寸测量特征沿X轴沿着距基准的距离。Y基准坐标尺寸沿Y轴沿着测量相同的距离。AutoCAD使用当前用户坐标系(UCS)的原点来确定测量坐标。使用坐标的绝对值。

无论当前尺寸样式定义的方向如何,尺寸文本都会与坐标引线对齐。您可以接受默认文本或使用自己的文本覆盖它。

通过创建 OrdinateDimension 对象的实例来创建同基准尺寸。当你创建一个 OrdinateDimension 对象的实例时,它的构造函数可以接受一组可选的参数。创建新的 OrdinateDimension 对象时,可以提供以下参数:

  • 使用X轴( UsingXAxis 属性)
  • 定义点( DefiningPoint 属性)
  • 引线端点( LeaderEndPoint 属性)
  • 标注文字( DimensionText 属性)
  • 标注样式( DimensionStyleName 或 DimensionStyle 属性)

当将值传递到 OrdinateDimension 对象构造器时,第一个值是布尔标志,它指定尺寸是X基准还是Y基准坐标尺寸。如果输入 TRUE ,将创建X基准坐标尺寸。如果输入 FALSE ,将创建Y基准坐标尺寸。 UsingXAxis 特性也可用于指定坐标尺寸是X基准还是Y基准。

创建坐标尺寸

本示例在“模型”空间中创建纵坐标标注。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[CommandMethod("CreateOrdinateDimension")]
public static void CreateOrdinateDimension()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create an ordinate dimension
using (OrdinateDimension acOrdDim = new OrdinateDimension())
{
acOrdDim.UsingXAxis = true;
acOrdDim.DefiningPoint = new Point3d(5, 5, 0);
acOrdDim.LeaderEndPoint = new Point3d(10, 5, 0);
acOrdDim.DimensionStyle = acCurDb.Dimstyle;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acOrdDim);
acTrans.AddNewlyCreatedDBObject(acOrdDim, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

编辑标注(.NET)

与AutoCAD中的其他图形对象一样,可以使用为对象提供的方法和属性编辑标注。

以下属性可用于大多数标注对象:

  • DimensionStyle

    指定尺寸样式的对象ID。

  • DimensionStyleName

    指定标注样式的名称。

  • DimensionText

    指定用户定义的标注文字字符串。

  • HorizontalRotation

    以弧度为单位指定尺寸的旋转角度。

  • Measurement

    指定标注的实际测量值。

  • TextPosition

    指定标注文字位置。

  • TextRotation

    指定标注文字的旋转角度。

此外,要使用标注对象的特定属性和方法修改标注对象,还可以复制和变换标注对象。

本节中的主题

  • 重写标注文字(.NET)

重写标注文字(.NET)

可以使用 DimensionText 特性替换显示的尺寸值。使用此属性可以完全替换尺寸的显示值,也可以将文本附加到值。要在重写尺寸文本中表示测量值,请在文本中使用字符串“<>”。

修改标注文字

此示例将一些文本附加到值,以便同时显示字符串和尺寸值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[CommandMethod("OverrideDimensionText")]
public static void OverrideDimensionText()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the aligned dimension
using (AlignedDimension acAliDim = new AlignedDimension())
{
acAliDim.XLine1Point = new Point3d(5, 3, 0);
acAliDim.XLine2Point = new Point3d(10, 3, 0);
acAliDim.DimLinePoint = new Point3d(7.5, 5, 0);
acAliDim.DimensionStyle = acCurDb.Dimstyle;

// Override the dimension text
acAliDim.DimensionText = "The value is <>";

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acAliDim);
acTrans.AddNewlyCreatedDBObject(acAliDim, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

使用标注样式(.NET)

命名标注样式是确定标注外观的一组设置。使用命名标注样式,可以为工程图中的标注建立和实施绘图标准。

所有标注都使用激活的标注样式创建。如果在创建标注之前未定义或应用样式,AutoCAD将应用默认样式STANDARD。要设置活动维度样式,请使用当前数据库的 Dimstyle 属性。

若要设置标注样式,请开始命名并保存样式。新样式以当前样式为基础,并包括定义尺寸部分、文字定位和注释外观的所有设置。在这种情况下,注释意味着主单位和换算单位、公差和文本。

本节中的主题

  • 创建、修改和复制标注样式(.NET)
  • 更改标注样式(.NET)

创建、修改和复制标注样式(.NET)

通过创建 DimStyleTableRecord 对象的实例,然后使用 Add 方法将其添加到 DimStyleTable 中,可以创建新的标注样式。在将标注样式添加到表中之前,请使用 Name 特性设置新样式的名称。

也可以复制现有样式或具有重载的样式。使用 CopyFrom 方法将尺寸样式从源对象复制到尺寸样式。源对象可以是另一个 DimStyleTableRecord 对象、 Dimension 、 Tolerance 或 Leader 对象,甚至是 Database 对象。如果从其他标注样式复制样式设置,则当前样式将完全复制。如果从 Dimension 、 Tolerance 或 Leader 对象复制样式设置,则当前设置(包括任何对象替代)将复制到样式中。如果复制 Database 对象的当前样式,则标注样式和任何图形重载都将复制到新样式中。

复制标注样式和重载

本示例创建三个新标注样式,并将当前设置从当前 Database 、给定标注样式和给定标注分别复制到每个新标注样式。通过在运行此示例之前执行相应的设置,您将发现已创建了不同的标注样式。

  1. 创建新图形并使其成为活动图形。
  2. 在新图形中创建线性尺寸标注。此尺寸应是图形中的唯一对象。
  3. 将尺寸线的颜色更改为黄色。
  4. 将DIMCLRD系统变量更改为5(蓝色)。
  5. 运行以下示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
[CommandMethod("CopyDimStyles")]
public static void CopyDimStyles()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for read
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;

object acObj = null;
foreach (ObjectId acObjId in acBlkTblRec)
{
// Get the first object in Model space
acObj = acTrans.GetObject(acObjId,
OpenMode.ForRead);

break;
}

// Open the DimStyle table for read
DimStyleTable acDimStyleTbl;
acDimStyleTbl = acTrans.GetObject(acCurDb.DimStyleTableId,
OpenMode.ForRead) as DimStyleTable;

string[] strDimStyleNames = new string[3];
strDimStyleNames[0] = "Style 1 copied from a dim";
strDimStyleNames[1] = "Style 2 copied from Style 1";
strDimStyleNames[2] = "Style 3 copied from the running drawing values";

int nCnt = 0;

// Keep a reference of the first dimension style for later
DimStyleTableRecord acDimStyleTblRec1 = null;

// Iterate the array of dimension style names
foreach (string strDimStyleName in strDimStyleNames)
{
DimStyleTableRecord acDimStyleTblRec;
DimStyleTableRecord acDimStyleTblRecCopy = null;

// Check to see if the dimension style exists or not
if (acDimStyleTbl.Has(strDimStyleName) == false)
{
if (acDimStyleTbl.IsWriteEnabled == false) acTrans.GetObject(acCurDb.DimStyleTableId, OpenMode.ForWrite);

acDimStyleTblRec = new DimStyleTableRecord();
acDimStyleTblRec.Name = strDimStyleName;

acDimStyleTbl.Add(acDimStyleTblRec);
acTrans.AddNewlyCreatedDBObject(acDimStyleTblRec, true);
}
else
{
acDimStyleTblRec = acTrans.GetObject(acDimStyleTbl[strDimStyleName],
OpenMode.ForWrite) as DimStyleTableRecord;
}

// Determine how the new dimension style is populated
switch ((int)nCnt)
{
// Assign the values of the dimension object to the new dimension style
case 0:
try
{
// Cast the object to a Dimension
Dimension acDim = acObj as Dimension;

// Copy the dimension style data from the dimension and
// set the name of the dimension style as the copied settings
// are unnamed.
acDimStyleTblRecCopy = acDim.GetDimstyleData();
acDimStyleTblRec1 = acDimStyleTblRec;
}
catch
{
// Object was not a dimension
}

break;

// Assign the values of the dimension style to the new dimension style
case 1:
acDimStyleTblRecCopy = acDimStyleTblRec1;
break;
// Assign the values of the current drawing to the dimension style
case 2:
acDimStyleTblRecCopy = acCurDb.GetDimstyleData();
break;
}

// Copy the dimension settings and set the name of the dimension style
acDimStyleTblRec.CopyFrom(acDimStyleTblRecCopy);
acDimStyleTblRec.Name = strDimStyleName;

// Dispose of the copied dimension style
acDimStyleTblRecCopy.Dispose();

nCnt = nCnt + 1;
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

使用DIMSTYLE命令打开标注样式管理器。现在应该列出了三种标注样式。样式1应具有黄色尺寸线。样式2应该与样式1相同。样式3应具有蓝色尺寸线。

重写标注样式(.NET)

每个标注都可以重写由标注样式指定给它的设置。以下属性可用于大多数标注对象:

  • Dimatfit

    指定尺寸线仅显示在尺寸界线内,并强制尺寸文字和箭头显示在尺寸界线内或外。

  • Dimaltrnd

    指定换算单位的舍入。

  • Dimasz

    指定尺寸线箭头、引线箭头和钩线的大小。

  • Dimaunit

    指定角度标注的单位格式。

  • Dimblk1, Dimblk2 ,

    指定用于尺寸线箭头的块。

  • Dimcen

    指定径向和直径标注的中心标记的类型和大小。

  • Dimclrd

    指定尺寸、引线或公差对象的尺寸线颜色。

  • Dimclre

    指定尺寸界线的颜色。

  • Dimclrt

    指定尺寸和公差对象的文字颜色。

  • Dimdec

    指定为尺寸或公差的基本单位显示的小数位数。

  • Dimdsep Dimdsep的

    指定在小数尺寸和公差值中用作小数分隔符的字符。

  • Dimexe

    指定尺寸界线超出尺寸线的距离。

  • Dimexo

    指定尺寸界线距原点的偏移距离。

  • Dimfrac

    指定尺寸和公差中分数值的格式。

  • Dimgap

    指定打断尺寸线以容纳标注文字时标注文字与尺寸线之间的距离。

  • Dimlfac

    指定线性标注测量的全局比例因子。

  • Dimltex1, Dimltex2

    指定尺寸界线的线型。

  • Dimlwd

    指定尺寸线的线宽。

  • Dimlwe

    指定尺寸界线的线宽。

  • Dimjust

    指定标注文字的水平对齐方式。

  • Dimrnd

    指定尺寸测量的距离舍入。

  • Dimsd1, Dimsd2

    指定尺寸线的抑制。

  • Dimse1, Dimse2

    指定尺寸界线的抑制。

  • Dimtad

    指定文字相对于尺寸线的垂直位置。

  • Dimtdec

    指定主尺寸中公差值的精度。

  • Dimtfac

    指定公差值的文字高度相对于标注文字高度的比例因子。

  • Dimlunit

    指定除角度标注外的所有标注的单位格式。

  • Dimtih

    指定是否在尺寸界线内绘制标注文字。

  • Dimtm

    指定标注文字的最小公差限制。

  • Dimtmove

    指定移动文字时如何绘制标注文字。

  • Dimtofl

    指定是否在尺寸界线之间绘制尺寸线(即使文字放置在尺寸界线之外)。

  • Dimtoh

    为除纵坐标外的所有标注类型指定尺寸标注文字在尺寸界线外的位置。

  • Dimtol

    指定公差是否与标注文字一起显示。

  • Dimtolj

    指定公差值相对于公称尺寸文字的垂直对正。

  • Dimtp

    指定标注文字的最大公差限制。

  • Dimtxt

    指定尺寸或公差文字的高度。

  • Dimzin

    指定尺寸值中是否隐藏前导零和尾随零以及零英尺和零英寸测量值。

  • Prefix

    指定标注值前缀。

  • Suffix

    指定标注值后缀。

  • TextPrecision

    指定角度标注文字的精度。

  • TextPosition

    指定标注文字位置。

  • TextRotation

    指定标注文字的旋转角度。

为对齐的标注输入用户定义的后缀

此示例在模型空间中创建对齐尺寸,并使用Suffix属性允许用户更改尺寸的文本后缀。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[CommandMethod("AddDimensionTextSuffix")]
public static void AddDimensionTextSuffix()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the aligned dimension
using (AlignedDimension acAliDim = new AlignedDimension())
{
acAliDim.XLine1Point = new Point3d(0, 5, 0);
acAliDim.XLine2Point = new Point3d(5, 5, 0);
acAliDim.DimLinePoint = new Point3d(5, 7, 0);
acAliDim.DimensionStyle = acCurDb.Dimstyle;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acAliDim);
acTrans.AddNewlyCreatedDBObject(acAliDim, true);

// Append a suffix to the dimension text
PromptStringOptions pStrOpts = new PromptStringOptions("");

pStrOpts.Message = "\nEnter a new text suffix for the dimension: ";
pStrOpts.AllowSpaces = true;
PromptResult pStrRes = acDoc.Editor.GetString(pStrOpts);

if (pStrRes.Status == PromptStatus.OK)
{
acAliDim.Suffix = pStrRes.StringResult;
}
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

模型空间和图纸空间中的尺寸标注(.NET)

可以在模型空间和图纸空间中绘制尺寸。但是,如果要标注尺寸的几何图形位于模型空间中,则最好在模型空间中绘制尺寸,因为AutoCAD会将定义点放置在绘制几何图形的空间中。

如果在图纸空间中绘制描述模型中几何图形的尺寸,则在使用编辑命令或更改“模型空间”视口中显示的放大率时,图纸空间尺寸不会更改。将视图从图纸空间更改为模型空间时,图纸空间尺寸的位置也保持不变。

如果在图纸空间中标注尺寸,并且线性标注的全局比例因子(DIMLFAC系统变量)设置为小于0,则测量的距离将乘以DIMLFAC的绝对值。如果在“模型”空间中标注尺寸,则即使DIMLFAC小于0,也将使用值1.0。如果在“尺寸”提示下更改变量并选择“视口”选项,AutoCAD将计算DIMLFAC的值。AutoCAD计算模型空间与图纸空间的比例,并将该值的负值指定给DIMLFAC。

创建引线和注释(.NET)

引线是将某个注释连接到图形中某个要素的线。引线及其注释是关联的,这意味着如果修改注释,引线将相应更新。不要将引线对象与AutoCAD自动生成为尺寸线一部分的引线混淆。

本节中的主题

  • 创建引线(.NET)
  • 向引线添加注释(.NET)
  • 引线关联性(.NET)
  • 编辑引线关联性(.NET)
  • 编辑引线(.NET)

创建引线(.NET)

可以从图形中的任何点或特征创建引线并控制其外观。引线可以是直线段或平滑的样条曲线。引线颜色由当前尺寸线颜色控制。引线比例由活动标注样式中设置的总体标注比例控制。如果存在箭头,则其类型和大小由活动样式中定义的箭头控制。

一条称为钩线的细线通常将注释连接到引线。如果最后一条引线线段与水平线的夹角大于15度,则勾线将与多行文字和几何公差框一起显示。钩线是单个箭头的长度。如果引线没有注释,则它没有钩线。

img

可以通过创建引线对象的实例来创建引线。创建Leader对象的实例时,其构造函数不接受任何参数。 AppendVertex 方法用于定义所创建引线的位置和长度。

创建引线

本示例在模型空间中创建引线。没有与引线关联的注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[CommandMethod("CreateLeader")]
public static void CreateLeader()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the leader
using (Leader acLdr = new Leader())
{
acLdr.AppendVertex(new Point3d(0, 0, 0));
acLdr.AppendVertex(new Point3d(4, 4, 0));
acLdr.AppendVertex(new Point3d(4, 5, 0));
acLdr.HasArrowHead = true;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acLdr);
acTrans.AddNewlyCreatedDBObject(acLdr, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

向引线添加注释(.NET)

Leader 对象的注释可以是 Tolerance 、 MText 或 BlockReference 对象。可以创建新注释对象,也可以附加现有注释对象的副本。通过将注释对象的对象ID分配给 Annotation 属性,可以将注释添加到引线。

引线关联性(.NET)

引线与其注释相关联,因此当注释移动时,引线的端点也随之移动。当您移动文字和几何公差符号注释时,最终引线线段根据注释与引线倒数第二点(倒数第二点)的关系,在附着到注释的左侧和右侧之间交替。如果注释的中点位于倒数第二个引线点的右侧,则引线将附着到右侧;否则,它将附着到左侧。

使用 Erase 、 Add (添加块)或 WBlock 方法从图形中删除任何一个对象都会破坏关联性。如果引线及其注释在单个操作中一起复制,则新副本是关联的。如果单独复制它们,它们将是非关联的。如果由于任何原因(例如,仅复制引线对象或删除注释)而破坏了关联性,则将从引线中删除钩线。

将引线与注记关联

这个例子创建了一个 MText 对象。然后使用 MText 对象作为其注释创建引线。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[CommandMethod("AddLeaderAnnotation")]
public static void AddLeaderAnnotation()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the MText annotation
using (MText acMText = new MText())
{
acMText.Contents = "Hello, World.";
acMText.Location = new Point3d(5, 5, 0);
acMText.Width = 2;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acMText);
acTrans.AddNewlyCreatedDBObject(acMText, true);

// Create the leader with annotation
using (Leader acLdr = new Leader())
{
acLdr.AppendVertex(new Point3d(0, 0, 0));
acLdr.AppendVertex(new Point3d(4, 4, 0));
acLdr.AppendVertex(new Point3d(4, 5, 0));
acLdr.HasArrowHead = true;

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acLdr);
acTrans.AddNewlyCreatedDBObject(acLdr, true);

// Attach the annotation after the leader object is added
acLdr.Annotation = acMText.ObjectId;
acLdr.EvaluateLeader();
}
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

编辑引线关联性(.NET)

除了引线和注释之间的关联关系之外,引线及其注释在图形中是完全独立的对象。编辑引线不会影响注释,编辑注释也不会影响引线。

虽然文字注释是使用DIMCLRT、DIMTXT和DIMTXSTY系统变量创建的,以定义其颜色、高度和样式,但不能通过这些系统变量进行更改,因为它不是真正的标注对象。文本注释的编辑方式必须与任何其他 MText 对象相同。

使用 Evaluate 方法计算引线与其关联注释的关系。如果需要,此方法将更新引线几何图形。

编辑引线(.NET)

对引线注释所做的任何更改其位置的修改都会影响关联引线端点的位置。此外,旋转注释会导致引线钩线(如果有)旋转。

通过缩放引线来调整引线的大小。如果缩放引线,注释将保持在相对于引线端点的相同位置,但不会缩放。除了缩放之外,还可以移动、镜像和旋转引线。使用 TransformBy 方法编辑引线。使用其成员属性和方法修改关联的批注

使用几何公差(.NET)

几何公差显示了特征的形状、轮廓、方向、位置和跳动的偏差。在几何公差符号中添加几何公差。这些框包含单个尺寸的所有公差信息。

本节中的主题

  • 创建几何公差(.NET)
  • 编辑几何公差(.NET)

创建几何公差(.NET)

通过创建 FeatureControlFrame 对象的实例来创建几何公差。当你创建一个 FeatureControlFrame 对象的实例时,它的构造函数可以接受一组可选的参数。创建新的 FeatureControlFrame 对象时,可以提供以下参数:

  • 包含公差符号的文本字符串( Text 属性)
  • 插入点( Location 属性)
  • 法向量( Normal 属性)
  • 方向矢量( Direction 属性)

创建几何公差

此示例在模型空间中创建简单的几何公差。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[CommandMethod("CreateGeometricTolerance")]
public static void CreateGeometricTolerance()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create the Geometric Tolerance (Feature Control Frame)
using (FeatureControlFrame acFcf = new FeatureControlFrame())
{
acFcf.Text = "{\\Fgdt;j}%%v{\\Fgdt;n}0.001%%v%%v%%v%%v";
acFcf.Location = new Point3d(5, 5, 0);

// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acFcf);
acTrans.AddNewlyCreatedDBObject(acFcf, true);
}

// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}

编辑几何公差(.NET)

几何公差受多个系统变量和特性的影响。下列系统变量和特性会影响几何公差的外观:

DIMCLRD

控制几何公差符号的颜色。

DIMCLRT

控制公差文字的颜色。

DIMGAP

控制几何公差符号和文字之间的差距。

DIMTXT

控制公差文字的大小。

DIMTXTSTY

控制公差文字的样式。

注:翻译自ObjectARX: Managed .NET Developer’s Guide,且只保留了C#部分的代码