+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Forum Newbie
    Using
    not specified
    Join Date
    Jul 2006
    Posts
    1

    Default Get coordinate of polyline by VBA

    Registered forum members do not see this ad.

    Hi,
    I've just started learning VBA in CAD.I wanna select polyline onscreen and then get its coordinate(of every vertex).But I don't know how to do it.
    Thanks for any help.
    Bye

  2. #2
    Full Member
    Using
    not specified
    Join Date
    Jul 2006
    Location
    BELGUIM
    Posts
    56

    Default

    Something like this?

    Code:
    Sub Example_Coordinates()
    
    Dim Selection As AcadSelectionSet
    Dim Poly As AcadLWPolyline
    Dim Obj As AcadEntity
    Dim Bound As Double
    
    'Makes a selectionset.
    'On Error Resume Next
        Set Selection = ThisDrawing.SelectionSets.Item("Select polyline.")
    If Err Then
        Set Selection = ThisDrawing.SelectionSets.Add("Select polyline.")
        Err.Clear
    Else
        Selection.Clear
    End If
    
    'Select the polyline.
    Selection.SelectOnScreen
    
    For Each Obj In Selection
    
        If Obj.ObjectName = "AcDbPolyline" Then
            
                Set Poly = Obj
                On Error Resume Next
                
                Bound = UBound(Poly.Coordinates)
                
                x = 0
                y = 1
                
                For i = 0 To Bound / 2
                    
                    MsgBox "X= " & Poly.Coordinates(x) & "  Y= " & Poly.Coordinates(y)
                    If Err Then Err.Clear
                    
                    x = x + 2
                    y = y + 2
                    
                Next
              
        End If
    
    Next Obj
    
    End Sub
    I hope this helps you!

    Greetings

  3. #3
    Junior Member
    Using
    AutoCAD 2010
    Join Date
    May 2011
    Posts
    12

    Default

    Hello, someone please,

    Sub Example_Coordinates()
    without the sub xx()

    how to run this code?

  4. #4
    Senior Member ketxu's Avatar
    Computer Details
    ketxu's Computer Details
    Operating System:
    Sorry, my English not well :(
    Computer:
    Sorry, my English not well :(
    Motherboard:
    Sorry, my English not well :(
    CPU:
    Sorry, my English not well :(
    RAM:
    Sorry, my English not well :(
    Graphics:
    Sorry, my English not well :(
    Primary Storage:
    Sorry, my English not well :(
    Secondary Storage:
    Sorry, my English not well :(
    Monitor:
    Sorry, my English not well :(
    Using
    AutoCAD 2007
    Join Date
    Sep 2010
    Location
    Sorry, my English not well :(
    Posts
    170

    Default

    Try to change ( by ( and ) by ) in code

  5. #5
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,613

    Default

    Quote Originally Posted by vitaminm View Post
    Hello, someone please,

    Sub Example_Coordinates()
    without the sub xx()

    how to run this code?
    Try another one instead,type vbarun in the command line,
    select "demo" sub

    HTML Code:
    Option Explicit
    Function PolyCoords(oEnt As AcadEntity) As Variant
         Dim cnt As Integer
         Dim i As Integer
         Dim j As Integer
         Dim iStep As Integer
         Dim varPt As Variant
         Dim dblCoords() As Double
         Dim dblVert() As Double
     
         If TypeOf oEnt Is AcadLWPolyline Then
              iStep = 2
         ElseIf TypeOf oEnt Is Acad3DPolyline Or _
                TypeOf oEnt Is AcadPolyline Then
              iStep = 3
         End If
         dblCoords = oEnt.Coordinates
     
         ReDim ptsArr(0 To (UBound(dblCoords) + 1) \ iStep - 1, 0 To iStep - 1) As Double
         For i = 0 To (UBound(dblCoords) + 1) \ iStep - 1
              For j = 0 To iStep - 1
                   ptsArr(i, j) = dblCoords(cnt)
                   Debug.Print ptsArr(i, j)
                   cnt = cnt + 1
              Next
         Next
         PolyCoords = ptsArr
    End Function
     
    Sub demo()
         Dim pts As Variant
         Dim varPt As Variant
         Dim oEnt As AcadEntity
         ThisDrawing.Utility.GetEntity oEnt, varPt, vbCr & "Select polyline"
         If Not TypeOf oEnt Is AcadLWPolyline And _
            Not TypeOf oEnt Is Acad3DPolyline And _
            Not TypeOf oEnt Is AcadPolyline Then
              MsgBox "Method is not applicable for this entity type"
              Exit Sub
         End If
         pts = PolyCoords(oEnt)
    End Sub
    Next tyme post your question on VBA branch, please:
    http://www.cadtutor.net/forum/forumd...ectARX-amp-VBA

    ~'J'~
    Last edited by fixo; 3rd Jun 2012 at 07:40 am.
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  6. #6
    Junior Member
    Using
    AutoCAD 2010
    Join Date
    May 2011
    Posts
    12

    Default

    Quote Originally Posted by ketxu View Post
    Try to change ( by ( and ) by ) in code
    Ya, tq, it woks now!

  7. #7
    Forum Newbie
    Using
    AutoCAD 2011
    Join Date
    Jan 2013
    Posts
    5

    Default

    Registered forum members do not see this ad.

    I’m a bit discouraged, I started to learn acad-vba since a few days too & I wanted to understand Steven Bastiaanse’s example which seems simple.
    Why #40 & #41 , what does it mean.
    Set Selection = ThisDrawing.SelectionSets.Item (”Select polyline” & #41; ???
    (ThisDrawing.selectionsets) expected object or method isn’t it?

    Thanks for helping me

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts