pdf with revision

pdf with revision

info
Contributor Contributor
2,196 Views
15 Replies
Message 1 of 16

pdf with revision

info
Contributor
Contributor

Hello everyone

I have an ilogic rule that creates PDF files from an iam.

now I also want to have an overhaul.

I have added revision but unfortunately this is not working.

Does anyone know what I did wrong

 

DI-A-0030-00026 SHU.idw (Sheet-1) rev1

 

 

 

Sub Main()    

 

'define the active document as an assembly file

Dim oAsmDoc As AssemblyDocument

oAsmDoc = ThisApplication.ActiveDocument

Dim oAsmName As String

oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then

MessageBox.Show("Please run this rule from the assembly file.", "iLogic")

Exit Sub

End If

'get user input

RUsure = MessageBox.Show ( _

"This will create a PDF file for all of the asembly components that are sheet metal." _

& vbLf & "This rule expects that the part file is saved." _

& vbLf & " " _

& vbLf & "Are you sure you want to create PDF for all of the assembly components?" _

& vbLf & "This could take a while.", "iLogic - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then

Return

Else

End If

oPath = ThisDoc.Path

Dim oDataMedium As DataMedium

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

 

oRevNum = iProperties.Value("Project", "Revision Number")

 

oContext = ThisApplication.TransientObjects.CreateTranslationContext

oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

oOptions = ThisApplication.TransientObjects.CreateNameValueMap

ExPath = "C:\Users\j.kleijnjans\Documents\PDF_DXF_STP\" & "PDF_DXF_STP"

If Not System.IO.Directory.Exists(ExPath) Then

System.IO.Directory.CreateDirectory(ExPath)

End If

 

    ' Define the active document As an Assembly file

        oIdwPathName = ThisDoc.ChangeExtension(".idw")

    'ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd").Execute

            ' Look at the files referenced by the assembly

    Dim oRefDocs As DocumentsEnumerator

    oRefDocs = oAsmDoc.AllReferencedDocuments

    Dim oRefDoc As Document

   

    ' Work the the drawing files for the referenced models. This expects that the model has a drawing of the same path and name

    For Each oRefDoc In oRefDocs

        idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"

        ' Check to see that the model has a drawing of the same path and name

        If(System.IO.File.Exists(idwPathName)) Then

            Dim oDrawDoc As DrawingDocument

            oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)

                                                    

                      

i = 1

 

'blader door sheets en exporteer

 

For Each oSheet In oDrawDoc.Sheets

   

ThisApplication.ActiveDocument.Sheets.Item(i).Activate

 

ThisApplication.ActiveView.Fit

Dim oDoc As DrawingDocument

oDoc = ThisApplication.ActiveDocument

Dim iSheet As Sheet

iSheet = oDoc.ActiveSheet

Dim oFileName As String

oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)'0

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _

("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

oDocument = ThisApplication.ActiveDocument

oContext = ThisApplication.TransientObjects.CreateTranslationContext

oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

oOptions = ThisApplication.TransientObjects.CreateNameValueMap

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

 

Dim lPos As Long

Dim rPos As Long

Dim sLen As Long

Dim sSheetName As String

Dim iSheetNumber As Integer

Dim ExportFolder As String

 

'find the seperator in the sheet name:number

lPos = InStr(iSheet.Name, ":")

'find the number of characters in the sheet name

sLen = Len(iSheet.Name)

'find the sheet name

sSheetName = Left(iSheet.Name, lPos -1)

'find the sheet number

iSheetNumber = Right(iSheet.Name, sLen -lPos)

'set PDF Options

'If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then

oOptions.Value("All_Color_AS_Black") = 0

oOptions.Value("Remove_Line_Weights") = 1

oOptions.Value("Vector_Resolution") = 400

oRevNum = iProperties.Value("Project", "Revision Number")

oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange

oOptions.Value("Custom_Begin_Sheet") = iSheetNumber

oOptions.Value("Custom_End_Sheet") = iSheetNumber

 

 

'oDataMedium.FileName = ExPath & "\" & "(" & sSheetName & "-" & ").pdf"

oDataMedium.FileName = ExPath & "\" & oFileName & " " & "(" & sSheetName & "-" & i & ")" & "_" & oRevNum & ".pdf"

'Publish document

oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)    

              

'sheet index ophogen

