+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

Thread: VBA and XML

  1. #1
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Bulgaria, Sofia
    Posts
    228

    Default VBA and XML

    I would like to write data in an xml file and later read the needed data from this file. Is this possible with VBA for AutoCad and can someone give me a short example (I'd like to write in an xml file handles and centerpoints of all the circles in the drawing, as well as Handles, StartPoints and EndPoints of all the lines in the drawing)

    Any help will be highly appreciated!

  2. #2
    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
    Thanks God, I'm far enough from Hitleropa
    Posts
    1,697

    Default

    This should get you started this way, I hope

    Code:
    Option Explicit
    Public Const strFilePath As String = "C:\TestXML.xml"
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    Public Sub WriteLinesToXML()
    ' request reference to library:
    ' Microsoft XML.
    '
        Dim objDoc As MSXML.DOMDocument
        Dim objNode As MSXML.IXMLDOMNode
        Dim objRoot As MSXML.IXMLDOMElement
        Dim objSub As MSXML.IXMLDOMElement
        Dim objColl As Collection
        Dim i As Integer
    
        Set objColl = GetLinesData
    
        Set objDoc = New DOMDocument
        objDoc.resolveExternals = True
    
        Set objNode = objDoc.createProcessingInstruction( _
                      "xml", "version='1.0' encoding='UTF-8'")
    
        Set objNode = objDoc.insertBefore(objNode, _
                                          objDoc.childNodes.Item(0))
    
        Set objRoot = objDoc.createElement("dataroot")
    
        Set objDoc.documentElement = objRoot
        objRoot.setAttribute "xmlns:od", _
                             "urn:schemas-microsoft-com:officedata"
    
        For i = 1 To objColl.Count
    
            Set objSub = objDoc.createElement("Line")
            objRoot.appendChild objSub
    
            Set objNode = objDoc.createElement("Handle")
            objNode.Text = objColl.Item(i)(0)
            objSub.appendChild objNode
    
            Set objNode = objDoc.createElement("StartPoint")
            objNode.Text = objColl.Item(i)(1)
            objSub.appendChild objNode
    
            Set objNode = objDoc.createElement("EndPoint")
            objNode.Text = objColl.Item(i)(2)
            objSub.appendChild objNode
    
        Next i
    
        objDoc.Save strFilePath
    
    End Sub
    '
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    '
    Private Function GetLinesData() As Collection
        Dim oSset As AcadSelectionSet
        Dim oEnt As AcadEntity
        Dim oLine As AcadLine
        Dim ftype(0) As Integer
        Dim fvalue(0) As Variant
        Dim dxfCode, dxfValue
        Dim varStp As Variant
        Dim varEnd As Variant
        Dim objColl As Collection
        Dim tmp(0 To 2) As String
        Set objColl = New Collection
    
        With ThisDrawing.SelectionSets
            While .Count > 0
                .Item(0).Delete
            Wend
            Set oSset = .Add("$Lines$")
        End With
    
        ftype(0) = 0: fvalue(0) = "LINE"
        dxfCode = ftype: dxfValue = fvalue
    
        oSset.SelectOnScreen dxfCode, dxfValue
    
        For Each oEnt In oSset
            Set oLine = oEnt
            tmp(0) = oEnt.Handle
            varStp = oLine.StartPoint
            varEnd = oLine.EndPoint
            tmp(1) = CStr(Round(varStp(0), 3)) & "," & CStr(Round(varStp(1), 3)) & "," & CStr(Round(varStp(2), 3))
            tmp(2) = CStr(Round(varEnd(0), 3)) & "," & CStr(Round(varEnd(1), 3)) & "," & CStr(Round(varEnd(2), 3))
            objColl.Add tmp
        Next oEnt
        
        Set GetLinesData = objColl
        
    End Function
    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  3. #3
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Bulgaria, Sofia
    Posts
    228

    Default

    Fixo, this works fine!!! Thanks!

    And how can I from another sub open this XML document and get the data for a line with handle "17E"?

    Do you know where I can read more abot XML foundamental for VBA? I think it might be very helpful to me...

  4. #4
    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
    Thanks God, I'm far enough from Hitleropa
    Posts
    1,697

    Default

    Hi Joro,

    I don't know a good links about XML
    and in fact I'm working with it just
    sporadically
    You can retrive the desired data by
    readind file as simple text file and then
    to strip unused chars, perhaps with
    Regular expression to find the particular
    string
    Search Google btw
    Of course. I'll let you know if I can
    to find something about

    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  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
    Thanks God, I'm far enough from Hitleropa
    Posts
    1,697

    Default

    Quote Originally Posted by Joro-- View Post
    Fixo, this works fine!!! Thanks!

    And how can I from another sub open this XML document and get the data for a line with handle "17E"?

    Do you know where I can read more abot XML foundamental for VBA? I think it might be very helpful to me...
    I wrote this code too quickly so maybe
    some mistakes here, not tested extensively
    Let's try

    Code:
    Private Function FindHandle(strFileName As String, strHandle As String) As Variant
    Dim objNode As MSXML.IXMLDOMNode
    Dim objNodeHdl As MSXML.IXMLDOMNode
    Dim objNodeSpt As MSXML.IXMLDOMNode
    Dim objNodeEpt As MSXML.IXMLDOMNode
    Dim outArr(0 To 2)
    Dim bln As Boolean
    
        Dim xmlDoc As MSXML.DOMDocument
    
        Set xmlDoc = New DOMDocument
    
        bln = xmlDoc.Load(strFileName)
            
            For Each objNode In xmlDoc.childNodes(1).selectNodes("Line")
            
              Set objNodeHdl = objNode.childNodes.Item(0)
              If StrComp(objNodeHdl.Text, strHandle, vbTextCompare) = 0 Then
              Set objNodeSpt = objNode.childNodes.Item(1)
              Set objNodeEpt = objNode.childNodes.Item(2)
              outArr(0) = objNodeHdl.Text
              outArr(1) = objNodeSpt.Text
              outArr(2) = objNodeEpt.Text
              Exit For
              End If
              
            Next objNode
    
        Set xmlDoc = Nothing
        
    FindHandle = outArr
    End Function
    
    'usage:
    Sub HandleTest()
    Dim ar
    Dim fn As String
    Dim hdl As String
    fn = "c:\TestXML.xml"   '<--change file name here
    hdl = "17E"   '<-- change Handle here
    ar = FindHandle(fn, hdl)
    
    MsgBox "Handle: " &  ar(0) & vbcr & _
               "Start point: " &  ar(1) & vbcr & _
               "End point: " &  ar(2) 
    
    End Sub
    ~'J'~
    Last edited by fixo; 25th Jun 2008 at 02:06 pm. Reason: missed ending line of code
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  6. #6
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Bulgaria, Sofia
    Posts
    228

    Default

    I tried it but there is a problem with the line

    Code:
    For Each objNode In xmlDoc.childNodes (1).selectNodes("Line")

  7. #7
    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
    Thanks God, I'm far enough from Hitleropa
    Posts
    1,697

    Default

    Quote Originally Posted by Joro-- View Post
    I tried it but there is a problem with the line

    Code:
    For Each objNode In xmlDoc.childNodes (1).selectNodes("Line")
    Worked just fine here, I don't know
    why it's not working for you

    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  8. #8
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Bulgaria, Sofia
    Posts
    228

    Default

    It says:

    Run-time error '91'

    Object variable or With block not set

    and the respective line is selected by the de******...

  9. #9
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Nov 2005
    Location
    Bulgaria, Sofia
    Posts
    228

    Default

    Fixo, sorry, it was my fault...

    I played a little bit with the first code, so I made it cut the initial lines of the xml file about the version and encoding. So now I changed the line to

    Code:
    For Each objNode In xmlDoc.childNodes(0).selectNodes("Line")
    and it's OK

    THANKS AGAIN!

  10. #10
    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
    Thanks God, I'm far enough from Hitleropa
    Posts
    1,697

    Default

    Registered forum members do not see this ad.

    Hi, Joro
    Please, upload here your final code,
    perhaps, it will helps to somebody else
    Glad, you solved it
    Cheers

    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

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