Batch Export Images

Batch Export Images

Anonymous
Not applicable
3,574 Views
15 Replies
Message 1 of 16

Batch Export Images

Anonymous
Not applicable

Hello,

 

If it's possible, I'd like to create an iLogic rule to batch export thumbnail/isometric camera images of an entire folder. the format needs to be .jpeg because the images will be imported into another program. It looked to me like task scheduler would only be able to do PDFs. 

 

Cound I run this from an external rule using a FOR loop?

 

This is sort of how I was thinking about going about it, but obviously I don't know what to do for the oFolderOccurance part:

 

iLogicStart

Dim sSaveLocation As String
sSaveLocation = "Z:\123fakepath\_IMAGES"

Dim sMessageBoxResult As String = InputBox("Save Location", "Batch Image Export", sSaveLocation)

If System.IO.FilePath.Exists(sMessageBoxResult) Then
    Try
        Dim oFolderOccurance As Document
        For Each oOcc In sMessageBoxResult
            oFolderOccurance.Open()
            Dim sImageName As String = iProperties.Value("Project","Description")
            oCamera = InventorVb.Application.ActiveView.Camera
            oCamera.fit
            oCamera.apply
            InventorVb.Application.ActiveView.SaveAsBitmap(sImageName,1200,800)
            oFolderOccurance.Close()
        Next
    Catch
        MessageBox.Show(oFolderOccurance.Name & "Failed to generate image.", "Error")
    End Try
Else
    MessageBox.Show(sSaveLocation & vbLf & "Does not exist.", "Error")
    Goto iLogicStart
End If

 Any help is appreciated! Thanks!

 

0 Likes
Accepted solutions (1)
3,575 Views
15 Replies
Replies (15)
Message 2 of 16

Anonymous
Not applicable

Made some progress. Still very stuck on how to define/open files within a folder Smiley Sad

 

iLogicImage:

sResultPathImage = InputBox("Image Location", "Batch Image Export",oDefaultPathImage)
If System.IO.Directory.Exists(sResultPathImage) Then
    'MessageBox.Show("Image Location Exists")
Else
    oImage = MessageBox.Show("Invalid Image Location","Batch Image Export",MessageBoxButtons.RetryCancel)
    If oImage = vbRetry Then
        Goto iLogicImage
    Else
        Goto iLogicEnd
    End If
End If

iLogicBatch:

sResultPathBatch = InputBox("Batch Location", "Batch Image Export",oDefaultPathBatch)
If System.IO.Directory.Exists(sResultPathBatch) Then
    'MessageBox.Show("Batch Location Exists")
Else
    oBatch = MessageBox.Show("Invalid Batch Location","Batch Image Export",MessageBoxButtons.RetryCancel)
    If oBatch = vbRetry Then
        Goto iLogicBatch
    Else
        Goto iLogicEnd
    End If
End If

Dim oHolderList As String
Dim oDocFile As Document
For Each oDocFile In sResultPathBatch
    On Error Resume Next
    oDoc = ThisApplication.Documents.Open(sResultPathBatch & oDocFile.Name, True)
    Dim sPartNumber As String = iProperties.Value("Project","Description")
    oCamera = InventorVb.Application.ActiveView.Camera
    oCamera.fit
    oCamera.apply
    InventorVb.Application.ActiveView.SaveAsBitmap(sPartNumber,1200,800)
    oDoc.Close()
    oHolderList = oHolderList & sPartNumber & vbLf
Next
MessageBox.Show(oHolderList)

iLogicEnd:
0 Likes
Message 3 of 16

rossano_praderi
Collaborator
Collaborator
Accepted solution

Hi,

the follow code is a possible solution.

 

Imports System.Windows.Forms
Imports System.IO

Sub Main
'sResultPathImage = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathImage)
sResultPathBatch = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathBatch)