i=i+1  

              

    Next

 

            oDrawDoc.Close        ' Close the file

        End If

    Next

 

 

'idw van opstaande samenstelling exporteren

 

        If(System.IO.File.Exists(oIdwPathName)) Then

            Dim oDrawDoc As DrawingDocument

            oDrawDoc = ThisApplication.Documents.Open(oIdwPathName, True)

                                                    

                      

i = 1

 

'blader door sheets en exporteer

 

For Each oSheet In oDrawDoc.Sheets

   

ThisApplication.ActiveDocument.Sheets.Item(i).Activate

 

ThisApplication.ActiveView.Fit

Dim oDoc As DrawingDocument

oDoc = ThisApplication.ActiveDocument

Dim iSheet As Sheet

iSheet = oDoc.ActiveSheet

Dim oFileName As String

oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName)-4)

 

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _

("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

oDocument = ThisApplication.ActiveDocument

oContext = ThisApplication.TransientObjects.CreateTranslationContext

oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

oOptions = ThisApplication.TransientObjects.CreateNameValueMap

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

 

Dim lPos As Long

Dim rPos As Long

Dim sLen As Long

Dim sSheetName As String

Dim iSheetNumber As Integer

Dim ExportFolder As String

 

'find the seperator in the sheet name:number

lPos = InStr(iSheet.Name, ":")

'find the number of characters in the sheet name

sLen = Len(iSheet.Name)

'find the sheet name

sSheetName = Left(iSheet.Name, lPos -1)

'find the sheet number

iSheetNumber = Right(iSheet.Name, sLen -lPos)

'set PDF Options

'If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then

oOptions.Value("All_Color_AS_Black") = 0

oOptions.Value("Remove_Line_Weights") = 1

oOptions.Value("Vector_Resolution") = 400

oRevNum = iProperties.Value("Project", "Revision Number")

oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange

oOptions.Value("Custom_Begin_Sheet") = iSheetNumber

oOptions.Value("Custom_End_Sheet") = iSheetNumber

 

 

'Set the PDF target file name

oDataMedium.FileName = ExPath & "\" & oFileName & " " & "(" & sSheetName & "-" & i & ")"& "_" & oRevNum &".pdf"

'oDataMedium.FileName = ExPath & "\" & oFileName & " " & "(" & sSheetName & "-" & i & ").pdf"

'Publish document

oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)    

              

'sheet index ophogen

i=i+1

                      

    Next

 

            oDrawDoc.Close        ' Close the file

        End If

 

'MessageBox.Show("PDF-bestanden opgeslagen")

 

MessageBox.Show("PDF FILES ARE READY")

proceed1 :

End Sub

 

 

Accepted solutions (1)
2,197 Views
15 Replies
Replies (15)
Message 2 of 16

marcin_otręba
Advisor
Advisor

Hi,

when you want to access iproperties from file different that document from which ilogic rule is run, than you need to do it through doc.property sets.item...

 

Try this:

Sub Main()    

'define the active document as an assembly file

Dim oAsmDoc As AssemblyDocument

oAsmDoc = ThisApplication.ActiveDocument

Dim oAsmName As String

oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub

End If

'get user input

