Friday, 8 January 2016

Batch Assembly Export

Not enough time today for a proper post just a quick one. I have been working on a vb-ilogic code to identify and resolve all references. On the same process I hope to get a code for renaming the files as well.

This will of course be free and available for everybody to download from this blog but you need to stick around and check my progress because I don’t have a time frame of when that will be available.

Replacing references is working but we get into errors and limitations for special files like skeletons in Frame Generator, Tube and Pipe Route and Runs, welded assemblies, documents marked as Reference in BOM, etc. It’s a slow progress and I can’t allocate it as much time as I would like at the moment.

On a side note, there is a NDA (Non Disclosure Agreement) between me and Autodesk so I can’t say much but I was assured that the T&P list is very important for them.

Enough said on that and for today I will share a bit of code that hopefully will get your productivity up and get those manual tasks to minimum. The code needs to be run from inside an assembly and will save all parts in STEP format in a folder right next to the main assembly. I have done this code for a friend and while he is still testing you can have a preview of it (hurry up Karol).

The SaveAs option in the code can be used with any formats listed in the Save Copy As dialog but you might want to investigate the results and use the ApplicationAddIns and HasSaveCopyAsOption which will give you more control over what gets exported and some of the options in the save as dialog.

The Save As dialog can be bypassed and process the assembly as a batch

If, for example, you need to export the current drawing to PDF you can’t (shouldn’t) use the SaveAs code because it will only export the first sheet and you can’t control, colors, resolution, line weights, printing sheet range, etc. There’s more info in my previous post here.

Here’s the code, enjoy:


 'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If
'get user input
RUsure = MessageBox.Show ( _
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If
'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = ThisDoc.Path
'get STEP target folder path
oFolder = oPath & "\" & oAsmName & " STEP Files"
'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stp") , True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
    Dim oCurFile As Document
    oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    oCurFileName = oCurFile.FullFileName
   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)

    Try
        oCurFile.SaveAs(oFolder & "\" & ShortName & (".stp") , True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)



I have tested it and save the assembly in all these other formats, including Inventor iam:

Results of exporting the assembly.
 
'- - - - - - - - - - - - -Assembly - - - - - - - - - - - - 
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".iam") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".dwg") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".bmp") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".CATProduct") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".dwf") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".dwfx") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".gif") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".igs") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".ige") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".iges") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".jpg") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".jt") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".pdf") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".png") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".x_b") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".x_t") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".g") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".neu") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".sat") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stp") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".ste") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".step") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".stl") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".tiff") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".xgl") , True)
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName &(".zgl") , True)


Later,
ADS


Edit 16-04-14

this is the code to append revision number at the end of the files:




Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If
'get user input
RUsure = MessageBox.Show ( _
"This will create a STEP file for all components." _
& vbLf & " " _
& vbLf & "Are you sure you want to create STEP Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output STEPs ",MessageBoxButtons.YesNo)
If RUsure = vbNo Then
    Return
Else
End If
'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = ThisDoc.Path
'get STEP target folder path
oFolder = oPath & "\" & oAsmName & " STEP Files"

'get the document revision to use in the new filename
oRevNumAsm = iProperties.Value("Project", "Revision Number")

'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If


'- - - - - - - - - - - - -Assembly - - - - - - - - - - - -
ThisDoc.Document.SaveAs(oFolder & "\" & oAsmName & "_Rev" & oRevNumAsm &(".stp") , True)

'- - - - - - - - - - - - -Components - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
    Dim oCurFile As Document
    oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
    oCurFileName = oCurFile.FullFileName

   
    'defines backslash As the subdirectory separator
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)
    oRevDoc = iProperties.Value(Name, "Project", "Revision Number")


    Try
        oCurFile.SaveAs(oFolder & "\" & ShortName & "_Rev" & oRevDoc & (".stp") , True)
    Catch
        MessageBox.Show("Error processing " & oCurFileName, "ilogic")
    End Try
    oCurFile.Close
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)