rule to auto update and save drawing to pdf

rule to auto update and save drawing to pdf

mitoborecek
Enthusiast Enthusiast
1,609 Views
32 Replies
Message 1 of 33

rule to auto update and save drawing to pdf

mitoborecek
Enthusiast
Enthusiast

How to make/run rule in drawing .dwg after change all parapeters and autoremodel assemble .iam.

 

When run rule in drawing I need tu update drawing (take about 10-15 sec) and save as .pdf with name (some parameter in assemble)

0 Likes
1,610 Views
32 Replies
Replies (32)
Message 2 of 33

JoAntt
Enthusiast
Enthusiast

try this!

 

Update 

InventorVb.DocumentUpdate()

 

Save pdf

ThisDoc.Document.SaveAs((Path to new pdf"), True)

 

0 Likes
Message 3 of 33

mitoborecek
Enthusiast
Enthusiast

Doesnt work

mitoborecek_0-1729018048482.png

 

0 Likes
Message 4 of 33

J-Camper
Advisor
Advisor
Accepted solution

@mitoborecek,

I've never tried the save as approach, but i know this works:

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
	
	'UPDATE
	InventorVb.DocumentUpdate()
	'Create PDF
	Call CreatePDFExport(ThisApplication.ActiveDocument)
	
End Sub

Private Sub CreatePDFExport(dDoc As DrawingDocument)

	' Create a DataMedium object
	Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'Set the destination file name
	oDataMedium.FileName = System.IO.Path.ChangeExtension(dDoc.FullFileName, ".pdf")

	' Get the PDF translator Add-In.
	Dim PDFAddIn As TranslatorAddIn
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

	Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

	' Create a NameValueMap object
	Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

	' Check whether the translator has 'SaveCopyAs' options
	If PDFAddIn.HasSaveCopyAsOptions(dDoc, oContext, oOptions) Then

		' Options for drawings...

		oOptions.Value("All_Color_AS_Black") = 0
		oOptions.Value("Remove_Line_Weights") = 0
		oOptions.Value("Publish_All_Sheets") = 0
		oOptions.Value("Launch_Viewer") = 0
		oOptions.Value("Vector_Resolution") = 2400
		oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Custom_Begin_Sheet") = 2
		'oOptions.Value("Custom_End_Sheet") = 4

	End If

	'Publish document.
	Try
		Call PDFAddIn.SaveCopyAs(dDoc, oContext, oOptions, oDataMedium)
	Catch ex As Exception
		MessageBox.Show(ex.Message, "Export Error")
	End Try

End Sub

 

The sub routine will currently save the PDF in the same location as the drawing, but you can change that by supplying the location to the Sub routine "CreatePDFExport"

Let me know if you have any questions

0 Likes
Message 5 of 33

mitoborecek
Enthusiast
Enthusiast

it works fine but it print to pdf all layouts. Need print to pdf only "4 sachta:1"

mitoborecek_0-1729515949463.png

How to edit rule to name it - some parameter from asseble?

0 Likes
Message 6 of 33

J-Camper
Advisor
Advisor

if you have that sheet active, you could change the options in the PDF sub like this:

 

oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintCurrentSheet

 

If the intended sheet is always sheet 1 then you could do this:

 

oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintSheetRange
oOptions.Value("Custom_Begin_Sheet") = 1
oOptions.Value("Custom_End_Sheet") = 1


 Options part:

temp_png.png

0 Likes
Message 7 of 33

mitoborecek
Enthusiast
Enthusiast

This works perfect

 

Noe just to change the name when saving the pdf - either according to some parameter in the assemble, which changes every time, or just from number 1, 2, 3 ...

0 Likes
Message 8 of 33

J-Camper
Advisor
Advisor
Accepted solution

@mitoborecek,

I replaced the filename creation with a custum function:

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
	
	'UPDATE
	InventorVb.DocumentUpdate()
	'Create PDF
	Call CreatePDFExport(ThisApplication.ActiveDocument)
	
End Sub

Private Sub CreatePDFExport(dDoc As DrawingDocument)

	' Create a DataMedium object
	Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'Set the destination file name
	oDataMedium.FileName = GetPDFFileName(dDoc)

	' Get the PDF translator Add-In.
	Dim PDFAddIn As TranslatorAddIn
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

	Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

	' Create a NameValueMap object
	Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

	' Check whether the translator has 'SaveCopyAs' options
	If PDFAddIn.HasSaveCopyAsOptions(dDoc, oContext, oOptions) Then

		' Options for drawings...

		oOptions.Value("All_Color_AS_Black") = 0
		oOptions.Value("Remove_Line_Weights") = 0
		oOptions.Value("Publish_All_Sheets") = 0
		oOptions.Value("Launch_Viewer") = 0
		oOptions.Value("Vector_Resolution") = 2400
		oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintSheetRange
		oOptions.Value("Custom_Begin_Sheet") = 1
		oOptions.Value("Custom_End_Sheet") = 1

	End If

	'Publish document.
	Try
		Call PDFAddIn.SaveCopyAs(dDoc, oContext, oOptions, oDataMedium)
	Catch ex As Exception
		MessageBox.Show(ex.Message, "Export Error")
	End Try

End Sub

Function GetPDFFileName(dDoc As DrawingDocument) As String
	
	Dim FileName As String = System.IO.Path.ChangeExtension(dDoc.FullFileName, ".pdf")
	
	Dim UserProperties As PropertySet = dDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim IssueIndex As String
	
	Try
		IssueIndex = UserProperties.Item("Issue Index").Value
	Catch
		UserProperties.Add("1", "Issue Index")
		IssueIndex = UserProperties.Item("Issue Index").Value
	End Try
	
	FileName = String.Format("{0}({1}){2}", Left(FileName, FileName.Length-4), IssueIndex, Right(FileName, 4)).ToString
	
	Return FileName
	
End Function

 

This uses an iProperty in the drawing file, and requires manual change.  Didn't think you would want it to increment automatically.

0 Likes
Message 9 of 33

mitoborecek
Enthusiast
Enthusiast

After change in my asssembly one of my parameters "Name" change value every time, when I change specific parameters in assembly.

 

This parameter "Name" will be perfect to use as name in drawing rule to named pdf

0 Likes
Message 10 of 33

J-Camper
Advisor
Advisor
Accepted solution

@mitoborecek,

Replace the GetPDFFileName function with this:

Function GetPDFFileName(dDoc As DrawingDocument) As String
	
	Dim FileName As String = System.IO.Path.ChangeExtension(dDoc.FullFileName, ".pdf")
	
	Dim IssueIndex As String = "Missing Reference Assembly"
	
	If dDoc.ReferencedDocuments.Count > 0
	
		Dim aDoc As AssemblyDocument = TryCast(dDoc.ReferencedDocuments.Item(1), AssemblyDocument)
		If aDoc IsNot Nothing
			
			Try
				IssueIndex = aDoc.ComponentDefinition.Parameters.Item("Name").Value.ToString
			Catch
				IssueIndex = "Missing_Name_Parameter"
			End Try
			
		End If
	
	End If
	
	FileName = String.Format("{0}({1}){2}", Left(FileName, FileName.Length-4), IssueIndex, Right(FileName, 4)).ToString
	
	Return FileName
	
End Function
0 Likes
Message 11 of 33

johan.degreef
Advisor
Advisor

@J-Camper 

Dear, what should I change to this code if I want to save it to a networkshare ex. "\\192.168.1.1\pdf-inventor"?

Many Thanks, Johan

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 12 of 33

mitoborecek
Enthusiast
Enthusiast

last 2 things

- name - just parameter "Name" without filename and "()"
- how to auto run this rule in drawing after all changes made in assemble?

0 Likes
Message 13 of 33

J-Camper
Advisor
Advisor

@johan.degreef,

Replace the FileName line with this:

Dim FileName As String = "\\192.168.1.1\pdf-inventor\" & System.IO.Path.GetFileNameWithoutExtension(dDoc.FullFileName) & ".pdf"

 

0 Likes
Message 14 of 33

J-Camper
Advisor
Advisor
Accepted solution

@mitoborecek,

The filename is easy,  Replace the GetFileName Function again:

Function GetPDFFileName(dDoc As DrawingDocument) As String
	
	Dim FileName As String = System.IO.Path.GetDirectoryName(dDoc.FullFileName) & "\"
	
	Dim IssueIndex As String = "Missing Reference Assembly"
	
	If dDoc.ReferencedDocuments.Count > 0
	
		Dim aDoc As AssemblyDocument = TryCast(dDoc.ReferencedDocuments.Item(1), AssemblyDocument)
		If aDoc IsNot Nothing
			
			Try
				IssueIndex = aDoc.ComponentDefinition.Parameters.Item("Name").Value.ToString
			Catch
				IssueIndex = "Missing_Name_Parameter"
			End Try
			
		End If
	
	End If
		
	FileName = String.Format("{0}{1}{2}", FileName, IssueIndex, ".pdf").ToString
	
	Return FileName
	
End Function

 

As far as triggering the rule to run, you could set it to an event trigger:

temp_png.png

0 Likes
Message 15 of 33

mitoborecek
Enthusiast
Enthusiast

but I need run "rule 2" in drawing, after finish "rule 1" in assembly

0 Likes
Message 16 of 33

J-Camper
Advisor
Advisor

with it set  to an event trigger "Drawing View Update".  It should run automatically when you open the drawing after making changes to the assembly because the drawing view will be updating

The other thing you could do is combine rules 1 & 2 so you make changes to assembly, then the rule opens the drawing and executes on the opened drawing.  I would need more context to help with this.

0 Likes
Message 17 of 33

mitoborecek
Enthusiast
Enthusiast

I change assembly meny times but dont close assembly (inventor) and update drawing after change assembly. Then save drawing to pdf. So

 

0. Open assembly

1. Make change in assembly

2. update drawing, then save pdf

3. make change in assembly

4. update drawing, then save pdf

.

.

.

5. close program

0 Likes
Message 18 of 33

J-Camper
Advisor
Advisor

Did you try the event trigger?  It should trigger the rule to run when you activate the drawing after making changes to the assembly.  Your process would be the same, Edit assembly then switch tabs to drawing [which triggers the rule to run as drawing views update], then switch back to the assembly and make more changes.

0 Likes
Message 19 of 33

mitoborecek
Enthusiast
Enthusiast

I dont switch between assembly and drawing. I am in drawing, and run rule with changes use Forms. After changes I click on update icon in drawing 

0 Likes
Message 20 of 33

J-Camper
Advisor
Advisor

Easiest thing to do is add a Run other rule call at the end of your current rule:

iLogicVb.RunRule("ruleName")

 

Otherwise you can do my suggestion about merging the 2 rules.  It should be easy enough to just move your current rule into the Sub Main portion of the rule I posted above the up date call, and Export PDF call.  Then you only have to run 1 rule that will do everything it does now then updates and creates PDF. 

 

In order for me to help with merging I need the other rule.

0 Likes