RUsure = MessageBox.Show ( _
"This will create a PDF file for all of the asembly components that are sheet metal." _
& vbLf & "This rule expects that the part file is saved." _
& vbLf & " " _
& vbLf & "Are you sure you want to create PDF for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
	Return
Else
End If
oPath = ThisDoc.Path
ExPath   = "C:\Users\j.kleijnjans\Documents\PDF_DXF_STP\" & "PDF_DXF_STP"
If Not System.IO.Directory.Exists(ExPath) Then
System.IO.Directory.CreateDirectory(ExPath)
End If
    
    Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
   ' Work the the drawing files for the referenced models. This expects that the model has a drawing of the same path and name
   	For Each oRefDoc In oRefDocs
     export (oRefDoc,ExPath)
    Next
export (oAsmDoc, ExPath)
 
MessageBox.Show("PDF FILES ARE READY")

proceed1 :

End Sub
Sub export(doc As Document, ExPath As String)
	 idwPathName = Left(doc.FullDocumentName, Len(doc.FullDocumentName) - 3) & "idw"
        ' Check to see that the model has a drawing of the same path and name
        If(System.IO.File.Exists(idwPathName)) Then
            Dim oDrawDoc As DrawingDocument
           	oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
			i = 1
			'blader door sheets en exporteer
			For Each oSheet In oDrawDoc.Sheets
				ThisApplication.ActiveDocument.Sheets.Item(i).Activate
				ThisApplication.ActiveView.Fit
				Dim oDoc As DrawingDocument
				oDoc = ThisApplication.ActiveDocument
				Dim iSheet As Sheet
				iSheet = oDoc.ActiveSheet
				Dim oFileName As String
				oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)'0
				oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
				("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
				oDocument = ThisApplication.ActiveDocument
				oContext = ThisApplication.TransientObjects.CreateTranslationContext
				oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
				oOptions = ThisApplication.TransientObjects.CreateNameValueMap
				oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 
				Dim lPos As Long
				Dim rPos As Long
				Dim sLen As Long
				Dim sSheetName As String
				Dim iSheetNumber As Integer
				Dim ExportFolder As String

				'find the sheet name

				sSheetName = Split(iSheet.Name,":")(0)

				'find the sheet number

				iSheetNumber = Split(iSheet.Name,":")(1)

				'set PDF Options
				'If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
				oOptions.Value("All_Color_AS_Black") = 0
				oOptions.Value("Remove_Line_Weights") = 1
				oOptions.Value("Vector_Resolution") = 400
				'oRevNum = iProperties.Value("Project", "Revision Number")
				oRevNum=doc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
				oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
				oOptions.Value("Custom_Begin_Sheet") = iSheetNumber
				oOptions.Value("Custom_End_Sheet") = iSheetNumber

				'oDataMedium.FileName = ExPath & "\" & "(" & sSheetName & "-" & ").pdf"

				oDataMedium.FileName = ExPath & "\" & oFileName & " " & "(" & sSheetName & "-" & i & ")" & "_" & oRevNum & ".pdf"

				'Publish document

				oPDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)    

				'sheet index ophogen

				i = i + 1  
			Next
            oDrawDoc.Close(True)        ' Close the file

        End If

End Sub

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 3 of 16

info
Contributor
Contributor

Perfectly what I was looking for.
One more question
If there is no revevision, can the pdf be saved as:
DI-A-0030-00026 SHU.idw (Sheet-1)
If there is a revision, save the file as
DI-A-0030-00026 SHU.idw (Sheet-1) rev1

 

Greet Jos

Message 4 of 16

WCrihfield
Mentor
Mentor

Your file system may not like having the dot/period ( . ) in the middle of the file name.

Perhaps either removing the drawing files extension, or replacing that dot with an underscore ( _ ) or other non-filesystem-offending characters would help.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 16

marcin_otręba
Advisor
Advisor

sure,

replace:

oDataMedium.FileName = ExPath & "\" & oFileName & " " & "(" & sSheetName & "-" & i & ")" & "_" & oRevNum & ".pdf"

with:

if oRevNum<>”” then
oDataMedium
.FileName = ExPath & "
\"
& oFileName & " " & "(" & sSheetName & "-" & i & ")" & "_" & oRevNum & ".pdf"
else
oDataMedium.FileName = ExPath & "
\" & oFileName & " " & "(" & sSheetName & "-" & i & ")" & ".pdf"
end if

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 6 of 16

info
Contributor
Contributor

Thanks

It works perfect 

Message 7 of 16

marcin_otręba
Advisor
Advisor
Accepted solution

nice to hear that. Can you accept my answer as solution then ?

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 8 of 16

Sprocket1077
Enthusiast
Enthusiast

Sorry.

Wrong destination.

0 Likes
Message 9 of 16

Sprocket1077
Enthusiast
Enthusiast

Hi you all.

I like this rule a lot, but I need a little update too.

First I would like it to apply the rule to the assembly drawing file as well as the parts. Witch I think it doesn't.

I would like it to create a PDF sub folder in the assembly folder, not in C:/user/... or dedicated folder.

I would like it to create also the step file of the parts in the same folder.

I would like it to apply the drawing revision number to the pdf file even if the revision is 0.

I would like it to apply the same revision number to the step files.

And I also would like the suffix scheme naming to looks like -R00. I mean ''-R'' with always double digits even if the drawing revision number is ''-R1'' (Should result as -R01).

 

I know you are very good at this, me not.

 

Thanks a lot.

0 Likes
Message 10 of 16

WCrihfield
Mentor
Mentor

