高级绘图和组织技术(.NET)

随着经验的积累,您可以利用许多高级功能来进一步增强应用程序。

可以参照其他图形,并在图形中包含光栅图像(如航拍、卫星和数字照片)以及计算机渲染图像。有关参照文件和光栅图像的信息,请参见AutoCAD产品文档。

除了增强图形的视觉图像外,AutoCAD还提供了多个功能来帮助您组织数据,从而使您可以进一步扩展图形中对象的智能。

本节中的主题

  • 使用光栅图像(.NET)
  • 使用块和属性(.NET)
  • 使用外部引用(.NET)
  • 分配和删除扩展数据(.NET)

使用光栅图像(.NET)

使用AutoCAD®,可以将光栅图像附着到基于矢量的图形,然后查看和打印参照文件。

本节中的主题

  • 附加和缩放光栅图像(.NET)
  • 管理光栅图像(.NET)
  • 修改图像和图像边界(.NET)
  • 更改图像的剪切边界(.NET)

附加和缩放光栅图像(.NET)

图像可以附着并放置在图形文件中,但它们实际上不是文件的一部分。图像通过路径名或数据管理文档ID链接到图形文件。可以随时更改或删除链接的图像路径。要附加图像,首先创建一个RasterImageDef对象,该对象包含有关存储在磁盘上的被引用图像文件的定义信息,然后创建一个RasterImage对象。

创建RasterImageDef对象后,将其指定给RasterImage对象。RasterImage对象表示您在绘图窗口中与之交互的对象,并允许您控制图像的插入点、比例、方向和外观以及其他属性。单个RasterImageDef对象可用于多次附加图像。每个附件都有自己的剪辑边界以及自己的亮度、对比度、淡入度和透明度设置。

创建RasterImage对象后,可以使用Scale特性设置光栅图像的比例因子,以便图像的几何图形比例与AutoCAD图形中创建的几何图形比例相匹配。附着图像时,将以1个图像测量单位比1个AutoCAD测量单位的比例因子插入图像。要设置图像比例因子,需要知道图像上几何图形的比例,还需要知道要使用什么测量单位(英寸、英尺等)来定义1个AutoCAD单位。图像文件必须包含定义DPI(每英寸点数)和图像像素数的分辨率信息。

如果图像具有分辨率信息,AutoCAD会将其与您提供的比例因子和AutoCAD测量单位相结合,以缩放图形中的图像。例如,如果光栅图像是扫描的蓝图,其比例为1英寸等于50英尺(或1:600),并且AutoCAD图形设置为1单位表示1英寸,则将图像的比例因子设置为600。AutoCAD会缩放图像,以便图像中的几何图形与图形中的矢量几何图形对齐。

注意:如果附着的图像文件未定义分辨率信息,AutoCAD将以一个单位计算图像的原始宽度。插入后,以AutoCAD单位表示的图像宽度等于比例因子。

附加光栅图像

此示例在模型空间中添加光栅图像。此示例使用在WEBSample目录中找到的WorldMap.tif文件。如果没有此图像,或者它位于其他目录中,请修改代码以使用有效的路径和文件名。

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
116
117
118
119
120
121
122
123
124
[CommandMethod("AttachRasterImage")]
public void AttachRasterImage()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Define the name and image to use
string strImgName = "WorldMap";
string strFileName = "C:\\AutoCAD\\Sample\\VBA\\WorldMap.TIF";

RasterImageDef acRasterDef;
bool bRasterDefCreated = false;
ObjectId acImgDefId;

// Get the image dictionary
ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);

// Check to see if the dictionary does not exist, it not then create it
if (acImgDctID.IsNull)
{
acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb);
}

// Open the image dictionary
DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;

// Check to see if the image definition already exists
if (acImgDict.Contains(strImgName))
{
acImgDefId = acImgDict.GetAt(strImgName);

acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef;
}
else
{
// Create a raster image definition
RasterImageDef acRasterDefNew = new RasterImageDef();

// Set the source for the image file
acRasterDefNew.SourceFileName = strFileName;

// Load the image into memory
acRasterDefNew.Load();

// Add the image definition to the dictionary
acTrans.GetObject(acImgDctID, OpenMode.ForWrite);
acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

acTrans.AddNewlyCreatedDBObject(acRasterDefNew, true);

acRasterDef = acRasterDefNew;

bRasterDefCreated = true;
}

// 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 new image and assign it the image definition
using (RasterImage acRaster = new RasterImage())
{
acRaster.ImageDefId = acImgDefId;

// Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768).
// Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you
// can convert the size of the drawing into other units or millimeters based on the
// drawing units used in the current drawing.

// Define the width and height of the image
Vector3d width;
Vector3d height;

// Check to see if the measurement is set to English (Imperial) or Metric units
if (acCurDb.Measurement == MeasurementValue.English)
{
width = new Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
height = new Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
}
else
{
width = new Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0);
height = new Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0);
}

// Define the position for the image
Point3d insPt = new Point3d(5.0, 5.0, 0.0);

// Define and assign a coordinate system for the image's orientation
CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width * 2, height * 2);
acRaster.Orientation = coordinateSystem;

// Set the rotation angle for the image
acRaster.Rotation = 0;

// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acRaster);
acTrans.AddNewlyCreatedDBObject(acRaster, true);

// Connect the raster definition and image together so the definition
// does not appear as "unreferenced" in the External References palette.
RasterImage.EnableReactors(true);
acRaster.AssociateRasterDef(acRasterDef);

