*.idw saveas PDF 我有困惑

*.idw saveas PDF 我有困惑

Anonymous
Not applicable
1,960 Views
13 Replies
Message 1 of 14

*.idw saveas PDF 我有困惑

Anonymous
Not applicable

QQ浏览器截图20180622153802.png大家好,我想用iLogic实现到idw转PDF,自定义的文件名遇见了问题,能不能帮我?Smiley Happy

 

NG Code

 

PDFPath = ThisDoc.Path
Dim Kubh As String=iProperties.Value("Project", "Stock Number")
ThisDoc.Document.SaveAs(PDFPath & "\" & ThisDoc.FileName(False) & " " & Kubh & (".pdf"),True)
ThisDoc.Document.Close

 

0 Likes
Accepted solutions (2)
1,961 Views
13 Replies
Replies (13)
Message 2 of 14

Xun.Zhang
Alumni
Alumni

Hello,

Show you an example and hope it helps!

Please define the export file name as what you want then.

' This is an example iLogic rule by Luke Davenport on Cadline Community
' It creates a 'folder browser dialog' to allow user to pick a directory location
' This selected directory location is then used for a generic PDF export operation
' Please modify to suit your requirements....

Imports System.Windows.Forms
    
' Get current location of this file
Dim ExportPath As String = ThisDoc.Path

' Check that this file has been saved and actually exists on disk
If String.IsNullOrEmpty(ExportPath) Then
    MsgBox("This file has not yet been saved and doesn't exist on disk!" _
	& vbLf & "Please save it first",64, "Formsprag iLogic")
Return
End If

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
    ' User clicked 'ok' on dialog box - capture the export path
    ExportPath = Dialog.SelectedPath & "\"    
Else
    ' User clicked 'cancel' on dialog box - exit
    Return
End If

oFileName = iProperties.Value("Custom", "Sales Order Number")

' Define the filename of the file to be exported
' In this Case it Is a PDF file extension
ExportFilename = oFileName & ".pdf"

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

'set PDF Options
If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
End If

'Set the PDF target file name
oDataMedium.FileName = ExportPath & ExportFilename

Try 
	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Catch
	MessageBox.Show("Error writing out PDF", "iLogic")
	bError = True
End Try

If bError <> True Then
	'Ask user If they want To open (launch) the file we just exported...
	oMessage = "File exported: " & _
			ExportPath & ExportFilename & vbLf & vbLf & _
			"Do you want to open the PDF Now?"
			
	oQuestion = MessageBox.Show(oMessage, _
	"Formsprag iLogic - File Exported",MessageBoxButtons.YesNo)
	
	If oQuestion = vbYes Then
		ThisDoc.Launch(ExportPath & ExportFilename)
	End If

End If

Xun
0 Likes
Message 3 of 14

Anonymous
Not applicable

Smiley Mad

SyntaxEditor Code Snippet

我修改了这句就报错了

oFileName = iProperties.Value("Project", "Stock Number")

 

System.NullReferenceException: 无效指针 (Exception from HRESULT: 0x80004003 (E_POINTER))
在 Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
在 Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
在 ThisRule.Main()
在 Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
在 iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Message 4 of 14

Xun.Zhang
Alumni
Alumni

Hello,

It looks a defect. @JaneFan, is it known for you?

Thanks!


Xun
0 Likes
Message 5 of 14

JaneFan
Autodesk
Autodesk

idw转PDF不能直接用inventor 文档的SaveCopyAs,需要调用inventor的TranslatorAddin来实现。这儿看,TranslatorAddin.HasSaveCopyAsOptions可能有点问题, 但是我们可以不用这个判断,直接调用。

以下是修改后的代码:

SyntaxEditor Code Snippet

   
' Get current location of this file
Dim ExportPath As String = ThisDoc.Path
oFileName = iProperties.Value("Project", "Stock Number")

' Define the filename of the file to be exported
' In this Case it Is a PDF file extension
ExportFilename = oFileName & ".pdf"

Dim oPDFAddIn As TranslatorAddIn 
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

'set PDF Options
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintSheetRange

'Set the PDF target file name
oDataMedium.FileName = ExportPath & ExportFilename

Try 
	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument , oContext, oOptions, oDataMedium)
Catch
	MessageBox.Show("Error writing out PDF", "iLogic")
	bError = True
End Try

