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

AutoCAD 2015 - Newly created INSERT's do not show

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
FrankLenoir
1013 Views, 5 Replies

AutoCAD 2015 - Newly created INSERT's do not show

I use a peace of code to add an INSERT (BlockID) to the drawing. In previous versions of AutoCAD this codes functions fine. But in version 2015 something strange is happening. The INSERT's are in the drawing but not visible. With select all (CTRL-A) the grips of the INSERT's become visible and their properties are displayed. When the drawing is saved and closed and reopened again the INSERT's are visible.

 

This is the peace of code i use:

Using doclock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument
    Using myTrans As Transaction = myDB.TransactionManager.StartTransaction()

        Dim myBT As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)
        Dim myCurrentSpace As BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)

        'Insert the Block
        Dim myBR As New BlockReference(myPoint, BlockId)
        myBR.LayerId = myDB.Clayer
        myBR.ScaleFactors = New Scale3d(ScaleFactor, ScaleFactor, ScaleFactor)
        myBR.Rotation = myRotation

        Dim myObjectId As ObjectId = myCurrentSpace.AppendEntity(myBR)
        myTrans.AddNewlyCreatedDBObject(myBR, True)

        'Append Attribute References to the BlockReference
        For Each myEntId As ObjectId In myBlockTableRecord
        Dim obj As DBObject = myEntId.GetObject(OpenMode.ForRead)
        If TypeOf obj Is AttributeDefinition Then
            Dim myAttDef As AttributeDefinition = CType(obj, AttributeDefinition)
            Dim myAttRef As New AttributeReference
            myAttRef.SetAttributeFromBlock(myAttDef, myBR.BlockTransform)
            myBR.AttributeCollection.AppendAttribute(myAttRef)
            myTrans.AddNewlyCreatedDBObject(myAttRef, True)
        End If
        Next
        myTrans.Commit()
    End Using
End Using

 

 

Does anyone have an solution to this problem?

 

Thanks,

Frank Lenoir

5 REPLIES 5
Message 2 of 6

This sounds like the exact same problem I've been having lately. I've tried forcing a .Regen(), tried .UpdateScreen(), .UpdateTiledViewportsIn/FromDatabase() still to no avail.

 

I'd be interested in a solution as well!

Message 3 of 6
hgasty1001
in reply to: FrankLenoir

Hi,

 

How are you obtaining myBlockTableRecord ? I'm asking as you need to obtain it from the just created blockreference, and I don't see that in your code.

 

Gaston Nunez

Message 4 of 6
FrankLenoir
in reply to: hgasty1001

Thank you for the comment Gaston.

Trying to keep the example clean, i stripped some code that calculates myPoint and myRotation. In the proces i also stripped the declaration of myBlockTableRecord.

 

Here is the corrected code.

 

Using doclock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument
    Using myTrans As Transaction = myDB.TransactionManager.StartTransaction()

        Dim myBlockTableRecord As BlockTableRecord = BlockId.GetObject(OpenMode.ForRead)

        Dim myBT As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)
        Dim myCurrentSpace As BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)

        'Insert the Block
        Dim myBR As New BlockReference(myPoint, BlockId)
        myBR.LayerId = myDB.Clayer
        myBR.ScaleFactors = New Scale3d(ScaleFactor, ScaleFactor, ScaleFactor)
        myBR.Rotation = myRotation

        Dim myObjectId As ObjectId = myCurrentSpace.AppendEntity(myBR)
        myTrans.AddNewlyCreatedDBObject(myBR, True)

        'Append Attribute References to the BlockReference
        For Each myEntId As ObjectId In myBlockTableRecord
        Dim obj As DBObject = myEntId.GetObject(OpenMode.ForRead)
        If TypeOf obj Is AttributeDefinition Then
            Dim myAttDef As AttributeDefinition = CType(obj, AttributeDefinition)
            Dim myAttRef As New AttributeReference
            myAttRef.SetAttributeFromBlock(myAttDef, myBR.BlockTransform)
            myBR.AttributeCollection.AppendAttribute(myAttRef)
            myTrans.AddNewlyCreatedDBObject(myAttRef, True)
        End If
        Next
        myTrans.Commit()
    End Using
