Help with LOOP

Discussion in 'AutoCAD' started by Miles, Oct 27, 2004.

  1. Miles

    Miles Guest

    All,
    I need help coding a simple loop. Basically, I have a text file (SYM.TXT) in
    this format:

    1348453.9878,2064999.5856
    1348787.1992,2064702.2654
    1349476.1138,2064569.9584
    1349082.5947,2064645.3053
    1348615.5279,2064734.8179

    These are the command line entries that I am using.

    -INSERT E_WTR_MH
    S
    300
    (looping values from text file)

    and hopefully when done, I will have 5,000+ symbols added to our drawing.

    Can anyone provide some pointers for doing this in VBA ?
    Thanks.
     
    Miles, Oct 27, 2004
    #1
  2. Miles

    Matt W Guest

    Try this...

    Code:
    Sub Main()
    Dim i As Integer
    Dim filenum As Integer
    Dim x As Variant
    Dim insPT(0 To 2) As Double
    Dim strText As String
    Dim objBlock As AcadBlockReference
    
    filenum = FreeFile
    Open "E:\Temp\File.txt" For Input As filenum
    Do While Not EOF(filenum)
    Line Input #filenum, strText
    x = Split(strText, ",", , vbTextCompare)
    insPT(0) = CDbl(x(0))
    insPT(1) = CDbl(x(1))
    insPT(2) = CDbl(0)
    ' Replace the following with your block name and desired
    scale/rotation angle
    Set objBlock = ThisDrawing.ModelSpace.InsertBlock(insPT,
    "e:\temp\my_block.dwg", 1#, 1#, 1#, 0#)
    objBlock.Update
    Loop
    Close filenum
    End Sub
    
    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | All,
    | I need help coding a simple loop. Basically, I have a text file (SYM.TXT)
    in
    | this format:
    |
    | 1348453.9878,2064999.5856
    | 1348787.1992,2064702.2654
    | 1349476.1138,2064569.9584
    | 1349082.5947,2064645.3053
    | 1348615.5279,2064734.8179
    |
    | These are the command line entries that I am using.
    |
    | -INSERT E_WTR_MH
    | S
    | 300
    | (looping values from text file)
    |
    | and hopefully when done, I will have 5,000+ symbols added to our drawing.
    |
    | Can anyone provide some pointers for doing this in VBA ?
    | Thanks.
    |
    |
     
    Matt W, Oct 27, 2004
    #2
  3. Miles

    Miles Guest

    Code:
    [QUOTE]
    Sub Main()
    Dim i As Integer
    Dim filenum As Integer
    Dim x As Variant
    Dim insPT(0 To 2) As Double
    Dim strText As String
    Dim objBlock As AcadBlockReference
    
    filenum = FreeFile
    Open "E:\Temp\File.txt" For Input As filenum
    Do While Not EOF(filenum)
    Line Input #filenum, strText
    x = Split(strText, ",", , vbTextCompare)
    insPT(0) = CDbl(x(0))
    insPT(1) = CDbl(x(1))
    insPT(2) = CDbl(0)
    ' Replace the following with your block name and desired
    scale/rotation angle
    Set objBlock = ThisDrawing.ModelSpace.InsertBlock(insPT,
    "e:\temp\my_block.dwg", 1#, 1#, 1#, 0#)
    objBlock.Update
    Loop
    Close filenum
    End Sub
    
    [/QUOTE]

    Thanks - I will give it a try. Since posting, I tried my hand at it, and
    this is what I came up with. It works, but not probably as elegant as yours
    (or others).

    Public Sub cmdImport_Click()
    Dim sTemp As String
    Dim sFile As String
    Dim sBlock As String
    Dim sMessage, Message, Title, Default, MyValue

    On Error GoTo ErrHandler

    CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt)"
    CommonDialog1.FilterIndex = 2
    CommonDialog1.ShowOpen
    sFile = CommonDialog1.FileName & ""

    Message = "Enter the NAME of your BLOCK"
    sMessage = "Enter the SCALE of your BLOCK"
    Title = "Symbol Input"
    sBlock = InputBox(Message, Title, Default)
    If sBlock = "" Then End
    sScale = InputBox(sMessage, Title, Default)
    If sScale = "" Then End

    Open sFile For Input As #1
    While Not EOF(1)
    Line Input #1, sTemp
    ThisDrawing.SendCommand "-insert" & vbCr & sBlock & vbCr & sTemp &
    vbCr & sScale & vbCr & sScale & vbCr & "0" & vbCr
    Wend
    Close #1
    End

    ErrHandler:
    End

    End Sub
     
    Miles, Oct 27, 2004
    #3
  4. Miles

    Matt W Guest

    I try to make it a habit to NOT use SendCommand.

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    |>
    Code:
    | > Sub Main()
    | >    Dim i As Integer
    | >    Dim filenum As Integer
    | >    Dim x As Variant
    | >    Dim insPT(0 To 2) As Double
    | >    Dim strText As String
    | >    Dim objBlock As AcadBlockReference
    | >
    | >    filenum = FreeFile
    | >    Open "E:\Temp\File.txt" For Input As filenum
    | >    Do While Not EOF(filenum)
    | >        Line Input #filenum, strText
    | >        x = Split(strText, ",", , vbTextCompare)
    | >        insPT(0) = CDbl(x(0))
    | >        insPT(1) = CDbl(x(1))
    | >        insPT(2) = CDbl(0)
    | >        ' Replace the following with your block name and desired
    | > scale/rotation angle
    | >        Set objBlock = ThisDrawing.ModelSpace.InsertBlock(insPT,
    | > "e:\temp\my_block.dwg", 1#, 1#, 1#, 0#)
    | >        objBlock.Update
    | >    Loop
    | >    Close filenum
    | > End Sub
    | > 
    | >
    |
    | Thanks - I will give it a try. Since posting, I tried my hand at it, and
    | this is what I came up with. It works, but not probably as elegant as
    yours
    | (or others).
    |
    | Public Sub cmdImport_Click()
    | Dim sTemp As String
    | Dim sFile As String
    | Dim sBlock As String
    | Dim sMessage, Message, Title, Default, MyValue
    |
    | On Error GoTo ErrHandler
    |
    | CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt)"
    | CommonDialog1.FilterIndex = 2
    | CommonDialog1.ShowOpen
    | sFile = CommonDialog1.FileName & ""
    |
    | Message = "Enter the NAME of your BLOCK"
    | sMessage = "Enter the SCALE of your BLOCK"
    | Title = "Symbol Input"
    | sBlock = InputBox(Message, Title, Default)
    | If sBlock = "" Then End
    | sScale = InputBox(sMessage, Title, Default)
    | If sScale = "" Then End
    |
    | Open sFile For Input As #1
    | While Not EOF(1)
    | Line Input #1, sTemp
    | ThisDrawing.SendCommand "-insert" & vbCr & sBlock & vbCr & sTemp &
    | vbCr & sScale & vbCr & sScale & vbCr & "0" & vbCr
    | Wend
    | Close #1
    | End
    |
    | ErrHandler:
    | End
    |
    | End Sub
    |
    |
     
    Matt W, Oct 27, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.