If bError <> True Then
	'Ask user If they want To open (launch) the file we just exported...
	oMessage = "File exported: " & _
			ExportPath & ExportFilename & vbLf & vbLf & _
			"Do you want to open the PDF Now?"
	oQuestion = MessageBox.Show(oMessage, _
	"Formsprag iLogic - File Exported",MessageBoxButtons.YesNo)
	
	If oQuestion = vbYes Then
		ThisDoc.Launch(ExportPath & ExportFilename)
	End If
End If

 




Jane Fan
Inventor/Fusion QA Engineer
Message 6 of 14

Anonymous
Not applicable

抱歉试过不行,生成的PDF会跑到目录的上一级生成,而且生成的PDF文件名的也不对

0 Likes
Message 7 of 14

JaneFan
Autodesk
Autodesk
Accepted solution

说明PDF文件导出成功了,跑到上一级文件目录以及文件名不对都是因为文件路径是要根据需要来改动代码的。

比如您最初的文件名是下面的字符串,那就根据需要改成这个就可以了。

您的文件名(根据代码猜测PDFPath和kubh应该都是在您自己的代码中已经定义过的变量名)

PDFPath & "\" & ThisDoc.FileName(False) & " " & Kubh & (".pdf")  

需要改动的代码:

'Set the PDF target file name
oDataMedium.FileName = ExportPath & ExportFilename

 




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 8 of 14

Anonymous
Not applicable

不行的啊,库存编号读取不了

 

 

SyntaxEditor Code Snippet

' Get current location of this file
Dim ExportPath As String = ThisDoc.Path
oFileName = iProperties.Value("Project", "Stock Number")

' Define the filename of the file to be exported
' In this Case it Is a PDF file extension
ExportFilename = ThisDoc.FileName(False) & " " & oFileName & ".pdf"

Dim oPDFAddIn As TranslatorAddIn
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

'set PDF Options
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintSheetRange

'Set the PDF target file name

oDataMedium.FileName = ExportPath & "\" & ExportFilename

Try
	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Catch
	MessageBox.Show("Error writing out PDF", "iLogic")
	bError = True
End Try

'If bError <> True Then
'	'Ask user If they want To open (launch) the file we just exported...
'	oMessage = "File exported: " & _
'			ExportPath & ExportFilename & vbLf & vbLf & _
'			"Do you want to open the PDF Now?"
'	oQuestion = MessageBox.Show(oMessage, _
'	"Formsprag iLogic - File Exported",MessageBoxButtons.YesNo)

'	If oQuestion = vbYes Then
'		ThisDoc.Launch(ExportPath & ExportFilename)
'	End If
'End If

 

 

 

 

0 Likes
Message 9 of 14

JaneFan
Autodesk
Autodesk

下面这句是读取库存编号的代码,是这行会报错吗?还是库存编号没有存入导出的文件名中?

oFileName = iProperties.Value("Project", "Stock Number")

 如果是库存编号没有存入文件名,请修改下面这句话使得oDataMedium.FileName满足您的需求。

oDataMedium.FileName = ExportPath & "\" & ExportFilename

 




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 10 of 14

Anonymous
Not applicable

*.ipt里的库存组件信息,*.idw就没读取到,造成保存的文件名没有库存编号

0 Likes
Message 11 of 14

JaneFan
Autodesk
Autodesk

模型文件里的库存信息不回自动到工程文件中,如果需要的话,需要在工程文件里通过如下语句设置一下,把VIEW1和Sheet1改成自己工程文档里面对应的名字。

SyntaxEditor Code Snippet

iProperties.Value("Project", "Stock Number")  = ActiveSheet.View("VIEW1").ModelDocument.PropertySets.Item(3).Item("Stock Number").Value

 或者:

SyntaxEditor Code Snippet

iProperties.Value("Project", "Stock Number")  = ThisDrawing.Sheet("Sheet:1").View("VIEW1").ModelDocument.PropertySets.Item(3).Item("Stock Number").Value

 




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 12 of 14

Anonymous
Not applicable

这样是可以了,但是他会在工程图里写内容,又没其他解决方案? 为何注解那些可以直接调用零件的库存编号?

0 Likes
Message 13 of 14

JaneFan
Autodesk
Autodesk
Accepted solution

如果不想写入工程图的IProperties,直接读模型的属性写入需要的文件名就好了。根据需要调整代码吧:

oFileName = ThisDrawing.Sheet("Sheet:1").View("VIEW1").ModelDocument.PropertySets.Item(3).Item("Stock Number").Value

 




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 14 of 14

Anonymous
Not applicable

非常感谢,Smiley Happy

0 Likes