Converting LOTS of STEP files

Converting LOTS of STEP files

Anonymous
Not applicable
2,580 Views
17 Replies
Message 1 of 18

Converting LOTS of STEP files

Anonymous
Not applicable

I'm using Inventor 2014, and I'm working with a FIRST FRC Robotics team. We'd like to convert the STEP files we get from vendors into Inventor files so we can use them more easily to create the design of our robot. The problem is, there are several thousand parts, so doing it by hand, while possible, would take too much time for the number of students we have.

 

I've written a script that renames the parts files, including the internal references, to match our part numbering system, but now I need to do the actual conversion. The simplest way appears to be to open each part in Inventor, which then creates a .ipt file in the project, and then close the part - rinse repeat. It seems like the perfect job for some kind of scripting or macro language. Inventor uses VBA, which I haven't touched for years, but I'm working on picking it back up.

 

I've done a lot of searching, but haven't found anything that works. Can someone please help me?

 

Thanks,

  Ray

 

Below is my test routine. In it, I'm just trying to open a STEP file in Inventor. The part filename comes from the filenames.txt file.

 

Sub ProcessDoc()
    ' Open filename file
    Dim File_Path As String
    File_Path = "C:\Users\Ray\Desktop\Unzipped Parts\filenames.txt"
    MsgBox "Trying to open " + File_Path
    Dim fs, f, ts, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(File_Path)
    Set ts = f.OpenAsTextStream(1, -2)
    s = ts.ReadLine
    MsgBox s
    Set oInv = ThisApplication.Documents.Open(s)
    ts.Close
End Sub

 

0 Likes
2,581 Views
17 Replies
Replies (17)
Message 2 of 18

rossano_praderi
Collaborator
Collaborator

This is a simple but functional example, useful as a starting point.

 

Sub ProcessDoc()
    Dim File_Path As String
    Dim FileNum As Integer
    Dim Fpath As String

    File_Path = "C:\Users\Ray\Desktop\Unzipped Parts\filenames.txt"
    If Dir(File_Path) <> "" Then
        MsgBox "Trying to open " + File_Path
        
        FileNum = FreeFile()
        Open File_Path For Input As #FileNum
    
        While Not EOF(FileNum)
            Line Input #FileNum, Fpath
            If Dir(Fpath) <> "" Then
                Debug.Print Fpath
                'MsgBox Fpath
                Set oInv = ThisApplication.Documents.Open(Fpath)
                Call oInv.SaveAs(Replace(Fpath, ".stp", ".ipt"), True)
                oInv.Close True
            End If
        Wend
    End If
End Sub

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 3 of 18

Anonymous
Not applicable

 

Thank you for the help. I'm still having problems, but I don't know if it's the code or something else. At first, it seemed to work, but had a problem because it was trying to open the first file, which I've been messing with a lot. So I changed the filenames.txt file to point to other, as yet unprocessed files, and now it seems to have a problem opening filenames.txt. I love programming, but don't enjoy moments like this. There's something subtle but simple wrong. I'll find it...

0 Likes
Message 4 of 18

Anonymous
Not applicable

Problem was with my filenames. I'm almost there ....

0 Likes
Message 5 of 18

Owner2229
Advisor
Advisor

Hi, wouldn't it be better for you to put all the STEP files in one folder and use rule that will find all "*.step" files, convert them and save them with new filename based on your numbering system? Or it can just convert them, if you have already renamed them.

E.g.: You can have one part number in your TXT file and it will be updated after each file conversion, so inicialy u'll have there something like "PN001" and after converting 5 files you will have "PN006".

 

Would be something like this acceptable for you? 

 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 6 of 18

Owner2229
Advisor
Advisor

Here is the rule for only converting all STEP files from one folder:

 

Imports System.IO
Imports System.IO.File
Sub Main()
    'You can change this line to match your location if you have all files in one folder
    oPath = "C:\FolderContainingAllParts"
    'Or you can use this to select the folder, delete it if not needed
    Dim dialog1 = New System.Windows.Forms.FolderBrowserDialog()
    dialog1.SelectedPath = oPath
    dialog1.ShowNewFolderButton = True
    If System.Windows.Forms.DialogResult.OK = dialog1.ShowDialog() Then
        oPath = dialog1.SelectedPath
    Else
        MsgBox("No folder selected. Copiyng canceled.")
        Exit Sub
    End If
    If Not System.IO.Directory.Exists(oPath) Then
        MsgBox("Folder doesn't exist")
	Exit Sub
    End If
    Dim oFile As String
    Dim oNewName As String
    'It will only look for STEP files, as there is "*.stp" at the end.
