- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello. I need to save pdf drawing files from an assembly. Each part has a designation and name (Title). I wrote the code for automation.. Now ilogic creates files with the designation (file name). I need him to add the name separated by a space. Here's a code example -
Imports System.Windows.Forms Imports System.IO
Sub Main() Try ' Получаем активную сборку Dim oAssemblyDoc As AssemblyDocument oAssemblyDoc = ThisApplication.ActiveDocument Dim assemblyFileName As String = oAssemblyDoc.FullFileName ' Сохраняем список файлов сборки и их путей Dim componentFiles As New List(Of String) For Each oComponent As ComponentOccurrence In oAssemblyDoc.ComponentDefinition.Occurrences Dim partFullPath As String = oComponent.Definition.Document.FullFileName Dim partDirectory As String = System.IO.Path.GetDirectoryName(partFullPath) Dim partFileName As String = System.IO.Path.GetFileNameWithoutExtension(partFullPath) Dim idwFile As String = System.IO.Path.Combine(partDirectory, partFileName & ".idw") ' Проверяем, существует ли IDW файл If System.IO.File.Exists(idwFile) Then componentFiles.Add(idwFile) End If Next ' Закрываем сборочный файл oAssemblyDoc.Close(False) ' Открываем диалоговое окно для выбора папки Dim folderDialog As New FolderBrowserDialog folderDialog.Description = "Выберите папку для сохранения PDF файлов" If folderDialog.ShowDialog() <> DialogResult.OK Then MessageBox.Show("Папка не выбрана. Операция отменена.") ' Открываем сборочный файл снова ThisApplication.Documents.Open(assemblyFileName) Exit Sub End If Dim saveFolder As String = folderDialog.SelectedPath ' Переменная для хранения путей сохраненных файлов Dim savedFiles As String = "" ' Проходим по всем IDW файлам из списка For Each idwFile As String In componentFiles ' Открываем IDW файл Dim oDoc As Document = ThisApplication.Documents.Open(idwFile) ' Получаем путь и имя файла Dim folder As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName) Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName) Dim title As String = "" ' Проверяем, существует ли набор свойств "Summary" Try title = iProperties.Value("Summary", "Title") Catch ex As Exception title = "NoTitle" End Try ' Формируем полное имя файла Dim fullFileName As String = System.IO.Path.Combine(saveFolder, fileName & " " & title & ".pdf") ' Применяем ваш код для сохранения PDF oDoc.SaveAs(fullFileName, True) ' Закрываем IDW файл без сохранения oDoc.Close(False) ' Добавляем путь сохраненного файла в переменную savedFiles &= fullFileName & vbCrLf Next ' Сообщаем пользователю о завершении и выводим пути сохраненных файлов If savedFiles = "" Then MessageBox.Show("Не найдено ни одного IDW файла для создания PDF.") Else MessageBox.Show("Создание PDF завершено. Файлы сохранены по следующим путям:" & vbCrLf & savedFiles) End If ' Открываем сборочный файл снова ThisApplication.Documents.Open(assemblyFileName) Catch ex As Exception MessageBox.Show("Произошла ошибка: " & ex.Message) End Try End Sub
- what am I doing wrong?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @dxmv. For some reason, the code you posted did not format correctly when you posted it on the forum. It was difficult reading through that code when it is not formatted correctly. However, I think I managed to get it back to the way it was supposed to be. I think your main problem is how you are getting the value of the title iProperty. That simple iLogic snippet would have been trying to get the title from the main assembly every single time, because there is nothing specifying what document to get it from. So, I changed the following line of code:
title = iProperties.Value("Summary", "Title")
...to be like this:
title = oDoc.PropertySets.Item(1).Item(1).Value
...because the 'oDoc' variable is the drawing document that has been opened, and that is the document we want to get the title from. Then, this digs down into that specific document's PropertySets and Properties to find its title property, then gets its value. The title is the first Property, that is within the first PropertySet, of every document, so I just used index numbers, instead of names. I am not sure if that one edit will fix your code, but you can give it a try.
By the way...just so it is easier for others later, below is the 'deciphered' code example, with that one line of code changed.
Sub Main()
Try ' Получаем активную сборку
Dim oAssemblyDoc As AssemblyDocument
oAssemblyDoc = ThisApplication.ActiveDocument
Dim assemblyFileName As String = oAssemblyDoc.FullFileName
' Сохраняем список файлов сборки и их путей
Dim componentFiles As New List(Of String)
For Each oComponent As ComponentOccurrence In oAssemblyDoc.ComponentDefinition.Occurrences
Dim partFullPath As String = oComponent.Definition.Document.FullFileName
Dim partDirectory As String = System.IO.Path.GetDirectoryName(partFullPath)
Dim partFileName As String = System.IO.Path.GetFileNameWithoutExtension(partFullPath)
Dim idwFile As String = System.IO.Path.Combine(partDirectory, partFileName & ".idw")
' Проверяем, существует ли IDW файл
If System.IO.File.Exists(idwFile) Then
componentFiles.Add(idwFile)
End If
Next ' Закрываем сборочный файл
oAssemblyDoc.Close(False)
' Открываем диалоговое окно для выбора папки
Dim folderDialog As New FolderBrowserDialog
folderDialog.Description = "Выберите папку для сохранения PDF файлов"
If folderDialog.ShowDialog() <> DialogResult.OK Then
MessageBox.Show("Папка не выбрана. Операция отменена.")
' Открываем сборочный файл снова
ThisApplication.Documents.Open(assemblyFileName)
Exit Sub
End If
Dim saveFolder As String = folderDialog.SelectedPath
' Переменная для хранения путей сохраненных файлов
Dim savedFiles As String = ""
' Проходим по всем IDW файлам из списка
For Each idwFile As String In componentFiles
' Открываем IDW файл
Dim oDoc As Document = ThisApplication.Documents.Open(idwFile)
' Получаем путь и имя файла
Dim folder As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
Dim title As String = ""
' Проверяем, существует ли набор свойств "Summary"
Try
title = oDoc.PropertySets.Item(1).Item(1).Value
'title = iProperties.Value("Summary", "Title")
Catch ex As Exception
title = "NoTitle"
End Try
' Формируем полное имя файла
Dim fullFileName As String = System.IO.Path.Combine(saveFolder, fileName & " " & title & ".pdf")
' Применяем ваш код для сохранения PDF
oDoc.SaveAs(fullFileName, True)
' Закрываем IDW файл без сохранения
oDoc.Close(False)
' Добавляем путь сохраненного файла в переменную
savedFiles &= fullFileName & vbCrLf
Next
' Сообщаем пользователю о завершении и выводим пути сохраненных файлов
If savedFiles = "" Then
MessageBox.Show("Не найдено ни одного IDW файла для создания PDF.")
Else
MessageBox.Show("Создание PDF завершено. Файлы сохранены по следующим путям:" & vbCrLf & savedFiles)
End If
' Открываем сборочный файл снова
ThisApplication.Documents.Open(assemblyFileName)
Catch ex As Exception
MessageBox.Show("Произошла ошибка: " & ex.Message)
End Try
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS)
.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report