.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Newly Inserted Blocks Not Displaying Until Drawing is Closed and Re-opened

1 REPLY 1
SOLVED
Reply
Message 1 of 2
cwelkusl
848 Views, 1 Reply

Newly Inserted Blocks Not Displaying Until Drawing is Closed and Re-opened

I can't for the life of me find anyone else referencing this issue when searching for the problem, so I'm hoping that maybe I'm just not searching for the right keywords or the solution is so easy and I'm just overlooking it.  But here is the issue:

 

The code I have shown here covers two kind of block inserts, one I'm cloning a blockrecord table from another drawing and inserting it as a block reference into the new drawing and then secondly, opening a drawing file which is a block unto itself and inserting that drawing into the new drawing database as a BlockTableRecord and then creating a block reference in the new drawing.  The first instance, works and displays the block as expected.  The second instance works, but will not display the block until the drawing is saved, closed and re-opened.  The reason for using both these methods is that I need to insert two blocks from different sources into the new drawing and then have the user visually confirm if their insertion points match up, and if not, then move one of the blocks in order to get a transformation matrix for the positioning difference between the two blocks.

 

So is there a solution to the display issue of the second block insertion type without closing and re-opening the drawing? or is this normal behaviour?

 

If you see something wrong or know where I should be looking for a resolution to this, it would be greatly appreciated!  I don't necessarily need the code to fix or change it, just a finger pointing in the right direction.

 

My code for inserting the block is here (I currently have it broken up into several transactions as I was checking to see if committing the changes for each section would resolve the issue as opposed to making the changes all in one transaction and committing them at the end):

 

