Code:
Public Class Class1 <CommandMethod("AddEmptyBlockDefinition")>
Public Sub AddEmptyBlockDefinition()
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Using mytrans As Transaction = myDB.TransactionManager.StartTransaction
Dim myBlockTable As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForWrite)
'--Abfrage ob der Blockbereits vorhanden ist??
If myBlockTable.Has("CircleBlock") = True Then
MsgBox("Bitte den Block CircleBlock löschen", MsgBoxStyle.Information)
Exit Sub
End If
Dim myNewBlock As New BlockTableRecord
myNewBlock.Name = "CircleBlock"
'BlockName = "CircleBlock"
Dim myCircle As New Circle(Point3d.Origin, Vector3d.ZAxis, 5)
myNewBlock.AppendEntity(myCircle)
myBlockTable.Add(myNewBlock)
mytrans.AddNewlyCreatedDBObject(myNewBlock, True)
Dim STK_STUECK, STK_FLAENGE, STK_FBREITE, STK_FDICKE, STK_BEST_TEXT, MATGRUPPE_ID, STK_END_TERMIN, STK_ADR_ID, STK_PD_NUM As New AttributeDefinition
Dim myAttRef As New AttributeReference
With STK_STUECK
.Prompt = "Stückzahl eingeben:"
.Tag = "STK_STUECK"
.TextString = "1"
.Position = New Point3d(0, -5, 0)
.Justify = AttachmentPoint.BaseLeft
.Height = 2.5
.ColorIndex = 1
'.Preset = True
End With
myNewBlock.AppendEntity(STK_STUECK)
mytrans.AddNewlyCreatedDBObject(STK_STUECK, True)
With STK_FLAENGE
.Prompt = "Länge eingeben:"
.Tag = "STK_FLAENGE"
.TextString = "1000"
.Position = New Point3d(0, -10, 0)
.Justify = AttachmentPoint.BaseLeft
.Height = 2.5
.ColorIndex = 1
'.Preset = True
End With
myNewBlock.AppendEntity(STK_FLAENGE)
mytrans.AddNewlyCreatedDBObject(STK_FLAENGE, True)
mytrans.Commit()
End Using
End Sub
Public Function AddEntity(ByVal DatabaseIn As Database, ByVal EntityToAdd As Entity, ByVal BlockName As String) As ObjectId
Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
Dim myBlockTable As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForWrite)
Dim myBlockTableRecord As BlockTableRecord = Nothing
If myBlockTable.Has(BlockName) = True Then
myBlockTableRecord = myBlockTable(BlockName).GetObject(OpenMode.ForWrite)
Else
myBlockTableRecord = New BlockTableRecord
myBlockTableRecord.Name = BlockName
myBlockTable.Add(myBlockTableRecord)
myTrans.AddNewlyCreatedDBObject(myBlockTableRecord, True)
End If
myBlockTableRecord.AppendEntity(EntityToAdd)
myTrans.AddNewlyCreatedDBObject(EntityToAdd, True)
myTrans.Commit()
Return EntityToAdd.Id
End Using
End Function
<CommandMethod("InsertBlock")>
Public Sub InsertBlock()
Dim acDoc As Document = DocumentManager.MdiActiveDocument
Dim ed As Editor = acDoc.Editor
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Using mytrans As Transaction = myDB.TransactionManager.StartTransaction
Dim myBlockTable As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)
Dim PPResu As PromptPointResult
Dim instPkt As New Point3d
Dim PPOpt As PromptPointOptions = New PromptPointOptions("")
PPOpt.Message = vbLf & "Einfügepunkt festlegen: "
PPResu = ed.GetPoint(PPOpt)
instPkt = PPResu.Value
Dim myBlockRef As New BlockReference(instPkt, myBlockTable("CircleBlock"))
AddEntity(myDB, myBlockRef, BlockTableRecord.ModelSpace)
mytrans.Commit()
End Using
End Sub
Public Function InsertBlock(ByVal DatabaseIn As Database, _
ByVal BTRToAddTo As String, _
ByVal InsPt As Point3d, _
ByVal BlockName As String, _
ByVal XScale As Double, _
ByVal YScale As Double, _
ByVal ZScale As Double) As ObjectId
Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
Dim myBlockTable As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForRead)
If myBlockTable.Has(BlockName) = False Then
Return Nothing
End If
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)
Dim myBlockRef As New BlockReference(InsPt, myBlockDef.Id)
myBlockRef.ScaleFactors = New Scale3d(XScale, YScale, ZScale)
myBlockTableRecord.AppendEntity(myBlockRef)
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
Dim myAttColl As AttributeCollection = myBlockRef.AttributeCollection
For Each myEntID As ObjectId In myBlockDef
Dim myEnt As Entity = myEntID.GetObject(OpenMode.ForRead)
If TypeOf myEnt Is AttributeDefinition Then
Dim myAttDef As AttributeDefinition = myEnt
Dim myAttRef As New AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttDef, True)
End If
Next
myTrans.Commit()
Return myBlockRef.Id
End Using
End Function
End Class