Overwrite files with rule

Overwrite files with rule

madstrolle
Enthusiast Enthusiast
357 Views
4 Replies
Message 1 of 5

Overwrite files with rule

madstrolle
Enthusiast
Enthusiast

Hi Guys,

I have the rule that generates PDF and DXF's and place them in specific folders. The PDF files are overwriting without asking, as intended and the DXF's generated in folder "06. DXF - Cut" is also overwriting without asking as intended. But when DXF files are generated into folder "02. DXF" it prompt me if i want to overwrite. 

I also want the DXF files in folder "02. DXF" to be overwritten without asking.

I tried for a long time now to figure it out, but without luck ☹️

Anybody have an idea, it would very much apreciated?

 

Here is the code:

 

Dim oDoc As Document = ThisApplication.ActiveDocument

If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
Dim file_path As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

If Right(file_path, 10) = "03. Native" Then
Dim new_file_path As String = Left(file_path, Len(file_path) - 10)

' Create the "02. DXF" folder in the same directory as the drawing
Dim dxf_folder_path As String = System.IO.Path.Combine(new_file_path, "02. DXF")
System.IO.Directory.CreateDirectory(dxf_folder_path)

' Create the "06. DXF - Cut" folder in the same directory as the drawing
Dim dxf_folder_path_cut As String = System.IO.Path.Combine(new_file_path, "06. DXF - Cut")
System.IO.Directory.CreateDirectory(dxf_folder_path_cut)

' Create the "01. PDF" folder in the same directory as the drawing
Dim pdf_folder_path As String = System.IO.Path.Combine(new_file_path, "01. PDF")
System.IO.Directory.CreateDirectory(pdf_folder_path)

' Iterate through all sheets in the drawing
For Each drawingSheet As Sheet In oDoc.Sheets
' Loop through all drawing views on the sheet
For Each oDrawingView As DrawingView In drawingSheet.DrawingViews
If oDrawingView.ReferencedFile IsNot Nothing Then
Dim oPartDoc As PartDocument = TryCast(oDrawingView.ReferencedFile.ReferencedDocument, PartDocument)
If oPartDoc IsNot Nothing Then
Dim oSMCD As SheetMetalComponentDefinition = TryCast(oPartDoc.ComponentDefinition, SheetMetalComponentDefinition)
If oSMCD IsNot Nothing Then
If Not oSMCD.HasFlatPattern Then
oSMCD.Unfold()
oSMCD.FlatPattern.ExitEdit()
End If
Dim part_file_name As String = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
Dim dxf_file_path As String = System.IO.Path.Combine(dxf_folder_path_cut, part_file_name & ".dxf")
Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=R12"
Try
oSMCD.DataIO.WriteDataToFile(sOut, dxf_file_path)
Catch ex As Exception
MsgBox("Error exporting DXF: " & ex.Message, vbExclamation, "DXF Export Error")
End Try
End If
End If
End If
Next
Next

' Export the entire drawing as a DXF
Dim file_name As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)

' Generate the PDF file
Dim pdf_file_path As String = System.IO.Path.Combine(pdf_folder_path, file_name & ".pdf")

' Use your provided PDF generation code here
ThisDoc.Document.SaveAs(pdf_file_path, True)

' Generate the DXF file for the whole drawing
If dxf_folder_path <> "" Then
Dim dxf_file_path_full As String = System.IO.Path.Combine(dxf_folder_path, file_name & ".dxf")
Try
oDoc.SaveAs(dxf_file_path_full, True)
Catch ex As Exception
MsgBox("Error exporting full DXF: " & ex.Message, vbExclamation, "DXF Export Error")
End Try
End If
Else
MsgBox("Forkert filsti. Der vil ikke blive lavet PDF og DXF filer.", vbExclamation, "Lav PDF/DXF")
End If
End If

 

0 Likes
358 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @madstrolle.  I copied the code you posted above into a new iLogic rule window, then modified its layout and structure a bit.  I am not sure that I can fix it in a way that will cause the prompt to overwrite existing files will go away though.  Sometimes we can use a couple tricks to help avoid normal dialogs while the code is running though.  The first, and likely the simplest, is with a setting within the iLogic rule editor itself, while editing that rule.  On the 'Options' tab you will see an option called "Silent Operation", with a checkbox next to it.  If you check that box, that will help avoid normal dialogs that might pop-up as a result of what that rule is doing while it is running.  There is also a similar Inventor application level setting that we can use (Application.SilentOperation), but if using that one, we need to make absolutely sure that it will get put back to the way it was before, even if the rule crashes at some point.  I noticed that you are just using SaveAs method, and specifying True for SaveCopyAs, instead of using the TranslatorAddIn routines for exporting the PDF & DXF of the drawing.  Those two export processes are very different, because when exporting a multi-page PDF, it just creates a single file with multiple pages, but when exporting a multi -sheet drawing to DXF, it creates a separate file for each sheet of the drawing.  I suspect that there must be some internal code processes going on behind the scenes for that DXF export process of the multi-sheet drawing to multiple DXF files, and if so, we may simply not have any control over the prompts about overwriting existing files.