'You may need to use "*.step" instead.
For Each oFile In System.IO.Directory.EnumerateFiles(oPath, "*.stp") oInv = ThisApplication.Documents.Open(oFile) oNewName = Left(oFile, Len(oFile) - 4) & ".ipt" Call oInv.SaveAs(oNewName, True) oInv.Close(True) Next End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 18

Anonymous
Not applicable

The code opens  the STEP file and reads it in, but the save as doesn't work. I get an error:

 

Data Exchange Export: problems encountered while executing this command.

Failed to create folder, ""

 

Thought maybe using the Dir command with the full filename was the problem, so I stripped off the filename. No change.

 

Here's current code:

    Public Sub Main()
        ' Process STEP files
        ProcessDoc
    End Sub
    
Sub ProcessDoc()
    Dim File_Path As String
    Dim FileNum As Integer
    Dim Fpath As String
    Dim Folder As String

    File_Path = "C:\Users\Ray\Desktop\Unzipped Parts\filenames.txt"
    If Dir(File_Path) <> "" Then
        MsgBox "Trying to open " + File_Path
        
        FileNum = FreeFile()
        Open File_Path For Input As #FileNum
    
        While Not EOF(FileNum)
            Line Input #FileNum, Fpath
            Folder = FolderFromPath(Fpath)
            If Dir(Folder) <> "" Then
                Debug.Print Fpath
                'MsgBox Fpath
                Set oInv = ThisApplication.Documents.Open(Fpath)
                Call oInv.SaveAs(Replace(Fpath, ".stp", ".ipt"), True)
                oInv.Close True
            Else
                MsgBox "Error: " + Fpath + " appears to be incorrect."
            End If
        Wend
        MsgBox "End of File"
    End If
