Ansys Workbench谐响应扫频结果,创建报告

需求:
前述文章已经从谐响应仿真计算后处理中,创建了结果txt文档和扫频曲线图。本节给出如何将计算结果填充到word 报告中,实现仿真报告的自动创建。
操作方法:
利用word 和 excel 的VBA编辑功能,以excel为控制界面,调用word模板,读取txt结果数据,创建报告。
示例说明:
以excel作为控制界面,本例需要在excel内确定三个输入参数:
1、word报告的标题。
2、零件的名称(对应仿真结果提取body1的名称)。
3、结果文件位置(仿真计算完成后默认路径是仿真计算文件中)。
点击“创建报告”按钮即可完成word 报告的自动创建。
操作说明:
1、 用户需要在excel中设定三个输入参数。
2、 本次示例需要在D盘设定test文件夹,其中包含word模板文件。
3、 生成的word报告文件是带有宏命令的docm文件,可以另存docx文件。
4、 生成的word报告存储在当前excel统计目录下。
附录:
Excel中的程序内容如下:
Sub harmonicReport()
titleName = Cells(7, 3).Value '读取表格数据,作为word报告的标题
partName = Cells(8, 3).Value '读取表格数据,作为word报告的零件名称
resultFileDir = Cells(10, 3).Value '读取表格数据,仿真计算结果的文件路径
wordTemplateDir = "D:\test\template\template.docm" 'word模板位置
wordReportDir = ThisWorkbook.Path & "\" & titleName & ".docm" 'word报告位置,当前excel同级
Dim wordFSO As Object
Dim wordReport As Object
Const forReading = 1, forWriting = 2, forAppending = 8, tristateFalse = 0
Set wordFSO = CreateObject("scripting.FileSystemObject")
wordFSO.Copyfile wordTemplateDir, wordReportDir, True '复制word 模板
Set appWD = CreateObject("word.application")
Set wordReport = appWD.Documents.Open(wordReportDir)
appWD.Visible = True
appWD.Run "wordReport", titleName, partName, resultFileDir '调用word程序,传递三个参数
appWD.Documents.Save
'appWD.Quit
End Sub
Word中的调用程序如下所示:
Sub wordReport(titleName, partName, resultFileDir)
'******************************************************************
'*****************************Word报告创建*************************
'******************************************************************
' 01 参数设定
Dim aRange As Range
resultTxtDir = resultFileDir & "\00_harmResultrecord.txt" '结果文件夹中的 txt文档
harmonicSumX = getContent(resultTxtDir, "谐响应应力计算结果", 2) '读取txt文档中的最大应力值和频率
picUseNum = 0
picName = resultFileDir & "\file" & Format(picUseNum, "000") & ".jpg" '结果文件夹中的 图片file000
' 02 创建文档标题和描述
Selection.EndKey Unit:=wdStory
Selection.InsertAfter titleName '仿真结果标题
Call formatTitle_B '转2级标题格式,并转接正文格式
Selection.InsertAfter "频响分析是一种研究结构或系统在受到谐波激励时的响应行为方法。**************"
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Call formatMain
' 03 创建结果统计表格
Selection.InsertAfter "下面给出计算结果示例:"
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Selection.InsertAfter titleName & "结果统计表"
Call formatChartList
'创建表格,2行、5列,列宽3.5
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Selection.Tables(1).Style = "网格型"
Selection.Tables(1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter '至中
Selection.Cells.PreferredWidth = CentimetersToPoints(3.5) '列宽3.5
Selection.Tables(1).Cell(1, 1).Range.Text = "序号"
Selection.Tables(1).Cell(1, 2).Range.Text = "方向"
Selection.Tables(1).Cell(1, 3).Range.Text = "零件名称"
Selection.Tables(1).Cell(1, 4).Range.Text = "响应频率Hz"
Selection.Tables(1).Cell(1, 5).Range.Text = "应力值MPa"
Selection.Tables(1).Cell(2, 1).Range.Text = "1"
Selection.Tables(1).Cell(2, 2).Range.Text = "/"
Selection.Tables(1).Cell(2, 3).Range.Text = partName
Selection.Tables(1).Cell(2, 4).Range.Text = CSng(Val(Split(Split(harmonicSumX, "最大,频率为")(1), "Hz")(0)))
Selection.Tables(1).Cell(2, 5).Range.Text = CSng(Val(Split(Split(harmonicSumX, "最大应力为")(1), "MPa")(0)))
Selection.EndKey Unit:=wdStory
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
' 04 创建结果应力响应图形
Selection.InsertAfter "下面给出计算结果应力曲线示例:"
Call formatMain
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
'表格中插入调整图片大小
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Set aRange = Selection.Range
Selection.InlineShapes.AddPicture fileName:=picName '插入图片
aRange.SetRange Start:=aRange.Start, End:=Selection.End
aRange.Select
With Selection.InlineShapes(1)
.LockAspectRatio = msoFalse '取消锁定图片纵横比
.Height = 7 * 28.3378 '设置高度为9cm,1cm = 28.3378 pt
.Width = 10 * 28.3378 '设置宽度为12cm
End With
Selection.EndKey Unit:=wdLine
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Selection.InsertAfter titleName & "应力曲线"
Call formatPictureList
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
End Sub
Function getContent(txtDir, keyWord, line) As String
'*************************读取txt关键字keyWord之后的第N行内容并返回****************
'*1.文档地址;2.所属关键字段;3.信息所在行数;return.返回行内容
Dim txtFSO As Object
Dim txtOpen As Object
Set txtFSO = CreateObject("scripting.FileSystemObject")
Set txtOpen = txtFSO.opentextfile(txtDir, 1, False)
For i = 1 To 300
temp = txtOpen.Readline
If temp = keyWord Then
For j = 1 To line
getContent = txtOpen.Readline
Next j
Exit For '退出大循环
End If
Next i
txtOpen.Close
Set txtFSO = Nothing
Set txtOpen = Nothing
End Function
Sub formatMain()
'************************“转图表格式,居中 小五,黑体”*******************
Selection.Style = ActiveDocument.Styles("正文")
Selection.Font.Name = "宋体"
Selection.Font.Size = 10.5
Selection.Font.Name = "Times New Roman"
With Selection.ParagraphFormat
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1.25)
.FirstLineIndent = CentimetersToPoints(0.35)
.CharacterUnitFirstLineIndent = 2
End With
End Sub
Sub formatTitle_B()
'************************文档格式控制——“二级目录标题”*******************new*********************************************new********************************
'*******************************************************“下一行起始正文,五号”
Selection.Style = ActiveDocument.Styles("标题B")
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Selection.Style = ActiveDocument.Styles("正文")
With Selection.ParagraphFormat
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1.25)
.FirstLineIndent = CentimetersToPoints(0.35)
.CharacterUnitFirstLineIndent = 2
End With
Selection.Font.Name = "宋体"
Selection.Font.Size = 10.5
Selection.Font.Name = "Times New Roman"
End Sub
Sub formatPictureList()
'************************“转图表格式,居中 小五,黑体”*******************
Selection.Style = ActiveDocument.Styles("图B")
Selection.Font.Name = "黑体"
Selection.Font.Size = 10
Selection.Font.Name = "Times New Roman"
With Selection.ParagraphFormat
.CharacterUnitFirstLineIndent = 0
.FirstLineIndent = CentimetersToPoints(0)
.Alignment = wdAlignParagraphCenter
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1.25)
End With
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Call formatMain
End Sub
Sub formatChartList()
'************************“转图表格式,居中 小五,黑体”*******************
Selection.Style = ActiveDocument.Styles("图B")
Selection.Font.Name = "黑体"
Selection.Font.Size = 10
Selection.Font.Name = "Times New Roman"
With Selection.ParagraphFormat
.CharacterUnitFirstLineIndent = 0
.FirstLineIndent = CentimetersToPoints(0)
.Alignment = wdAlignParagraphCenter
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1.25)
End With
Selection.InsertAfter Chr(13)
Selection.EndKey Unit:=wdStory
Call formatMain
End Sub
以下内容为付费内容,请购买后观看
包含4个文件
如有不当之处,请指正 1/excel示例文件; 2/word 模板文件; 3/说明文件; 注意:template.docm 需要放在D/test/template,路径下

工程师必备
- 项目客服
- 培训客服
- 平台客服
TOP