Anyways, the edited code is below.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Return
Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim sPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDDoc.FullFileName)
If Right(sPath, 10) <> "03. Native" Then
	MsgBox("Forkert filsti. Der vil ikke blive lavet PDF og DXF filer.", vbExclamation, "Lav PDF/DXF")
	Return
End If
Dim sNewPath As String = Left(sPath, Len(sPath) - 10)
' Create the "02. DXF" folder in the same directory as the drawing
Dim sDXF_folder As String = System.IO.Path.Combine(sNewPath, "02. DXF")
If System.IO.Directory.Exists(sDXF_folder) Then
	System.IO.Directory.CreateDirectory(sDXF_folder)
End If
' Create the "06. DXF - Cut" folder in the same directory as the drawing
Dim sDXF_folder_cut As String = System.IO.Path.Combine(sNewPath, "06. DXF - Cut")
If System.IO.Directory.Exists(sDXF_folder_cut) Then
	System.IO.Directory.CreateDirectory(sDXF_folder_cut)
End If
' Create the "01. PDF" folder in the same directory as the drawing
Dim sPDF_folder As String = System.IO.Path.Combine(sNewPath, "01. PDF")
If System.IO.Directory.Exists(sPDF_folder) Then
	System.IO.Directory.CreateDirectory(sPDF_folder)
End If
Dim oSheets As Inventor.Sheets = oDDoc.Sheets
For Each oSheet As Inventor.Sheet In oSheets
	Dim oViews As DrawingViews = oSheet.DrawingViews
	For Each oView As DrawingView In oViews
		If oView.ReferencedFile Is Nothing Then Continue For
		Dim oPartDoc As PartDocument = TryCast(oView.ReferencedFile.ReferencedDocument, PartDocument)
		If oPartDoc Is Nothing Then Continue For
		Dim oSMCD As SheetMetalComponentDefinition = TryCast(oPartDoc.ComponentDefinition, SheetMetalComponentDefinition)
		If oSMCD Is Nothing Then Continue For
		If Not oSMCD.HasFlatPattern Then
			Try
				oSMCD.Unfold()
				oSMCD.FlatPattern.ExitEdit()
			Catch
			End Try
		End If
		Dim sPart_FileName As String = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
		Dim sDXF_PartFile As String = System.IO.Path.Combine(sDXF_folder_cut, sPart_FileName & ".dxf")
		Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=R12"
		Try
			oSMCD.DataIO.WriteDataToFile(sOut, sDXF_PartFile)
		Catch ex As Exception
			MsgBox("Error exporting part to DXF: " & ex.Message, vbExclamation, "DXF Export Error")
		End Try
	Next 'oView
Next 'oSheet
' Generate the PDF file
Dim sPDF_file As String = System.IO.Path.Combine(sPDF_folder, sFileName & ".pdf")
' Use your provided PDF generation code here
oDDoc.SaveAs(sPDF_file, True)
' Generate the DXF file for the whole drawing
If sDXF_folder <> "" Then
	Dim sDXF_DrawingFile As String = System.IO.Path.Combine(sDXF_folder, sFileName & ".dxf")
	Try
		ThisApplication.SilentOperation = True
		oDDoc.SaveAs(sDXF_DrawingFile, True)
	Catch ex As Exception
		MsgBox("Error exporting drawing to DXF: " & ex.Message, vbExclamation, "DXF Export Error")
	Finally
		ThisApplication.SilentOperation = False
	End Try
End If

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

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

Thank you for your quick response.

 

I get this error 6 times: 

madstrolle_0-1699370172602.png

But when i have pushed the ok button 6 times, it does create the PDF and DXF "02. DXF", but the "06. DXF - Cut" folder and the flat pattern files within the folder is not created.

And when i try to create them again and overwrite the files it promps me, if i want to overwrite the files:

madstrolle_1-1699370709430.png

 

0 Likes
Message 4 of 5

madstrolle
Enthusiast
Enthusiast

And btw it is set to "silent operation"

madstrolle_0-1699372025818.png

 

0 Likes
Message 5 of 5

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield 