Public Function BlockInsertCheck(ByVal strSourceBlockName As String, ByVal strSourcePath As String, ByVal strDestBlockName As String, ByVal strDestPath As String, ByVal strDWTName As String) As Matrix3d
        Dim m3dTransformPoint As Matrix3d = New Matrix3d()
        Dim acDocMgr As DocumentCollection = acApp.DocumentManager
        Dim acDocTemp As Document

        Try
            'Create New Temporary Drawing
            If File.Exists(strDWTName) Then
                acDocTemp = acDocMgr.Add(strDWTName)
                acDocMgr.MdiActiveDocument = acDocTemp
                Using acDocLock As DocumentLock = acDocTemp.LockDocument
                    Using acTrans As Transaction = acDocTemp.Database.TransactionManager.StartTransaction()

                        'Bring in the source block
                        If File.Exists(strSourcePath) Then
                            Using acSourceDB As Database = New Database(False, True)
                                If Not IsFileInUse(strSourcePath) Then
                                    acSourceDB.ReadDwgFile(strSourcePath, FileShare.Read, True, "")
                                Else
                                    MessageBox.Show("The source drawing (" & strSourcePath & ") is in use and cannot be opened.  Please close this drawing before proceeding.", "DASMAP - Error Opening Source File", MessageBoxButtons.OK, MessageBoxIcon.Error)
                                    Return Nothing
                                End If

                                Using acSourceTrans As Transaction = acSourceDB.TransactionManager.StartTransaction()
                                    Dim acSourceBlockTable As BlockTable = acSourceTrans.GetObject(acSourceDB.BlockTableId, OpenMode.ForRead)
                                    If acSourceBlockTable.Has(strSourceBlockName) Then


                                        Dim acDestBlockTable As BlockTable = acTrans.GetObject(acDocTemp.Database.BlockTableId, OpenMode.ForWrite)
                                        Dim acDestModelSpace As BlockTableRecord = acTrans.GetObject(acDestBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                                        Dim acObjIDs As ObjectIdCollection = New ObjectIdCollection()
                                        Dim acIDMap As New IdMapping()

                                        acObjIDs.Add(acSourceBlockTable(strSourceBlockName))
                                        acSourceDB.WblockCloneObjects(acObjIDs, acDestBlockTable.ObjectId, acIDMap, DuplicateRecordCloning.Ignore, False)

                                        For Each id As IdPair In acIDMap
                                            'Check if its a block ref and if so create a new one
                                            Dim acdbObj As DBObject = acTrans.GetObject(id.Value, OpenMode.ForRead)
                                            If TypeOf (acdbObj) Is BlockTableRecord Then
                                                Dim actempBlockRef As BlockTableRecord = CType(acdbObj, BlockTableRecord)
                                                If actempBlockRef.Name = strSourceBlockName Then
                                                    Dim brefNew As BlockReference = New BlockReference(New Point3d(0, 0, 0), actempBlockRef.ObjectId)
                                                    acDestModelSpace.AppendEntity(brefNew)
                                                    acTrans.AddNewlyCreatedDBObject(brefNew, True)
                                                    Exit For
                                                End If
                                            End If
                                        Next id

                                    Else
                                        'Source block was not found
                                        Return Nothing
                                    End If
                                End Using
                            End Using
                        Else
                            'Source file could not be found
                            Return Nothing
                        End If
                        acTrans.Commit()
                    End Using
                End Using


                Using acDocLock As DocumentLock = acDocTemp.LockDocument
                    Using acTrans As Transaction = acDocTemp.Database.TransactionManager.StartTransaction()
                        'Bring in Dest Block
                        If File.Exists(strDestPath) Then
                            Using acDestDB As Database = New Database(False, True)
                                If Not IsFileInUse(strDestPath) Then
                                    acDestDB.ReadDwgFile(strDestPath, FileShare.Read, True, "")
                                Else
                                    MessageBox.Show("The source drawing (" & strDestPath & ") is in use and cannot be opened.  Please close this drawing before proceeding.", "DASMAP - Error Opening Source File", MessageBoxButtons.OK, MessageBoxIcon.Error)
                                    Return Nothing
                                End If

                                Using acDestTrans As Transaction = acDestDB.TransactionManager.StartTransaction()

                                    Dim bAnno As Boolean = acDestDB.AnnotativeDwg
                                    Dim acDestBlockID As ObjectId = acDocTemp.Database.Insert(strDestBlockName, acDestDB, True)
                                    Dim acDestBlockTable As BlockTable = acTrans.GetObject(acDocTemp.Database.BlockTableId, OpenMode.ForWrite)

                                    If bAnno Then
                                        Dim acDestBlockBTR As BlockTableRecord = acTrans.GetObject(acDestBlockID, OpenMode.ForWrite)
                                        acDestBlockBTR.Annotative = AnnotativeStates.True
                                    End If

                                End Using
                            End Using
                        Else
                            'Destination File was not found
                            Return Nothing
                        End If
                        acTrans.Commit()
                    End Using
                End Using

                Using acDocLock As DocumentLock = acDocTemp.LockDocument
                    Using acTrans As Transaction = acDocTemp.Database.TransactionManager.StartTransaction()

                        Dim acDestBlockTable As BlockTable = acTrans.GetObject(acDocTemp.Database.BlockTableId, OpenMode.ForRead)
                        Dim acDestModelSpace As BlockTableRecord = acTrans.GetObject(acDestBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                        If acDestBlockTable.Has(strDestBlockName) Then
                            Dim brefNew As BlockReference = New BlockReference(New Point3d(0, 0, 0), acDestBlockTable(strDestBlockName))
                            acDestModelSpace.AppendEntity(brefNew)
                            acTrans.AddNewlyCreatedDBObject(brefNew, True)
                            acTrans.Commit()
                        End If
                    End Using
                End Using

                Return m3dTransformPoint
            Else
                'Template file could not be found
                Return Nothing
            End If


        Catch ex As Exception
            MessageBox.Show("Error checking block insertion points: " & ex.Message & vbCrLf & vbTab & ex.StackTrace, "DASMAP - " & System.Reflection.MethodBase.GetCurrentMethod().Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
        End Try

    End Function

Chris Welk
Urban Systems Ltd.
Engineering Technologist (Civil)/CADD Applications Developer
1 REPLY 1
Message 2 of 2
cwelkusl
in reply to: cwelkusl

I've found the solution to my issue.  After doing some major digging, I finally found a post that mentioned adding annotation scales to annotative blocks when inserting them.  So that was the issue.  The new blocks I was inserting were annotative and since I had not been adding a scale to it, it was not displaying.  See code snippit below (bold text is new code, unbolded for existing code from first post for location reference):

 

 acDestModelSpace.AppendEntity(brefNew)

 

'Get current annotative scale and assign to block

If befNew.Annotative = AnnotativeStates.True Then
        Dim ocm As ObjectContextManager = acDB.ObjectContextManager
        Dim occ As ObjectContextCollection = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES")
        brefNew.AddContext(occ.CurrentContext)

End If

 

acDB.AddNewlyCreatedDBObject(brefNew, True)

Chris Welk
Urban Systems Ltd.
Engineering Technologist (Civil)/CADD Applications Developer

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost