【2023.11月更新】本站提供Autodesk Inventor 2015-2024免费版供大家试用,关注本站公众号(右侧),在 个性化-联系客服 中输入INVENTOR 可获取插件文件下载链接。或者访问www.lnv.cn获取内置型插件。
近期在修改一个Inventor插件时,有一个有趣的需求。就是有时候,我们在标注圆或者R角尺寸时,需要在尺寸的前面增加数量,比如类似标注“ 6- φ5 ”这样。
正常操作时,我们标注完尺寸后,需要再次双击该尺寸,然后到文本编辑里面去增加数量。当涉及大量这样的操作时,会觉得很累,且动作机械。
现在,我们来说说如何通过Autodesk Inventor API来实现快速修改标注尺寸。下面是摘自官方API帮助文件的一段修改尺寸标注的样例代码(VBA):
Public Sub EditDrawingDimensions()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
'Set a reference to the active sheet.
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
Dim counter As Long
counter = 1
Dim oDrawingDim As DrawingDimension
For Each oDrawingDim In oSheet.DrawingDimensions
' Add some formatted text to all dimensions on the sheet.
oDrawingDim.Text.FormattedText = " (Metric)"
counter = counter + 1
Next
' Set a reference to the first general dimension in the collection.
Dim oGeneralDim As GeneralDimension
Set oGeneralDim = oSheet.DrawingDimensions.GeneralDimensions.Item(1)
' Set a reference to the dimension style of that dimension.
Dim oDimStyle As DimensionStyle
Set oDimStyle = oGeneralDim.Style
' Modify some properties of the dimension style.
' This will modify all dimensions that use this style.
oDimStyle.LinearPrecision = kFourDecimalPlacesLinearPrecision
oDimStyle.AngularPrecision = kFourDecimalPlacesAngularPrecision
oDimStyle.LeadingZeroDisplay = False
oDimStyle.Tolerance.SetToSymmetric (0.02)
End Sub
这段VBA代码需要打开一张工程图,运行后,结果如下:
这个样例,通过修改标注样式来修改尺寸,一般不建议这么做,因为这样会更改工程图中所有尺寸。事实上,这也不是此次我们想要实现的效果。下面,我们通过稍微简单的代码来实现我们的需求。(Vb.net)
Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
If g_inventorApplication.Documents.Count = 0 Then
MsgBox("请打开零件文档")
Return
End If
Dim oDoc As Document = g_inventorApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("请打开工程图。")
Return
End If
Try
Dim inum As String = InputBox("输入数量")
Dim oDrawingDim As DrawingDimension
oDrawingDim = g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingDimensionFilter, "选择尺寸标注")
oDrawingDim.Text.FormattedText = inum & "-<DimensionValue/>"
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
运行这段代码后,首先会弹出输入框,提示你输入数量,确定后,再提醒你选择尺寸标注,选中尺寸标注后,就可以完成尺寸的快捷修改了。这里通过使用FormattedText对象,对标注文本进行修改,“<DimensionValue/>”是尺寸数值的占位符号,通过移动这个占位符可以自定义尺寸值的位置。
1