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

0 Members and 1 Guest are viewing this topic.

mgreven

  • Guest
Need help with creation of MultiViewBLock
« on: March 04, 2012, 12:29:46 PM »
Hello,

I figured out how to create a New MultiViewBLock Definition with the code below.
Can someone show me how to set the Acad Block "TestBlock" to the "Plan" DisplayRepresentation of the MultiViewBlock?

I get Lost in the collections and object of the MultiViewBlockDisplayRepresentationDefinition and MultiViewBlockViewInstance.
From the Database i know that a MultiViewBlockDefinition has references to DisplayRepresentationDefinitions which holds MultiViewBlockViewInstances.

The code i have so far is:

        <CommandMethod("TestMvBlock")> _
        Public Sub MyMvBlock()
            Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim MyDB As Database = HostApplicationServices.WorkingDatabase
            Dim Trans As Transaction = MyDB.TransactionManager.StartTransaction

            Dim ObjID As ObjectId
            Dim MvBlockName As String = "Marcos_MVBlock"
            Dim AcBlockName As String = "RoutingBlock"
            Dim MvbStyleDict As New AecDb.DictionaryMultiViewBlockDefinition(MyDB)

            Try
                ' Check if the MvBlock exists
                ObjID = MvbStyleDict.GetAt(MvBlockName)
                trans.Commit()

            Catch ex As Autodesk.AutoCAD.Runtime.Exception

                If ex.ErrorStatus = ErrorStatus.KeyNotFound Then
                    ' The MvBlock doesn't exist... Create the MvBlock...
                    Dim MyMvbDef As New MultiViewBlockDefinition
                    MyMvbDef.SetToStandard(MyDB)
                    MyMvbDef.SubSetDatabaseDefaults(MyDB)

                    MvbStyleDict.AddNewRecord(MvBlockName, MyMvbDef)

                    trans.AddNewlyCreatedDBObject(MyMvbDef, True)
                    trans.Commit()
                    MsgBox("Mvblock added")

                Else
                    MsgBox("An Error has occurred." & vbCr & ex.Message)
                End If

            Finally
                trans.Dispose()
            End Try
        End Sub


        Kind Regards,

        Marco

 

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Need help with creation of MultiViewBLock
« Reply #1 on: March 04, 2012, 04:26:29 PM »
Marco
I can't make time at the moment to look at your issue but I will offer this advice:
Do not use an Exception trap to direct your program logic.

Elegance aside, it indicates that the process flow for the program has not been thought through.
Regards
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Need help with creation of MultiViewBLock
« Reply #2 on: March 04, 2012, 04:33:56 PM »
... also, do some study on the use of database transactions.
The call to trans.dispose would not be necessary if your routine made use of the more conventional
using trans construct.

Regards,
« Last Edit: March 04, 2012, 06:05:54 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #3 on: March 04, 2012, 04:45:11 PM »
Thanks for your respons Kerry.

I am new to .NET and have a lot to learn.
I am more a Lisp guy, but for some things i need to get it done in VB .Net.

I am learning by study code found on the internet.

Regards,

Marco

kaefer

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #4 on: March 04, 2012, 04:54:58 PM »
Can someone show me how to set the Acad Block "TestBlock" to the "Plan" DisplayRepresentation of the MultiViewBlock?

Can I demonstrate a way instead to supply the ObjectId of a BlockDefinition to a MultiViewBlockViewDefinition which is added to a MultiViewBlockDisplayRepresentationDefinition pointing to the "Plan" DisplayRepresentation? Love this stuff...

The hardest part is getting the ObjectId of the DisplayRepresentation and I'm not at all certain if this is the best place to deploy a Linq query.