Hi @Sprocket1077.  I think I have something that may work for your needs.  It's a fairly large code, because it's a fairly large and complex task.  I put the two main export processes (PDF & STEP) into two different Sub routines that can be called by the main Sub.  One thing that may cause problems in this process is if your Revision number is a Letter instead of a Number.  In your post, it looked like you were referring to a zero (0) in your Revision number.  Is that zero always present, or is it just 'empty' when there is no revision?  If it is 'empty' then we may have to adjust this code a bit to put that initial zero in place when it is found to be empty, just to be safe.  I am retrieving that revision number as an Integer, then using a technique to convert it to a String with double digits, as part of the revision text in the file name.

Let me know how this works for you, of if this isn't what you were looking for.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisAssembly.Document
	oDirChar = System.IO.Path.DirectorySeparatorChar
	oPath = System.IO.Path.GetDirectoryName(oADoc.FullFileName) & oDirChar & "PDF & STEP" & oDirChar
	'get file name without path and without file extension
	oAsmName = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
	'gets Rev Number as Integer (Won't work if using Letters!)
	Dim oRevNum As Integer = oADoc.PropertySets(1).Item("Revision Number").Value
	Dim oRev As String = "-R" & oRevNum.ToString("00")
	If Not System.IO.Directory.Exists(oPath) Then
		System.IO.Directory.CreateDirectory(oPath)
	End If
	
	oNewPDFName = oPath & oAsmName & oRev & ".pdf"
	ExportToPDF(oADoc, oNewPDFName)
	
	oNewSTEPName = oPath & oAsmName & oRev & ".stp"
	ExportToSTEP(oADoc, oNewSTEPName)

	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
		'get file name without path and without file extension
		oRefName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		'gets Rev Number as Integer (Won't work if using Letters!)
		oRevNum = oADoc.PropertySets(1).Item("Revision Number").Value
		oRev = "-R" & oRevNum.ToString("00")
		
		oNewPDFName = oPath & oRefName & oRev & ".pdf"
		ExportToPDF(oRefDoc, oNewPDFName)
		
		oNewSTEPName = oPath & oRefName & oRev & ".stp"
		ExportToSTEP(oRefDoc, oNewSTEPName)
	Next
	MsgBox("All new PDF & STEP files were saved to:" & vbCrLf & oPath, , "FINISHED")
End Sub

Sub ExportToPDF(oDoc As Document, oNewFileName As String)
	Dim oDDoc As DrawingDocument
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		oDDoc = oDoc
	Else
		oDrawingFile = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".idw")
		If System.IO.File.Exists(oDrawingFile) Then
			oDDoc = ThisApplication.Documents.Open(oDrawingFile, False)
		Else
			Exit Sub
		End If
	End If
	
	'get the PDF Translator Add-in
	Dim oPDFAddin As TranslatorAddIn
	For Each oAddin As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddin.DisplayName = "Translator: PDF" Then
			oPDFAddin = oAddin
		End If
	Next

	'Create the variables needed by the Translator Add-in
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	oDataMedium.FileName = oNewFileName

	'Check to see if the PDF already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewFileName) = True Then
		oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If
	
	'The following If-Then statement defines the Options available, and their Values.
	If oPDFAddin.HasSaveCopyAsOptions(oDDoc, oContext, oOptions) Then
		oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
		'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
		'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
		'oOptions.Value("Custom_Begin_Sheet") = 1
		'oOptions.Value("Custom_End_Sheet") = 4
		oOptions.Value("Launch_Viewer") = 0 ' 0 = False, 1 = True
		oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
		oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
		oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
	End If

	'Publish PDF
	oPDFAddin.SaveCopyAs(oDDoc, oContext, oOptions, oDataMedium)
