iLogic - Index was outside the bounds of the array - PDF Batch output

iLogic - Index was outside the bounds of the array - PDF Batch output

will_roe
Enthusiast Enthusiast
1,356 Views
3 Replies
Message 1 of 4

iLogic - Index was outside the bounds of the array - PDF Batch output

will_roe
Enthusiast
Enthusiast

Hi,

 

I've added to the Batch PDF iLogic rule from "From the trenches" blog (http://inventortrenches.blogspot.com/2012/11/ilogic-batch-output-pdfs-from-assembly.html) to add the Revision to PDF file name which works fine but I've also added for it to only PDF drawing numbers that start with "M" (non standard parts).

 

I works perfect in my test assembly but hasn't worked in any other models and gives me the below error-

 

Error in rule: PDF ALL, in document: xxxxxxx.iam

Index was outside the bounds of the array.

 

More Info:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.String.get_Chars(Int32 index)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

I believe it's the below snippet that tells it to only PDF drawings numbers tarting with "M" . When I remove it runs fine.

 

oFL = Right (oRefDoc.FullDocumentName((Len(ThisDoc.Path)+1)) ,1)

 

If oFL="M" Then

 

At this stage I've tried nothing and I'm all out of ideas so any help is much appreciated.

 

The Code- ( I think there might be some updates from what was originally posted that I found on this forum)

 

Sub Main()
	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
	If 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) = vbNo Then
		
		Exit Sub
	End If
		
	Dim PDFAddIn As TranslatorAddIn
        Dim oContext As TranslationContext
	Dim oOptions As NameValueMap
	Dim oDataMedium As DataMedium
	
	Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	
	oPath = ThisDoc.Path
	oFolder = oPath & "\" & oAsmName & " PDF Files"
	
	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 oRefDoc As Document
	
'	For Each oRefDoc In oRefDocs = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc In oAsmDoc.AllReferencedDocuments
		idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
	
	oFL = Right (oRefDoc.FullDocumentName((Len(ThisDoc.Path)+1)) ,1)
		
	If oFL="M" Then
	
		If(System.IO.File.Exists(idwPathName)) Then
			Dim oDrawDoc As DrawingDocument
			oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
			oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -4) & "_Rev" & iProperties.Value("Project", "Revision Number")

			On Error Resume Next
				oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf"
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				oDrawDoc.Close
			On Error GoTo 0
		End If
	End If
		
	Next
	'- - - - - - - - - - - - -
	
	'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
	Dim oAsmDrawingDoc As DrawingDocument
	oAsmDrawingDoc = ThisApplication.Documents.Open(ThisDoc.ChangeExtension(".idw"), True)
	oAsmDrawingName = (Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -4))&"_Rev" & iProperties.Value("Project", "Revision Number")

	On Error Resume Next
		oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & ".pdf"
		Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
		oAsmDrawingDoc.Close
	
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
	Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)

	oPath = ThisDoc.Path
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	oOptions.Value("Custom_Begin_Sheet") = 1
	oOptions.Value("Custom_End_Sheet") = 1

	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

 

 

 

 

0 Likes
Accepted solutions (2)
1,357 Views
3 Replies
Replies (3)
Message 2 of 4

dutt.thakar
Collaborator
Collaborator
Accepted solution

@will_roe 

 

I think there must be some assembly in which the full document character counts are less than the index you are referring to in your oFL value.

 

I split the lines you mentioned and tested in one of my assemblies and what I found is this :

	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oRefDoc As Document
	For Each oRefDoc In oAsmDoc.AllReferencedDocuments
	oL = Len(ThisDoc.Path) 'This is the total character count of the active filename with file path
	MessageBox.Show(oRefDoc.DisplayName) 'Document which is being iterated now, will show in the messagebox
	oNa = oRefDoc.FullDocumentName 
	MessageBox.Show(oNa.Length) 'Character count of the document currently being iterated , if this is less than oL it will give out of bound error
	Next

 

So in a nutshell, I think one of your sub-part/subassembly in the main assembly has less character count than the value it is counting in oFL, and that is why it is giving error (Index was outside array bounds), try the above code to check which component is giving you an error and probably changing its location will solve the issue. Make sure all your subparts and sub-assemblies(file path with filename) must have more character lengths than your main assembly (file path and filename) to make it work.

 

