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

Iterating trough items in block

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
wbdehaan
882 Views, 8 Replies

Iterating trough items in block

Hi,

 

at multiple places i have drawn the same block in a drawing, and i would like select one block in the drawing, then iterate through the items in the block, and find the actual coordinates of a 3d-polyline (that is drawn in that block) in modelspace. How can I do this?

 

kind regards,

 

Wouter de Haan

the Netherlands

8 REPLIES 8
Message 2 of 9
Hallex
in reply to: wbdehaan

Take a look at GetNestedEntity method of Document Editor

What is the language you prefer C# /  VB.NET?

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 9
wbdehaan
in reply to: Hallex

Thanks for your reply,

 

where can I find the 'document editor' ? I use Visual Studio 2008 (vb.net)

 

Wouter

 

Message 4 of 9
Alfred.NESWADBA
in reply to: wbdehaan

Hi,

 

for your currentlyy opened DWG:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MDIActiveDocument.Editor

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 9
Hallex
in reply to: wbdehaan

Here you go, tested on A2009 only

{code}

  Public Shared Function GetAllVertices(ent As Polyline) As Point3dCollection
   Dim points As New Point3dCollection()
   Dim pt As New Point3d()
   Dim i As Integer = 0
   For i = 0 To ent.NumberOfVertices - 1
    pt = ent.GetPoint3dAt(i)
    points.Add(pt)
   Next
   Return points
  End Function


  <CommandMethod("Gnp")> _
  Public Shared Sub TestSubEntity()
   Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

   Dim db As Database = doc.Database

   Dim ed As Editor = doc.Editor

   Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem

   Dim tr As Transaction = db.TransactionManager.StartTransaction()
   Try

    Using tr

     Dim pno As New PromptNestedEntityOptions(vbLf & "Select Nested Polyline >>")

     pno.AllowNone = False

     Dim rs As PromptNestedEntityResult = ed.GetNestedEntity(pno)


     If rs.Status <> PromptStatus.OK Then

      Return
     End If

     Dim entId As ObjectId = rs.ObjectId

     Dim selent As Entity = TryCast(tr.GetObject(entId, OpenMode.ForWrite), Entity)

     Dim ids As ObjectId() = rs.GetContainers()
     Dim blkId As ObjectId = ids(0)

     If blkId.IsNull Then

      Return
     End If

     Dim blkEnt As Entity = TryCast(tr.GetObject(blkId, OpenMode.ForRead), Entity)
     If blkEnt Is Nothing Then

      Return
     End If


     Dim bref As BlockReference = TryCast(blkEnt, BlockReference)

     If bref Is Nothing Then

      Return
     End If

     Dim mat As Matrix3d = bref.BlockTransform

     Dim btrec As BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)

     Dim pline As Polyline = TryCast(selent, Polyline)

     If pline Is Nothing Then

      Return
     End If

     Dim circ As Circle

     Dim points As Point3dCollection = GetAllVertices(pline)

     For i As Integer = 0 To points.Count - 1
      Dim p As Point3d = points(i)

      circ = New Circle(p.TransformBy(mat), Vector3d.ZAxis, 0.1)

      btrec.AppendEntity(circ)

      tr.AddNewlyCreatedDBObject(circ, True)
     Next

 

     tr.Commit()

    End Using
   Catch ex As Autodesk.AutoCAD.Runtime.Exception


    ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)

   Finally
   End Try
  End Sub

{code}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 6 of 9
wbdehaan
in reply to: Hallex

Thank you very much for this code, it works!

 

kind regards

 

Wouter

Message 7 of 9
Hallex
in reply to: wbdehaan

You're welcome, Wouter

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 9
wbdehaan
in reply to: Hallex

In addition, i have another question.

i want to make an adjustment to the supplied code, and therefore i need to step through all objects in the block, but since i am new to .net i cannot get it to work. How can i step through all the objects in the selected block?

 

Wouter

Message 9 of 9
Hallex
in reply to: wbdehaan

Wouter,

Here ya go

This crappy code of mine is based on Tony Tanzillo's idea that  I did find on this forum

ss give that a try

 

 

{code}

 _______________________________________

        <CommandMethod("GAS")> _
        Public Shared Sub GetAllSubentitiesTest()
            Dim sb As New StringBuilder
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
            Try
                Dim pso As New PromptEntityOptions(vbCr & "Select a Block Reference: ")
                pso.SetRejectMessage(vbLf & " Select  a Block Reference only!")
                pso.AddAllowedClass(GetType(BlockReference), False)
                Dim psr As PromptEntityResult = ed.GetEntity(pso)
                If psr.Status <> PromptStatus.OK Then
                    Return
                End If
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Dim ent As Entity = DirectCast(tr.GetObject(psr.ObjectId, OpenMode.ForRead), Entity)
                    Dim bref As BlockReference = DirectCast(ent, BlockReference)
                    If bref Is Nothing Then
                        Return
                    Else
                        bref.UpgradeOpen()
                    End If

                    Dim btr As BlockTableRecord = TryCast(tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead, False), BlockTableRecord)
                    If btr Is Nothing Then
                        Return
                    End If

                    Dim expIds As New DBObjectCollection
                    bref.Explode(expIds)
                    If expIds.Count > 0 Then
                        For i As Integer = 0 To expIds.Count - 1
                            Dim obj As Entity = TryCast(expIds(i), Entity)
                            If obj IsNot Nothing Then
                                If Not obj.IsWriteEnabled Then
                                    obj.UpgradeOpen()
                                    obj.TransformBy(bref.BlockTransform) '<-- to get right geometric properties
                                End If

                                sb.AppendLine(String.Format("{0}" + vbTab + "{1}" + vbLf, obj.GetRXClass().Name, obj.GetRXClass().DxfName))

                                obj.Dispose()
                            End If
                        Next
                    End If
                    bref.DowngradeOpen()
                End Using

            Catch ex As System.Exception
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message & vbCr & ex.StackTrace)
            Finally
                If sb.ToString <> String.Empty Then
                    MsgBox(sb.ToString)
                End If
            End Try
        End Sub

_______________________

{code}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

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