Friday, December 21, 2012

iLogic Replace Derived Reference

Issue:
You wish there was an option in parts created from derived components to replace the derived component.



Solution:
Here is a quick iLogic rule that will allow you to browse for a new file with which you can replace the derived reference of an existing derived component.

UPDATE:
A reader recently pointed out a limitation of this iLogic snippet stemming from its use of the API's FileDescriptor.ReplaceReference method. This method requires that the file being replaced and
the replacement file must share ancestry, that is to say that one file was saved from the other file, or both files were saved from the same "parent" file. Simply being aware of this will result in predictable results, but if you were attempting to use this snippet to replace two completely different files you might not have been able to get this to work.

Thanks to Peter for taking the time to bring my attention to this!


Dim oDoc as Document
oDoc = ThisDoc.Document
Dim oRefFile As FileDescriptor
Dim oOrigRefName As Object     

For Each oRefFile In oDoc.file.ReferencedFileDescriptors
'get the full file path to the original internal references
oOrigRefName = oRefFile.FullFileName

'present a File Selection dialog
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.InitialDirectory = oOrigRefName
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
Return
ElseIf oFileDlg.FileName <> "" Then
selectedfile = oFileDlg.FileName
End if

‘replace the reference
oRefFile.ReplaceReference (selectedfile)     
InventorVb.DocumentUpdate()
oOrigRefName = “”                                        
Next

iLogicVb.UpdateWhenDone = True

Thursday, November 29, 2012

iLogic: Batch Output PDFs from an Assembly File

Issue:
You'd like to output a PDF file from the drawings of all the components found in an assembly file (if those components have a drawing file created), and you like also create a PDF of the top level assembly drawing as well. All of these PDFs should be written out to a single folder. Can this be done?

Solution:
Here's  an ilogic rule to batch output PDF files for each component in an assembly. This rule assumes that the component drawings share the same name and location of the component.

For example: C:\Temp\widget-450.ipt has a drawing: C:\Temp\widget-450.idw
If the drawing file is not found, then it simply moves on and looks at the next component.


This rule may not work for everyone, but it should provide a starting point from which it can be modified as needed. If you work with *.dwg files rather than *.idw files you can search and replace this code for idw and replace with dwg.

Here is the code in a *.txt file that can be downloaded as well:
http://forums.autodesk.com/autodesk/attachments/autodesk/78/455124/2/Batch%20PDFs.txt 



'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file
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 PDF file for all of the asembly components that have drawings files." _
& vblf & "This rule expects that the drawing file shares the same name and location as the component." _
& vblf & " " _
& vblf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
& vblf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
oPath = ThisDoc.Path
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
'oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
End If

'get PDF target folder path
oFolder = oPath & "\" & oAsmName & " PDF Files"

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

'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document

'work the the drawing files for the referenced models
'this expects that the model has a drawing of the same path and name
For Each oRefDoc In oRefDocs
idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
'check to see that the model has a drawing of the same path and name
If(System.IO.File.Exists(idwPathName)) Then
                        Dim oDrawDoc As DrawingDocument
                oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
            oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)

            On error Resume Next ' if PDF exists and is open or read only, resume next
                 'Set the PDF target file name
                oDataMedium.FileName = oFolder & "\" & oFileName & "pdf"
                'Write out the PDF
                Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            'close the file
                oDrawDoc.Close
Else
'If the model has no drawing of the same path and name - do nothing
End If
Next
'- - - - - - - - - - - - -

'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
oAsmDrawing = ThisDoc.ChangeExtension(".idw")
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
'write out the PDF for the Top Level Assembly Drawing file
On error Resume Next ' if PDF exists and is open or read only, resume next
 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & "pdf"
'Write out the PDF
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
'Close the top level drawing
oAsmDrawingDoc.Close
'- - - - - - - - - - - - -

MessageBox.Show("New Files Created in: " & vblf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)