Code - C#: [Select]
  1. static void SetDispRep(
  2.     MultiViewBlockDefinition mvbDef,
  3.     acadObjectId blockId,
  4.     string dispRepName,
  5.     ViewDirection[] viewDirs)
  6. {
  7.     DisplayRepresentationManager dispRepMgr =
  8.         new DisplayRepresentationManager();
  9.     // Open all DisplayRepresentations for specified type and
  10.     // filter them by name
  11.     IEnumerable<DisplayRepresentation> allDispReps =
  12.         from acadObjectId oid in
  13.             dispRepMgr.GetAllDisplayRepresentationsWorkForSpecifiedClass(
  14.                 RXClass.GetClass(typeof(MultiViewBlockReference)))
  15.         select (DisplayRepresentation)oid.GetObject(OpenMode.ForRead) into dispRep
  16.         where dispRep.DisplayRepresentationName.Equals(
  17.             dispRepName, System.StringComparison.OrdinalIgnoreCase)
  18.         select dispRep;
  19.     foreach (DisplayRepresentation dispRep in allDispReps)
  20.     {
  21.         MultiViewBlockViewDefinition mvbViewDef =
  22.             new MultiViewBlockViewDefinition() { BlockId = blockId };
  23.         foreach (ViewDirection viewDir in viewDirs)
  24.             mvbViewDef.SetViewOn(viewDir, true);
  25.         MultiViewBlockDisplayRepresentationDefinition mvbDispRep =
  26.             new MultiViewBlockDisplayRepresentationDefinition() { DisplayRepresentationId = dispRep.ObjectId };
  27.         mvbDispRep.ViewDefinitions.Add(mvbViewDef);
  28.         mvbDef.DisplayRepresentationDefinitions.Add(mvbDispRep);
  29.     }
  30. }

The calling site, before the MultiViewBlockDefinition is added to the Dictionary:
Code - C#: [Select]
  1.     BlockTable bt =
  2.         (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
  3.     SetDispRep(
  4.         mvbDef,
  5.         bt[acBlockName],
  6.         "Plan",
  7.         new ViewDirection[] { ViewDirection.Top, ViewDirection.Bottom });

mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #5 on: March 04, 2012, 06:10:40 PM »
Thanks for the respons....
Great to get a respons so quick at this time.


I am not familiar with c#, but this does the trick?
What i am trying to do is make a MultiViewBlock, and assign 'blockA' to 'Plan general' / 'blockB' to 'model Medium detail' / 'blockC' to 'Plan Medium detail' displayrepresentation of that multiviewblock.

Do you have a VB .net example?

Regards,


marco



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

kaefer

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #7 on: March 05, 2012, 01:59:04 AM »
What i am trying to do is make a MultiViewBlock, and assign 'blockA' to 'Plan general' / 'blockB' to 'model Medium detail' / 'blockC' to 'Plan Medium detail' displayrepresentation of that multiviewblock.

Yes, that should do the trick; just call the sub for each DisplayRepresentation you want defined. Except that those names do not seem to signify a DisplayRepresentation, but a DisplaySet or DisplayConfiguration.

Quote
Do you have a VB .net example?

Did you look at the VB.NET samples, especially AecDisplaySampleMgd\DisplySample.vb? There's a section titled "Get All the AecDbMassElem/Massgroup Display Reps for the current display config" in it, which would be broadly relevant to this task.

Another one is ConvertToMvPart\ConvertToMvPart.cs (curiously missing from the VB.NET folder), which does a similar thing for a MultiViewPart.

At any rate, heed Kerry's advice.

mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #8 on: March 05, 2012, 02:14:54 AM »
I did not know the existance of the converters... that makes live easier :wink:

I looked at the VB.net sample and the section with the "Get All the AecDbMassElem/Massgroup Display Reps for the current display config", but i could not figure out how to transform this sample to a working situation for creating a MultiViewBlock with some referenceblocks for the different displayrepresentation.

With Lisp i was able to get all the DisplayRepDefs, ViewBlockDefs and finally the refferenced blocks.
But i was not able to create a MVBlock.

I will try further with your code Keafer... Thanks!


mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #9 on: March 05, 2012, 09:06:50 AM »
I converted the code to VB. net and had trouble with the Linq translation so i changed the code.
The code is giving me a exeption.... what is going wrong... Argh this is frustrating.


        <CommandMethod("TestMvBlock")> _
        Public Sub TestMvBlock()
            Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim MyDB As Database = HostApplicationServices.WorkingDatabase
            Dim Trans As Transaction = MyDB.TransactionManager.StartTransaction

            Dim ObjID As ObjectId
            Dim MvBlockName As String = "Marcos_MVBlock"
            Dim AcBlockName As String = "RoutingBlock"
            Dim MvbStyleDict As New AecDb.DictionaryMultiViewBlockDefinition(MyDB)

            Try
                ' Check if the MvBlock exists
                ObjID = MvbStyleDict.GetAt(MvBlockName)
                Trans.Commit()

            Catch ex As Autodesk.AutoCAD.Runtime.Exception

                If ex.ErrorStatus = ErrorStatus.KeyNotFound Then
                    ' The MvBlock doesn't exist... Create the MvBlock...
                    Dim MyMvbDef As New MultiViewBlockDefinition
                    MyMvbDef.SetToStandard(MyDB)
                    MyMvbDef.SubSetDatabaseDefaults(MyDB)

                    ' Check if the block exists
                    Dim MyBT As BlockTable
                    Dim AcBlock As BlockTableRecord

                    MyBT = MyDB.BlockTableId.GetObject(OpenMode.ForRead)
                    AcBlock = MyBT(AcBlockName).GetObject(OpenMode.ForRead)

                    ' Set the DisplayRepresentationDef
                    SetDispRep(MyMvbDef, AcBlock.Id, "Plan (MyStyle)")

                    MvbStyleDict.AddNewRecord(MvBlockName, MyMvbDef)

                    Trans.AddNewlyCreatedDBObject(MyMvbDef, True)
                    Trans.Commit()
                    MsgBox("Mvblock added")

                Else
                    MsgBox("An Error has occurred." & vbCr & ex.Message)
                End If

            Finally
                Trans.Dispose()
            End Try
        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 Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection = DisplayRepresentationManager.GetActiveDisplayRepresentationSets(db)

            'Dim acds As DisplaySet
            Dim ds As DisplaySet = New DisplaySet()
            allDispReps = Nothing

            Try
                Dim id As ObjectId
                ' loop through the active disp sets (i.e., disp sets of active dis confid.)
                For Each id In ids
                    Dim acds As DisplaySet = trans.GetObject(id, OpenMode.ForRead)

                    Dim drid As ObjectId
                    ' look through the set of dispRep of the given disp set.
                    For Each drid In acds.DisplayRepresentationIds
                        Dim dr As DisplayRepresentation = trans.GetObject(drid, OpenMode.ForRead)
                        dr = trans.GetObject(drid, OpenMode.ForRead)
                        Dim rc As RXClass = dr.WorksWith
                        If (rc.Name.CompareTo("AecDbMvBlockRef") = 0) Then
                            If (ds.DisplayRepresentationIds.Contains(drid)) Then
                                Continue For
                            Else
                                'ds.DisplayRepresentationIds.Add(drid)
                                allDispReps.Add(drid)
                            End If
                        End If
                    Next drid
                Next id

                For Each dispRep As DisplayRepresentation In allDispReps

                    Dim mvbViewDef As New MultiViewBlockViewDefinition()
                    With mvbViewDef

                        .BlockId = blockId
                        .SetAllViews(True)

                        Dim mvbDispRep As New MultiViewBlockDisplayRepresentationDefinition()
                        With mvbDispRep
                            .DisplayRepresentationId = dispRep.ObjectId
                            mvbDispRep.ViewDefinitions.Add(mvbViewDef)
                            mvbDef.DisplayRepresentationDefinitions.Add(mvbDispRep)
                        End With

                    End With
                Next

                ''trans.Commit()

            Finally
                ''trans.Dispose()
            End Try


        End Sub


mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #10 on: March 05, 2012, 10:41:15 AM »
And this must be the result:


kaefer

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #11 on: March 05, 2012, 11:16:23 AM »
And this must be the result:

Nope. If you iron out the quirks in your code, you will have managed to set the ViewBlock against the name of a DisplaySet the active DisplaySets, not the name of a DisplayRepresentation as in your picture. Which is not bad at all...

Now on to the ironing:
Code - Visual Basic: [Select]
  1.         Using Trans As Transaction = MyDB.TransactionManager.StartTransaction()
  2.             ...
  3.             Trans.Commit()
  4.         End Using
This is what Kerry meant by the conventional approach to the disposal of Transactions.

Code - Visual Basic: [Select]
  1.             ' Check if the MvBlock exists
  2.            If Not MvbStyleDict.Has(MvBlockName, Trans) Then
  3.             ...
  4.             End If
This is what Kerry meant by the avoidance of the use of exceptions for control flow.

Code - Visual Basic: [Select]
  1.                 Dim MyBT As BlockTable = MyDB.BlockTableId.GetObject(OpenMode.ForRead)
  2.                 Dim AcBlockId = MyBT(AcBlockName)
You don't have to open a DBObject just to get its ObjectId.

Code - Visual Basic: [Select]
  1.             Dim allDispReps As Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
  2.             ...
  3.             ' allDispReps = Nothing
  4.            ' Noooo! You have declared the variable correctly, but it isn't initialized.
  5.            ' That should read:
  6.            allDispReps = New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection()

...and finally:
Code - Visual Basic: [Select]
  1.                 ' For Each dispRep As DisplayRepresentation In allDispReps
  2.                ' Stop right here. That will perhaps work in 2013, but just now you can't iterate over an
  3.                ' ObjectIdCollection and cast directly to the target type without opening the Object first.
  4.                ' Should read:
  5.                For Each dispRepId As acadObjectId In allDispReps
  6.                     ....

Edited b/c of deeper analysis.
« Last Edit: March 05, 2012, 12:32:41 PM by kaefer »

mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #12 on: March 05, 2012, 04:31:31 PM »
Thanks for the help.
I ended up with the following code, which works, but has a problem.
When i run the command the MultiViewBLock is created with the ViewBLocks against the DisplayRepresentation.

In the StyleManager i can't see the MultiViewBlock at first. After a click on the Apply Button, the MultiViewblock appears.
And the symbol in front of de MultiViewBlock is different from the other MultiViewBlocks. There is something wrong with the code, but what?

        <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)")

                    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 Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection = DisplayRepresentationManager.GetActiveDisplayRepresentationSets(db)

            Using trans

                'Dim acds As DisplaySet
                Dim ds As DisplaySet = New DisplaySet()
                allDispReps = New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection()

                Dim id As ObjectId
                ' loop through the active disp sets (i.e., disp sets of active dis confid.)
                For Each id In ids
                    Dim acds As DisplaySet = trans.GetObject(id, OpenMode.ForRead)

                    Dim drid As ObjectId
                    ' look through the set of dispRep of the given disp set.
                    For Each drid In acds.DisplayRepresentationIds
                        Dim dr As DisplayRepresentation = trans.GetObject(drid, OpenMode.ForRead)
                        dr = trans.GetObject(drid, OpenMode.ForRead)
                        Dim rc As RXClass = dr.WorksWith
                        If (rc.Name.CompareTo("AecDbMvBlockRef") = 0) Then
                            If (ds.DisplayRepresentationIds.Contains(drid)) Then
                                Continue For
                            Else
                                'ds.DisplayRepresentationIds.Add(drid)
                                allDispReps.Add(drid)
                            End If
                        End If
                    Next drid
                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 #13 on: March 05, 2012, 05:36:42 PM »
There is something wrong with the code, but what?

Your argument dispRepName is unused, and there are three pointers now to each DisplayRepresentation in the active DisplaySet.

If you need to find a DisplayRepresentation by name, do look into DisplayManager.GetAllDisplayRepresentationsWorkForSpecifiedClass.

mgreven

  • Guest
Re: Need help with creation of MultiViewBLock
« Reply #14 on: March 05, 2012, 06:03:36 PM »
Keafer... Thanks for your support and patience...

I did not notice that the dispRepName was not used.
I get the feeling i spent the day chasing ghosts... Your Ling query looked a bit easier than the code i have now.

Can you show me how to use the DisplayManager.GetAllDisplayRepresentationsWorkForSpecifiedClass.
Why is this stuff so poorly documentated... is there a place where i can find more info?


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