Hope this is helpful.

 

If this has answered your question please accept this as a solution.

 

Regards,

Dutt Thakar

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 3 of 4

JelteDeJong
Mentor
Mentor
Accepted solution

As @dutt.thakar already wrote strange things happen in that line. But there are much better ways to check if a file name starts with a letter. There is a build in function to do that.

While checking the rest of the code i discoverd also some other stuff that i would different. in quite some places the code is searching for file/directory names. Also for that there are functions build in .net that can do that for you. The class IO.Path is realy good at that stuff 😉 Last i found that the revision numbers probaly where not going to work like you would expect. the function "iProperties.Value("Project", "Revision Number")" would always return the revision of the assembly. Not of the drawing. 

There for i rewrote much of the original code.  You can try it:

Sub Main()
    If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
        MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
        Exit Sub
    End If

    Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
    Dim oAsmName As String = IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)

    'get user input
    If 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) = vbNo Then

        Exit Sub
    End If

    Dim oPath As String = IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
    Dim oFolder As String = IO.Path.Combine(oPath, oAsmName & " PDF Files")

    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
    For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
        Dim idwPath = IO.Path.GetDirectoryName(oRefDoc.FullDocumentName)
        Dim idwName = IO.Path.GetFileNameWithoutExtension(oRefDoc.FullDocumentName) & ".idw"
        Dim idwPathName = IO.Path.Combine(idwPath, idwName)

        If idwName.StartsWith("k", StringComparison.InvariantCultureIgnoreCase) Then
            If (System.IO.File.Exists(idwPathName)) Then
                Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
                export(oDrawDoc, oFolder)
                oDrawDoc.Close()
            End If
        End If
    Next

    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
    Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(ThisDoc.ChangeExtension(".idw"), True)

    export(oAsmDrawingDoc, oFolder)

    oAsmDrawingDoc.Close()

    MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
    Shell("explorer.exe " & oFolder, vbNormalFocus)
End Sub

Sub export(doc As DrawingDocument, oFolder As String)

    Dim oPropSets As PropertySets = doc.PropertySets
    Dim oPropSet As PropertySet = oPropSets.Item("Inventor Summary Information") 
    Dim revProp As [Property] = oPropSet.Item("Revision Number") 	
    Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(doc.DisplayName)
    oFileName = oFileName & "_Rev" & revProp.Value & ".pdf"

    Dim PDFAddIn As TranslatorAddIn
    Dim oContext As TranslationContext
    Dim oOptions As NameValueMap
    Dim oDataMedium As DataMedium

    Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)

    Try
        oDataMedium.FileName = IO.Path.Combine(oFolder, oFileName)
        Call PDFAddIn.SaveCopyAs(doc, oContext, oOptions, oDataMedium)
    Catch ex As Exception
        MsgBox("Exception while saving pdf: " & oFileName)
    End Try
End Sub

Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)

    Dim oPath As String = IO.Path.GetDirectoryName(ThisDoc.Document.FullFileName)
    PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    oOptions.Value("All_Color_AS_Black") = 1
    oOptions.Value("Remove_Line_Weights") = 0
    oOptions.Value("Vector_Resolution") = 400
    oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
    oOptions.Value("Custom_Begin_Sheet") = 1
    oOptions.Value("Custom_End_Sheet") = 1

    oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 4 of 4

will_roe
Enthusiast
Enthusiast

@dutt.thakar Thanks you! There were some suppliers parts responsible as they were in a different folder. I assumed if it couldn't find the character it would just move onto next but I was obviously wrong 😥.

 

@JelteDeJong Thank you! You're a absolute legend! I have so much to learn 😅

 

One thing that I've noticed while testing is that for some reason sometimes it opens my document folder upon completion not the folder it created. 

 

To be honest I'm more than happy with how it's running but, I've run the below and it returns the correct oFolder on the files. 

 

    Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
    Dim oAsmName As String = IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)

    Dim oPath As String = IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
    Dim oFolder As String = IO.Path.Combine(oPath, oAsmName & " PDF Files")


    MessageBox.Show(oFolder, "Test")
  

 

0 Likes