Note: I couldn't attach the iLogic rule as .iLogicVb, so I renamed it to .txt. I don't know if that breaks it when sharing.
What I believe the code is supposed to do / what I want it to do:
I'm getting an error about illegal characters in path. I imagine that's something like a comma in the assembly? Is there a simple thing I can add to this code to make it skip past anything that would generate an error?
The text of the rule itself is here:
Option Explicit On
Imports System.io
Imports System.LINQ
Sub Main
Dim extensions As String() = {".dwg", ".idw"}
Dim defaultDrawingExtension = ".idw" ' ".dwg"
Dim dir As DirectoryInfo = Nothing
Dim JustDoIt As Boolean = True
Dim newPDFName As String = ""
Dim trimmedfilename As String = ""
Dim drawingfiles As List(Of FileInfo) = Nothing
'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If
'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName) 'Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)
'MessageBox.Show(oAsmName)
'get user input
Dim RUsure As DialogResult = MessageBox.Show(
"This will create a PDF file for all of the assembly components that have drawing 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
Exit Sub
End If
'MessageBox.Show("Continuing")
'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
Dim oPath As String = ThisDoc.Path
Dim PDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'MessageBox.Show("Continuing")
If PDFAddIn.HasSaveCopyAsOptions(oAsmDoc, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
'oOptions.Value("Remove_Line_Weights") = 0
'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
'MessageBox.Show("Continuing")
'get PDF target folder path
Dim oFolder As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & oAsmName & " PDF Files"
MessageBox.Show("Looking for: " & oFolder & " & Creating it if it doesn't already exist!")
'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
For Each oRefDoc In oRefDocs
Dim filename As String = oRefDoc.FullDocumentName
Dim ThisFileDir As String = System.IO.Path.GetDirectoryName(oRefDoc.FullDocumentName)
If Not JustDoIt Then
RUsure = MessageBox.Show(filename, "PDF This file?", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Continue For
End If
End If
dir = New DirectoryInfo(ThisFileDir)
trimmedfilename = System.IO.Path.GetFileNameWithoutExtension(filename)
drawingfiles = GetFilesByExtensions(dir, trimmedfilename, extensions)
If drawingfiles.Count = 1 Then
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.Documents.Open(drawingfiles.Item(0).FullName, True)
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.DisplayName)
Dim tempRev As String = GetRevisionProp(oDrawDoc)
newPDFName = oFolder & "\" & oFileName & " R" & tempRev & ".pdf"
If System.IO.File.Exists(newPDFName) Then
If CheckReadOnly(newPDFName) Then
MessageBox.Show("PDF Exists and is read only," & vbCrLf & "Suggest you close it or check it out of Vault!" & vbCrLf & "before trying this rule again!")
Exit Sub
End If
End If
oDataMedium.FileName = newPDFName
Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
oDrawDoc.Close()
ElseIf drawingfiles.Count > 1 Then
MessageBox.Show("We found multiple format drawing files!" & vbCrLf & "Please correct this before trying again!")
ElseIf drawingfiles.Count = 0 Then
ThisApplication.StatusBarText = "No Matching drawing file found for: " & filename
End If
Next
'- - - - - - - - - - - - -
'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
Dim assyfilename As String = oAsmDoc.FullFileName
Dim assyFileDir As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
dir = New DirectoryInfo(assyFileDir)
trimmedfilename = System.IO.Path.GetFileNameWithoutExtension(assyfilename)
drawingfiles = GetFilesByExtensions(dir, trimmedfilename, extensions)
If drawingfiles.Count = 1 Then
Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(drawingfiles.Item(0).FullName, True)
Dim oAsmDrawingName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDrawingDoc.FullFileName)
Dim tempRev = GetRevisionProp(oAsmDrawingDoc)
newPDFName = oFolder & "\" & oAsmDrawingName & " R" & tempRev & ".pdf"
oDataMedium.FileName = newPDFName
If System.IO.File.Exists(newPDFName) Then
If CheckReadOnly(newPDFName) Then
MessageBox.Show("PDF Exists and is read only," & vbCrLf & "Suggest you close it or check it out of Vault!" & vbCrLf & "before trying this rule again!")
Exit Sub
Else
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
End If
Else
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
End If
'Close the top level drawing
oAsmDrawingDoc.Close()
End If
'- - - - - - - - - - - - -
'MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder, vbNormalFocus)
End Sub
Public Function GetFilesByExtensions(dir As DirectoryInfo, filename As String, ParamArray extensions As String()) As List(Of FileInfo)
If extensions Is Nothing Then
Throw New ArgumentNullException("extensions")
End If
Dim files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories).Where(Function(s As FileInfo) Not (s.FullName.Contains("OldVersions")) And (System.IO.Path.GetFileNameWithoutExtension(s.FullName).ToLower() = filename.ToLower()))
'Dim files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories).Where(Function(s As FileInfo) Not (s.FullName.Contains("-")) And Not (s.FullName.contains("OldVersions")))
Return files.Where(Function(f As FileInfo) extensions.Contains(f.Extension)).OrderBy(Function(x As FileInfo) x.Name).ToList()
End Function
Public Shared Function CheckReadOnly(ByVal doc As String) As Boolean
Try
' Handle the case with the active document never saved
If System.IO.File.Exists(doc) = False Then
MessageBox.Show("Save file before executing this method. Exiting ...")
Return False
End If
Dim atts As System.IO.FileAttributes = IO.File.GetAttributes(doc)
If ((atts And System.IO.FileAttributes.ReadOnly) = System.IO.FileAttributes.ReadOnly) Then
Return True
Else
'The file is Read/Write
Return False
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
End Function
Public Shared Function GetRevisionProp(ByRef MyDocument As Document) As String
Dim oPropSets As PropertySets
Dim oPropSet As PropertySet
Dim oRevNumiProp As [Property]
' Get the PropertySets object.
oPropSets = MyDocument.PropertySets
' Get the design tracking property set.
oPropSet = oPropSets.Item("Inventor Summary Information")
' Get the part number iProperty.
oRevNumiProp = oPropSet.Item("Revision Number")
If oRevNumiProp.Value <> "" Then
GetRevisionProp = oRevNumiProp.Value
Else
GetRevisionProp = "A"
End If
End Function
Solved! Go to Solution.
Note: I couldn't attach the iLogic rule as .iLogicVb, so I renamed it to .txt. I don't know if that breaks it when sharing.
What I believe the code is supposed to do / what I want it to do:
I'm getting an error about illegal characters in path. I imagine that's something like a comma in the assembly? Is there a simple thing I can add to this code to make it skip past anything that would generate an error?
The text of the rule itself is here:
Option Explicit On
Imports System.io
Imports System.LINQ
Sub Main
Dim extensions As String() = {".dwg", ".idw"}
Dim defaultDrawingExtension = ".idw" ' ".dwg"
Dim dir As DirectoryInfo = Nothing
Dim JustDoIt As Boolean = True
Dim newPDFName As String = ""
Dim trimmedfilename As String = ""
Dim drawingfiles As List(Of FileInfo) = Nothing
'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If
'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName) 'Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)
'MessageBox.Show(oAsmName)
'get user input
Dim RUsure As DialogResult = MessageBox.Show(
"This will create a PDF file for all of the assembly components that have drawing 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
Exit Sub
End If
'MessageBox.Show("Continuing")
'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
Dim oPath As String = ThisDoc.Path
Dim PDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'MessageBox.Show("Continuing")
If PDFAddIn.HasSaveCopyAsOptions(oAsmDoc, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
'oOptions.Value("Remove_Line_Weights") = 0
'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
'MessageBox.Show("Continuing")
'get PDF target folder path
Dim oFolder As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & oAsmName & " PDF Files"
MessageBox.Show("Looking for: " & oFolder & " & Creating it if it doesn't already exist!")
'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
For Each oRefDoc In oRefDocs
Dim filename As String = oRefDoc.FullDocumentName
Dim ThisFileDir As String = System.IO.Path.GetDirectoryName(oRefDoc.FullDocumentName)
If Not JustDoIt Then
RUsure = MessageBox.Show(filename, "PDF This file?", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Continue For
End If
End If
dir = New DirectoryInfo(ThisFileDir)
trimmedfilename = System.IO.Path.GetFileNameWithoutExtension(filename)
drawingfiles = GetFilesByExtensions(dir, trimmedfilename, extensions)
If drawingfiles.Count = 1 Then
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.Documents.Open(drawingfiles.Item(0).FullName, True)
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.DisplayName)
Dim tempRev As String = GetRevisionProp(oDrawDoc)
newPDFName = oFolder & "\" & oFileName & " R" & tempRev & ".pdf"
If System.IO.File.Exists(newPDFName) Then
If CheckReadOnly(newPDFName) Then
MessageBox.Show("PDF Exists and is read only," & vbCrLf & "Suggest you close it or check it out of Vault!" & vbCrLf & "before trying this rule again!")
Exit Sub
End If
End If
oDataMedium.FileName = newPDFName
Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
oDrawDoc.Close()
ElseIf drawingfiles.Count > 1 Then
MessageBox.Show("We found multiple format drawing files!" & vbCrLf & "Please correct this before trying again!")
ElseIf drawingfiles.Count = 0 Then
ThisApplication.StatusBarText = "No Matching drawing file found for: " & filename
End If
Next
'- - - - - - - - - - - - -
'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
Dim assyfilename As String = oAsmDoc.FullFileName
Dim assyFileDir As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
dir = New DirectoryInfo(assyFileDir)
trimmedfilename = System.IO.Path.GetFileNameWithoutExtension(assyfilename)
drawingfiles = GetFilesByExtensions(dir, trimmedfilename, extensions)
If drawingfiles.Count = 1 Then
Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(drawingfiles.Item(0).FullName, True)
Dim oAsmDrawingName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDrawingDoc.FullFileName)
Dim tempRev = GetRevisionProp(oAsmDrawingDoc)
newPDFName = oFolder & "\" & oAsmDrawingName & " R" & tempRev & ".pdf"
oDataMedium.FileName = newPDFName
If System.IO.File.Exists(newPDFName) Then
If CheckReadOnly(newPDFName) Then
MessageBox.Show("PDF Exists and is read only," & vbCrLf & "Suggest you close it or check it out of Vault!" & vbCrLf & "before trying this rule again!")
Exit Sub
Else
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
End If
Else
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
End If
'Close the top level drawing
oAsmDrawingDoc.Close()
End If
'- - - - - - - - - - - - -
'MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder, vbNormalFocus)
End Sub
Public Function GetFilesByExtensions(dir As DirectoryInfo, filename As String, ParamArray extensions As String()) As List(Of FileInfo)
If extensions Is Nothing Then
Throw New ArgumentNullException("extensions")
End If
Dim files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories).Where(Function(s As FileInfo) Not (s.FullName.Contains("OldVersions")) And (System.IO.Path.GetFileNameWithoutExtension(s.FullName).ToLower() = filename.ToLower()))
'Dim files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories).Where(Function(s As FileInfo) Not (s.FullName.Contains("-")) And Not (s.FullName.contains("OldVersions")))
Return files.Where(Function(f As FileInfo) extensions.Contains(f.Extension)).OrderBy(Function(x As FileInfo) x.Name).ToList()
End Function
Public Shared Function CheckReadOnly(ByVal doc As String) As Boolean
Try
' Handle the case with the active document never saved
If System.IO.File.Exists(doc) = False Then
MessageBox.Show("Save file before executing this method. Exiting ...")
Return False
End If
Dim atts As System.IO.FileAttributes = IO.File.GetAttributes(doc)
If ((atts And System.IO.FileAttributes.ReadOnly) = System.IO.FileAttributes.ReadOnly) Then
Return True
Else
'The file is Read/Write
Return False
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
End Function
Public Shared Function GetRevisionProp(ByRef MyDocument As Document) As String
Dim oPropSets As PropertySets
Dim oPropSet As PropertySet
Dim oRevNumiProp As [Property]
' Get the PropertySets object.
oPropSets = MyDocument.PropertySets
' Get the design tracking property set.
oPropSet = oPropSets.Item("Inventor Summary Information")
' Get the part number iProperty.
oRevNumiProp = oPropSet.Item("Revision Number")
If oRevNumiProp.Value <> "" Then
GetRevisionProp = oRevNumiProp.Value
Else
GetRevisionProp = "A"
End If
End Function
Solved! Go to Solution.
Solved by AlexKorzun. Go to Solution.
I looked into your code, and there are uses of
oRefDoc.FullDocumentName
FullDocumentName may contain <LODName> , and these <> characters cannot be used in the file/folder names.
So, you may want one of the following:
I looked into your code, and there are uses of
oRefDoc.FullDocumentName
FullDocumentName may contain <LODName> , and these <> characters cannot be used in the file/folder names.
So, you may want one of the following:
If I replace FullDocumentName with FullFileName and I still get the error, where else should I look?
If I replace FullDocumentName with FullFileName and I still get the error, where else should I look?
Please inspect other places of the code that generate the file/path names from the contents. For example, the last Function in your code sample (GetRevisionProp) builds the name based on the values from Revision ##.
To narrow down the problem, you may put the MessageBoxes into your code and check whether they appear, or an exception is thrown first.
There are two functions in .Net world that return illegal characters for the file/path names:
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidfilenamechars?view=netframework...
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidpathchars?view=netframework-4.8
Please inspect other places of the code that generate the file/path names from the contents. For example, the last Function in your code sample (GetRevisionProp) builds the name based on the values from Revision ##.
To narrow down the problem, you may put the MessageBoxes into your code and check whether they appear, or an exception is thrown first.
There are two functions in .Net world that return illegal characters for the file/path names:
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidfilenamechars?view=netframework...
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidpathchars?view=netframework-4.8
Can't find what you're looking for? Ask the community or share your knowledge.