End Sub

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
	'get file name without path, but with file extension (for use with iProperties calls)
	oDocName = System.IO.Path.GetFileName(oDoc.FullFileName)
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	If IsNothing(oSTEP) Then
		MsgBox("STEP Translator Add-in not found.  Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If

	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	'Check to see if the STEP file already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewFileName) Then
		oAns = MsgBox("A STEP file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = oNewFileName

	If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
		' Set application protocol.
		 ' 2 = AP 203 - Configuration Controlled Design
		 ' 3 = AP 214 - Automotive Design
		 oOptions.Value("ApplicationProtocolType") = 3
		 oOptions.Value("IncludeSketches") = True
		 oOptions.Value("export_fit_tolerance") = .000393701  'minimum
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 oOptions.Value("Authorization") = ""
		 oOptions.Value("Description") = iProperties.Value(oDocName, "Project", "Description")
		 oOptions.Value("Organization") = iProperties.Value(oDocName, "Summary", "Company")
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export this document as a STEP file FAILED!", vbOKOnly + vbExclamation, "Export to STEP Error")
		End Try
	End If
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) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 16

Sprocket1077
Enthusiast
Enthusiast

Hi WCrihfield.

Thank you very much for the work you did.

But as I tried it, it does not work.

I jammed at start.

Here is my error message.

 

FYI, my revision number will always be a number.

It start with 00 then 01, 02, 03, etc.

Sorry for the dialog box, the message is in french.

 

Awaiting your reply.

Thank you.

0 Likes
Message 12 of 16

WCrihfield
Mentor
Mentor

OK. It looks to me like at some point it is causing this error because it won't convert a String into an Integer. This is obviously where I'm trying to retrieve the value of that Revision Number iProperty as an Integer.  I just double checked my documentation about that specific iProperty, and it is supposed to return a String.  So, I likely just need to change how I'm defining my variables in that area of the code (2 places).  I updated that part of the code (in those 2 places), and I'm hoping this fixes the problem.  Now I'm pre-defining that oRevNum variable as a String, instead of an Integer, which should eliminate that error message.  Then, in order to still use that little formatting trick, I'm using a simple data type converter to convert the String into an Integer, then back to a String, with the formatting we want, and setting that as the value of the oRev variable (which is also a String).  I believe this ToString("00") needs to be following an Integer type data of a 2 digit value or less.  I also added a couple lines in there to ensure the values of these two variables gets reset to an empty string each time around, just to be extra safe.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisAssembly.Document
	oDirChar = System.IO.Path.DirectorySeparatorChar
	oPath = System.IO.Path.GetDirectoryName(oADoc.FullFileName) & oDirChar & "PDF & STEP" & oDirChar
	'get file name without path and without file extension
	oAsmName = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
	'get Revision Number Value (Won't work if using Letters!)
	Dim oRevNum As String = oADoc.PropertySets(1).Item("Revision Number").Value
	If String.IsNullOrEmpty(oRevNum) Then
		oRevNum = "0" 'a zero in quotes
	End If
	Dim oRev As String = "-R" & CInt(oRevNum).ToString("00")
	If Not System.IO.Directory.Exists(oPath) Then
		System.IO.Directory.CreateDirectory(oPath)
	End If
	
	oNewPDFName = oPath & oAsmName & oRev & ".pdf"
	ExportToPDF(oADoc, oNewPDFName)
	
	oNewSTEPName = oPath & oAsmName & oRev & ".stp"
	ExportToSTEP(oADoc, oNewSTEPName)

	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
		'get file name without path and without file extension
		oRefName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		'gets Rev Number as Integer (Won't work if using Letters!)
		oRevNum = String.Empty 'ensuring it is reset to an empty string
		oRevNum = oRefDoc.PropertySets(1).Item("Revision Number").Value
		If String.IsNullOrEmpty(oRevNum) Then
			oRevNum = "0" 'a zero in quotes
		End If
		oRev = String.Empty 'ensuring it is reset to an empty string
		oRev = "-R" & CInt(oRevNum).ToString("00")
		
		oNewPDFName = oPath & oRefName & oRev & ".pdf"
		ExportToPDF(oRefDoc, oNewPDFName)
		
		oNewSTEPName = oPath & oRefName & oRev & ".stp"
		ExportToSTEP(oRefDoc, oNewSTEPName)
	Next
	MsgBox("All new PDF & STEP files were saved to:" & vbCrLf & oPath, , "FINISHED")
End Sub

