Posts: 25
Registered: ‎01-27-2011
Message 1 of 4 (174 Views)
Accepted Solution

Change block layer after insertio

174 Views, 3 Replies
07-31-2013 08:36 AM


I've inserted a block into my drawing with this code:


Dim tmpDb As New Database(False, True)
        tmpDb.ReadDwgFile(nombrebloque, System.IO.FileShare.Read, True, "")
        Dim Transform As Matrix3d = Matrix3d _
                                    .Scaling(scale, Point3d.Origin) _
                                    .PreMultiplyBy(Matrix3d.Displacement(DispacementVector)) _
        mydb.Insert(Transform, tmpDb, True)


and I would like to change some properties (layer, color,...) of the entities. (without modify the original block)


How can I access to these entities?


*Expert Elite*
Posts: 1,566
Registered: ‎10-08-2008
Message 2 of 4 (162 Views)

Re: Change block layer after insertio

07-31-2013 11:02 AM in reply to: karea

Try this code, just change the path of your source drawing in the code

(assume your drawing is contains just separate objects like attribues, lines,and other

entities but not complete block itself)

        <CommandMethod("nombreblk", CommandFlags.Session And CommandFlags.Redraw)> _
        Public Sub testExternalInsert()
                Dim doc As Document = Application.DocumentManager.MdiActiveDocument
                Dim myed As Editor = doc.Editor
                Dim mydb As Database = doc.Database
                Dim sourceFileName As String = "C:\Test\NombreBloque.dwg"	'<-- source file path
                Dim blkId As ObjectId = ObjectId.Null
                Dim nombrebloque As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(sourceFileName)
                Using docloc As DocumentLock = doc.LockDocument
                    Using tr As Transaction = doc.TransactionManager.StartTransaction
                        Dim blkTbl As BlockTable = tr.GetObject(mydb.BlockTableId, OpenMode.ForRead, False, True)
                        If blkTbl.Has(nombrebloque) Then
                            Application.ShowAlertDialog("Block " + nombrebloque + " already exist. Exit.")
                        End If
                        Dim myBtr As BlockTableRecord = TryCast(tr.GetObject(mydb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                        Using tmpDb As New Database(True, True)
                            tmpDb.ReadDwgFile(sourceFileName, System.IO.FileShare.Read, False, "")

                            blkId = mydb.Insert(nombrebloque, tmpDb, True)


                        End Using

                        If Not blkTbl.Has(nombrebloque) Then
                            Application.ShowAlertDialog("Problem with import block. Exit")
                        End If
                        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(mydb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                        Dim bref As BlockReference = New BlockReference(Point3d.Origin, blkId)
                        'change some properties:
                        Dim scale As Double = 1.0
                        Dim scl As Scale3d = New Scale3d(scale)
                        bref.ScaleFactors = scl
                        bref.Rotation = Math.PI / 4
                        bref.LayerId = mydb.Clayer
                        bref.Color = mydb.Cecolor
                        tr.AddNewlyCreatedDBObject(bref, True)

                        Dim blkRec As BlockTableRecord = DirectCast(tr.GetObject(blkTbl(nombrebloque), OpenMode.ForRead, False, True), BlockTableRecord)

                        If blkRec.HasAttributeDefinitions Then

                            Dim attColl As AttributeCollection = bref.AttributeCollection

                            For Each attId As ObjectId In blkRec
                                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, bref.BlockTransform)
                                    attRef.Tag = attDef.Tag
                                    attRef.TextString = attDef.TextString
                                    Dim id As ObjectId = attColl.AppendAttribute(attRef)
                                    tr.AddNewlyCreatedDBObject(attRef, True)
                                End If

                        End If

                    End Using
                End Using

            Catch ex As System.Exception
                Application.ShowAlertDialog(ex.Message.ToString() & vbLf & ex.StackTrace)

                Application.ShowAlertDialog("See result.")
            End Try
        End Sub



Posts: 25
Registered: ‎01-27-2011
Message 3 of 4 (130 Views)

Re: Change block layer after insertio

08-01-2013 05:28 AM in reply to: Hallex
great! it runs!

*Expert Elite*
Posts: 1,566
Registered: ‎10-08-2008
Message 4 of 4 (122 Views)

Re: Change block layer after insertio

08-01-2013 11:57 AM in reply to: karea

Im happy to help

Cheers :smileyhappy:


You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community