Works for Ilogic but not VBA?

Works for Ilogic but not VBA?

mslosar
Advisor Advisor
808 Views
6 Replies
Message 1 of 7

Works for Ilogic but not VBA?

mslosar
Advisor
Advisor

I realize vba and iLogic aren't the exact same thing, but ilogic is based on vba from what i understand.

 

Anyhow, this statement in Ilogic works just fine.

 

Dim ReadTAB As New System.IO.StreamReader
Dim WordSets As New ArrayList

 

However, both seem to fail as non-existent under VBA in inventor (2013). I'm not an authority on programming by a longshot but i fail to see why vb.net statements work fine in ilogic but not in vba.

 

Am I missing a callout or reference file or something?

 

Anyhelp would be appreciated. t

 

Thanks

0 Likes
Accepted solutions (1)
809 Views
6 Replies
Replies (6)
Message 2 of 7

mrattray
Advisor
Advisor
Because VBA is not .NET, it is COM.
Mike (not Matt) Rattray

0 Likes
Message 3 of 7

mslosar
Advisor
Advisor
And iLogic is?

Is there a way to make it work or is it just not possible?
0 Likes
Message 4 of 7

mrattray
Advisor
Advisor

iLogic is a bastardized combination of .NET and COM.

I would avoid using the As New statement. Instead, declare your variables and then Set New in a separate line.

 

Here's a quick and dirty example from my project file where I read data from a carriage return delimited text file:

Private Sub load_Click()

Dim TA As Double
Dim TB As Double
Dim TC As Double
Dim TD As Double
Dim TE As Double
Dim BA As Double
Dim BB As Double
Dim BC As Double
Dim BD As Double
Dim BE As Double
Dim job As String
Dim FSO As FileSystemObject
Dim FSOfile As TextStream
Dim filePath As String
Dim box As Boolean

job = jobNum.value

filePath = "R:\Products\DWG-2007\DM~Drawer Magnets(STN)\Flange Text Files\" & job & ".txt"

Set FSO = New FileSystemObject
On Error GoTo fileNotFound
Set FSOfile = FSO.OpenTextFile(filePath)

On Error GoTo fileNotRight
If FSOfile.ReadLine <> "UDR" Then
    box = MsgBox("This flange file does not match this CDM design (Round UnDrilled)", vbOKOnly, "Error")
    Exit Sub
End If
TA = FSOfile.ReadLine
TB = FSOfile.ReadLine
TC = FSOfile.ReadLine
TD = FSOfile.ReadLine
TE = FSOfile.ReadLine
BA = FSOfile.ReadLine
BB = FSOfile.ReadLine
BC = FSOfile.ReadLine
BD = FSOfile.ReadLine
BE = FSOfile.ReadLine
botEqTop.value = FSOfile.ReadLine

boxTA.value = TA
boxTB.value = TB
boxTC.value = TC
boxTD.value = TD
boxTE.value = TE
boxBA.value = BA
boxBB.value = BB
boxBC.value = BC
boxBD.value = BD
boxBE.value = BE

FSOfile.Close

box = MsgBox("Flange data was loaded successfully", vbOKOnly, "Success!")

Exit Sub

fileNotFound:
box = MsgBox("Job file not found.", vbOKOnly, "Error")
Exit Sub

fileNotRight:
box = MsgBox("There was a problem reading from the job file.", vbOKOnly, "Error")
Exit Sub
End Sub

 

 

Mike (not Matt) Rattray

0 Likes
Message 5 of 7

mslosar
Advisor
Advisor

I'll look into that. I was going to look at FSO later today since i feel like i'm spinning my wheels. What i'm trying to do is essentially the same thing shown here:

 

http://forums.autodesk.com/t5/Autodesk-Inventor/BOM-export-to-formatted-text/m-p/3396033/highlight/t...

 

but wanting to do so in VBA instead.

 

We have a very, shall we say, unique backend server we have to input our BOM's into. We're trying to to find a way to export the BOM from an Inventor assembly and put them (almost) directly into our system. The trick is formating the specific fields in basically the same manner as the linked post where we have to have x amount of spaces per variable all strung together.

 

I had a good (and functioning) framework in iLogic but thought it'd be better off in VBA. I managed to get everything else working in VBA except the reading of the bom file and parsing it into the same basic array structure.

 

Can this FSO method still accomplish that?

0 Likes
Message 6 of 7

ekinsb
Alumni
Alumni
Accepted solution

I looked at the link but I'm not sure I fully understand what you're trying to do.  But first to explain a bit about why iLogic code doesn't work in VBA.  iLogic is based on VB.Net and VBA is using the same engine as Visual Basic 6.  They're very similar but still two different flavor of the Visual Basic language.  One very big difference is that the full .Net Framework is accessible from VB.Net.  The object you were trying to use in VBA is a .Net Framework object and is not available in VBA.

 

Here's a pure VBA program that does something somewhat similar to what you asked.  It reads in a file, extracts pieces it's interested in and writes it out in a reformatted way.

 

First, here's the original input data.

 

Block, Steel, 5, Red
Cylinder, Copper, 2, Yellow
    Cone,    Glass, 8, Green
Mushroom,    Steel, 59, Blue-Green
Tree,Mud,33, Purple
Sphere, Titanium, 11, Silver
Pyramid, Carbon Fiber, 2, Black

 

And here's the result file that the program creates.

 

Block                    5          Red
Cylinder                 2          Yellow
Cone                     8          Green
Mushroom                 59         Blue-Green
Tree                     33         Purple
Sphere                   11         Silver
Pyramid                  2          Black 

 

Here's the program:

 

Public Sub Reformat()
    Dim newFilename As String
    newFilename = "C:\Temp\Newfile.txt"
    Dim newfile As Integer
    newfile = FreeFile
   
    Open newFilename For Output As newfile
   
    Dim existingFilename As String
    existingFilename = "C:\Temp\Test.txt"
   
    Dim existingFile As Integer
    existingFile = FreeFile
    Open existingFilename For Input As existingFile
    Do While Not EOF(existingFile)
        ' Read the existing line from the file.
        Dim inputData As String
        Line Input #existingFile, inputData
       
        ' Extract the bits out of the line that you want.  This
        ' example assumes they're comma delimited.
        Dim pieces() As String
        pieces = Split(inputData, ",")
       
        ' Clean up any white space
        Dim i As Integer
        For i = 0 To UBound(pieces) - 1
            pieces(i) = Trim$(pieces(i))
        Next
       
        ' Using the wanted pieces, build the output string.
        Dim result As String
        result = pieces(0) & Space(25 - Len(pieces(0)))
        result = result & pieces(2) & Space(10 - Len(pieces(2)))
        result = result & pieces(3)
       
        ' Write the line, reformatted.
        Print #newfile, result
    Loop
   
    Close #newfile
    Close #existingFile
End Sub


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 7 of 7

mslosar
Advisor
Advisor

Thank you!

 

That's what I needed. I think i've got a solid base now. I need to find a good place to study up on arrays because i'm not good with them, but i've got a better understanding now.

 

 

0 Likes