'Dim oHolderList As String
If sResultPathBatch <> "" Then
    Dim oFiles = Directory.GetFiles(sResultPathBatch, "*.IPT")
    For Each FileName As String In oFiles
        On Error Resume Next
        oDoc = ThisApplication.Documents.Open(FileName, True)
    '    Dim sPartNumber As String = iProperties.Value("Project","Description")
    '    oCamera = InventorVb.Application.ActiveView.Camera
    '    oCamera.fit
    '    oCamera.apply
    '    InventorVb.Application.ActiveView.SaveAsBitmap(sPartNumber,1200,800)
    '    oDoc.Close()
    '    oHolderList = oHolderList & sPartNumber & vbLf
    Next
End If
'MessageBox.Show(oHolderList)
End Sub

Private Function SelFolder(sPath As String) As String
On Error Resume Next
    If sPath = "" Then sPath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
    Dim FBrowserDialog As New FolderBrowserDialog
    FBrowserDialog.SelectedPath = sPath
    Dim result As DialogResult = FBrowserDialog.ShowDialog()
    SelFolder = ""
    If ( result = DialogResult.OK ) Then 'And ( FBrowserDialog.SelectedPath <> sPath ) Then
        SelFolder = FBrowserDialog.SelectedPath
    End If
End Function

 



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

Anonymous
Not applicable

This is awesome. Using the browser dialog is a great touch. Thanks!

0 Likes
Message 5 of 16

jfildes
Enthusiast
Enthusiast

I'm trying to use this code but I'm having difficulty. It seems that when I execute the code that it does batch open the folder I selected and it will save the image file. However, the part number property or rather, the file name of the image, is always the same for every file in a given folder because it's pulling the iProperty from the first drawing or part I have open not explicitly the file that was just opened. This seems like a great solution if I could get the file name to match and select an output folder. Any ideas on how to go about that? 

0 Likes
Message 6 of 16

MechMachineMan
Advisor
Advisor

Use this instead of the existing line.

 

Dim sPartNumber As String = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 7 of 16

jfildes
Enthusiast
Enthusiast

Works like a dream! Thanks!

0 Likes
Message 8 of 16

Anonymous
Not applicable

I'm very much a beginner when it comes to this stuff:

What language is that script written in? And where/how would I execute it? Thanks in advance!

0 Likes
Message 9 of 16

rossano_praderi
Collaborator
Collaborator
Hi patricksmith0808,
the code is written in VB.NET and you can use it with the integrated ILogic editor.

Some Ilogic useful info:
https://knowledge.autodesk.com/support/inventor/learn-explore/caas/CloudHelp/cloudhelp/2018/ENU/Inve...

Some VB.NET useful info:
https://docs.microsoft.com/en-us/dotnet/visual-basic/

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 16

jweiss1313
Collaborator
Collaborator

I'm trying to run this rule in Inventor 2019 and I get prompted for a folder that I assume if the folder that contains the Inventor files and when I hit OK Inventor opens every Inventor file in the folder and then nothing happens.  No image files get created and I never even get prompted to where I want the image files to go.

 

Any help would be appreciated.  I'd like to get this to work for a project I'm on.  Thanks in advance!

John C Weiss Jr.
CAD Manager
Follett LLC.
0 Likes
Message 11 of 16

jweiss1313
Collaborator
Collaborator

Here is the code I'm using:

Imports System.Windows.Forms
Imports System.IO

Sub Main
'sResultPathImage = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathImage)
sResultPathBatch = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathBatch)

'Dim oHolderList As String
If sResultPathBatch <> "" Then
    Dim oFiles = Directory.GetFiles(sResultPathBatch, "*.IPT")
    For Each FileName As String In oFiles
        On Error Resume Next
        oDoc = ThisApplication.Documents.Open(FileName, True)
    '    Dim sPartNumber As String = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value
    '    oCamera = InventorVb.Application.ActiveView.Camera
    '    oCamera.fit
    '    oCamera.apply
    '    InventorVb.Application.ActiveView.SaveAsBitmap(sPartNumber,1200,800)
    '    oDoc.Close()
    '    oHolderList = oHolderList & sPartNumber & vbLf
    Next
