蒹葭苍苍,白露为霜。
所谓伊人,在水一方。

通过API修改Inventor工程图尺寸标注

【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/>”是尺寸数值的占位符号,通过移动这个占位符可以自定义尺寸值的位置。

赞(2) 打赏
未经允许不得转载:酷居科技 » 通过API修改Inventor工程图尺寸标注

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    1

    kuju4年前 (2019-12-25)回复

锦瑟无端五十弦,一弦一柱思华年

酷居科技联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