The full code gets rather long but on further investigation i am getting an eLockViolation at this point in the code:
Dim myBlockTableRecord As BlockTableRecord = _
myBlockTable(BTRToAddTo).GetObject(OpenMode.ForWrite)
The full code is here:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.LayerManager
Imports Autodesk.AutoCAD.Windows
Public Class Commands
<CommandMethod("DrawShed")> _
Public Sub DrawShed()
Dim recFrm As New ShedForm()
Application.ShowModelessDialog(recFrm)
End Sub
End Class
Public Class InsBlock
Public Function InsertBlock(ByVal DatabaseIn As Database, _
ByVal BTRToAddTo As String, _
ByVal InsPt As Geometry.Point3d, _
ByVal BlockName As String, _
ByVal XScale As Double, _
ByVal YScale As Double, _
ByVal ZScale As Double) As DatabaseServices.ObjectId
Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
Dim myBlockTable As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForRead)
'If the suppplied Block Name is not
'in the specified Database, get out gracefully.
If myBlockTable.Has(BlockName) = False Then
Return Nothing
End If
'If the specified BlockTableRecord does not exist,
'get out gracefully
If myBlockTable.Has(BTRToAddTo) = False Then
Return Nothing
End If
Dim myBlockDef As BlockTableRecord = _
myBlockTable(BlockName).GetObject(OpenMode.ForRead)
Dim myBlockTableRecord As BlockTableRecord = _
myBlockTable(BTRToAddTo).GetObject(OpenMode.ForWrite)
'Create a new BlockReference
Dim myBlockRef As New BlockReference(InsPt, myBlockDef.Id)
'Set the scale factors
myBlockRef.ScaleFactors = New Geometry.Scale3d(XScale, YScale, ZScale)
'Add the new BlockReference to the specified BlockTableRecord
myBlockTableRecord.AppendEntity(myBlockRef)
'Add the BlockReference to the BlockTableRecord.
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
Dim myAttColl As DatabaseServices.AttributeCollection = _
myBlockRef.AttributeCollection
'Find Attributes and add them to the AttributeCollection
'of the BlockReference
For Each myEntID As ObjectId In myBlockDef
Dim myEnt As Entity = myEntID.GetObject(OpenMode.ForRead)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
Next
myTrans.Commit()
Return myBlockRef.Id
End Using
End Function
Function SetParameter(ByVal BlockID As ObjectId, ByVal ParameterName As String, _
ByVal Value As Double) As Boolean
Using myTrans As Transaction = BlockID.Database.TransactionManager.StartTransaction
Dim myBRef As BlockReference = BlockID.GetObject(OpenMode.ForRead)
For Each myDynamProp As DynamicBlockReferenceProperty In _
myBRef.DynamicBlockReferencePropertyCollection
If myDynamProp.PropertyName.Equals( _
ParameterName, StringComparison.OrdinalIgnoreCase) = True Then
myDynamProp.Value = Value
myTrans.Commit()
Return True
Exit For
End If
Next
Return False
End Using
End Function
<CommandMethod("SizeShed")> _
Public Sub SizeShed()
Dim myBlockID As ObjectId = InsertBlock(HostApplicationServices.WorkingDatabase, _
BlockTableRecord.ModelSpace, _
New Point3d(1, 1, 0), "Plan_Base", 1, 1, 1)
SetParameter(myBlockID, "P_Len", 30000)
SetParameter(myBlockID, "P_Width", 20000)
SetParameter(myBlockID, "Ridge_Height_LE", 8000)
SetParameter(myBlockID, "Ridge_Height_RE", 8000)
SetParameter(myBlockID, "Shed_Height_LE", 5000)
SetParameter(myBlockID, "Shed_Height_RE", 5000)
SetParameter(myBlockID, "Shed_Height_Front", 5000)
SetParameter(myBlockID, "Ridge_Height_Front", 8000)
SetParameter(myBlockID, "Shed_Height_Back", 5000)
SetParameter(myBlockID, "Ridge_Height_Back", 8000)
End Sub
End Class