1.8、Assign a Linetype to a Layer指定图层线型
When you are defining layers,linetypes provide another way to convey visual information. A linetype is a repeating pattern of dashes,dots,and blank spaces you can use to distinguish the purpose of one line from another.
定义图层时,线型提供了传达视觉信息的另一个手段。线型是划线、点及空格的重复图案,可以用来区分不同线条的作用。
The linetype name and definition describe the particular dash-dot sequence,the relative lengths of dashes and blank spaces,and the characteristics of any included text or shapes.
线型名称和定义描述了不同线型的点划序列、划线或空白的相对长度、包含的文字或形状的特性等。
Use the Linetype property to assign a linetype to a layer. This property takes the name of the linetype as input.
使用Linetype属性来设置图层的线型,该属性以线型名作为输入参数。
Note Before a linetype can be assigned to a layer it must be defined in the drawing first. For information on working with linetypes,seeWork with Linetypes.
注意:线型赋给图层前必须先在图形中定义。关于使用线型的内容,见AutoCAD用户指南中的“使用线型”。
Set the linetype for a layer 设置图层线型
The following example creates a new layer named "ABC" and assigns it the "Center" linetype.
下面示例创建一个名为“ABC”的新图层,设置该图层线型为“Center”。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("SetLayerLinetype")> _
Public Sub SetLayerLinetype()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Layer table for read
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,_
OpenMode.ForRead)
Dim sLayerName As String = "ABC"
Dim acLyrTblRec As LayerTableRecord
If acLyrTbl.Has(sLayerName) = False Then
acLyrTblRec = New LayerTableRecord()
'' Assign the layer a name
acLyrTblRec.Name = sLayerName
'' Upgrade the Layer table for write
acLyrTbl.UpgradeOpen()
'' Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec)
acTrans.AddNewlyCreatedDBObject(acLyrTblRec,True)
Else
acLyrTblRec = acTrans.GetObject(acLyrTbl(sLayerName),_
OpenMode.ForRead)
End If
'' Open the Layer table for read
Dim acLinTbl As LinetypeTable
acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,_
OpenMode.ForRead)
If acLinTbl.Has("Center") = True Then
'' Upgrade the Layer Table Record for write
acLyrTblRec.UpgradeOpen()
'' Set the linetype for the layer
acLyrTblRec.LinetypeObjectId = acLinTbl("Center")
End If
'' Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
C#
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("SetLayerLinetype")]
public static void SetLayerLinetype()
{
// Get the current document and database获取当前文档和数据库
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Layer table for read以读打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
// Assign the layer a name图层名称
acLyrTblRec.Name = sLayerName;
// Upgrade the Layer table for write升级打开图层表
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction将新图层添加到图层表,登记事务记录
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec,true);
}
else
{
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForRead) as LayerTableRecord;
}
// Open the Layer table for read以读打开块表
LinetypeTable acLinTbl;
acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
if (acLinTbl.Has("Center") == true)
{
// Upgrade the Layer Table Record for write升级打开图层表记录
acLyrTblRec.UpgradeOpen();
// Set the linetype for the layer设置图层线型
acLyrTblRec.LinetypeObjectId = acLinTbl["Center"];
}
// Save the changes and dispose of the transaction保存修改关闭事务
acTrans.Commit();
}
}
VBA/ActiveX Code Reference
Sub SetLayerLinetype()
On Error Resume Next
Dim layerObj As AcadLayer
Set layerObj = ThisDrawing.Layers.Add("ABC")
layerObj.Linetype = "Center"
End Sub