if (bRasterDefCreated)
{
acRasterDef.Dispose();
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

管理光栅图像(.NET)

可以使用 RasterImage 对象的属性管理光栅图像名称、文件名和文件路径。

本节中的主题

  • 变更图像路径(.NET)
  • 命名图像(.NET)

变更图像路径(.NET)

使用分配给 RasterImage 对象的 RasterImageDef 对象的 SourceFileName 属性查询或更改图像的路径和文件名。此特性设置的路径是AutoCAD查找图像的实际路径。

如果AutoCAD无法找到图形(例如,如果将文件移动到与 SourceFileName 特性保存的目录不同的目录),则它会从名称中删除相对或绝对路径信息(例如,\images\tree.tgac:\my project\images\tree.tga变为tree.tga),并搜索使用Preferences对象上的 SetProjectFilePath 方法定义的路径。如果文件不在路径中,它将再次尝试第一个搜索路径。如果AutoCAD找到并加载图像文件,则会更新 ActiveFileName 特性以反映找到该文件的位置。

可以从文件名中删除路径,或通过重置 SourceFileName 属性指定相对路径。

更改 SourceFileName 属性中的路径不会影响项目文件的搜索路径设置。

命名图像(.NET)

图像名称不一定与图像文件名称相同。将图像附着到图形时,将在“图像”字典中为图像文件参照指定一个名称。您可以更改图像名称而不影响文件的名称。

图像文件由 RasterImageDef 对象上的 SourceFileName 属性表示。更改 SourceFileName 特性将更改图形中的图像。图像名称由表示 RasterImageDef 对象的 DBDictionaryEntry 对象的 Key 属性表示,更改 Key 属性将仅更改图像的名称,而不会更改与之关联的文件。

修改图像和图像边界(.NET)

所有图像都有图像边界。将图像附着到图形时,图像边界将继承当前特性设置,包括颜色、图层、线型和线型比例。如果图像是黑白图像,则图像颜色和边界颜色相同。

与其他AutoCAD对象一样,可以修改图像及其边界特性。例如,您可以:

  • 显示或隐藏图像边界
  • 修改图像图层、边界颜色和线型
  • 更改图像位置
  • 缩放、旋转和更改图像的宽度和高度
  • 切换图像可见性
  • 更改图像透明度
  • 更改图像亮度、对比度和淡入度
  • 更改图像显示的质量和速度

显示和隐藏图像边界(.NET)

隐藏图像边界可确保图像不会意外移动或修改,并防止打印或显示边界。隐藏图像边界时,剪裁的图像仍显示为其指定的边界限制;只有边界受到影响。显示和隐藏图像边界会影响附着到图形的所有图像。

如果要显示或隐藏图像边界,请使用 IsClipped 属性。

注意:此属性仅影响图像边界。若要查看切换此属性时图像的变化,请仔细查看图像周围的小边界。

更改图像和边界属性(.NET)

可以使用以下特性更改图像边界和图像图层的颜色、线型、线宽和透明度:

  • Layer

    指定图像的图层

  • Color

    指定图像边界的颜色

  • Linetype

    指定图像边界的线型

  • Lineweight

    指定图像边界的线宽

  • Transparency

    指定图像边界的透明度

更改图像缩放、旋转、位置、宽度和高度(.NET)

可以使用以下方法和属性更改图像的位置、缩放、旋转、位置、宽度和高度:

  • Orientation

    指定图像的位置和比例

  • Rotation

    旋转图像

  • Width

    以像素为单位指定图像的宽度

  • Height

    以像素为单位指定图像的高度

  • ImageWidth

    以数据库单位指定图像的宽度

  • ImageHeight

    以数据库单位指定图像的高度

更改图像可见性(.NET)

图像可见性通过隐藏当前绘图任务中的图像来影响重画速度。不显示或打印隐藏图像;仅显示图形边界。要隐藏图像,请将其 ShowImage 属性设置为 False 。若要重新显示图像,请将其 ShowImage 属性设置为 True 。

修改黑白图像颜色和透明度(.NET)

双色光栅图像是仅由前景色和背景色组成的图像。附着黑白图像时,图像中的前景像素将继承当前图层的颜色设置。除了可以对任何附加图像进行修改之外,还可以通过更改前景色以及打开和关闭背景的透明度来修改黑白图像。

注意:黑白图像和黑白图像边界的颜色始终相同。

若要更改黑白图像的前景色,请使用 Color 属性。要打开和关闭透明度,请使用 ImageTransparency 属性。

调整图像亮度、对比度和淡入淡出(.NET)

可以在AutoCAD中调整图像亮度、对比度和淡入度,以控制图像的显示和打印输出方式,而不会影响原始光栅图像文件。

使用以下属性调整亮度、对比度和淡入度:

  • Brightness

    指定图像的亮度级别

  • Contrast

    指定图像的对比度

  • Fade

    指定图像的淡入度

更改图像的剪切边界(.NET)

可以通过剪裁图像来定义图像的显示和打印区域。剪裁边界必须是二维多边形或矩形,其顶点被约束为位于图像的边界内。同一图像的多个实例可以具有不同的边界。

如果要显示或隐藏图像边界,请使用IsClipped属性;如果要确定剪裁边界是定义为矩形还是多边形,请使用ClipBoundaryType属性。

使用以下方法获取或设置图像的剪裁边界:

  • GetClipBoundary

    返回以图像像素坐标表示的剪辑边界顶点的集合。反转PixelToModelTransform矩阵,将其转换为模型坐标。

  • SetClipBoundary

    指定多边形剪裁边界。

  • SetClipBoundaryToWholeImage

    将剪辑边界设置为与图像边界重合。将删除所有现有的剪裁边界。

裁剪光栅图像

此示例在模型空间中添加光栅图像,然后基于剪裁边界剪裁该图像。此示例使用在VBA示例目录中找到的WorldMap.tif文件。如果没有此图像,或者它位于其他目录中,请修改代码以使用有效的路径和文件名。

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
[CommandMethod("ClippingRasterBoundary")]
public void ClippingRasterBoundary()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Define the name and image to use
string strImgName = "WorldMap";
string strFileName = "C:\\AutoCAD\\Sample\\VBA\\WorldMap.TIF";

RasterImageDef acRasterDef;
bool bRasterDefCreated = false;
ObjectId acImgDefId;

// Get the image dictionary
ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);

