Author Topic: Need help with creation of MultiViewBLock  (Read 5055 times)

0 Members and 1 Guest are viewing this topic.

mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #15 on: March 06, 2012, 05:42:40 AM »
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


kaefer

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #16 on: March 06, 2012, 07:06:58 AM »
After a good night sleep i changed the code to the code below.

Congratulation!

Concerning the documentation, there do exist Windows Help Files (e.g. AecBaseMgd.chm), but their content seems to be largely generated from inline XML documentation tags, so they won't tell you anything which isn't already apparent from the Object Browser.

Just for completeness sake, here's the Linq version. The call to the Cast extension seems to generate more efficient code than just supplying the type to the iteration variable, which would involve a delegate and a null check.
Code - Visual Basic: [Select]
  1.     Shared Sub SetDispRep(mvbDef As MultiViewBlockDefinition, blockId As acadObjectId, dispRepName As String)
  2.         Dim dispRepMgr As New DisplayRepresentationManager()
  3.         Dim allDispRepWorkForMVBRef =
  4.             dispRepMgr.GetAllDisplayRepresentationsWorkForSpecifiedClass(
  5.                RXClass.GetClass(GetType(MultiViewBlockReference)))
  6.         Dim allDispReps =
  7.             From oid In allDispRepWorkForMVBRef.Cast(Of acadObjectId)()
  8.             Let dispRep As DisplayRepresentation = oid.GetObject(OpenMode.ForRead)
  9.             Where dispRep.DisplayRepresentationName.Equals(dispRepName, System.StringComparison.OrdinalIgnoreCase)
  10.             Select dispRep.ObjectId
  11.         For Each dispRepId In allDispReps
  12.             Dim mvbViewDef As New MultiViewBlockViewDefinition() With {.BlockId = blockId}
  13.             mvbViewDef.SetAllViews(True)
  14.             Dim mvbDispRep As New MultiViewBlockDisplayRepresentationDefinition() With {.DisplayRepresentationId = dispRepId}
  15.             mvbDispRep.ViewDefinitions.Add(mvbViewDef)
  16.             mvbDef.DisplayRepresentationDefinitions.Add(mvbDispRep)
  17.         Next
  18.     End Sub