End If
'MessageBox.Show(oHolderList)
End Sub

Private Function SelFolder(sPath As String) As String
On Error Resume Next
    If sPath = "" Then sPath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
    Dim FBrowserDialog As New FolderBrowserDialog
    FBrowserDialog.SelectedPath = sPath
    Dim result As DialogResult = FBrowserDialog.ShowDialog()
    SelFolder = ""
    If ( result = DialogResult.OK ) Then 'And ( FBrowserDialog.SelectedPath <> sPath ) Then
        SelFolder = FBrowserDialog.SelectedPath
    End If
End FunctionImports System.Windows.Forms
Imports System.IO

Sub Main
'sResultPathImage = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathImage)
sResultPathBatch = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathBatch)

'Dim oHolderList As String
If sResultPathBatch <> "" Then
    Dim oFiles = Directory.GetFiles(sResultPathBatch, "*.IPT")
    For Each FileName As String In oFiles
        On Error Resume Next
        oDoc = ThisApplication.Documents.Open(FileName, True)
    '    Dim sPartNumber As String = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value
    '    oCamera = InventorVb.Application.ActiveView.Camera
    '    oCamera.fit
    '    oCamera.apply
    '    InventorVb.Application.ActiveView.SaveAsBitmap(sPartNumber,1200,800)
    '    oDoc.Close()
    '    oHolderList = oHolderList & sPartNumber & vbLf
    Next
End If
'MessageBox.Show(oHolderList)
End Sub

Private Function SelFolder(sPath As String) As String
On Error Resume Next
    If sPath = "" Then sPath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
    Dim FBrowserDialog As New FolderBrowserDialog
    FBrowserDialog.SelectedPath = sPath
    Dim result As DialogResult = FBrowserDialog.ShowDialog()
    SelFolder = ""
    If ( result = DialogResult.OK ) Then 'And ( FBrowserDialog.SelectedPath <> sPath ) Then
        SelFolder = FBrowserDialog.SelectedPath
    End If
End Function
John C Weiss Jr.
CAD Manager
Follett LLC.
0 Likes
Message 12 of 16

_dscholtes_
Advocate
Advocate

@jweiss1313 

    '    oCamera.fit
    '    oCamera.apply
    '    InventorVb.Application.ActiveView.SaveAsBitmap(sPartNumber,1200,800)

 

You are aware that any line starting with a ' is not executed?
' is used for adding comments to code.

@Anonymous 

oCamera = InventorVb.Application.ActiveView.Camera
oCamera.fit
oCamera.apply
InventorVb.Application.ActiveView.SaveAsBitmap(sPartNumber,1200,800)

Any specific reason you didn't use the camera object in the last line of code?

oCamera.SaveAsBitmap(sPartNumber,1200,800)

 

0 Likes
Message 13 of 16

jweiss1313
Collaborator
Collaborator

Thanks for your help.  I made the changes you suggested and the program opens the files and closes them, but it still does not save the bitmap images.

 

I have never used iLogic so I'm trying to use someone else's code that apparently worked for them.

 

I'm posted the code I'm trying to use again if anyone can help I would really appreciate it.

Imports System.Windows.Forms
Imports System.IO

Sub Main
sResultPathImage = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathImage)
sResultPathBatch = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathBatch)

Dim oHolderList As String
If sResultPathBatch <> "" Then
    Dim oFiles = Directory.GetFiles(sResultPathBatch, "*.IPT")
    For Each FileName As String In oFiles
        On Error Resume Next
        oDoc = ThisApplication.Documents.Open(FileName, True)
        Dim sPartNumber As String = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value
        oCamera = InventorVb.Application.ActiveView.Camera
        oCamera.fit
        oCamera.apply
        oCamera.SaveAsBitmap(sPartNumber,1200,800)
        oDoc.Close()
        oHolderList = oHolderList & sPartNumber & vbLf
    Next