// Check to see if the dictionary does not exist, it not then create it
if (acImgDctID.IsNull)
{
acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb);
}

// Open the image dictionary
DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;

// Check to see if the image definition already exists
if (acImgDict.Contains(strImgName))
{
acImgDefId = acImgDict.GetAt(strImgName);

acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef;
}
else
{
// Create a raster image definition
RasterImageDef acRasterDefNew = new RasterImageDef();

// Set the source for the image file
acRasterDefNew.SourceFileName = strFileName;

// Load the image into memory
acRasterDefNew.Load();

// Add the image definition to the dictionary
acTrans.GetObject(acImgDctID, OpenMode.ForWrite);
acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

acTrans.AddNewlyCreatedDBObject(acRasterDefNew, true);

acRasterDef = acRasterDefNew;

bRasterDefCreated = true;
}

// 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 new image and assign it the image definition
using (RasterImage acRaster = new RasterImage())
{
acRaster.ImageDefId = acImgDefId;

// Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768).
// Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you
// can convert the size of the drawing into other units or millimeters based on the
// drawing units used in the current drawing.

// Define the width and height of the image
Vector3d width;
Vector3d height;

// Check to see if the measurement is set to English (Imperial) or Metric units
if (acCurDb.Measurement == MeasurementValue.English)
{
width = new Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
height = new Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
}
else
{
width = new Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0);
height = new Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0);
}

// Define the position for the image
Point3d insPt = new Point3d(5.0, 5.0, 0.0);

// Define and assign a coordinate system for the image's orientation
CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width * 2, height * 2);
acRaster.Orientation = coordinateSystem;

// Set the rotation angle for the image
acRaster.Rotation = 0;

// Define the clipping boundary in drawing units
Point2dCollection acPt2dColl = new Point2dCollection();
Matrix3d acMat3d = acRaster.PixelToModelTransform.Inverse();
acPt2dColl.Add(new Point3d(5.5, 15, 0).TransformBy(acMat3d).Convert2d(new Plane()));
acPt2dColl.Add(new Point3d(12.5, 15, 0).TransformBy(acMat3d).Convert2d(new Plane()));
acPt2dColl.Add(new Point3d(12.5, 10.5, 0).TransformBy(acMat3d).Convert2d(new Plane()));
acPt2dColl.Add(new Point3d(5.5, 10.5, 0).TransformBy(acMat3d).Convert2d(new Plane()));
acPt2dColl.Add(new Point3d(5.5, 15, 0).TransformBy(acMat3d).Convert2d(new Plane()));

// Clip the image
acRaster.SetClipBoundary(ClipBoundaryType.Rectangle, acPt2dColl);

// Enable the display of the clip
acRaster.IsClipped = true;

// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acRaster);
acTrans.AddNewlyCreatedDBObject(acRaster, true);

// Connect the raster definition and image together so the definition
// does not appear as "unreferenced" in the External References palette.
RasterImage.EnableReactors(true);
acRaster.AssociateRasterDef(acRasterDef);

if (bRasterDefCreated)
{
acRasterDef.Dispose();
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

使用块和特性(.NET)

AutoCAD提供了多种功能来帮助您管理图形中的对象。使用块,可以将许多对象作为一个组件进行组织和操作。特性将信息项与数据库中的块相关联,例如零件号和价格。

使用AutoCAD外部参照,可以将整个图形附着或覆盖到当前图形。打开当前图形时,在参照图形中所做的任何更改都会显示在当前图形中。

本节中的主题

  • 使用块(.NET)
  • 使用特性(.NET)

使用块(.NET)

块是可以关联在一起以形成单个对象或块参照的对象集合。可以在图形中插入、缩放和旋转块参照。可以将块参照分解为其组件对象、修改它们以及重定义块。AutoCAD将根据块的定义更新该块参照的所有将来实例。

可以从最初在不同图层上绘制的具有不同颜色和线型的对象定义块。可以保留块中对象的图层、颜色和线型信息。然后,每次插入块时,都将块中的每个对象以其原始颜色和线型绘制在其原始图层上。

有关使用块的详细信息,请参见产品帮助系统中的“关于插入块”和“关于定义块”。

本节中的主题

  • 定义块(.NET)
  • 插入块(.NET)
  • 分解块参照(.NET)
  • 重新定义块(.NET)

定义块(.NET)

若要创建新块,请创建一个新的BlockTableRecord对象,并使用Add方法将其追加到BlockTable对象。创建BlockTableRecord对象后,请使用Name特性为块指定名称,并在使用BulldEntity方法将块插入图形时显示对象。

然后将任何几何对象或另一个块添加到新创建的BlockTableRecord对象。对象是使用BulldEntity方法添加到BlockTableRecord对象的。然后,可以通过创建新的BlockReference对象并将其附加到模型空间或与Layout对象关联的BlockTableRecord对象,将块的实例插入到图形中。插入的块是称为块参照的对象。

也可以通过使用WBlock方法将对象写出到单独的图形文件来创建块。然后,图形文件可以用作其他图形的块定义。AutoCAD将插入到其他图形中的任何图形视为块。

有关定义块的详细信息,请参阅产品帮助系统中的“关于定义块”。

定义块
此示例定义了一个块并向块定义中添加了一个圆。然后将块作为块参照插入到图形中。

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
[CommandMethod("CreatingABlock")]
public void CreatingABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

插入块(.NET)

在创建BlockReference对象,然后使用BlockEntity方法将其附加到BlockTableRecord对象之后,可以插入在当前图形的块表中定义的块。创建新的BlockReference时,向构造函数传递要插入的块引用的原点以及几何体应从其继承的BlockTableRecord对象的ObjectID。

也可以将整个图形文件插入到当前图形中,方法是使用ReadDwgFile方法在内存中打开图形文件,然后使用Insert方法将内存中的图形插入到当前图形中。将整个图形插入到另一个图形中时,AutoCAD会将插入的图形视为任何其他块参照。后续插入将参照具有不同位置、比例和旋转设置的块定义(其中包含块的几何描述)。

如果在插入原始图形后对其进行更改,则这些更改对插入的块没有影响。如果希望插入的块反映对原始图形所做的更改,可以通过重新插入原始图形来重定义块。

默认情况下,AutoCAD在插入图形文件时使用坐标(0,0,0)作为基点。通过打开原始图形并使用SetSystemVariable方法为INSBASE系统变量指定不同的插入基点,可以修改图形的基点。下次插入图形时,AutoCAD将使用新基点。

如果插入的图形包含PaperSpace对象,则这些对象不包含在当前图形的块定义中。若要在其他图形中使用PaperSpace对象,请打开原始图形并将PaperSpace对象定义为块。可以将图形插入到图纸空间或模型空间中的其他图形中。

不能通过迭代块参照来查找组成它的原始对象。但是,可以重新定义原始块定义,或者将块参照分解为其原始组件。

也可以使用MInsertBlock对象插入块数组。此对象类型不会像使用BlockReference对象那样在图形中插入单个块,而是插入指定块的数组。

有关插入块的详细信息,请参见产品帮助系统中的“关于插入块”。

定义和插入块

此示例定义了一个块并向块定义中添加了一个圆。然后将块作为块参照插入到图形中。

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
[CommandMethod("InsertingABlock")]
public void InsertingABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

ObjectId blkRecId = ObjectId.Null;

if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}

blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlock"];
}

// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

分解块参照(.NET)

使用Explode方法打断块参照。通过分解块参照,可以修改块或添加或删除定义块的对象。

显示分解块参照的结果

此示例创建一个块并将圆添加到块的定义中。然后,块将作为块参照插入到图形中。然后分解块参照,并沿着显示分解过程生成的对象及其对象类型。

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
[CommandMethod("ExplodingABlock")]
public void ExplodingABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

ObjectId blkRecId = ObjectId.Null;

if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}

blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlock"];
}

// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

using (DBObjectCollection dbObjCol = new DBObjectCollection())
{
acBlkRef.Explode(dbObjCol);

foreach (DBObject dbObj in dbObjCol)
{
Entity acEnt = dbObj as Entity;

acCurSpaceBlkTblRec.AppendEntity(acEnt);
acTrans.AddNewlyCreatedDBObject(dbObj, true);

acEnt = acTrans.GetObject(dbObj.ObjectId, OpenMode.ForWrite) as Entity;

acEnt.ColorIndex = 1;
Application.ShowAlertDialog("Exploded Object: " + acEnt.GetRXClass().DxfName);
}
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

重新定义块(.NET)

使用任何Block对象方法和特性来重定义块。重定义块时,图形中对该块的所有参照将立即更新以反映新定义。

重定义会影响以前和将来插入的块。常量属性将丢失并被任何新的常量属性替换。变量属性保持不变,即使新块没有属性。

在块定义中重定义对象

此示例创建一个块并将圆添加到块的定义中。然后,块将作为块参照插入到图形中。块定义中的圆将更新,块参照也将自动更新。

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
[CommandMethod("RedefiningABlock")]
public void RedefiningABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);

// Insert the block into the current space
using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), acBlkTblRec.Id))
{
BlockTableRecord acModelSpace;
acModelSpace = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acModelSpace.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

Application.ShowAlertDialog("CircleBlock has been created.");
}
}
}
}
else
{
// Redefine the block if it exists
BlockTableRecord acBlkTblRec =
acTrans.GetObject(acBlkTbl["CircleBlock"], OpenMode.ForWrite) as BlockTableRecord;

// Step through each object in the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

// Revise the circle in the block
if (dbObj is Circle)
{
Circle acCirc = dbObj as Circle;

acTrans.GetObject(objID, OpenMode.ForWrite);
acCirc.Radius = acCirc.Radius * 2;
}
}

// Update existing block references
foreach (ObjectId objID in acBlkTblRec.GetBlockReferenceIds(false, true))
{
BlockReference acBlkRef = acTrans.GetObject(objID, OpenMode.ForWrite) as BlockReference;
acBlkRef.RecordGraphicsModified(true);
}

Application.ShowAlertDialog("CircleBlock has been revised.");
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

使用特性(.NET)

特性参照提供交互式标签或标记,以便将文字附着到块。数据的示例包括部件号、价格、注释和所有者姓名。

可以从工程图中提取特性参照信息,并在电子表格或数据库中使用该信息来生成明细表或BOM表等条目。可以将多个特性参照与块关联,前提是每个特性参照具有不同的标记。还可以定义常量特性。由于它们在块的每个引用中具有相同的值,因此在插入块时AutoCAD不会提示输入值。

特性可以是不可见的,这意味着不显示或打印特性参照。但是,有关特性参照的信息存储在图形文件中。

有关使用特性的详细信息,请参见产品帮助系统中的“关于定义和附着块特性”。

本节中的主题

  • 创建特性定义和特性引用(.NET)
  • 编辑特性定义(.NET)
  • 提取特性信息(.NET)

创建特性定义和特性引用(.NET)

若要创建特性定义,首先必须创建一个AttributeDefinition对象,然后使用ObjectEntity方法将其追加到BlockTableRecord对象。定义特性定义时,应指定特性文字的高度、特性模式、提示和标记字符串、插入点以及默认特性值。

特性定义的模式由以下属性控制:

  • Constant

    为块插入赋予特性一个固定值。

  • Invisible

    指定插入块时不显示或打印特性值。ATTDISP命令将覆盖所有特性的不可见模式。

  • IsMTextAttributeDefinition

    指定特性值可以包含多行文本。选择此选项后,可以指定特性的边界宽度。

  • LockPositionInBlock

    锁定块参照中特性的位置。解锁后,可以使用夹点编辑相对于块的其余部分移动特性,并且可以调整多行特性的大小。

  • Preset

    插入包含预设特性的块时,将特性设置为其默认值。

  • Verifiable

    允许您在插入块时验证特性值是否正确。

当插入包含特性的块并使用Prompt属性进行设置时,将显示提示字符串。该特性的默认值使用TextString属性设置。如果“常量”属性设定为“真”,则在插入具有特性定义的块时,将禁用提示输入新值。

标记字符串标识特性的每次出现,并分配有Tag属性。您可以使用除空格或感叹号以外的任何字符。AutoCAD将小写字母更改为大写。

一旦在块中定义了特性定义,则无论何时使用NULL命令插入块,都可以为每个未定义为常量的特性参照指定不同的值。创建BlockReference对象时,该对象不包含在BlockTableRecord对象中定义的任何特性,直到这些特性使用BlockAttribute方法附加到BlockReference对象。

在将特性附加到BlockReference对象之前,请使用SetAttributeFromBlock方法将AttributeDefinition对象的特性复制到AttributeReference对象。HasAttributeDefinitions特性可用于块表记录,以查看它是否包含特性定义。

在模型空间或图纸空间中创建的特性定义不被视为附着到任何给定块。

定义特性定义
此示例创建一个块,然后向该块添加特性。

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
[CommandMethod("AddingAttributeToABlock")]
public void AddingAttributeToABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

if (!acBlkTbl.Has("CircleBlockWithAttributes"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlockWithAttributes";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(0, 0, 0);
acAttDef.Verifiable = true;
acAttDef.Prompt = "Door #: ";
acAttDef.Tag = "Door#";
acAttDef.TextString = "DXX";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;

acBlkTblRec.AppendEntity(acAttDef);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

插入具有特性的块
此示例创建一个具有属性的块,然后将该块插入当前空间。

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
[CommandMethod("InsertingBlockWithAnAttribute")]
public void InsertingBlockWithAnAttribute()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

ObjectId blkRecId = ObjectId.Null;

if (!acBlkTbl.Has("CircleBlockWithAttributes"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlockWithAttributes";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(0, 0, 0);
acAttDef.Prompt = "Door #: ";
acAttDef.Tag = "Door#";
acAttDef.TextString = "DXX";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(acAttDef);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}

blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlockWithAttributes"];
}

// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;

// Create and insert the new block reference
using (BlockReference acBlkRef = new BlockReference(new Point3d(2, 2, 0), blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

// Verify block table record has attribute definitions associated with it
if (acBlkTblRec.HasAttributeDefinitions)
{
// Add attributes from the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;

if (!acAtt.Constant)
{
using (AttributeReference acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);

acAttRef.TextString = acAtt.TextString;

acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

编辑特性定义(.NET)

可以使用Attribute对象属性和方法来编辑特性。特性上的某些属性包括:

  • FieldLength

    指定特性的字段长度

  • Height

    指定特性的高度

  • HorizontalMode

    指定特性的水平对齐方式

  • IsMirroredInX

    指定特性的文本向后显示

  • IsMirroredInY

    指定特性的文本上下颠倒显示

  • Position

    指定特性的插入点

  • Prompt

    指定特性的提示字符串

  • Rotation

    指定特性的旋转

  • Tag

    指定特性的标记字符串

  • TextString

    指定特性的默认值

  • VerticalMode

    指定特性的垂直对齐方式

重定义特性定义
此示例创建一个块,然后向该块添加特性。然后将块插入到图形中。然后,特性文字将更新为向后显示。

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
[CommandMethod("RedefiningAnAttribute")]
public void RedefiningAnAttribute()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

ObjectId blkRecId = ObjectId.Null;

if (!acBlkTbl.Has("CircleBlockWithAttributes"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlockWithAttributes";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;

acBlkTblRec.AppendEntity(acCirc);

// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(0, 0, 0);
acAttDef.Prompt = "Door #: ";
acAttDef.Tag = "Door#";
acAttDef.TextString = "DXX";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(acAttDef);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}

blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlockWithAttributes"];
}

// Create and insert the new block reference
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;

using (BlockReference acBlkRef = new BlockReference(new Point3d(2, 2, 0), blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

// Verify block table record has attribute definitions associated with it
if (acBlkTblRec.HasAttributeDefinitions)
{
// Add attributes from the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;

if (!acAtt.Constant)
{
using (AttributeReference acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);

acAttRef.TextString = acAtt.TextString;

acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}

// Change the attribute definition to be displayed as backwards
acTrans.GetObject(objID, OpenMode.ForWrite);
acAtt.IsMirroredInX = true;
}
}
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

提取特性信息(.NET)

可以使用AttributeCollection属性从块参照中提取特性信息。AttributeCollection属性返回附加到块的特性引用的集合。单步执行集合时,可以使用IsConstant属性查看是否可以更改某个特性值。

不需要模板文件来提取特性信息,并且不创建特性信息文件。只需使用特性引用的Tag和TextString属性检查属性引用集合的特性信息。您还需要检查IsMTextAttribute是否为True或False,如果为True,则需要使用MTextAttribute属性获取多行特性的文本值。

有关提取特性信息的详细信息,请参见产品帮助系统中的“关于从块特性提取数据”。

获取特性引用信息

此示例创建一个块,然后向该块添加特性。然后将块插入到图形中。然后返回特性数据并使用消息框显示。然后将更新块参照的特性数据,并再次返回和显示特性数据。

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
[CommandMethod("GettingAttributes")]
public void GettingAttributes()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

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

ObjectId blkRecId = ObjectId.Null;

if (!acBlkTbl.Has("TESTBLOCK"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "TESTBLOCK";

// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);

// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(5, 5, 0);
acAttDef.Prompt = "Attribute Prompt";
acAttDef.Tag = "AttributeTag";
acAttDef.TextString = "Attribute Value";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(acAttDef);

acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}

blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlockWithAttributes"];
}

// Create and insert the new block reference
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;

using (BlockReference acBlkRef = new BlockReference(new Point3d(5, 5, 0), acBlkTblRec.Id))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

// Verify block table record has attribute definitions associated with it
if (acBlkTblRec.HasAttributeDefinitions)
{
// Add attributes from the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;

if (!acAtt.Constant)
{
using (AttributeReference acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);

acAttRef.TextString = acAtt.TextString;

acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}

// Display the tags and values of the attached attributes
string strMessage = "";
AttributeCollection attCol = acBlkRef.AttributeCollection;

foreach (ObjectId objID in attCol)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

AttributeReference acAttRef = dbObj as AttributeReference;

strMessage = strMessage + "Tag: " + acAttRef.Tag + "\n" +
"Value: " + acAttRef.TextString + "\n";

// Change the value of the attribute
acAttRef.TextString = "NEW VALUE!";
}

Application.ShowAlertDialog("The attributes for blockReference " + acBlkRef.Name + " are:\n" + strMessage);

strMessage = "";
foreach (ObjectId objID in attCol)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

AttributeReference acAttRef = dbObj as AttributeReference;

strMessage = strMessage + "Tag: " + acAttRef.Tag + "\n" +
"Value: " + acAttRef.TextString + "\n";
}

Application.ShowAlertDialog("The attributes for blockReference " + acBlkRef.Name + " are:\n" + strMessage);
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

使用外部引用(.NET)

外部参照将另一个图形链接到当前图形。将图形作为块插入时,块和所有关联的几何图形都存储在当前图形数据库中。如果原始图形发生更改,则不会更新该文件。但是,当您将图形作为外部参照插入时,外部参照将在原始图形更改时更新。因此,包含外部参照的图形始终反映每个外部参照文件的最新编辑。

与块参照一样,外部参照在当前图形中显示为单个对象。但是,外部参照不会显著增加当前图形的文件大小,因此不能分解。与块一样,可以嵌套附着到图形的外部参照。

有关外部参照的详细信息,请参见产品帮助系统中的“关于附着和拆离参照图形(外部参照)”。

本节中的主题

  • 更新外部参照(.NET)
  • 附着外部参照(.NET)
  • 分离外部参照(.NET)
  • 重新加载外部参照(.NET)
  • 卸载外部参照(.NET)
  • 绑定外部参照(.NET)
  • 剪裁块和外部参照(.NET)
  • 加载和外部参照性能(.NET)

更新外部参照(.NET)

打开或打印图形时,AutoCAD会重新加载每个外部参照以反映参照图形的最新状态。对外部参照图形进行修改并保存文件后,其他用户可以通过重新加载外部参照立即访问您所做的修改。

附着外部参照(.NET)

附着外部参照将一个图形(参照文件或外部参照)链接到当前图形。当图形参照外部参照时,AutoCAD仅将外部参照定义附着到图形,这与常规块不同,常规块的块定义和块的内容与当前图形一起存储。AutoCAD将读取参照图形以确定要在当前图形中显示的内容。如果参照文件丢失或损坏,则其数据不会显示在当前图形中。每次打开图形时,AutoCAD都会加载参照文件中的所有图形和非图形(例如图层、线型和文字样式)对象。如果VISRETAIN系统变量处于启用状态,AutoCAD将在当前图形中存储任何更新的外部参照相关图层信息。

可以附着任意数量的外部参照副本,每个副本都可以具有不同的位置、比例和旋转。还可以控制在外部参照中定义的从属图层和线型特性。

要附着外部参照,请使用 AttachXref 方法。此方法要求输入要参照的图形的路径和文件名,以及当前图形中使用的外部参照的名称。 AttachXref 方法返回新创建对象的 ObjectId 。返回的 ObjectId 用于创建新的 BlockReference 对象并定义其在图形中的位置。创建 BlockReference 对象后,可以调整其旋转和缩放。

有关附着外部参照的详细信息,请参见产品帮助系统中的“关于附着和拆离参照图形(外部参照)”。

将外部参照附着到图形

此示例显示添加外部参照前后当前图形中的所有块。本示例使用Sample目录中的Exterior Elevations.dwg文件。如果没有此文件,或者它位于其他目录中,请插入有效的路径和文件名。

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
[CommandMethod("AttachingExternalReference")]
public void AttachingExternalReference()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Create a reference to a DWG file
string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

// If a valid reference is created then continue
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Point3d insPt = new Point3d(1, 1, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}
}

// Save the new objects to the database
acTrans.Commit();

// Dispose of the transaction
}
}

分离外部参照(.NET)

可以拆离外部参照定义,以便从图形中完全删除外部参照。也可以删除各个外部参照实例。拆离外部参照定义将删除与该外部参照关联的所有从属符号。如果从图形中删除了外部参照的所有实例,则AutoCAD将在下次打开图形时删除外部参照定义。

要拆离外部参照,请使用 DetachXref 方法。不能拆离嵌套的外部参照。

分离外部参照定义

此示例附着外部参照,然后分离外部参照。本示例使用Sample目录中的Exterior Elevations.dwg文件。如果您没有此图像,或者它位于其他目录中,请插入有效的路径和文件名。

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("DetachingExternalReference")]
public void DetachingExternalReference()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Create a reference to a DWG file
string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

// If a valid reference is created then continue
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Point3d insPt = new Point3d(1, 1, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}

Application.ShowAlertDialog("The external reference is attached.");

acCurDb.DetachXref(acXrefId);

Application.ShowAlertDialog("The external reference is detached.");
}

// Save the new objects to the database
acTrans.Commit();

// Dispose of the transaction
}
}