End Using

 

Frank Lenoir

 

Message 5 of 6
hgasty1001
in reply to: FrankLenoir

Hi,

 

I see some problems with BlockId variable, where and how it's defined?

 

See if this code works for you:

 

Public Class Class1

    Public Structure block
        Public Name As String
        Public InsertPoint As Point3d
        Public Layer As String
        Public Rotation As Double
        Public GlobalScale As Double
        Public Xscale As Double
        Public Yscale As Double
        Public Attribs As Dictionary(Of String, String)
        Public DynProps As Dictionary(Of String, Object)
        Public BlockLibPath As String
        Public IsDynamic As Boolean
        Public HasAttribs As Boolean
    End Structure

    Private Sub InsertAttibuteInBlockRef(ByVal blkRef As BlockReference, ByVal attributeTag As String, ByVal attributeText As String, ByVal tr As Transaction)
        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
        For Each attId As ObjectId In btr
            Dim ent As Entity = DirectCast(tr.GetObject(attId, OpenMode.ForRead), Entity)
            If TypeOf ent Is AttributeDefinition Then
                Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
                Dim attRef As New AttributeReference()
                attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform)
                If attRef.Tag = attributeTag.ToUpper Then
                    attRef.TextString = attributeText + ""
                    Dim id As ObjectId = blkRef.AttributeCollection.AppendAttribute(attRef)
                    tr.AddNewlyCreatedDBObject(attRef, True)
                End If
                attRef.Dispose()
            End If
        Next
    End Sub

    Public Shared Sub DynBlkInsertWithAtt(blk As block)
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database

        acCurDb = HostApplicationServices.WorkingDatabase()

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            Dim acBlkTbl As BlockTable
            Dim blkObjId As ObjectId

            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

            If acBlkTbl.Has(blk.Name) Then
                blkObjId = acBlkTbl(blk.Name)
            Else
                Dim blkFile As String = blk.BlockLibPath + "\\" + blk.Name + ".dwg"
                Dim dbDwg As New Database(False, True)
                dbDwg.ReadDwgFile(blkFile, IO.FileShare.Read, True, "")
                blkObjId = acCurDb.Insert(blk.Name, dbDwg, True)
                dbDwg.Dispose()
            End If

            Dim acBlkTblRec As BlockTableRecord

            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            Dim blkRef As BlockReference = New BlockReference(blk.InsertPoint, blkObjId)

            blkRef.Rotation = blk.Rotation
            blkRef.ScaleFactors = New Scale3d(blk.GlobalScale, blk.GlobalScale, 1)
            blkRef.Layer = blk.Layer
            acBlkTblRec.AppendEntity(blkRef)
            acTrans.AddNewlyCreatedDBObject(blkRef, True)

            If blk.IsDynamic Then
                Dim dPropsCollection As DynamicBlockReferencePropertyCollection

                dPropsCollection = blkRef.DynamicBlockReferencePropertyCollection

                For Each dprop As DynamicBlockReferenceProperty In dPropsCollection
                    If blk.DynProps.ContainsKey(dprop.PropertyName) Then
                        If dprop.PropertyName = "Visibility1" Then
                            dprop.Value = CStr(blk.DynProps("Visibility1"))
                        Else
                            dprop.Value = CDbl(blk.DynProps(dprop.PropertyName))
                        End If
                    End If
                Next
            End If

            If blk.HasAttribs Then
                For Each attrib As String In blk.Attribs.Keys
                    InsertAttibuteInBlockRef(blkRef, attrib, blk.Attribs(attrib), acTrans)
                Next
            End If

            blkRef.RecordGraphicsModified(True)
            acBlkTblRec.UpdateAnonymousBlocks()

            acTrans.Commit()
        End Using
    End Sub
End Class

Gaston Nunez

Message 6 of 6
FrankLenoir
in reply to: hgasty1001

Thanks for the example Gaston. It led me to the solution of the problem.

 

After loading the external block (like in your example) i have in my code a check if the new block is annotative. If so it should set the Annotative property accordingly. Here i had an error in my code. Setting the Annotative property true to a block that is not annotative by itself does not show the content of the insert of that block. I removed this error and now my code works as aspected.

 

 

Frank

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