End If
MessageBox.Show(oHolderList)
End Sub

Private Function SelFolder(sPath As String) As String
On Error Resume Next
    If sPath = "" Then sPath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
    Dim FBrowserDialog As New FolderBrowserDialog
    FBrowserDialog.SelectedPath = sPath
    Dim result As DialogResult = FBrowserDialog.ShowDialog()
    SelFolder = ""
    If ( result = DialogResult.OK ) Then 'And ( FBrowserDialog.SelectedPath <> sPath ) Then
        SelFolder = FBrowserDialog.SelectedPath
    End If
End Function

 

John C Weiss Jr.
CAD Manager
Follett LLC.
0 Likes
Message 14 of 16

_dscholtes_
Advocate
Advocate

I think the problem lies in this line of code:

oCamera.SaveAsBitmap(sPartNumber,1200,800)

sPartNumber is just a number (e.g. 12345) and not a full file name (e.g. c:\my images\12345.bmp).

If there's no error message, I think the images are being created, but they're saved at the default location of where windows saves images and not in your selected location (you could try to search for them to check this).
I don't see a reference to your earlier selected location in above line of code and it seems like the file name is also missing a file extension (but maybe inventor adds a default extension).
Try to replace with:

oCamera.SaveAsBitmap(sResultPathImage & "\" & sPartNumber & ".bmp",1200,800)

 

0 Likes
Message 15 of 16

jweiss1313
Collaborator
Collaborator

I worked with our Autodesk reseller and we got the code to work.  You first get prompted for the location of the jpeg's and then get prompted for the location of the Inventor part files.  It then opens every part file, creates the image file and closes all files.  Here is the code:  Special shout out to Synergis Technologies.

 

Imports System.Windows.Forms
Imports System.IO

Sub Main
sResultPathImage = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathImage)
sResultPathBatch = selFolder(ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath) 'oDefaultPathBatch)

Dim oHolderList As String
If sResultPathBatch <> "" Then
    Dim oFiles = Directory.GetFiles(sResultPathBatch, "*.IPT")
    For Each FileName As String In oFiles
        On Error Resume Next
        oDoc = ThisApplication.Documents.Open(FileName, True)
        Dim sPartNumber As String = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value
        oCamera = InventorVb.Application.ActiveView.Camera
        oCamera.Fit
        oCamera.Apply
		Dim Test As String = sResultPathImage + "\" + sPartNumber + ".bmp"
        oCamera.SaveAsBitmap(Test,1200,800)
        oDoc.Close()
        oHolderList = oHolderList & sPartNumber & vbLf
    Next
End If
MessageBox.Show(oHolderList)
End Sub

Private Function SelFolder(sPath As String) As String
On Error Resume Next
    If sPath = "" Then sPath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
    Dim FBrowserDialog As New FolderBrowserDialog
    FBrowserDialog.SelectedPath = sPath
    Dim result As DialogResult = FBrowserDialog.ShowDialog()
    SelFolder = ""
    If ( result = DialogResult.OK ) Then 'And ( FBrowserDialog.SelectedPath <> sPath ) Then
        SelFolder = FBrowserDialog.SelectedPath
    End If
End Function

 

 

 

John C Weiss Jr.
CAD Manager
Follett LLC.
Message 16 of 16

rkerrC4X6B
Explorer
Explorer

Hey all,

Thanks for the code, it mostly works perfectly for my needs. Hopwever Im trying to adjust it to set the outputs file name as the file name for the part itself, not the iproperties part number as we reguarly have different values in each, the file name is the correct one. 

 

I was hoping it would be as simple as changing this line:

 Dim sPartNumber As String = oDoc.PropertySets("Design Tracking Properties")("Part Number").Value

 to something like:

 Dim sPartNumber As String = FileName

But this doesn't work at all and breaks the rule basically. I'm using Johns version of the code. Does anyone know how to make what im after actually work? Thanks!

0 Likes