重新加载外部参照(.NET)

如果在您处理外部参照附着到的宿主图形时,有人修改了该外部参照图形,则可以使用 Reload 方法更新该外部参照图形。重新加载时,选定的外部参照图形将在宿主图形中更新。此外,如果已卸载外部参照,则可以随时选择重新载入该外部参照图形。

添加外部参照定义

此示例附着外部参照,然后重新加载该外部参照。本示例使用Sample目录中的Exterior Elevations.dwg文件。如果您没有此图像,或者它位于其他目录中,请插入有效的路径和文件名。

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
[CommandMethod("ReloadingExternalReference")]
public void ReloadingExternalReference()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Create a reference to a DWG file
string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

// If a valid reference is created then continue
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Point3d insPt = new Point3d(1, 1, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}

Application.ShowAlertDialog("The external reference is attached.");

using (ObjectIdCollection acXrefIdCol = new ObjectIdCollection())
{
acXrefIdCol.Add(acXrefId);

acCurDb.ReloadXrefs(acXrefIdCol);
}

Application.ShowAlertDialog("The external reference is reloaded.");
}

// Save the new objects to the database
acTrans.Commit();

// Dispose of the transaction
}
}

卸载外部参照(.NET)

要卸载外部参照,请使用 Unload 方法。卸载当前图形中未使用的参照文件时,AutoCAD性能将得到增强,因为不必读取和显示不必要的图形几何图形或符号表信息。在重新加载外部参照之前,外部参照几何图形和任何嵌套外部参照的几何图形都不会显示在当前图形中。

卸载外部参照定义

此示例附着外部参照,然后卸载外部参照。本示例使用Sample目录中的Exterior Elevations.dwg文件。如果您没有此图像,或者它位于其他目录中,请插入有效的路径和文件名。

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
[CommandMethod("UnloadingExternalReference")]
public void UnloadingExternalReference()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Create a reference to a DWG file
string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

// If a valid reference is created then continue
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Point3d insPt = new Point3d(1, 1, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}

Application.ShowAlertDialog("The external reference is attached.");

using (ObjectIdCollection acXrefIdCol = new ObjectIdCollection())
{
acXrefIdCol.Add(acXrefId);

acCurDb.UnloadXrefs(acXrefIdCol);
}

Application.ShowAlertDialog("The external reference is unloaded.");
}

// Save the new objects to the database
acTrans.Commit();

// Dispose of the transaction
}
}

绑定外部参照(.NET)

使用Bind方法将外部参照绑定到图形使外部参照成为图形的永久部分,而不再是外部参照文件。外部引用的信息成为一个块。更新外部参照图形时,不会更新绑定的外部参照。此过程将绑定整个图形的数据库,包括其所有从属符号。

从属符号是命名对象,例如块、标注样式、图层、线型和文字样式。绑定外部参照允许在当前图形中使用外部参照中的命名对象。

BindXref 方法需要两个参数: xrefIds (ObjectID的集合)和 insertBind (布尔值)。如果将 insertBind 参数设置为True,则外部参照图形的符号名称在当前图形中将以*$x $作为前缀,其中x*是一个整数,它将自动递增以避免替代现有的块定义。如果将 insertBind 参数设置为 False ,则外部参照图形的符号名称将合并到当前图形中,而不带前缀。如果存在重复的名称,AutoCAD将使用已在本地图形中定义的符号。如果不确定图形是否包含重复的符号名称,建议将 insertBind 设置为 True 。

有关绑定外部参照的详细信息,请参见产品帮助系统中的“关于使用外部参照绘制图形(绑定)”。

绑定外部参照定义
此示例附着外部参照,然后将外部参照绑定到图形。本示例使用Sample目录中的Exterior Elevations.dwg文件。如果您没有此图像,或者它位于其他目录中,请插入有效的路径和文件名。

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
[CommandMethod("BindingExternalReference")]
public void BindingExternalReference()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Create a reference to a DWG file
string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

// If a valid reference is created then continue
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Point3d insPt = new Point3d(1, 1, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}

Application.ShowAlertDialog("The external reference is attached.");

using (ObjectIdCollection acXrefIdCol = new ObjectIdCollection())
{
acXrefIdCol.Add(acXrefId);

acCurDb.BindXrefs(acXrefIdCol, false);
}

Application.ShowAlertDialog("The external reference is bound.");
}

// Save the new objects to the database
acTrans.Commit();

// Dispose of the transaction
}
}

剪裁块和外部参照(.NET)

通过剪裁外部参照,可以定义外部参照的区域以进行显示和打印。剪裁边界必须是二维多边形或矩形,其顶点被约束为位于外部参照的边界内。同一外部参照的多个实例可以具有不同的边界。

SpatialFilter和SpatialFilterDefinition对象用于定义外部参照的剪裁边界的属性。使用SpatialFilterDefinition对象的Enabled属性显示或隐藏剪裁边界。

剪裁外部参照定义
此示例附着外部参照,然后剪裁外部参照,使其仅显示一部分。本示例使用Sample目录中的Exterior Elevations.dwg文件。如果您没有此图像,或者它位于其他目录中,请插入有效的路径和文件名。

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
116
117
[CommandMethod("ClippingExternalReference")]
public void ClippingExternalReference()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Create a reference to a DWG file
string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