Sub ExportToPDF(oDoc As Document, oNewFileName As String)
	Dim oDDoc As DrawingDocument
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		oDDoc = oDoc
	Else
		oDrawingFile = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".idw")
		If System.IO.File.Exists(oDrawingFile) Then
			oDDoc = ThisApplication.Documents.Open(oDrawingFile, False)
		Else
			Exit Sub
		End If
	End If
	
	'get the PDF Translator Add-in
	Dim oPDFAddin As TranslatorAddIn
	For Each oAddin As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddin.DisplayName = "Translator: PDF" Then
			oPDFAddin = oAddin
		End If
	Next

	'Create the variables needed by the Translator Add-in
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	oDataMedium.FileName = oNewFileName

	'Check to see if the PDF already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewFileName) = True Then
		oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If
	
	'The following If-Then statement defines the Options available, and their Values.
	If oPDFAddin.HasSaveCopyAsOptions(oDDoc, oContext, oOptions) Then
		oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
		'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
		'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
		'oOptions.Value("Custom_Begin_Sheet") = 1
		'oOptions.Value("Custom_End_Sheet") = 4
		oOptions.Value("Launch_Viewer") = 0 ' 0 = False, 1 = True
		oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
		oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
		oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
	End If

	'Publish PDF
	oPDFAddin.SaveCopyAs(oDDoc, oContext, oOptions, oDataMedium)
End Sub

Sub ExportToSTEP(oDoc As Document, oNewFileName As String)
	'get file name without path, but with file extension (for use with iProperties calls)
	oDocName = System.IO.Path.GetFileName(oDoc.FullFileName)
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	If IsNothing(oSTEP) Then
		MsgBox("STEP Translator Add-in not found.  Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If

	'create needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	'Check to see if the STEP file already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewFileName) Then
		oAns = MsgBox("A STEP file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = oNewFileName

	If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
		' Set application protocol.
		 ' 2 = AP 203 - Configuration Controlled Design
		 ' 3 = AP 214 - Automotive Design
		 oOptions.Value("ApplicationProtocolType") = 3
		 oOptions.Value("IncludeSketches") = True
		 oOptions.Value("export_fit_tolerance") = .000393701  'minimum
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 oOptions.Value("Authorization") = ""
		 oOptions.Value("Description") = iProperties.Value(oDocName, "Project", "Description")
		 oOptions.Value("Organization") = iProperties.Value(oDocName, "Summary", "Company")
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export this document as a STEP file FAILED!", vbOKOnly + vbExclamation, "Export to STEP Error")
		End Try
	End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 16

Sprocket1077
Enthusiast
Enthusiast

Hi again WCrihfield.

You are great but I'm stuck again.

It seems that it does not find the starting assembly witch I'm in.

Can we try something else?

 

Here is my error message.

0 Likes
Message 14 of 16

A.Acheson
Mentor
Mentor

Can you screen shot the more info section of the error message.

AAcheson_1-1634958988988.png

 

To narrow down the error switch of each sub routine like so in both the main assembly and the for loop of the reference documents. Then switch on the main assembly first to ensure this section is working and then the reference documents.

'ExportToPDF(oADoc, oNewPDFName)
	
	oNewSTEPName = oPath & oAsmName & oRev & ".stp"
	'ExportToSTEP(oADoc, oNewSTEPName)

 

 

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

Sprocket1077
Enthusiast
Enthusiast

Hi A.Acheson.

Thank you very much to try to help me with this.

Here is the error message.

 

I looked for the result and I have to say that even if it returns an error message about the assembly file, the PDF file of the assembly have been made. (Then it stops)

Also I don't need the step file of the assembly.

The final result should be an assembly pdf file and all parts pdf files and step files with revision number as suffix.

Here is an image of what should be the result.

 

Please notice that I understand what you try to do in the macro, but I'm not good enough with VB to execute it as you told me.

 

Can we try something again?

Thanks.

0 Likes
Message 16 of 16

WCrihfield
Mentor
Mentor

That more detailed error message indicates that it is still encountering an error while attempting to access the iProperties of a component or document.  However, the way that error message is laid out, looks like it was trying to use the iLogic snippet for iProperties (iProperties.Value(componentOrDocName As Object, setName As String, propertyName As String)), but I'm not using that snippet of code anywhere in the code I posted.  I'm using the traditional API route to access the iProperties (Document.PropertySets.PropertySet.Property.Value).  Since the only place I'm trying to access iProperties is in the Sub Main portion of the code, I don't suspect any problems with the other two Sub routines at this time.  However, if the file name given to those sub routines has not been assembled property before it is given to them, this might cause a problem.  I'm now thinking that we may need to include a couple of Try...Catch...End Try blocks of code in there around the two lines of code where we are accessing the iProperty, and include a feedback message about when it encounters an error/failure, so we can better pinpoint the problem.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes