.NET

Reply
Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 1 of 31 (612 Views)

How to get Block's Layer name?

612 Views, 30 Replies
04-18-2012 01:38 AM

Hi,

 

After searching for a long time on the web, does anyone know how to get the block's layer name?

 

I iterate through Blocks to get informations but i'm not able to code the source to obtain what i want?

 

Sample source taken from AutoCAD.NET site:

 

 Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

            '' Open the Block table for read
            Dim acBlkTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

            '' Open the Block table record Model space for read
            Dim acBlkTblRec As BlockTableRecord

            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

            '' Step through the Block table record
            For Each acObjId As ObjectId In acBlkTblRec
                acDoc.Editor.WriteMessage(vbLf & "DXF name: " & acObjId.ObjectClass().DxfName)
                acDoc.Editor.WriteMessage(vbLf & "ObjectID: " & acObjId.ToString())
                acDoc.Editor.WriteMessage(vbLf & "Handle: " & acObjId.Handle.ToString())
                acDoc.Editor.WriteMessage(vbLf)
            Next

            '' Dispose of the transaction
        End Using

 Maybe use nested transaction to iterate through LayerTableId?

 

ideas?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 31 (610 Views)

Re: How to get Block's Layer name?

04-18-2012 01:42 AM in reply to: MGOMKD

Try this function to get the block name

		Public Shared Function EffectiveName(blkref As BlockReference) As String
			If blkref.IsDynamicBlock Then
				Using obj As BlockTableRecord = DirectCast(blkref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead), BlockTableRecord)
					Return obj.Name
				End Using
			End If
			Return blkref.Name
		End Function

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 3 of 31 (607 Views)

Re: How to get Block's Layer name?

04-18-2012 01:52 AM in reply to: MGOMKD

Hi Hallex,

 

I don't want to get the block's name but the layer's name on which the block is attached!

 

Thanks a lot.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 31 (603 Views)

Re: How to get Block's Layer name?

04-18-2012 02:01 AM in reply to: MGOMKD

Sorry for that

To get a layer you have to cast object as Entity, something like:

          Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                '' Open the Block table for read 
                Dim acBlkTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                '' Open the Block table record Model space for read
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)
                '' Step through the Block table record 
                For Each acObjId As ObjectId In acBlkTblRec
                    Dim ent As Entity = acTrans.GetObject(acObjId, OpenMode.ForRead)
                    acDoc.Editor.WriteMessage(vbLf & "DXF name: " & acObjId.ObjectClass().DxfName)
                    acDoc.Editor.WriteMessage(vbLf & "ObjectID: " & acObjId.ToString())
                    acDoc.Editor.WriteMessage(vbLf & "Handle: " & acObjId.Handle.ToString())
                    acDoc.Editor.WriteMessage(vbLf & "Layer: " & ent.Layer.ToString())
                    acDoc.Editor.WriteMessage(vbLf)
                Next
                ''Dispose of the transaction
            End Using

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 5 of 31 (601 Views)

Re: How to get Block's Layer name?

04-18-2012 02:09 AM in reply to: Hallex

What the...

It  was so easy that i didn't see it! I'll try this.

 

You're the fastest and the best Hallex!

 

See you.

 

PS: I'm French, so all apollogies to my syntaxe script.

Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 6 of 31 (587 Views)

Re: How to get Block's Layer name?

04-18-2012 04:57 AM in reply to: MGOMKD

yeah it works!

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 31 (584 Views)

Re: How to get Block's Layer name?

04-18-2012 05:08 AM in reply to: MGOMKD

Glad to help

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 8 of 31 (519 Views)

Re: How to get Block's Layer name?

07-30-2012 12:38 AM in reply to: Hallex

Hi Hallex,

 

How are you? I have a new matter that i cannot solve.

You helped me to find le layer's name of a block.

Now i would like to find property's block (AcadObject: Polyline, Arc....). For instance for each block in a particular layer, iterate through each ones, then showing all properties like length,  boundary closed or opened, width....

 

'Compte du nombre d'objet(s) dans le calque séléctionné'
                Using acTrans = acDocs.MdiActiveDocument.Database.TransactionManager.StartTransaction()
                    acBlkTbl = acTrans.GetObject(acDocs.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead)
                    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

                    For Each acObjId As ObjectId In acBlkTblRec
                        Dim MyEnt As Entity = acTrans.GetObject(acObjId, OpenMode.ForRead)
                        If MyEnt.Layer = LayerVal Then
                            'MsgBox(acObjId.ObjectClass.DxfName)
                            Select Case acObjId.ObjectClass.DxfName

                                '*******'
                                '* ARC *'
                                '*******'
                                Case Is = "ARC"
                                                                        
                                    'LENGTH'
                                    MsgBox (?)

                                Case Is = "CIRCLE"
                                Case Is = "DIMENSION"
                                Case Is = "HATCH"
                                Case Is = "LINE"
                                Case Is = "LWPOLYLINE"
                                Case Is = "MTEXTE"
                                Case Is = "POINT"
                                Case Is = "RAY"
                                Case Is = "REGION"
                                Case Is = "TEXTE"
                                Case Is = "SPLINE"
                                Case Is = "XLINE"
                            End Select
                        End If
                    Next acObjId

                End Using

            End With

 Do you have any ideas?

Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 9 of 31 (505 Views)

Re: How to get Block's Layer name?

07-30-2012 02:38 AM in reply to: MGOMKD

I think i could find a way through this link!

 

Through the Interface

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 10 of 31 (497 Views)

Re: How to get Block's Layer name?

07-30-2012 05:17 AM in reply to: MGOMKD

This will get you started, I have no time on complete solution

Change properties for every type what you'll want to retrieve

Here is just a quick example:

 

        <CommandMethod("getsubentitiesinfo", "GetS", CommandFlags.Modal)> _
        Public Shared Sub explodeRef()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim info As String = ""
            Dim opts As New PromptEntityOptions(vbLf & "Select block reference")
            opts.SetRejectMessage(vbLf & "Select only type of block reference!")
            opts.AddAllowedClass(GetType(BlockReference), False)

            Dim res As PromptEntityResult = ed.GetEntity(opts)
            If res.Status <> PromptStatus.OK Then Return
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim bref As BlockReference = TryCast(tr.GetObject(res.ObjectId, OpenMode.ForRead), BlockReference)

                Dim expobjs As New DBObjectCollection()
                bref.Explode(expobjs)

                Dim table As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                For Each obj As DBObject In expobjs
                    If TypeOf obj Is Entity Then
                        Select Case obj.GetRXClass().DxfName
                            Case "LWPOLYLINE"
                                info = ""
                                Dim poly As Polyline = TryCast(obj, Polyline)
                                info = String.Format(vbLf + "Number of points: {0}" + vbLf + "Closed? {1}" + vbLf + "Layer: {2}" + vbLf + "Color: {3}", poly.NumberOfVertices, poly.Closed, poly.Layer, poly.ColorIndex)
                                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(info)
                            Case "ARC"
                                info = ""
                                Dim arc As Arc = TryCast(obj, Arc)
                                info = String.Format(vbLf + "Center: {0}" + vbLf + "Start angle: {1}" + vbLf + "End angle: {2}" + vbLf + "Start point{3}" + vbLf + "End point: {4}" + vbLf + "Layer: {5}" + vbLf + "Color: {6}", arc.Center, arc.StartAngle, arc.EndAngle, arc.StartPoint, arc.EndPoint, arc.Layer, arc.ColorIndex)
                                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(info)
                                ''ETC..ETC...
                            Case "CIRCLE"
                            Case "DIMENSION"
                            Case "HATCH"
                            Case "LINE"
                            Case "LWPOLYLINE"
                            Case "MTEXT"
                            Case "POINT"
                            Case "RAY"
                            Case "REGION"
                            Case "TEXT"
                            Case "SPLINE"
                            Case "XLINE"

                        End Select

                    End If
                Next

                'clean up memory, optional
                For Each obj As DBObject In expobjs
                    obj.Dispose()
                Next
                tr.Commit()

            End Using

        End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.