// If a valid reference is created then continue
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Point3d insPt = new Point3d(1, 1, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

acBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

Application.ShowAlertDialog("The external reference is attached.");

Matrix3d mat = acBlkRef.BlockTransform;
mat.Inverse();

Point2dCollection ptCol = new Point2dCollection();

// Define the first corner of the clipping boundary
Point3d pt3d = new Point3d(-330, 400, 0);
pt3d.TransformBy(mat);
ptCol.Add(new Point2d(pt3d.X, pt3d.Y));

// Define the second corner of the clipping boundary
pt3d = new Point3d(1320, 1120, 0);
pt3d.TransformBy(mat);
ptCol.Add(new Point2d(pt3d.X, pt3d.Y));

// Define the normal and elevation for the clipping boundary
Vector3d normal;
double elev = 0;

if (acCurDb.TileMode == true)
{
normal = acCurDb.Ucsxdir.CrossProduct(acCurDb.Ucsydir);
elev = acCurDb.Elevation;
}
else
{
normal = acCurDb.Pucsxdir.CrossProduct(acCurDb.Pucsydir);
elev = acCurDb.Pelevation;
}

// Set the clipping boundary and enable it
using (Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilter filter =
new Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilter())
{
Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilterDefinition filterDef =
new Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilterDefinition(ptCol, normal, elev, 0, 0, true);
filter.Definition = filterDef;

// Define the name of the extension dictionary and entry name
string dictName = "ACAD_FILTER";
string spName = "SPATIAL";

// Check to see if the Extension Dictionary exists, if not create it
if (acBlkRef.ExtensionDictionary.IsNull)
{
acBlkRef.CreateExtensionDictionary();
}

// Open the Extension Dictionary for write
DBDictionary extDict = acTrans.GetObject(acBlkRef.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;

// Check to see if the dictionary for clipped boundaries exists,
// and add the spatial filter to the dictionary
if (extDict.Contains(dictName))
{
DBDictionary filterDict = acTrans.GetObject(extDict.GetAt(dictName), OpenMode.ForWrite) as DBDictionary;

if (filterDict.Contains(spName))
{
filterDict.Remove(spName);
}

filterDict.SetAt(spName, filter);
}
else
{
using (DBDictionary filterDict = new DBDictionary())
{
extDict.SetAt(dictName, filterDict);

acTrans.AddNewlyCreatedDBObject(filterDict, true);
filterDict.SetAt(spName, filter);
}
}

// Append the spatial filter to the drawing
acTrans.AddNewlyCreatedDBObject(filter, true);
}
}

Application.ShowAlertDialog("The external reference is clipped.");
}

// Save the new objects to the database
acTrans.Commit();

// Dispose of the transaction
}
}

加载和外部参照性能(.NET)

通过将按需加载和保存带有索引的图形相结合,可以提高带有外部参照的图形的性能。按需加载与XLOADCTL和INDEXCTL系统变量一起使用。打开按需加载时,如果索引已保存在参照图形中,AutoCAD将仅将参照图形中重生成当前图形所需的数据加载到内存中。换句话说,引用的材料是“按需”阅读的。

要打开按需加载,请使用 SetSystemVariable 方法设置XLOADCTL系统变量。以下设置适用于XLOADCTL系统变量:

  • 0 =关闭按需加载。将加载整个图形
  • 1 =打开按需加载。参照图形保持打开和锁定状态
  • 2 =打开按需加载。参照图形的副本将打开并锁定;参照图形不会锁定

默认情况下,XLOADCTL设置为2,并与AutoCAD用户配置一起存储。

要实现按需加载的最大好处,需要使用图层和空间索引保存参照图形。当剪裁外部参照以显示其一小部分,并且空间索引保存在外部参照图形中时,按需加载的性能优势最为明显。

冻结外部参照的多个图层,外部参照图形将与图层索引一起保存。

要启用图层和空间索引,请使用 SetSystemVariable 方法设置INDEXCTL系统变量。以下设置适用于INDEXCTL系统变量:

  • 0 =未创建索引
  • 1 =已创建图层索引
  • 2 =已创建空间索引
  • 3 =同时创建空间索引和图层索引

默认情况下,创建新AutoCAD图形时,INDEXCTL设置为0。

有关按需加载和外部参照的详细信息,请参见产品帮助系统中的“关于在使用外部参照时提高性能”。

分配和删除扩展数据(.NET)

可以使用扩展数据作为将信息与图形中的对象链接的方法。

将扩展数据指定给选择集中的所有对象

此示例提示用户从图形中选择对象。选定的对象将放置到选择集中,指定的扩展数据将附着到该选择集中的所有对象。

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
[CommandMethod("AttachXDataToSelectionSetObjects")]
public void AttachXDataToSelectionSetObjects()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

Document acDoc = Application.DocumentManager.MdiActiveDocument;

string appName = "MY_APP";
string xdataStr = "This is some xdata";

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Request objects to be selected in the drawing area
PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();

// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
// Open the Registered Applications table for read
RegAppTable acRegAppTbl;
acRegAppTbl = acTrans.GetObject(acCurDb.RegAppTableId, OpenMode.ForRead) as RegAppTable;

// Check to see if the Registered Applications table record for the custom app exists
if (acRegAppTbl.Has(appName) == false)
{
using (RegAppTableRecord acRegAppTblRec = new RegAppTableRecord())
{
acRegAppTblRec.Name = appName;

acTrans.GetObject(acCurDb.RegAppTableId, OpenMode.ForWrite);
acRegAppTbl.Add(acRegAppTblRec);
acTrans.AddNewlyCreatedDBObject(acRegAppTblRec, true);
}
}

// Define the Xdata to add to each selected object
using (ResultBuffer rb = new ResultBuffer())
{
rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, xdataStr));

SelectionSet acSSet = acSSPrompt.Value;

// Step through the objects in the selection set
foreach (SelectedObject acSSObj in acSSet)
{
// Open the selected object for write
Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
OpenMode.ForWrite) as Entity;

// Append the extended data to each object
acEnt.XData = rb;
}
}
}

// Save the new object to the database
acTrans.Commit();

// Dispose of the transaction
}
}

查看选择集中所有对象的扩展数据
此示例显示与上一示例附着的扩展数据。如果附加的扩展数据不是字符串(类型1000),则需要修改此代码。