End Sub
Public Function FolderFromPath(strFullPath As String) As String
    FolderFromPath = Left(strFullPath, InStrRev(strFullPath, "\"))
End Function
0 Likes
Message 8 of 18

Anonymous
Not applicable
Have you tried using the Inventor task scheduler to do the translation? you could run a sequential task, one to run the rename command, then a second to translate the contents of a folder from step to Inventor.
0 Likes
Message 9 of 18

rossano_praderi
Collaborator
Collaborator

There no error in my original code, I've tested before post it.


The following can be a variation of the code, that is not necessary but if you would like to try....

 

If Dir(Folder, vbDirectory) then ....

 

An other possible issue are the spaces in the path of each file, sometimes you get back a wrong response.

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 10 of 18

Anonymous
Not applicable

I don't know what I'm doing wrong. I tried the latest suggested code and I'm still getting an error: Run-time error '-2147467259 (80004005)': Method 'SaveAs' of object 'AssemblyDocument' failed.

 

There are some sub directories. Could these be causing the problem?

0 Likes
Message 11 of 18

rossano_praderi
Collaborator
Collaborator

Hi Benjamin,
you have this problem because some "STEPs" are opened as assembly.

I think you can have only two possible solutions:
- you should adapt your code to suite the "STEP" traslation "ADDIN" needs

- by using the "Inventor Scheduler" (as suggested by Donovan) to convert your files

 

Is much more easier to use the scheduler, give it a try.

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 12 of 18

Anonymous
Not applicable

Bregs,

 

The error I'm still getting with your code is:

 

Data Exchange Export: problems encountered while executing this command.

    Failed to create folder, ""

 

I'll try moving everything to a directory path that doesn't have spaces. I'll let you know how it goes.

 

----

 

Mike,

 

I'm getting errors on your Imports statements and the Dim dialog1 statement. Not sure what's going on there.

 

 

Ray

0 Likes
Message 13 of 18

Anonymous
Not applicable

I think I know what the problem is, but not how to fix it. The first problem was that my files had the extension .STEP, which I needed to fix in the Replace statement. The second problem is that some of my "parts" are actually assemblies, so they need to be saved as .IAM rather than .IPT files. Do any of you know how to detect if it's an assembly or a part so I know which way to save it in the code?

 

Thanks,

  Ray

0 Likes
Message 14 of 18

Owner2229
Advisor
Advisor

Hi, try the code below:

 

oDoc = ThisApplication.ActiveDocument
If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
    MsgBox("This is assemby.")
Else If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
    MsgBox("This is part.")
End If
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 15 of 18

rossano_praderi
Collaborator
Collaborator

Hi Ray,

next days I'll give you a more complete code to solve this issue.

 

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 16 of 18

Anonymous
Not applicable

That's what I'm leaning toward now. I can always set up sub-directories later if needed.

 

I'm really close. The last problem I have is determining if it's a part file or an assembly file and saving it with the correct extension. The script now seems to work on regular part files.

 

I really appreciate all the help I've gotten from everyone here.

 

Ray

0 Likes
Message 17 of 18

Anonymous
Not applicable

Here's the code right now. The problem is that the STEP file is seen as an uknown document. I can't save it as a part file if it's an assembly file, or vice versa, so I need some way to detect which type of file it is. I'm open to any suggestions. I'm so close I can taste it. I hope this is the last obstacle to getting this script working.

 

 

 

 

    Public Sub Main()
        ' Process STEP files
        ProcessDoc
    End Sub


Sub ProcessDoc()
    Dim File_Path As String
    Dim FileNum As Integer
    Dim Fpath As String
    Dim Folder As String

    File_Path = "C:\Users\Ray\Desktop\Unzipped\filenames.txt"
    If Dir(File_Path) <> "" Then
        MsgBox "Trying to open " + File_Path
        
        FileNum = FreeFile()
        Open File_Path For Input As #FileNum
    
        Do While Not EOF(FileNum)
            Line Input #FileNum, Fpath
            Folder = FolderFromPath(Fpath)
            If Dir(Folder) <> "" Then
                Debug.Print Fpath
                'MsgBox Fpath
                Set oInv = ThisApplication.Documents.Open(Fpath)
                Dim docType As DocumentTypeEnum
                Dim ext As String
                Select Case docType
                    Case kAssemblyDocumentObject
                        ext = ".iam"
                    Case kPartDocumentObject
                        ext = ".ipt"
                    Case kDesignElementDocumentObject
                        ext = "???"
                        MsgBox "Error: " + Fpath + " is a Design Element Object."
                        Exit Do
                    Case kDrawingDocumentObject
                        MsgBox "Error: " + Fpath + " is a Drawing Element Object."
                        Exit Do
                    Case kForeignModelDocumentObject
                        MsgBox "Error: " + Fpath + " is a Foreign Model Object."
                        Exit Do
                    Case kNoDocument
                        MsgBox "Error: " + Fpath + " is a Not recognizable as a document."
                        Exit Do
                    Case kPresentationDocumentObject
                        MsgBox "Error: " + Fpath + " is a Presentation document."
                        Exit Do
                    Case kSATFileDocumentObject
                        MsgBox "Error: " + Fpath + " is a SAT File document."
                        Exit Do
                    Case kUnknownDocumentObject
                        MsgBox "Error: " + Fpath + " is an Unknown Document Object."
                        Exit Do
                    Case Else
                        MsgBox "Error: " + Fpath + " is unknown doctype."
                        Exit Do
                End Select
                Call oInv.SaveAs(Replace(Fpath, ".STEP", ext), True)
                oInv.Close True
            Else
                MsgBox "Error: " + Fpath + " appears to be incorrect."
            End If
        Loop
        MsgBox "End of File"
    End If
End Sub
Public Function FolderFromPath(strFullPath As String) As String
    FolderFromPath = Left(strFullPath, InStrRev(strFullPath, "\"))
End Function

0 Likes
Message 18 of 18

rossano_praderi
Collaborator
Collaborator

Hi Ray,

this is the next step...
Check the folder name which will contain all files.

 

....
                Select Case oinv.DocumentType
                    Case kAssemblyDocumentObject
                        Set oDoc = ThisApplication.ActiveDocument
                        oDoc.FullFileName = Replace(Fpath, ".stp", ".iam")
                        oPath = Left(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\", -1))
                        For Each oref In oDoc.AllReferencedDocuments
                            oref.FullFileName = oPath & Right(oref.FullFileName, Len(oref.FullFileName) - InStrRev(oref.FullFileName, "\", -1))
                        Next
                        oDoc.Save2 True ' if the assembly or any other file exist you will be prompted to save it
                        oDoc.Close True
                    Case kPartDocumentObject
                        Call oinv.SaveAs(Replace(Fpath, ".stp", ".ipt"), True)
....
....
                End Select
                oinv.Close True
....

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes