After a good night sleep i changed the code to the code below.
I seems to do the trick.... DO you guys have tips about the code?
<CommandMethod("CreateMvBlock")> _
Public Sub MyMvBlock()
Dim MyDB As Database = HostApplicationServices.WorkingDatabase
Dim MvBlockName As String = "Marcos_MVBlock"
Dim RoutingBlockName As String = "RoutingBlock"
Dim ProgrammatieBlockName As String = "ProgrammatieBlock"
Dim BekistingBlockName As String = "BekistingBlock"
Dim MvbStyleDict As New AecDb.DictionaryMultiViewBlockDefinition(MyDB)
Using trans As Transaction = MyDB.TransactionManager.StartTransaction
'Check if the MvBlock exists...
If Not MvbStyleDict.Has(MvBlockName, trans) Then
' Het MvBlock doesn't exist... create it...
Dim MyMvbDef As New MultiViewBlockDefinition
MyMvbDef.SetToStandard(MyDB)
MyMvbDef.SubSetDatabaseDefaults(MyDB)
''trans.Commit()
' Open the Database for Read...
Dim MyBT As BlockTable = MyDB.BlockTableId.GetObject(OpenMode.ForRead)
' Get the ID of the RoutingBlock
Dim AcBlockId = MyBT(RoutingBlockName)
' Set the DisplayRepresentationDef
SetDispRep(MyMvbDef, AcBlockId, "Model (KNS Routing)")
SetDispRep(MyMvbDef, AcBlockId, "Plan (KNS Routing)")
SetDispRep(MyMvbDef, AcBlockId, "General (KNS Routing)")
AcBlockId = MyBT(BekistingBlockName)
' Set the DisplayRepresentationDef
SetDispRep(MyMvbDef, AcBlockId, "Model (KNS Bekisting)")
SetDispRep(MyMvbDef, AcBlockId, "Plan (KNS Bekisting)")
SetDispRep(MyMvbDef, AcBlockId, "General (KNS Bekisting)")
AcBlockId = MyBT(ProgrammatieBlockName)
' Set the DisplayRepresentationDef
SetDispRep(MyMvbDef, AcBlockId, "Model (KNS Programmatie)")
SetDispRep(MyMvbDef, AcBlockId, "Plan (KNS Programmatie)")
SetDispRep(MyMvbDef, AcBlockId, "General (KNS Programmatie)")
MvbStyleDict.AddNewRecord(MvBlockName, MyMvbDef)
trans.Commit()
MsgBox("Mvblock added")
End If
End Using
End Sub
Private Shared Sub SetDispRep(mvbDef As MultiViewBlockDefinition, blockId As ObjectId, dispRepName As String)
' Open all DisplayRepresentations for specified type and
' filter them by name
Dim dispRepMgr As New DisplayRepresentationManager()
Dim allDispReps As Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim trans As Transaction = db.TransactionManager.StartTransaction()
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim ids As ObjectIdCollection = dispRepMgr.GetAllDisplayRepresentationsWorkForSpecifiedClass(RXObject.GetClass(GetType(MultiViewBlockReference)))
Using trans
allDispReps = New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection()
Dim id As ObjectId
' Loop through the DisplayRepresentations found...
For Each id In ids
Dim dr As DisplayRepresentation = trans.GetObject(id, OpenMode.ForRead)
dr = trans.GetObject(id, OpenMode.ForRead)
If dr.DisplayRepresentationName.Equals(dispRepName, System.StringComparison.OrdinalIgnoreCase) Then
allDispReps.Add(id)
End If
Next id
For Each dispRepId As ObjectId In allDispReps
Dim mvbViewDef As New MultiViewBlockViewDefinition()
With mvbViewDef
.BlockId = blockId
.SetAllViews(True)
Dim mvbDispRep As New MultiViewBlockDisplayRepresentationDefinition()
With mvbDispRep
.DisplayRepresentationId = dispRepId
mvbDispRep.ViewDefinitions.Add(mvbViewDef)
mvbDef.DisplayRepresentationDefinitions.Add(mvbDispRep)
End With
End With
Next
trans.Commit()
End Using
End Sub