Public Shared Function InsertLayer(ByRef doc As Document, ByRef ed As Editor, ByRef db As Autodesk.AutoCAD.DatabaseServices.Database, ByRef dtKlicLayerPrefList As System.Data.DataTable, strLayName As String, sLayerBase As String)
Using acLockDoc As DocumentLock = doc.LockDocument()
Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = db.TransactionManager.StartTransaction
Try
Dim ltTable As Autodesk.AutoCAD.DatabaseServices.LayerTable = acTrans.GetObject(db.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
If ltTable.Has(strLayName) Then
'layer exists
'TODO: check layer settings
Else
'create layer
Dim ltTableR As Autodesk.AutoCAD.DatabaseServices.LayerTableRecord = New Autodesk.AutoCAD.DatabaseServices.LayerTableRecord()
ltTableR.Name = strLayName
ltTable.UpgradeOpen()
ltTable.Add(ltTableR)
Try
Dim arrLayerBase
() As String = sLayerBase
.Split("_") Dim expression As String = "[type] = '" & arrLayerBase(0) & "'"
Dim drResults() As System.Data.DataRow = dtKlicLayerPrefList.Select(expression)
If drResults.Length > 0 Then
For Each row As DataRow In drResults
ltTableR.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, CShort(row(1).ToString))
Dim oLineWeight As Autodesk.AutoCAD.DatabaseServices.LineWeight
Dim sLineWeight As String = row(4).ToString
If sLineWeight.Length > 0 Then
oLineWeight = CInt(sLineWeight.Substring(sLineWeight.Length - 3))
ltTableR.LineWeight = oLineWeight
End If
ltTableR.Description = arrLayerBase(1) & " " & row(6).ToString
Dim sLineType As String = row(2)
Dim sLinetTypeSuff As String = row(3)
Dim acLineTypeTbl As LinetypeTable = acTrans.GetObject(db.LinetypeTableId, OpenMode.ForWrite)
If acLineTypeTbl.Has(sLineType) = False Then
'create custom linetype bases on info from DB
Dim tt As TextStyleTable = acTrans.GetObject(db.TextStyleTableId, OpenMode.ForRead)
Dim ltr As LinetypeTableRecord = New LinetypeTableRecord()
ltr.Name = "ITM " & sLineType
ltr.AsciiDescription = " ---- " & sLineType & " ---- "
ltr.PatternLength = 3.0
ltr.NumDashes = 3
'dash #1
ltr.SetDashLengthAt(0, 4)
'dash #2
ltr.SetDashLengthAt(1, -4)
ltr.SetShapeStyleAt(1, tt("Standard"))
ltr.SetShapeNumberAt(1, 0)
'text size
Dim iLtTXTscale As Integer = 1
Dim iLtTXTYpos As Double = ((iLtTXTscale / 2) * -1)
ltr.SetShapeOffsetAt(1, New Vector2d(-0.8, iLtTXTYpos))
ltr.SetShapeScaleAt(1, iLtTXTscale)
ltr.SetShapeIsUcsOrientedAt(1, False)
ltr.SetShapeRotationAt(1, 0)
ltr.SetTextAt(1, sLineType)
'dash #3
Dim iLtDashSize As Integer = (sLineType.Length * iLtTXTscale) * -1
ltr.SetDashLengthAt(2, iLtDashSize)
Dim ltId As ObjectId = acLineTypeTbl.Add(ltr)
acTrans.AddNewlyCreatedDBObject(ltr, True)
ltTableR.LinetypeObjectId = ltId
End If
Next
End If
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox("Error creating linetype" & vbcrlf
7 ex
.Message.ToString) End Try
acTrans.AddNewlyCreatedDBObject(ltTableR, True)
End If
acTrans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox("Error creating layer " & strLayName
& vbcrlf
& ex
.Message.ToString) End Try
End Using
End Using
End Function