FYI.

I got the rule working😊

I delete the files inside "02. DXF" folder at first and then create new ones.

Here is the code:

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Return
Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim sPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDDoc.FullFileName)
Dim sNewPath As String = Left(sPath, Len(sPath) - 10)

' Create the "02. DXF" folder in the same directory as the drawing
Dim sDXF_folder As String = System.IO.Path.Combine(sNewPath, "02. DXF")
If Not System.IO.Directory.Exists(sDXF_folder) Then
    System.IO.Directory.CreateDirectory(sDXF_folder)
End If

' Create the "06. DXF - Cut" folder in the same directory as the drawing if it doesn't exist
Dim sDXF_folder_cut As String = System.IO.Path.Combine(sNewPath, "06. DXF - Cut")
If Not System.IO.Directory.Exists(sDXF_folder_cut) Then
    System.IO.Directory.CreateDirectory(sDXF_folder_cut)
End If

' Create the "01. PDF" folder in the same directory as the drawing
Dim sPDF_folder As String = System.IO.Path.Combine(sNewPath, "01. PDF")
If Not System.IO.Directory.Exists(sPDF_folder) Then
    System.IO.Directory.CreateDirectory(sPDF_folder)
End If

' Check if the PDF file already exists
Dim sPDF_file As String = System.IO.Path.Combine(sPDF_folder, sFileName & ".pdf")
Dim sPDF_old_folder As String = System.IO.Path.Combine(sNewPath, "Tidligere revisioner")
Dim sPDF_old_file As String = sPDF_file
Dim i As Integer = 0
While System.IO.File.Exists(sPDF_old_file)
    If i = 0 Then
        sPDF_old_file = System.IO.Path.Combine(sPDF_old_folder, sFileName & "_rev0.pdf")
    Else
        sPDF_old_file = System.IO.Path.Combine(sPDF_old_folder, sFileName & "_rev" & i.ToString("D2") & ".pdf")
    End If
    i += 1
End While
If i > 0 Then
    System.IO.File.Move(sPDF_file, sPDF_old_file)
End If

' Generate the new PDF file
' Use your provided PDF generation code here
oDDoc.SaveAs(sPDF_file, True)

' The rest of your original code for generating DXF files can follow here


Dim oSheets As Inventor.Sheets = oDDoc.Sheets
For Each oSheet As Inventor.Sheet In oSheets
    Dim oViews As DrawingViews = oSheet.DrawingViews
    For Each oView As DrawingView In oViews
        If oView.ReferencedFile Is Nothing Then Continue For
        Dim oPartDoc As PartDocument = TryCast(oView.ReferencedFile.ReferencedDocument, PartDocument)
        If oPartDoc Is Nothing Then Continue For
        Dim oSMCD As SheetMetalComponentDefinition = TryCast(oPartDoc.ComponentDefinition, SheetMetalComponentDefinition)
        If oSMCD Is Nothing Then Continue For
        If Not oSMCD.HasFlatPattern Then
            Try
                oSMCD.Unfold()
                oSMCD.FlatPattern.ExitEdit()
            Catch
            End Try
        End If
        Dim sPart_FileName As String = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
        Dim sDXF_PartFile As String = System.IO.Path.Combine(sDXF_folder_cut, sPart_FileName & ".dxf")
        Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=R12"
        Try
            oSMCD.DataIO.WriteDataToFile(sOut, sDXF_PartFile)
        Catch ex As Exception
            MsgBox("Error exporting part to DXF: " & ex.Message, vbExclamation, "DXF Export Error")
        End Try
    Next 'oView
Next 'oSheet

' Delete the existing DXF files for the current drawing inside the "02. DXF" folder
If sDXF_folder <> "" Then
    Dim sDXF_DrawingFilePattern As String = sFileName & "*.dxf"
    Dim dxfFiles As String() = System.IO.Directory.GetFiles(sDXF_folder, sDXF_DrawingFilePattern)
    For Each file As String In dxfFiles
        System.IO.File.Delete(file)
		Next
End If

' Generate the DXF file for the whole drawing
If sDXF_folder <> "" Then
    Dim sDXF_DrawingFile As String = System.IO.Path.Combine(sDXF_folder, sFileName & ".dxf")
    Try
        ThisApplication.SilentOperation = True
        oDDoc.SaveAs(sDXF_DrawingFile, True)
    Catch ex As Exception
        MsgBox("Error exporting drawing to DXF: " & ex.Message, vbExclamation, "DXF Export Error")
    Finally
        ThisApplication.SilentOperation = False
    End Try
End If

0 Likes