DWG & PDF ilogic, change code for batchexport

DWG & PDF ilogic, change code for batchexport

koenroovers
Enthusiast Enthusiast
621 Views
8 Replies
Message 1 of 9

DWG & PDF ilogic, change code for batchexport

koenroovers
Enthusiast
Enthusiast

Hello Everyone,

 

I have been searching for a Ilogic code to export .idw to .dwg. I have found a working ilogic. But i would like to change 2 things:

1. I want to be able to select the drawings to export instead of the prompt below:

koenroovers_0-1677833036476.png

2. I want to put the files in the same location as the .IDW. Currently it puts it in a new folder.

 

Can you help me with this?

 

kind regards,

 

koen

 

Sub Main()
  Dim myDate As String = Now().ToString("dd-MM-yyyy")
  myDate = myDate.Replace(":","")  ' & " - " & TypeString
 userChoice = InputRadioBox("Defined the scope", "This Document", "Open Documents", True, Title := "Defined the scope")
 UserSelectedActionList = New String(){"DWG & PDF", "PDF Only", "DWG Only"}
  UserSelectedAction = InputListBox("What action must be performed with selected views?", _
          UserSelectedActionList, UserSelectedActionList(0), Title := "Action to Perform", ListName := "Options")
      Select UserSelectedAction
   Case "DWG & PDF": UserSelectedAction = 3
   Case "PDF Only": UserSelectedAction = 1
   Case "DWG Only":    UserSelectedAction = 2
   End Select
 If userChoice Then
   Call MakePDFFromDoc(ThisApplication.ActiveDocument, myDate, UserSelectedAction)
  Else
   For Each oDoc In ThisApplication.Documents
    If oDoc.DocumentType = kDrawingDocumentObject
     Try
      If Len(oDoc.File.FullFileName)>0 Then
       Call MakePDFFromDoc(oDoc, myDate, UserSelectedAction)
      End If
     Catch
     End Try
    End If
   Next
  End If
 End Sub
 Sub MakePDFFromDoc(ByRef oDocument As Document, DateString As String, UserSelectedAction As Integer)
 ' oPath = oDocument.Path
 ' oFileName = oDocument.FileName(False) 'without extension
  'oDocument = ThisApplication.ActiveDocument
  oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
  ("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
  oContext = ThisApplication.TransientObjects.CreateTranslationContext
  oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
  oOptions = ThisApplication.TransientObjects.CreateNameValueMap
  oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
 oFullFileName = oDocument.File.FullFileName
  oPath = Left(oFullFileName, InStrRev(oFullFileName, "\")-1)
  oFileName = Right(oFullFileName, Len(oFullFileName)-InStrRev(oFullFileName, "\"))
  oFilePart = Left(oFileName, InStrRev(oFileName, ".")-1)
 'oRevNum = oDocument.iProperties.Value("Project", "Revision Number")
  'oDocument = ThisApplication.ActiveDocument
 ' If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, 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
 'get PDF target folder path
  'oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
  oFolder = oPath & "\Dokumentace(" & DateString & ")"
  oDirectoryName = System.IO.Path.GetDirectoryName(oFullFileName)
 '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
 'Set the PDF target file name
  oDataMedium.FileName = oFolder & "\" & oFilePart & ".pdf"
 'Publish document
  If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
   oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)'For PDF's
  End If
  If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
   oDocument.SaveAs(oFolder & "\" & oFilePart & ".dwg", True) 'For DWG's
  End If
  'oDocument.SaveAs(oFolder & "\" & ThisDoc.ChangeExtension(".DWG"), True) 'For DWG's
  '------end of iLogic-------
 End Sub

 

0 Likes
Accepted solutions (1)
622 Views
8 Replies
Replies (8)
Message 2 of 9

A.Acheson
Mentor
Mentor

You can delete the following as it isn’t needed to create a folder anymore

 'get PDF target folder path
  'oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
  oFolder = oPath & "\Dokumentace(" & DateString & ")"
  oDirectoryName = System.IO.Path.GetDirectoryName(oFullFileName)
 '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

Next you already have the path and filename of the drawing so give that info to the translator

oDataMedium.FileName = oPath & "\" & oFilePart & ".pdf"

And here are ingredients for file selecting in windows.

 

Edit:

Change some formatting and errors: 

 

 

 

Public Sub TestFileDialog()
	
	Dim mylist As New List(Of String)
	Dim sFiles As New List(Of String)
	
    ' Create a new FileDialog object.
    Dim oFileDlg As Inventor.FileDialog
     ThisApplication.CreateFileDialog(oFileDlg)

    ' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.idw ;*.dwg)|*.idw;*.dwg|All Files (*.*)|*.*"

    ' Define the drawing files filter to be the default filter.
    oFileDlg.FilterIndex = 1

    ' Set the title for the dialog.
    oFileDlg.DialogTitle = "Open File Test"

    ' Set the initial directory that will be displayed in the dialog.
    oFileDlg.InitialDirectory = "C:\Users\Enter Name Here"
	oFileDlg.MultiSelectEnabled = True
'    oFileDlg.ShowSave
	 oFileDlg.ShowOpen
		
		If oFileDlg.FileName = Nothing Then
	    ElseIf oFileDlg.FileName <> "" Then
			
			'Process the multi selected files which form a  pipe like "C\123.ipt|C\124.ipt"
			mylist = oFileDlg.FileName.Split("|").ToList
			
			For Each item In mylist
				sFiles.Add(item.ToString)
			Next
		End If
		
	d0 = InputListBox("Prompt", sFiles, d0, Title := "FileList", ListName := "FileList")

	For Each sFile As String In sFiles
		Dim oDoc As DrawingDocument = ThisApplication.Documents.Open(sFile, True)
     	         'Call your pdf sub routine here. 
	Next
	
	'Clear the list
	sFiles.Clear
End Sub

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 9

koenroovers
Enthusiast
Enthusiast

@A.Acheson 
I got it to work, i just want to change a detail. While selecting the files all files are opend. I want the rule to do this while executing the rule, not beforehand.  


Workflow now is:

- Select files

- Open all files

- Select action

- Export

 

I want it like this:

- Select files

- Select action

- Run action for each file

 

Can you assist me with this?

0 Likes
Message 4 of 9

A.Acheson
Mentor
Mentor

If you want the select action to happen before the file is opened then the user input section box must appear before the open file method. If you still need assistance please post the revised code to see where the changes are needed. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 9

koenroovers
Enthusiast
Enthusiast

I don't think you understand what I meant. For reference, I have a different Ilogic for batch plotting PDF's from IDW which works exactly how I described. Upon executing the rule i can select the .IDW's which i want to export. After selecting it starts the rule by opening every drawing 1 by 1 and closing the drawings. The difference with the "new" rule is that the new rule opens every drawing after the selection, after which the export starts per drawing. 

 

See below:

 

Sub Main()
	
Dim PDFAddIn As TranslatorAddIn
Dim oContext As TranslationContext
Dim oOptions As NameValueMap
Dim oDataMedium As DataMedium
Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Inventor Files (*.idw)|*.idw"
oFileDlg.DialogTitle = "Delete Ilogic Rule in drawing"
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.MultiSelectEnabled =True 
oFileDlg.FilterIndex = 1
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
	MessageBox.Show("File not chosen.", "Dialog Cancellation")
ElseIf oFileDlg.FileName <> "" Then
	For Each wrd In oFileDlg.FileName.Split("|") 'Words
		Dim odoc As Document = ThisApplication.Documents.Open(wrd, False)
		On Error Resume Next
		oDataMedium.FileName = Left(odoc.FullFileName, (InStrRev(odoc.FullFileName, ".", -1, vbTextCompare) - 1)) & ".pdf"
		Call PDFAddIn.SaveCopyAs(odoc, oContext, oOptions, oDataMedium)
		odoc.Close
	Next
End If

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") = 0
	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

 

My "new" rule is now like this: 

Sub Main()

	Dim mylist As New List(Of String)
	Dim sFiles As New List(Of String)
	
    ' Create a new FileDialog object.
    Dim oFileDlg As Inventor.FileDialog
     ThisApplication.CreateFileDialog(oFileDlg)

    ' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.idw ;*.dwg)|*.idw;*.dwg|All Files (*.*)|*.*"

    ' Define the drawing files filter to be the default filter.
    oFileDlg.FilterIndex = 1

    ' Set the title for the dialog.
    oFileDlg.DialogTitle = "Open File Test"

    ' Set the initial directory that will be displayed in the dialog.
    oFileDlg.InitialDirectory = "C:\Users\Enter Name Here"
	oFileDlg.MultiSelectEnabled = True
'    oFileDlg.ShowSave
	 oFileDlg.ShowOpen
		
		If oFileDlg.FileName = Nothing Then
	    ElseIf oFileDlg.FileName <> "" Then
			
			'Process the multi selected files which form a  pipe like "C\123.ipt|C\124.ipt"
			mylist = oFileDlg.FileName.Split("|").ToList
			
			For Each item In mylist
				sFiles.Add(item.ToString)
			Next
		End If
		
	d0 = InputListBox("Prompt", sFiles, d0, Title := "FileList", ListName := "FileList")

	For Each sFile As String In sFiles
		Dim oDoc As DrawingDocument = ThisApplication.Documents.Open(sFile, True)
     	         'Call your pdf sub routine here. 
	Next
	
	'Clear the list
	sFiles.Clear

  Dim myDate As String = Now().ToString("dd-MM-yyyy")
  myDate = myDate.Replace(":","")  ' & " - " & TypeString
 UserSelectedActionList = New String(){"DWG & PDF", "PDF Only", "DWG Only"}
  UserSelectedAction = InputListBox("What action must be performed with selected views?", _
          UserSelectedActionList, UserSelectedActionList(0), Title := "Action to Perform", ListName := "Options")
      Select UserSelectedAction
   Case "DWG & PDF": UserSelectedAction = 3
   Case "PDF Only": UserSelectedAction = 1
   Case "DWG Only":    UserSelectedAction = 2
   End Select
 If userChoice Then
   Call MakePDFFromDoc(ThisApplication.ActiveDocument, myDate, UserSelectedAction)
  Else
   For Each oDoc In ThisApplication.Documents
    If oDoc.DocumentType = kDrawingDocumentObject
     Try
      If Len(oDoc.File.FullFileName)>0 Then
       Call MakePDFFromDoc(oDoc, myDate, UserSelectedAction)
      End If
     Catch
     End Try
    End If
   Next
  End If
 End Sub
 Sub MakePDFFromDoc(ByRef oDocument As Document, DateString As String, UserSelectedAction As Integer)
 ' oPath = oDocument.Path
 ' oFileName = oDocument.FileName(False) 'without extension
  'oDocument = ThisApplication.ActiveDocument
  oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
  ("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
  oContext = ThisApplication.TransientObjects.CreateTranslationContext
  oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
  oOptions = ThisApplication.TransientObjects.CreateNameValueMap
  oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
 oFullFileName = oDocument.File.FullFileName
  oPath = Left(oFullFileName, InStrRev(oFullFileName, "\")-1)
  oFileName = Right(oFullFileName, Len(oFullFileName)-InStrRev(oFullFileName, "\"))
  oFilePart = Left(oFileName, InStrRev(oFileName, ".")-1)
 'oRevNum = oDocument.iProperties.Value("Project", "Revision Number")
  'oDocument = ThisApplication.ActiveDocument
 ' If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, 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
 'get PDF target folder path
  'oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
  oFolder = oPath 
  oDirectoryName = System.IO.Path.GetDirectoryName(oFullFileName)
 '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
 'Set the PDF target file name
  oDataMedium.FileName = oFolder & "\" & oFilePart & ".pdf"
 'Publish document
  If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
   oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)'For PDF's
  End If
  If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
   oDocument.SaveAs(oFolder & "\" & oFilePart & ".dwg", True) 'For DWG's
  End If
  'oDocument.SaveAs(oFolder & "\" & ThisDoc.ChangeExtension(".DWG"), True) 'For DWG's
  '------end of iLogic-------
 End Sub
0 Likes
Message 6 of 9

A.Acheson
Mentor
Mentor

Hi @koenroovers if you want actions to happen and sub routines to be called after every file open you need to add that before and after the file open action. See the commented line Call your pdf sub routine in the for loop below. This is where you need to call the subroutine to print. If you want your action message box to open per file keep it inside this for loop. If you want the same action on all files bring it before this for loop. 

 

For Each sFile As String In sFiles
		Dim oDoc As DrawingDocument = ThisApplication.Documents.Open(sFile, True)
     	         'Call your pdf sub routine here. 
	Next

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 9

koenroovers
Enthusiast
Enthusiast

@A.Acheson 

I'm just doing trial and error by now, if I must be honest this is a little to in dept for me. Could you point out the specific part i have to change?

 

I only want to do the action box once. 

0 Likes
Message 8 of 9

A.Acheson
Mentor
Mentor
Accepted solution

Sure here is a graphic of the explanation. Sequence is then to make your selection, open the file and call your  sub routine.

AAcheson_0-1678377319481.png

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 9 of 9

koenroovers
Enthusiast
Enthusiast

@A.Acheson 

Thank you! This works perfectly!

0 Likes