Inventorbasic.vb 101.7 KB
Newer Older
leaky114's avatar
leaky114 已提交
1 2 3 4 5 6 7 8 9 10
Imports Inventor
Imports Inventor.SelectionFilterEnum
Imports Inventor.DocumentTypeEnum
Imports Inventor.DrawingViewTypeEnum
Imports Inventor.PropertyTypeEnum
Imports Inventor.BOMStructureEnum
Imports Inventor.IOMechanismEnum
Imports System.Windows.Forms
Imports Inventor.PrintOrientationEnum
Imports System.Text
leaky114's avatar
leaky114 已提交
11
Imports System.Collections.ObjectModel
leaky114's avatar
leaky114 已提交
12

13
Module InventorBasic
leaky114's avatar
leaky114 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    Public Structure StockNumPartName
        Dim IsGet As Boolean
        Dim StockNum As String
        Dim PartName As String
        Dim PartNum As String
    End Structure

    Public ThisApplication As Inventor.Application

    'Public ThisApprenticeApp As Inventor.ApprenticeServerComponent   '学徒服务器

    Public WithEvents ThisApplicationEvents As ApplicationEvents

    Public ClientID As String

    Public DWG As String = ".dwg"
    Public IAM As String = ".iam"
    Public IPT As String = ".ipt"
    Public IDW As String = ".idw"

    Public ContentCenterFiles As String  '零件库文件夹

    Public IsAutoSetPartName As Boolean  'true 为进行中,false则退出进程

    Public Map_StochNum As String   '映射图号
    Public Map_PartName As String   '映射文件名
    Public Map_PartNum As String    '映射存货编码

    Public Map_Mir_StochNum As String   '映射对称件图号
    Public Map_Mir_PartName As String   '映射对称件文件名

    Public Map_DrawingScale As String '映射比例
    Public Map_Mass As String '映射质量

    Public Map_PrintDay As String '映射打印时间
leaky114's avatar
leaky114 已提交
50 51
    Public IsOpenPrint As String    '设置打印时间后是否进入打印预览
    Public IsDayAndName As String   '同时签字
leaky114's avatar
leaky114 已提交
52 53 54 55 56 57 58 59

    Public EngineerName As String '工程师

    Public BOMTiTle As String       '导出BOM用的项目

    Public Mass_Accuracy As String '质量精度
    Public Area_Accuracy As String  '面积精度

leaky114's avatar
leaky114 已提交
60 61
    Public IsSetDrawingScale As String    '打开工程图时是否写 比例 到ipro   是赋值为1
    Public IsSetMass As String   '打开工程图时是否写 质量 到ipro  是赋值为1
leaky114's avatar
leaky114 已提交
62

leaky114's avatar
leaky114 已提交
63
    Public CheckUpdate As String    '启动检查更新
leaky114's avatar
leaky114 已提交
64

leaky114's avatar
leaky114 已提交
65 66 67 68 69
    Public TotalItem As Integer 'BOM序号

    Public OPosition(9) As Point   '点
    Public TempPoint(9) As SketchPoint   '临时绘制的点

leaky114's avatar
22.2.25  
leaky114 已提交
70 71 72
    Public IsShowUpdateMsg As Boolean    '检查更新时是否显示是最新版本的msgbox


leaky114's avatar
leaky114 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    '-------------------------------------------------------------------------------
    Public Sub SetStatusBarText(Optional ByVal StatusBarText As String = "就绪")
        ThisApplication.StatusBarText = StatusBarText
    End Sub

    'inventor是否打开文件,未打开文件返回false
    Public Function IsInventorOpenDoc() As Boolean
        Try
            If ThisApplication.FileManager.Files.Count = 0 Then
                MsgBox("未打开文件", MsgBoxStyle.Critical)
                Return False
                Exit Function
            Else
                Return True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function

    '更改零件/部件文件名
leaky114's avatar
leaky114 已提交
95
    Public Function RenameAssPartDocumentName(ByVal oInventorDocument As Inventor.Document, ByVal OldComponentOccurrence As ComponentOccurrence, ByVal NewFileName As String) As Boolean
leaky114's avatar
leaky114 已提交
96 97 98

        Dim OldFullFileName As String   '被替换的旧文件全名
        Dim OldFileName As String   '被替换的旧文件仅文件名
leaky114's avatar
leaky114 已提交
99
        OldFullFileName = OldComponentOccurrence.ReferencedDocumentDescriptor.FullDocumentName
leaky114's avatar
leaky114 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        OldFileName = GetFileNameInfo(OldFullFileName).ONlyName

        If IsFileExsts(OldFullFileName) = False Then
            MsgBox("文件: " & OldFullFileName & "不存在!", MsgBoxStyle.Critical, "修改文件名")
            Return True
            Exit Function
        End If

        'Dim oOccDef As PartComponentDefinition
        'oOccDef = OldOcc.Definition

        'If Not oOccDef.IsContentMember = False Then         '跳过零件库文件
        '    MsgBox(OldFullFileName & "为零件库文件", MsgBoxStyle.Information)
        '    'OldInventorDoc.Close()
        '    Return False
        '    Exit Function
        'End If

        If InStr(OldFullFileName, ContentCenterFiles) > 0 Then         '跳过零件库文件
            MsgBox("无法修改资源中心文件: " & OldFullFileName, MsgBoxStyle.Information, "修改文件名")
            'OldInventorDoc.Close()
            Return True
            Exit Function
        End If

leaky114's avatar
leaky114 已提交
125
        Select Case OldComponentOccurrence.DefinitionDocumentType
leaky114's avatar
leaky114 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            Case kPartDocumentObject, kAssemblyDocumentObject      '选择的是部件或零件
                Dim NewFullFileName As String   '新文件全名

                '新图号
                'frmain.Focus()

                '取消输入
                If NewFileName = "" Then
                    Return True
                    Exit Select
                End If

                '新旧文件名一致
                If OldFileName = NewFileName Then
                    MsgBox("未重新命名 ", MsgBoxStyle.Information, "修改文件名")
                    Return True
                    Exit Select
                End If

                '替换旧文件全名为新文件全名
                NewFullFileName = GetNewFileName(OldFullFileName, NewFileName)

                '检查新文件是否存在
                If IsFileExsts(NewFullFileName) = True Then
                    Select Case MsgBox("存在文件:" & NewFullFileName & vbCrLf & "是-直接替换" & vbCrLf & "否-重新生成替换" & vbCrLf & "取消-退出重新命名 ", MsgBoxStyle.Information + MsgBoxStyle.YesNoCancel)
                        Case MsgBoxResult.Yes   '直接用新文件替换
                            '全部替换为新文件
                            If MsgBox("是否替换全部零件?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.SystemModal) = MsgBoxResult.Yes Then
leaky114's avatar
leaky114 已提交
154
                                OldComponentOccurrence.Replace(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
155
                            Else
leaky114's avatar
leaky114 已提交
156
                                OldComponentOccurrence.Replace(NewFullFileName, False)
leaky114's avatar
leaky114 已提交
157 158 159 160 161 162 163 164 165 166
                            End If
                            Return True
                        Case MsgBoxResult.No    '重新另存为新文件,再替换

                        Case MsgBoxResult.Cancel    '取消退出
                            Return False
                    End Select
                End If

                '打开旧文件,不显示
leaky114's avatar
leaky114 已提交
167 168
                Dim OldInventorDocument As Inventor.Document
                OldInventorDocument = ThisApplication.Documents.Open(OldFullFileName, False)
leaky114's avatar
leaky114 已提交
169 170

                '另存为新文件
leaky114's avatar
leaky114 已提交
171
                OldInventorDocument.SaveAs(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
172 173

                '关闭旧图
leaky114's avatar
leaky114 已提交
174
                OldInventorDocument.Close()
leaky114's avatar
leaky114 已提交
175 176 177

                '全部替换为新文件
                If MsgBox("是否替换全部零件?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.SystemModal + MsgBoxStyle.DefaultButton1) = MsgBoxResult.Yes Then
leaky114's avatar
leaky114 已提交
178
                    OldComponentOccurrence.Replace(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
179
                Else
leaky114's avatar
leaky114 已提交
180
                    OldComponentOccurrence.Replace(NewFullFileName, False)
leaky114's avatar
leaky114 已提交
181 182 183 184
                End If

                ThisApplication.Documents.ItemByName(OldFullFileName).Close()
                '后台打开文件,修改ipro
leaky114's avatar
leaky114 已提交
185
                oInventorDocument = ThisApplication.Documents.Open(NewFullFileName, False)  '打开文件,不显示
leaky114's avatar
leaky114 已提交
186

leaky114's avatar
leaky114 已提交
187
                SetDocumentIpropertyFromFileName(oInventorDocument, True) '设置Iproperty,打开文件后需关闭
leaky114's avatar
leaky114 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

                Dim IsSaveAsOld As MsgBoxResult
                IsSaveAsOld = MsgBox("是否更改原文件为备份文件,扩展名增加 .old ?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "备份文件")

                '是否有对应的工程图文件,同时复制后修改文件名和模型链接
                Dim OldIdwFullFileName As String
                OldIdwFullFileName = GetNewExtensionFileName(OldFullFileName, ".idw")   '旧工程图

                Dim TempFullFileName As String       '更改旧模型文件的名字存档

                If IsFileExsts(OldIdwFullFileName) = True Then
                    Dim NewIdwFullFileName As String
                    NewIdwFullFileName = GetNewExtensionFileName(NewFullFileName, ".idw")   '新工程图
                    FileSystem.FileCopy(OldIdwFullFileName, NewIdwFullFileName)             '复制为新工程图

                    'MsgBox("找到有对应的旧工程图,生成新的工程图,将打开,请链接到文件:" & vbCrLf & NewFullFileName & vbCrLf & "该文件名已复制,粘贴到对话框即可。", MsgBoxStyle.Information)
                    'Windows.Forms.Clipboard.SetText(NewFullFileName)
                    'ThisApplication.Documents.Open(NewIdwFullFileName, False)      '打开新的工程图,使其手动链接零件或部件
                    'ThisApplication.Documents.ItemByName(NewIdwFullFileName).Save2() '保存链接并关闭工程图
                    'ThisApplication.Documents.ItemByName(NewIdwFullFileName).Close()

leaky114's avatar
leaky114 已提交
209 210 211 212
                    oInventorDocument = ThisApplication.Documents.Open(NewIdwFullFileName, False)  '打开文件,不显示
                    oInventorDocument.ReferencedDocumentDescriptors(1).ReferencedFileDescriptor.ReplaceReference(NewFullFileName)
                    oInventorDocument.Save2()
                    oInventorDocument.Close()
leaky114's avatar
leaky114 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

                    If IsSaveAsOld = MsgBoxResult.Yes Then
                        TempFullFileName = OldIdwFullFileName & ".old"   '暂时更改旧工程图文件的名字存档
                        ReFileName(OldIdwFullFileName, TempFullFileName)
                        'ReFileName(TempFullFileName, OldFullFileName)   '恢复旧零件或部件文件名
                    End If
                End If

                If IsSaveAsOld = MsgBoxResult.Yes Then
                    TempFullFileName = OldFullFileName & ".old"
                    ReFileName(OldFullFileName, TempFullFileName)
                End If
                Return True
            Case MsgBox("选择的文件不是零件或部件", MsgBoxStyle.Information)
                Return False
        End Select

    End Function

    '更改镜像零件文件名
leaky114's avatar
leaky114 已提交
233
    Public Function RenameMirrorAssPartDocumentName(ByVal oInventorDocument As Inventor.Document, ByVal OldComponentOccurrence As ComponentOccurrence, ByVal NewFileName As String) As Boolean
leaky114's avatar
leaky114 已提交
234 235
        Dim OldFullFileName As String   '被替换的旧文件全名
        Dim OldFileName As String   '被替换的旧文件仅文件名
leaky114's avatar
leaky114 已提交
236
        OldFullFileName = OldComponentOccurrence.ReferencedDocumentDescriptor.FullDocumentName
leaky114's avatar
leaky114 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        OldFileName = GetFileNameInfo(OldFullFileName).ONlyName

        If IsFileExsts(OldFullFileName) = False Then
            MsgBox(OldFullFileName & "不存在!", MsgBoxStyle.Critical)
            Return True
            Exit Function
        End If

        'Dim oOccDef As PartComponentDefinition
        'oOccDef = OldOcc.Definition

        'If Not oOccDef.IsContentMember = False Then         '跳过零件库文件
        '    MsgBox(OldFullFileName & "为零件库文件", MsgBoxStyle.Information)
        '    'OldInventorDoc.Close()
        '    Return False
        '    Exit Function
        'End If

        If InStr(OldFullFileName, ContentCenterFiles) > 0 Then         '跳过零件库文件
            MsgBox(OldFullFileName & "为零件库文件", MsgBoxStyle.Information)
            'OldInventorDoc.Close()
            Return True
            Exit Function
        End If

        'Select Case OldOcc.DefinitionDocumentType
        '    Case kPartDocumentObject, kAssemblyDocumentObject      '选择的是部件或零件
        Dim NewFullFileName As String   '新文件全名
        'Dim NewFileName As String   '新文件仅文件名
        '新图号
        'frmain.Focus()
        'NewFileName = InputBox("重命名 " & OldFullFileName, "重命名", OldFileName)  '输入新文件名

        ''取消输入
        'If NewFileName = "" Then
        '    Return True
        '    Exit Select
        'End If

        '替换旧文件全名为新文件全名
        NewFullFileName = GetNewFileName(OldFullFileName, NewFileName)

        '检查新文件是否存在
        If IsFileExsts(NewFullFileName) = True Then
            Select Case MsgBox("存在文件:" & NewFullFileName & " ,是-直接替换  否-重新生成替换  取消-退出重新命名 ", MsgBoxStyle.Information + MsgBoxStyle.YesNoCancel)
                Case MsgBoxResult.Yes   '直接用新文件替换
                    '全部替换为新文件
                    If MsgBox("是否替换全部零件?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.SystemModal) = MsgBoxResult.Yes Then
leaky114's avatar
leaky114 已提交
285
                        OldComponentOccurrence.Replace(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
286
                    Else
leaky114's avatar
leaky114 已提交
287
                        OldComponentOccurrence.Replace(NewFullFileName, False)
leaky114's avatar
leaky114 已提交
288 289 290 291 292 293 294 295 296 297 298
                    End If

                    Return True
                Case MsgBoxResult.No    '重新另存为新文件,再替换

                Case MsgBoxResult.Cancel    '取消退出
                    Return False
            End Select
        End If

        '打开旧文件,不显示
leaky114's avatar
leaky114 已提交
299 300
        Dim OldInventorDocument As Inventor.Document
        OldInventorDocument = ThisApplication.Documents.Open(OldFullFileName, False)
leaky114's avatar
leaky114 已提交
301 302 303 304

        '基础文件
        Dim ReferencedFullFileName As String
        Dim ReferencedFullFileNameTemp As String
leaky114's avatar
leaky114 已提交
305
        ReferencedFullFileName = OldInventorDocument.ReferencedDocuments(1).FullFileName
leaky114's avatar
leaky114 已提交
306 307 308 309 310 311
        ReferencedFullFileNameTemp = ReferencedFullFileName & ".old"

        '重命名基础文件
        ReFileName(ReferencedFullFileName, ReferencedFullFileNameTemp)

        '另存为新文件
leaky114's avatar
leaky114 已提交
312
        OldInventorDocument.SaveAs(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
313 314

        '关闭旧图
leaky114's avatar
leaky114 已提交
315
        OldInventorDocument.Close()
leaky114's avatar
leaky114 已提交
316 317 318 319 320

        '全部替换为新文件

        If MsgBox("是否替换全部零件?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.SystemModal) = MsgBoxResult.Yes Then
            MsgBox("选择 " & NewFullFileName & "  的基础文件!")
leaky114's avatar
leaky114 已提交
321
            OldComponentOccurrence.Replace(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
322 323
        Else
            MsgBox("选择 " & NewFullFileName & "  的基础文件!")
leaky114's avatar
leaky114 已提交
324
            OldComponentOccurrence.Replace(NewFullFileName, False)
leaky114's avatar
leaky114 已提交
325 326 327 328
        End If

        ThisApplication.Documents.ItemByName(OldFullFileName).Close()
        '后台打开文件,修改ipro
leaky114's avatar
leaky114 已提交
329
        oInventorDocument = ThisApplication.Documents.Open(NewFullFileName, False)  '打开文件,不显示
leaky114's avatar
leaky114 已提交
330

leaky114's avatar
leaky114 已提交
331
        SetDocumentIpropertyFromFileName(oInventorDocument, True) '设置Iproperty,打开文件后需关闭
leaky114's avatar
leaky114 已提交
332 333 334 335 336 337 338 339 340 341 342 343

        '还原早一个版本的文件()
        ReFileName(ReferencedFullFileNameTemp, ReferencedFullFileName)

        Return True
        '    Case MsgBox("选择的文件不是零件或部件", MsgBoxStyle.Information)
        'Return False
        'End Select

    End Function

    '根据文件名提取到iproperty  (  文件对象 ; 文件是否需打开,打开的文件用后要关闭)
leaky114's avatar
leaky114 已提交
344
    Public Function SetDocumentIpropertyFromFileName(ByVal oInventorDocument As Inventor.Document, ByVal IsNeedClose As Boolean) As Boolean
leaky114's avatar
leaky114 已提交
345 346 347
        Dim FullFileName As String      '当前文件全名
        Dim FileName As String

leaky114's avatar
leaky114 已提交
348
        FullFileName = oInventorDocument.FullFileName
leaky114's avatar
leaky114 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        FileName = GetFileNameInfo(FullFileName).ONlyName

        If InStr(FullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
            MsgBox("无法修改资源中心文件: " & FullFileName, MsgBoxStyle.Information, "修改iProperty")

            Return True
            Exit Function
        End If

        Dim StockNumPartName As StockNumPartName
        StockNumPartName = GetStockNumPartName(FullFileName)

        Dim oPropSets As PropertySets
        Dim oPropSet As PropertySet
        Dim propitem As [Property]
leaky114's avatar
leaky114 已提交
364
        oPropSets = oInventorDocument.PropertySets
leaky114's avatar
leaky114 已提交
365 366 367 368 369
        oPropSet = oPropSets.Item(3)

        For Each propitem In oPropSet    '设置iproperty
            Select Case propitem.DisplayName
                Case Map_PartName
leaky114's avatar
leaky114 已提交
370 371 372
                    If StockNumPartName.PartName <> "" Then
                        propitem.Value = StockNumPartName.PartName
                    End If
leaky114's avatar
leaky114 已提交
373
                Case Map_StochNum
leaky114's avatar
leaky114 已提交
374 375 376
                    If StockNumPartName.StockNum <> "" Then
                        propitem.Value = StockNumPartName.StockNum
                    End If
leaky114's avatar
leaky114 已提交
377
                Case Map_PartNum
leaky114's avatar
leaky114 已提交
378 379 380
                    If StockNumPartName.PartNum <> "" Then
                        propitem.Value = StockNumPartName.PartNum
                    End If
leaky114's avatar
leaky114 已提交
381 382 383 384
                Case "描述"
                    ' propitem.Value = ""
            End Select
        Next
385
999:
leaky114's avatar
leaky114 已提交
386 387
        '是否为打开的文件,是的话就关闭
        If IsNeedClose = True Then
leaky114's avatar
leaky114 已提交
388
            oInventorDocument.Close(True)
leaky114's avatar
leaky114 已提交
389 390 391 392
        End If
        Return True
    End Function

leaky114's avatar
leaky114 已提交
393 394
    '修改部件包含文件的iProperty   ( 部件文件对象 ; 文件是否需打开,打开的文件用后要关闭)
    Public Function SetDocumentsInAssIpropertyFromFileName(ByVal oAssemblyDocument As AssemblyDocument, ByVal IsNeedClose As Boolean) As Boolean
leaky114's avatar
21.12.3  
leaky114 已提交
395
        ' 获取所有引用文档
leaky114's avatar
leaky114 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

        Dim FirstLevelOnly As Boolean

        Select Case MsgBox("修改模式:是-仅修改第一级零件   否-修改所有级别零件 ", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel, "修改模式")
            Case MsgBoxResult.Yes
                'RefDocs = AsmDoc.ReferencedDocuments
                FirstLevelOnly = True
            Case MsgBoxResult.No
                'RefDocs = AsmDoc.AllReferencedDocuments
                FirstLevelOnly = False
            Case Else
                Return True
        End Select

        '==============================================================================================
        '基于bom结构化数据,可跳过参考的文件
        ' Set a reference to the BOM
        Dim oBOM As BOM
leaky114's avatar
leaky114 已提交
414
        oBOM = oAssemblyDocument.ComponentDefinition.BOM
leaky114's avatar
leaky114 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        oBOM.StructuredViewEnabled = True

        'Set a reference to the "Structured" BOMView
        Dim oBOMView As BOMView

        '获取结构化的bom页面
        For Each oBOMView In oBOM.BOMViews
            If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
                '遍历这个bom页面
                QueryBOMRowToSetiPro(oBOMView.BOMRows, FirstLevelOnly)
            End If
        Next
        '==============================================================================================
        Return True
    End Function

    '遍历BOM结构,查询row文件修改ipro
    Public Sub QueryBOMRowToSetiPro(ByVal oBOMRows As BOMRowsEnumerator, ByVal FirstLevelOnly As Boolean)
        Dim i As Integer

        Dim iStepCount As Short
        iStepCount = oBOMRows.Count

        'Create a new ProgressBar object.
        Dim oProgressBar As Inventor.ProgressBar

        oProgressBar = ThisApplication.CreateProgressBar(False, iStepCount, "当前文件: ")

        For i = 1 To oBOMRows.Count
            ' Get the current row.
leaky114's avatar
leaky114 已提交
445 446
            Dim oBOMRow As BOMRow
            oBOMRow = oBOMRows.Item(i)
leaky114's avatar
leaky114 已提交
447

leaky114's avatar
leaky114 已提交
448
            Dim oFullFileName As String
leaky114's avatar
leaky114 已提交
449

leaky114's avatar
leaky114 已提交
450
            oFullFileName = oBOMRow.ReferencedFileDescriptor.FullFileName
leaky114's avatar
leaky114 已提交
451 452

            '测试文件
leaky114's avatar
leaky114 已提交
453
            Debug.Print(oFullFileName)
leaky114's avatar
leaky114 已提交
454 455

            ' Set the message for the progress bar
leaky114's avatar
leaky114 已提交
456
            oProgressBar.Message = oFullFileName
leaky114's avatar
leaky114 已提交
457

leaky114's avatar
leaky114 已提交
458
            If IsFileExsts(oFullFileName) = False Then   '跳过不存在的文件
leaky114's avatar
leaky114 已提交
459 460 461
                GoTo 999
            End If

leaky114's avatar
leaky114 已提交
462
            If InStr(oFullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
leaky114's avatar
leaky114 已提交
463 464 465
                GoTo 999
            End If

leaky114's avatar
leaky114 已提交
466 467
            Dim oInventorDocument As Inventor.Document
            oInventorDocument = ThisApplication.Documents.Open(oFullFileName, False)  '打开文件,不显示
leaky114's avatar
leaky114 已提交
468

leaky114's avatar
leaky114 已提交
469
            SetDocumentIpropertyFromFileName(oInventorDocument, True) '设置Iproperty,打开文件后需关闭
leaky114's avatar
leaky114 已提交
470 471

            '遍历下一级
leaky114's avatar
leaky114 已提交
472 473
            If (Not oBOMRow.ChildRows Is Nothing) And FirstLevelOnly = False Then
                Call QueryBOMRowToSetiPro(oBOMRow.ChildRows, FirstLevelOnly)
leaky114's avatar
leaky114 已提交
474 475 476 477 478 479 480 481 482 483 484
            End If

999:
            oProgressBar.UpdateProgress()
        Next

        oProgressBar.Close()

    End Sub

    '自动生成零件图号(部件文件对象;进度条)
leaky114's avatar
leaky114 已提交
485
    Public Function AutoSetPartNumber(ByVal oAssemblyDocument As AssemblyDocument) As Boolean
leaky114's avatar
21.12.3  
leaky114 已提交
486
        'With ProgressBar
leaky114's avatar
leaky114 已提交
487 488 489 490 491 492 493 494 495
        '    .Minimum = 0
        '    '.Maximum = AsmDoc.ReferencedDocuments.Count
        '    .Value = 0
        'End With
        Dim FullFileName As String      '当前部件全名
        Dim FileName As String      '当前部件名
        Dim BasicNumber As String   '当前部件图号

        '部件全文件名 和 仅文件名
leaky114's avatar
leaky114 已提交
496
        FullFileName = oAssemblyDocument.FullFileName
leaky114's avatar
leaky114 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        FileName = GetFileNameInfo(FullFileName).ONlyName

        'Dim i As Integer
        'Dim s As String

        ''获取汉字的位置
        'i = 1
        'Do
        '    s = Mid(FileName, i, 1)
        '    i = i + 1
        '    If s = "" Then
        '        Exit Do
        '    End If
        'Loop Until (CheckCharType(s) = "Unicode字符")

        ''判断图号情况
        'Select Case True       '第一个字符为汉字
        '    Case i = 2
        '        BasicNumber = InputBox("部件 " & FullFileName & "  无图号,输入图号", "输入图号", "")
        '        If BasicNumber.ToString = "" Then
        '            Return False
        '            Exit Function
        '        End If
        '    Case s = ""  '无汉字
        '        BasicNumber = GetFileNameInfo(FullFileName).ONlyName
        '    Case Else       '正常情况
        '        BasicNumber = Left(FileName, i - 2)
        'End Select
        Dim StockNumPartName As StockNumPartName
        '获取图号和零件名
        StockNumPartName = GetStockNumPartName(FullFileName)
        '未获取图号,退出
        If StockNumPartName.IsGet = False Then
            Return False
        End If
        '基本图号
        BasicNumber = StockNumPartName.StockNum

        Dim PartNumberStep As Integer   '零件图号变化步长
        Dim AssNumberStep As Integer    '部件图号变化步长

        AssNumberStep = Val(InputBox("输入部件文件的编号变化,部件XXX-0000000 下第一个部件为XXX-0000100 则 输入 100 "))
        If AssNumberStep = 0 Then
            Return False
            Exit Function
        End If

        PartNumberStep = Val(InputBox("输入零件文件的编号变化,部件XXX-0000000 下第一个零件为XXX-0000001 则 输入 1 "))
        If PartNumberStep = 0 Then
            Return False
            Exit Function
        End If

        '重命名还是续命名
        Dim PartNumberItem As Integer     '第几个零件文件
        Dim AssNumberItem As Integer   '第几个部件文件

        If MsgBox("是否续编部件文件名?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            'Dim BasicOcc As ComponentOccurrence   '选择续编的的部件或零件
            'Dim BasicFullFileName As String   '续编的文档全名
            'Dim BasicFileName As String       '续编的文件名
            'BasicOcc =   ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "选择续编的部件文件")
            'BasicFullFileName = BasicOcc.ReferencedDocumentDescriptor.FullDocumentName
            'BasicFileName = GetStockNumPartName(BasicFullFileName).StockNum
            'AssNumberItem = (Val(BasicFileName) - Val(BasicNumber)) / AssNumberStep
            AssNumberItem = Val(InputBox("输入续编部件文件的文件名,部件XXX-0000200 则 输入 200 ", "续编部件", "")) / AssNumberStep

        Else
            '重0开始命名,不续编
            AssNumberItem = 0
        End If

        If MsgBox("是否续编零件文件名?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            'Dim BasicOcc As ComponentOccurrence   '选择续编的的部件或零件
            'Dim BasicFullFileName As String   '续编的文档全名
            'Dim BasicFileName As String       '续编的文件名
            'BasicOcc =   ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "选择续编的零件文件")
            'BasicFullFileName = BasicOcc.ReferencedDocumentDescriptor.FullDocumentName
            'BasicFileName = GetStockNumPartName(BasicFullFileName).StockNum
            'Dim a As Double = GetNumbers(BasicFileName)
            'Dim b As Double = GetNumbers(BasicNumber)
            'PartNumberItem = (a - b) / PartNumberStep
            PartNumberItem = Val(InputBox("输入续编零件文件的文件名,零件XXX-0000105 则 输入 5 ", "续编零件", "")) / PartNumberStep
        Else
            '重0开始命名,不续编
            PartNumberItem = 0
        End If

leaky114's avatar
leaky114 已提交
585
        Dim OldComponentOccurrence As ComponentOccurrence   '选择的部件或零件
leaky114's avatar
leaky114 已提交
586

leaky114's avatar
leaky114 已提交
587
        Dim OldInventorDocument As Document   '旧的文档
leaky114's avatar
leaky114 已提交
588 589 590 591
        Dim OldFullFileName As String   '旧的文档全名
        Dim OldFileName As String       '旧的文件名
        Dim OldFileInfo As FileNameInfo

leaky114's avatar
leaky114 已提交
592
        Dim NewInventorDocument As Document
leaky114's avatar
leaky114 已提交
593 594 595 596
        Dim NewFullFileName As String       '新的文档全名
        Dim NewFileName As String           '新的文档名
        Dim NewStockNum As String = Nothing      '新的图号

leaky114's avatar
21.12.3  
leaky114 已提交
597
        ' 获取所有引用文档
leaky114's avatar
leaky114 已提交
598 599
        Dim oDocumentsEnumerator As DocumentsEnumerator
        oDocumentsEnumerator = oAssemblyDocument.ReferencedDocuments
leaky114's avatar
leaky114 已提交
600

leaky114's avatar
21.12.3  
leaky114 已提交
601
        ' 遍历这些文档
leaky114's avatar
leaky114 已提交
602
        Do
leaky114's avatar
leaky114 已提交
603
            OldComponentOccurrence = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "选择要编号的文件")
leaky114's avatar
leaky114 已提交
604

leaky114's avatar
leaky114 已提交
605
            If OldComponentOccurrence Is Nothing Then       '取消选择
leaky114's avatar
leaky114 已提交
606 607 608
                Exit Do
            End If

leaky114's avatar
leaky114 已提交
609
            OldFullFileName = OldComponentOccurrence.ReferencedDocumentDescriptor.FullDocumentName      '旧文件全文件名
leaky114's avatar
leaky114 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
            OldFileInfo = GetFileNameInfo(OldFullFileName)
            OldFileName = OldFileInfo.ONlyName     '旧文件 仅文件名

            If IsFileExsts(OldFullFileName) = False Then   '跳过不存在的文件
                MsgBox(OldFullFileName & "不存在", MsgBoxStyle.Critical)
                GoTo 999
            End If

            If InStr(OldFullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
                MsgBox(OldFullFileName & "为零件库文件", MsgBoxStyle.Information)
                'OldInventorDoc.Close()
                GoTo 999
            End If

            '如果旧文件目录下有一个文件名相同的已有零件号的文件,是否替换或者重新命名当前文件
            For Each FoundFile As String In My.Computer.FileSystem.GetFiles(OldFileInfo.Folder, FileIO.SearchOption.SearchTopLevelOnly) ' OldFileInfo.ExtensionName)
                If InStr(GetFileNameInfo(FoundFile).SigleName, OldFileInfo.SigleName) > 1 Then  '存在一个已命名图号的文件
                    Select Case MsgBox("存在一个已命名图号的文件:" & FoundFile & " ,是-直接替换  否-重新生成替换 ", MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton1, "自动生成零件图号")
                        Case MsgBoxResult.Yes   '替换文件
leaky114's avatar
leaky114 已提交
629
                            OldComponentOccurrence.Replace(FoundFile, True)
leaky114's avatar
leaky114 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642
                            GoTo 999
                        Case MsgBoxResult.No    '重新命名

                    End Select
                    Exit For
                End If
            Next

            If IsAutoSetPartName = False Then
                Exit Do
            End If

            If CheckCharType(Strings.Left(OldFileName, 1)) = "Unicode字符" Then  '第一个为中文就编号
leaky114's avatar
leaky114 已提交
643
                Select Case OldComponentOccurrence.ReferencedDocumentDescriptor.ReferencedDocumentType      '零件和部件分别计数
leaky114's avatar
leaky114 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                    Case kPartDocumentObject     '零件
                        PartNumberItem = PartNumberItem + 1     '第几个文件
                        NewStockNum = Strings.Left(BasicNumber, (Strings.Len(BasicNumber)) - Len((PartNumberStep * PartNumberItem).ToString))   '获取不改变的前部分图号
                        NewStockNum = NewStockNum & (PartNumberStep * PartNumberItem).ToString    '获取全图号

                    Case kAssemblyDocumentObject    '部件
                        AssNumberItem = AssNumberItem + 1
                        NewStockNum = Strings.Left(BasicNumber, (Strings.Len(BasicNumber)) - Len((AssNumberStep * AssNumberItem).ToString))
                        NewStockNum = NewStockNum & (AssNumberStep * AssNumberItem).ToString
                    Case Else

                End Select

                NewFileName = NewStockNum & OldFileName     '获取全文件名
                NewFullFileName = GetNewFileName(OldFullFileName, NewFileName)  '替换旧文件全名为新文件全名

                '后台打开旧文件,另存为新文件
leaky114's avatar
leaky114 已提交
661 662 663
                OldInventorDocument = ThisApplication.Documents.Open(OldFullFileName, False)
                OldInventorDocument.SaveAs(NewFullFileName, True)  '另存为新图号文件
                OldInventorDocument.Close()
leaky114's avatar
leaky114 已提交
664 665

                '替换旧文件
leaky114's avatar
leaky114 已提交
666
                OldComponentOccurrence.Replace(NewFullFileName, True)
leaky114's avatar
leaky114 已提交
667 668

                '后台打开新文件,修改ipro
leaky114's avatar
leaky114 已提交
669 670
                NewInventorDocument = ThisApplication.Documents.Open(NewFullFileName, False)  '打开文件,不显示
                SetDocumentIpropertyFromFileName(NewInventorDocument, False) '设置Iproperty,打开文件后需关闭
leaky114's avatar
leaky114 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

                '是否有对应的工程图文件,同时复制后修改文件名和模型链接
                Dim OldIdwFullFileName As String
                OldIdwFullFileName = GetNewExtensionFileName(OldFullFileName, ".idw")   '旧工程图
                If IsFileExsts(OldIdwFullFileName) = True Then
                    Dim NewIdwFullFileName As String
                    NewIdwFullFileName = GetNewExtensionFileName(NewFullFileName, ".idw")   '新工程图
                    FileSystem.FileCopy(OldIdwFullFileName, NewIdwFullFileName)             '复制为新工程图

                    Dim TempFullFileName As String       '暂时更改旧文件名字
                    TempFullFileName = OldFullFileName & ".old"
                    ReFileName(OldFullFileName, TempFullFileName)
                    MsgBox("找到有对应的旧工程图,生成新的工程图,将打开,请链接到文件:" & vbCrLf & NewFullFileName & vbCrLf & "该文件名已复制,粘贴到对话框即可。", MsgBoxStyle.Information)
                    Windows.Forms.Clipboard.SetText(NewFullFileName)
                    ThisApplication.Documents.Open(NewIdwFullFileName, False)      '打开新的工程图,使其手动链接零件或部件
                    ThisApplication.Documents.ItemByName(NewIdwFullFileName).Save2() '保存链接并关闭工程图
                    ThisApplication.Documents.ItemByName(NewIdwFullFileName).Close()

                    'ReFileName(TempFullFileName, OldFullFileName)   '恢复旧零件或部件文件名

                End If

            Else
                MsgBox(OldFullFileName & "可能已有图号", MsgBoxStyle.Information)
            End If
999:
            'TSProgressBar.Value = TSProgressBar.Value + 1
        Loop While True
        Return True
    End Function

    '另存为dwg
leaky114's avatar
leaky114 已提交
703 704
    Public Function SaveAsDwg(ByVal oDrawingDocument As Inventor.DrawingDocument) As String
        Dim IdwFullFileName As String         '工程图全文件名
leaky114's avatar
leaky114 已提交
705 706
        Dim DwgFullFileName As String        'cad 文件全文件名

leaky114's avatar
leaky114 已提交
707
        IdwFullFileName = oDrawingDocument.FullFileName
leaky114's avatar
leaky114 已提交
708 709 710 711 712 713 714 715 716 717 718 719
        DwgFullFileName = Strings.Replace(IdwFullFileName, LCaseGetFileExtension(IdwFullFileName), ".dwg")

        If IsFileExsts(DwgFullFileName) Then
            Dim msg As MsgBoxResult = MsgBox("已存在文件: " & DwgFullFileName & "  是否覆盖(是)或另存为(否)?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel, "另存为Dwg")
            Select Case msg
                Case MsgBoxResult.Yes

                Case MsgBoxResult.No
                    Dim ofd As New SaveFileDialog
                    With ofd
                        .Title = "选择 dwg 文件"
                        .Filter = "AutoCAD文件(*.dwg)|*.dwg"
leaky114's avatar
leaky114 已提交
720
                        .InitialDirectory = GetParentFolder(oDrawingDocument.FullDocumentName)
leaky114's avatar
leaky114 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
                        If .ShowDialog = DialogResult.OK Then
                            DwgFullFileName = .FileName
                        Else
                            Return "取消"
                        End If
                    End With
                Case MsgBoxResult.Cancel
                    Return "取消"
            End Select
        End If

        'IdwDoc.SaveAs(DwgFullFileName, True)

        'If IsFileExsts(DwgFullFileName) = False Then
        '    DwgFullFileName = Strings.Replace(DwgFullFileName, ".dwg", ".zip")
        'End If

        ' 获取对应的Translator.
leaky114's avatar
leaky114 已提交
739 740
        Dim oTranslatorAddIn As TranslatorAddIn
        oTranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
leaky114's avatar
leaky114 已提交
741 742 743

        ' 获取当前零件或装配文档.

leaky114's avatar
leaky114 已提交
744 745
        Dim oTransientObjects As TransientObjects
        oTransientObjects = ThisApplication.TransientObjects
leaky114's avatar
leaky114 已提交
746 747

        ' 设置导出文件
leaky114's avatar
leaky114 已提交
748 749 750
        Dim oTranslationContext As TranslationContext
        oTranslationContext = oTransientObjects.CreateTranslationContext
        oTranslationContext.Type = kFileBrowseIOMechanism
leaky114's avatar
leaky114 已提交
751 752 753

        ' 获取可操作的选项
        Dim options As NameValueMap
leaky114's avatar
leaky114 已提交
754 755
        options = oTransientObjects.CreateNameValueMap
        If oTranslatorAddIn.HasSaveCopyAsOptions(oDrawingDocument, oTranslationContext, options) Then
leaky114's avatar
leaky114 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            ' 设置导出样式.
            options.Value("Solid") = True      ' 导出 solids.
            options.Value("Surface") = False   ' 导出 surfaces.
            options.Value("Sketch") = False    ' 导出 sketches.

            ' 设置导出DWG的版本.
            ' 23 = ACAD 2000
            ' 25 = ACAD 2004
            ' 27 = ACAD 2007
            ' 29 = ACAD 2010
            options.Value("DwgVersion") = 23
        End If

        ' 设置导出文件名.
        Dim oDataMedium As DataMedium
leaky114's avatar
leaky114 已提交
771
        oDataMedium = oTransientObjects.CreateDataMedium
leaky114's avatar
leaky114 已提交
772 773 774
        oDataMedium.FileName = DwgFullFileName

        ' 调用SaveCopyAs
leaky114's avatar
leaky114 已提交
775
        Call oTranslatorAddIn.SaveCopyAs(oDrawingDocument, oTranslationContext, options, oDataMedium)
leaky114's avatar
leaky114 已提交
776 777 778 779 780 781

        Return DwgFullFileName

    End Function

    '另存为pdf
leaky114's avatar
leaky114 已提交
782
    Public Function SaveAsPdf(ByVal InventorDocument As Inventor.Document) As String
leaky114's avatar
leaky114 已提交
783 784 785 786

        Dim InventorFullFileName As String  '工程图全文件名
        Dim PdfFullFileName As String        'pdf 文件全文件名

leaky114's avatar
leaky114 已提交
787
        InventorFullFileName = InventorDocument.FullFileName
leaky114's avatar
leaky114 已提交
788 789 790 791 792 793 794 795 796 797 798 799
        PdfFullFileName = Strings.Replace(InventorFullFileName, LCaseGetFileExtension(InventorFullFileName), ".pdf")

        If IsFileExsts(PdfFullFileName) Then
            Dim msg As MsgBoxResult = MsgBox("已存在文件: " & PdfFullFileName & "  是否覆盖(是)或另存为(否)?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel, "另存为Dwg")
            Select Case msg
                Case MsgBoxResult.Yes

                Case MsgBoxResult.No
                    Dim ofd As New SaveFileDialog
                    With ofd
                        .Title = "选择 Pdf 文件"
                        .Filter = "Adobe PDF文件(*.pdf)|*.pdf"
leaky114's avatar
leaky114 已提交
800
                        .InitialDirectory = GetParentFolder(InventorDocument.FullDocumentName)
leaky114's avatar
leaky114 已提交
801 802 803 804 805 806 807 808 809 810 811
                        If .ShowDialog = DialogResult.OK Then
                            PdfFullFileName = .FileName
                        Else
                            Return "取消"
                        End If
                    End With
                Case MsgBoxResult.Cancel
                    Return "取消"
            End Select
        End If

leaky114's avatar
leaky114 已提交
812
        InventorDocument.SaveAs(PdfFullFileName, True)
leaky114's avatar
leaky114 已提交
813 814 815 816 817 818

        Return PdfFullFileName

    End Function

    '设置工程图自定义比例
leaky114's avatar
leaky114 已提交
819
    Public Function SetDrawingScale(ByVal oDrawingDocument As DrawingDocument) As Boolean
leaky114's avatar
leaky114 已提交
820 821

        Dim oView As DrawingView
leaky114's avatar
leaky114 已提交
822
        For Each oView In oDrawingDocument.Sheets(1).DrawingViews
leaky114's avatar
leaky114 已提交
823 824 825 826 827 828 829 830 831 832 833
            If GetViewType(oView) = "主视图" Then
                'View.Scale.ToString()
                Dim oPropertyName As String
                oPropertyName = Map_DrawingScale

                Dim StrScale As String
                StrScale = oView.ScaleString

                Dim pEachScale As [Property]

                Try
leaky114's avatar
21.12.3  
leaky114 已提交
834
                    '若该iProperty已经存在,则直接修改其值
leaky114's avatar
leaky114 已提交
835
                    pEachScale = oDrawingDocument.PropertySets.Item("User Defined Properties").Item(oPropertyName)
leaky114's avatar
leaky114 已提交
836 837 838
                    pEachScale.Value = StrScale

                Catch
leaky114's avatar
21.12.3  
leaky114 已提交
839
                    ' 若该iProperty不存在,则添加一个
leaky114's avatar
leaky114 已提交
840
                    oDrawingDocument.PropertySets.Item("User Defined Properties").Add(StrScale, oPropertyName)
leaky114's avatar
leaky114 已提交
841 842
                End Try

leaky114's avatar
leaky114 已提交
843
                oDrawingDocument.Update()   '刷新数据
leaky114's avatar
leaky114 已提交
844 845 846 847 848 849 850 851 852

                Return True
            End If
        Next

        Return False
    End Function

    '获取零部件质量
leaky114's avatar
leaky114 已提交
853
    Public Function GetMass(ByVal oInventorDocument As Inventor.Document) As Double
leaky114's avatar
leaky114 已提交
854
        Dim valMass As Double
leaky114's avatar
leaky114 已提交
855
        If oInventorDocument.DocumentType = kPartDocumentObject Then
leaky114's avatar
leaky114 已提交
856
            Dim IptDoc As PartDocument
leaky114's avatar
leaky114 已提交
857
            IptDoc = oInventorDocument
leaky114's avatar
leaky114 已提交
858
            valMass = IptDoc.ComponentDefinition.MassProperties.Mass
leaky114's avatar
leaky114 已提交
859
        ElseIf oInventorDocument.DocumentType = kAssemblyDocumentObject Then
leaky114's avatar
leaky114 已提交
860
            Dim AsmDoc As AssemblyDocument
leaky114's avatar
leaky114 已提交
861
            AsmDoc = oInventorDocument
leaky114's avatar
leaky114 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
            valMass = AsmDoc.ComponentDefinition.MassProperties.Mass
        Else
            valMass = 0
        End If

        valMass = valMass + 0.00000001

        Dim Val_Mass_Accuracy As Integer
        Val_Mass_Accuracy = Val(Mass_Accuracy)
        valMass = Math.Round(valMass, Val_Mass_Accuracy)

        Return valMass
    End Function

    '获取零部件面积
leaky114's avatar
leaky114 已提交
877
    Public Function GetArea(ByVal oInventorDocument As Inventor.Document) As Double
leaky114's avatar
leaky114 已提交
878
        Dim valArea As Double
leaky114's avatar
leaky114 已提交
879
        If oInventorDocument.DocumentType = kPartDocumentObject Then
leaky114's avatar
leaky114 已提交
880
            Dim IptDoc As PartDocument
leaky114's avatar
leaky114 已提交
881
            IptDoc = oInventorDocument
leaky114's avatar
leaky114 已提交
882
            valArea = IptDoc.ComponentDefinition.MassProperties.Area / 10 ^ 4
leaky114's avatar
leaky114 已提交
883
        ElseIf oInventorDocument.DocumentType = kAssemblyDocumentObject Then
leaky114's avatar
leaky114 已提交
884
            Dim AsmDoc As AssemblyDocument
leaky114's avatar
leaky114 已提交
885
            AsmDoc = oInventorDocument
leaky114's avatar
leaky114 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
            valArea = AsmDoc.ComponentDefinition.MassProperties.Area / 10 ^ 4
        Else
            valArea = 0
        End If

        valArea = valArea + 0.00000001

        Dim Val_Area_Accuracy As Integer
        Val_Area_Accuracy = Val(Area_Accuracy)
        valArea = Math.Round(valArea, Val_Area_Accuracy)

        Return valArea
    End Function

    '设置工程图自定义质量
leaky114's avatar
leaky114 已提交
901
    Public Function SetMass(ByVal oDrawingDocument As DrawingDocument) As Boolean
leaky114's avatar
leaky114 已提交
902 903 904 905 906 907 908
        Dim oPropertyName As String
        oPropertyName = "质量"

        Dim InventorDoc As Inventor.Document

        Dim valMass As Double = 0
        Dim TempdoubleMass As Double = 0
leaky114's avatar
leaky114 已提交
909
        For Each InventorDoc In oDrawingDocument.ReferencedDocuments
leaky114's avatar
leaky114 已提交
910 911 912 913 914 915 916 917 918 919 920 921
            TempdoubleMass = GetMass(InventorDoc)
            If TempdoubleMass > valMass Then
                valMass = TempdoubleMass
            End If
        Next

        Dim strMass As String
        strMass = valMass.ToString

        Dim pEachScale As [Property]

        Try
leaky114's avatar
21.12.3  
leaky114 已提交
922
            '若该iProperty已经存在,则直接修改其值
leaky114's avatar
leaky114 已提交
923
            pEachScale = oDrawingDocument.PropertySets.Item("User Defined Properties").Item(oPropertyName)
leaky114's avatar
leaky114 已提交
924 925 926
            pEachScale.Value = strMass

        Catch
leaky114's avatar
21.12.3  
leaky114 已提交
927
            ' 若该iProperty不存在,则添加一个
leaky114's avatar
leaky114 已提交
928
            oDrawingDocument.PropertySets.Item("User Defined Properties").Add(strMass, oPropertyName)
leaky114's avatar
leaky114 已提交
929 930
        End Try

leaky114's avatar
leaky114 已提交
931
        oDrawingDocument.Update()   '刷新数据
leaky114's avatar
leaky114 已提交
932 933 934 935 936 937

        Return True

    End Function

    '获取视图类型
leaky114's avatar
leaky114 已提交
938
    Public Function GetViewType(ByVal oDrawingView As DrawingView) As String
leaky114's avatar
leaky114 已提交
939
        '遍历每个视图
leaky114's avatar
leaky114 已提交
940
        Select Case (oDrawingView.ViewType)
leaky114's avatar
leaky114 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
            Case kStandardDrawingViewType
                Return ("主视图")
            Case kAssociativeDraftDrawingViewType
                Return ("关联草图视图")
            Case kAuxiliaryDrawingViewType
                Return ("辅助视图")
            Case kCustomDrawingViewType
                Return ("自定义视图")
            Case kDefaultDrawingViewType
                Return ("缺省视图")
            Case kDetailDrawingViewType
                Return ("详细视图")
            Case kDraftDrawingViewType
                Return ("草图视图")
            Case kOLEAttachmentDrawingViewType
                Return ("OLE附着视图")
            Case kOverlayDrawingViewType
                Return ("覆盖视图")
            Case kProjectedDrawingViewType
                Return ("投影视图")
            Case kSectionDrawingViewType
                Return ("局部视图")
        End Select

        Return "无法识别"
    End Function

    '设置工程图自定义属性:对称件IPro
leaky114's avatar
leaky114 已提交
969
    Public Function SetDrawingMirPartIPro(ByVal oDrawingDocument As DrawingDocument) As Boolean
leaky114's avatar
leaky114 已提交
970
        Dim oSheet As Sheet
leaky114's avatar
leaky114 已提交
971
        oSheet = oDrawingDocument.ActiveSheet
leaky114's avatar
leaky114 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

        Dim oView As DrawingView
        oView = oSheet.DrawingViews.Item(1)

        Dim oRef As DocumentDescriptor
        oRef = oView.ReferencedDocumentDescriptor

        '获取本零件文件夹路径
        Dim MirFile_FullFileName As String
        Dim ofd As New OpenFileDialog
        With ofd
            .Multiselect = False
            .Title = "选择 ipt iam 文件"
            .Filter = "Inventor 文件(*.ipt;*.iam)|*.ipt;*.iam|Inventor 零件(*.ipt)|*.ipt|Inventor 部件(*.iam)|*.iam"
            .InitialDirectory = GetParentFolder(oRef.FullDocumentName)
            If .ShowDialog = DialogResult.OK Then
                MirFile_FullFileName = .FileName
            Else
                Return True
                Exit Function
            End If
        End With

        '获取镜像零件ipro
        Dim StockNumPartName As StockNumPartName
        StockNumPartName = GetStockNumPartName(MirFile_FullFileName)

        '设置ipro
        Dim pEachScale As [Property]
        Try
leaky114's avatar
21.12.3  
leaky114 已提交
1002
            '若该iProperty已经存在,则直接修改其值
leaky114's avatar
leaky114 已提交
1003
            pEachScale = oDrawingDocument.PropertySets.Item("User Defined Properties").Item(Map_Mir_StochNum)
leaky114's avatar
leaky114 已提交
1004 1005
            pEachScale.Value = StockNumPartName.StockNum

leaky114's avatar
leaky114 已提交
1006
            pEachScale = oDrawingDocument.PropertySets.Item("User Defined Properties").Item(Map_Mir_PartName)
leaky114's avatar
leaky114 已提交
1007 1008 1009
            pEachScale.Value = StockNumPartName.PartName

        Catch
leaky114's avatar
21.12.3  
leaky114 已提交
1010
            ' 若该iProperty不存在,则添加一个
leaky114's avatar
leaky114 已提交
1011 1012
            oDrawingDocument.PropertySets.Item("User Defined Properties").Add(StockNumPartName.StockNum, Map_Mir_StochNum)
            oDrawingDocument.PropertySets.Item("User Defined Properties").Add(StockNumPartName.PartName, Map_Mir_PartName)
leaky114's avatar
leaky114 已提交
1013 1014
        End Try

leaky114's avatar
leaky114 已提交
1015
        oDrawingDocument.Update()   '刷新数据
leaky114's avatar
leaky114 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

        Return True

    End Function

    '设置打印时间
    'Public Function SetPrintTime(ByVal IdwDoc As DrawingDocument, ByVal AddTime As Short) As Boolean
    '    Dim pEachScale As [Property]

    '    Dim Print_Day As String

    '    Print_Day = " "
    '    Select Case AddTime
    '        Case 0    '清除数据改为空白

    '        Case 1   '当前日写数据
    '            Print_Day = Today.Year & "." & Today.Month & "." & Today.Day
    '        Case 2      '自定义日期写数据
    '            Print_Day = Today.Year & "." & Today.Month & "." & Today.Day
    '            Print_Day = InputBox("输入日期", "日期", Print_Day)
    '    End Select

    '    Try
leaky114's avatar
21.12.3  
leaky114 已提交
1039
    '        '若该iProperty已经存在,则直接修改其值
leaky114's avatar
leaky114 已提交
1040 1041 1042 1043

    '        pEachScale = IdwDoc.PropertySets.Item("User Defined Properties").Item(Map_PrintDay)
    '        pEachScale.Value = Print_Day
    '    Catch
leaky114's avatar
21.12.3  
leaky114 已提交
1044
    '        ' 若该iProperty不存在,则添加一个
leaky114's avatar
leaky114 已提交
1045 1046 1047 1048 1049 1050 1051
    '        IdwDoc.PropertySets.Item("User Defined Properties").Add(Print_Day, Map_PrintDay)
    '    End Try
    '    IdwDoc.Update()   '刷新数据
    '    Return True
    'End Function

    '设置签字
leaky114's avatar
leaky114 已提交
1052
    Public Function SetSign(ByVal oDrawingDocument As DrawingDocument, ByVal EngineerName As String, ByVal Print_Day As String, ByVal IsOPenPrintDialog As Boolean) As Boolean
leaky114's avatar
leaky114 已提交
1053 1054 1055 1056
        Dim oPropSets As PropertySets
        Dim oPropSet As PropertySet
        Dim propitem As [Property]

leaky114's avatar
leaky114 已提交
1057
        oPropSets = oDrawingDocument.PropertySets
leaky114's avatar
leaky114 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
        oPropSet = oPropSets.Item(3)

        For Each propitem In oPropSet   '设置iproperty
            Select Case propitem.DisplayName
                Case "工程师"
                    propitem.Value = EngineerName
            End Select
        Next

        Dim pEachScale As [Property]

        Try
leaky114's avatar
21.12.3  
leaky114 已提交
1070
            '若该iProperty已经存在,则直接修改其值
leaky114's avatar
leaky114 已提交
1071
            pEachScale = oDrawingDocument.PropertySets.Item("User Defined Properties").Item(Map_PrintDay)
leaky114's avatar
leaky114 已提交
1072 1073
            pEachScale.Value = Print_Day
        Catch
leaky114's avatar
21.12.3  
leaky114 已提交
1074
            ' 若该iProperty不存在,则添加一个
leaky114's avatar
leaky114 已提交
1075
            oDrawingDocument.PropertySets.Item("User Defined Properties").Add(Print_Day, Map_PrintDay)
leaky114's avatar
leaky114 已提交
1076 1077
        End Try

leaky114's avatar
leaky114 已提交
1078
        oDrawingDocument.Update()   '刷新数据
leaky114's avatar
leaky114 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

        '打开打印窗口()
        If IsOpenPrint = 1 And IsOPenPrintDialog = True Then
            Dim oCommmandbars As CommandControl
            For Each oCommmandbars In ThisApplication.UserInterfaceManager.FileBrowserControls
                If oCommmandbars.DisplayName = "打印" Then
                    Dim oCommandbarPrint As CommandControl
                    For Each oCommandbarPrint In oCommmandbars.ChildControls
                        If oCommandbarPrint.DisplayName = "打印" Then
                            oCommandbarPrint.ControlDefinition.Execute2(True)
                        End If
                    Next
                    Exit For
                End If
            Next
        End If

        Return True

    End Function

    '获取单个描述
leaky114's avatar
leaky114 已提交
1101
    Public Function GetPropitem(ByVal oInventorDocument As Inventor.Document, ByVal propitemName As String) As String
leaky114's avatar
leaky114 已提交
1102 1103 1104 1105
        Dim oPropSets As PropertySets
        Dim oPropSet As PropertySet
        Dim propitem As [Property]

leaky114's avatar
leaky114 已提交
1106
        oPropSets = oInventorDocument.PropertySets
leaky114's avatar
leaky114 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
        oPropSet = oPropSets.Item(3)

        '获取iproperty
        Dim StockNumPartName As StockNumPartName = Nothing
        For Each propitem In oPropSet
            Select Case propitem.DisplayName
                Case propitemName
                    Return propitem.Value
            End Select
        Next

leaky114's avatar
leaky114 已提交
1118
        oInventorDocument.Update()   '刷新数据
leaky114's avatar
leaky114 已提交
1119 1120 1121 1122 1123 1124

        Return True

    End Function

    '设置单个propitem
leaky114's avatar
leaky114 已提交
1125
    Public Function SetPropitem(ByVal oInventorDocument As Inventor.Document, ByVal propitemName As String, ByVal propitemValue As String) As Boolean
leaky114's avatar
leaky114 已提交
1126 1127 1128 1129
        Dim oPropSets As PropertySets
        Dim oPropSet As PropertySet
        Dim propitem As [Property]

leaky114's avatar
leaky114 已提交
1130
        oPropSets = oInventorDocument.PropertySets
leaky114's avatar
leaky114 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
        oPropSet = oPropSets.Item(3)

        '获取iproperty
        Dim StockNumPartName As StockNumPartName = Nothing
        For Each propitem In oPropSet
            Select Case propitem.DisplayName
                Case propitemName
                    propitem.Value = propitemValue
            End Select
        Next

leaky114's avatar
leaky114 已提交
1142
        oInventorDocument.Update()   '刷新数据
leaky114's avatar
leaky114 已提交
1143 1144 1145 1146 1147

        Return True

    End Function

leaky114's avatar
leaky114 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    '获取 StockNumPartName
    Public Function GetPropitems(ByVal oInventorDocument As Inventor.Document) As StockNumPartName
        Dim oPropSets As PropertySets
        Dim oPropSet As PropertySet
        Dim propitem As [Property]

        oPropSets = oInventorDocument.PropertySets
        oPropSet = oPropSets.Item(3)

        Dim oStockNumPartName As StockNumPartName = Nothing

        For Each propitem In oPropSet
            Select Case propitem.DisplayName
                Case Map_PartName
                    oStockNumPartName.PartName = propitem.Value
                Case Map_StochNum
                    oStockNumPartName.StockNum = propitem.Value
                Case Map_PartNum
                    oStockNumPartName.PartNum = propitem.Value
            End Select
        Next

        oInventorDocument.Update()   '刷新数据

        Return oStockNumPartName

    End Function

    '设置 StockNumPartName
    Public Function SetPropitems(ByVal oInventorDocument As Inventor.Document, ByVal oStockNumPartName As StockNumPartName) As Boolean
        Dim oPropSets As PropertySets
        Dim oPropSet As PropertySet
        Dim propitem As [Property]

        oPropSets = oInventorDocument.PropertySets
        oPropSet = oPropSets.Item(3)


        For Each propitem In oPropSet
            Select Case propitem.DisplayName
                Case Map_PartName
                    propitem.Value = oStockNumPartName.PartName
                Case Map_StochNum
                    propitem.Value = oStockNumPartName.StockNum
                Case Map_PartNum
                    propitem.Value = oStockNumPartName.PartNum
            End Select
        Next

        oInventorDocument.Update()   '刷新数据

        Return True

    End Function



leaky114's avatar
21.12.3  
leaky114 已提交
1205
    '提取iproperty更改文件名
leaky114's avatar
leaky114 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    Public Function GetIpropertyToRename(ByVal InventorDoc As Inventor.Document, ByVal OldOcc As ComponentOccurrence) As Boolean
        Dim OldFullFileName As String   '被替换的旧文件全名
        Dim OldFileName As String   '被替换的旧文件仅文件名
        OldFullFileName = OldOcc.ReferencedDocumentDescriptor.FullDocumentName
        OldFileName = GetFileNameInfo(OldFullFileName).ONlyName

        If IsFileExsts(OldFullFileName) = False Then
            MsgBox("文件: " & OldFullFileName & "不存在!", MsgBoxStyle.Critical, "修改文件名")
            Return True
            Exit Function
        End If

        If InStr(OldFullFileName, ContentCenterFiles) > 0 Then         '跳过零件库文件
            MsgBox("无法修改资源中心文件: " & OldFullFileName, MsgBoxStyle.Information, "修改文件名")
            'OldInventorDoc.Close()
            Return True
            Exit Function
        End If

        Select Case OldOcc.DefinitionDocumentType
            Case kPartDocumentObject, kAssemblyDocumentObject      '选择的是部件或零件
                Dim NewFullFileName As String   '新文件全名
                Dim NewFileName As String   '新文件仅文件名
                '新图号
                'frmain.Focus()
                '打开旧文件,不显示
                Dim OldInventorDoc As Document
                OldInventorDoc = ThisApplication.Documents.Open(OldFullFileName, False)

                Dim oPropSets As PropertySets
                Dim oPropSet As PropertySet
                Dim propitem As [Property]

                oPropSets = OldInventorDoc.PropertySets
                oPropSet = oPropSets.Item(3)

                '获取iproperty
                Dim StockNumPartName As StockNumPartName = Nothing
                For Each propitem In oPropSet
                    Select Case propitem.DisplayName
                        Case Map_PartName
                            StockNumPartName.PartName = propitem.Value
                        Case Map_StochNum
                            StockNumPartName.StockNum = propitem.Value
                        Case "描述"
                            ' propitem.Value = ""
                    End Select
                Next

                '新文件名
                NewFileName = StockNumPartName.StockNum & StockNumPartName.PartName

                '替换旧文件全名为新文件全名
                NewFullFileName = GetNewFileName(OldFullFileName, NewFileName)

                If NewFullFileName = OldFullFileName Then
                    MsgBox("iProperty与文件名匹配,无需重命名文件!", MsgBoxStyle.Information)
                    '关闭旧图,不保存
                    OldInventorDoc.Close(True)
                    Return True
                End If

                '检查新文件是否存在
                If IsFileExsts(NewFullFileName) = True Then
                    Select Case MsgBox("存在文件:" & NewFullFileName & " ,是-直接替换  否-重新生成替换  取消-退出重新命名 ", MsgBoxStyle.Information + MsgBoxStyle.YesNoCancel)
                        Case MsgBoxResult.Yes   '直接用新文件替换
                            '全部替换为新文件
                            'If MsgBox("是否替换全部零件?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.SystemModal) = MsgBoxResult.Yes Then
                            OldOcc.Replace(NewFullFileName, True)
                            'Else
                            'OldOcc.Replace(NewFullFileName, False)
                            'End If
                            OldInventorDoc.Close(True)
                            Return True
                        Case MsgBoxResult.No    '重新另存为新文件,再替换

                        Case MsgBoxResult.Cancel    '取消退出
                            '关闭旧图
                            OldInventorDoc.Close(True)
                            Return True
                    End Select
                End If

                '另存为新文件
                OldInventorDoc.SaveAs(NewFullFileName, True)

                '全部替换为新文件
                OldOcc.Replace(NewFullFileName, True)

                '后台打开新文件,修改ipro
                Dim NewInventorDoc As Inventor.Document
                NewInventorDoc = ThisApplication.Documents.Open(NewFullFileName, False)  '打开文件,不显示
                '设置新文件的Iproperty,打开文件后不关闭
                SetDocumentIpropertyFromFileName(NewInventorDoc, False)

                '检查是否有对应的工程图文件,同时复制后修改文件名和模型链接
                Dim OldIdwFullFileName As String
                OldIdwFullFileName = GetNewExtensionFileName(OldFullFileName, ".idw")   '旧工程图
                If IsFileExsts(OldIdwFullFileName) = True Then
                    Dim NewIdwFullFileName As String
                    '新工程图
                    NewIdwFullFileName = GetNewExtensionFileName(NewFullFileName, ".idw")
                    '复制为新工程图
                    FileSystem.FileCopy(OldIdwFullFileName, NewIdwFullFileName)

                    Dim NewIdwDoc As Inventor.DrawingDocument
                    '打开新工程图文件,不显示
                    NewIdwDoc = ThisApplication.Documents.Open(NewIdwFullFileName, False)
                    '在新工程图中替换新的零件部件引用
                    NewIdwDoc.ReferencedDocumentDescriptors(1).ReferencedFileDescriptor.ReplaceReference(NewFullFileName)
                    '保存关闭新工程图
                    NewIdwDoc.Save2()
                    NewIdwDoc.Close()
                    '关闭旧的零件部件
                    OldInventorDoc.Close(True)
                    NewInventorDoc.Close()
                End If

                Return True
            Case MsgBox("选择的文件不是零件或部件", MsgBoxStyle.Information)
                Return False
        End Select
    End Function

    '设置序号
leaky114's avatar
leaky114 已提交
1331
    Public Function SetSerialNumber(ByVal oDrawingDocument As DrawingDocument) As Boolean
leaky114's avatar
leaky114 已提交
1332
        Dim oActiveSheet As Sheet
leaky114's avatar
leaky114 已提交
1333
        oActiveSheet = oDrawingDocument.ActiveSheet
leaky114's avatar
leaky114 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

        If oActiveSheet.PartsLists.Count = 0 Then
            MsgBox("该工程图无明细表", MsgBoxStyle.Critical)
            Return False
            Exit Function
        End If

        Dim FirstBalloonNumber As Short
        Dim BalloonNumber As Short
        FirstBalloonNumber = InputBox("输入第一个序号:", "重建序号", "1")

        BalloonNumber = FirstBalloonNumber

        '        '设置序号为0
        Dim partslistrow As Inventor.PartsListRow
        For Each partslistrow In oActiveSheet.PartsLists.Item(1).PartsListRows
            If partslistrow.Item(1).Value >= FirstBalloonNumber Then
                partslistrow.Item(1).Value = 0
            End If
        Next
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

        '获取当前balloon的textstyle
        Dim OldBalloonTextStyl As String = oDrawingDocument.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.BalloonStyle.TextStyle.Name

        '获取当前balloonstyle
        Dim oActiveBalloonStyle As BalloonStyle = oDrawingDocument.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.BalloonStyle

        '新建 ZeroBalloonText
        Try
            If oDrawingDocument.StylesManager.TextStyles.Item("ZeroBalloonText") Is Nothing Then

            End If
        Catch ex As Exception
            Dim oZeroBalloonText As TextStyle
            oZeroBalloonText = oDrawingDocument.StylesManager.TextStyles.Item(OldBalloonTextStyl).Copy("ZeroBalloonText")

            Dim oZeroBalloonTextColor As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 128)
            oZeroBalloonText.Color = oZeroBalloonTextColor
        End Try

        '设置当前balloon style 为新的 zeroballoonstyle
        oActiveBalloonStyle.TextStyle = oDrawingDocument.StylesManager.TextStyles.Item("ZeroBalloonText")

        ' '' ''
        ' '' ''        '点击每个序号组
        ' '' ''        Dim oBalloon As Balloon
        ' '' ''        For i = 1 To oActiveSheet.PartsLists.Item(1).PartsListRows.Count
        ' '' ''            oBalloon =   ThisApplication.CommandManager.Pick(kDrawingBalloonFilter, "选择引出序号")
        ' '' ''            '遍历序号组中的序号,不为0就设置序号,并加1,设置下一个,有序号则跳过
        ' '' ''            For Each oBalloonValueSet As BalloonValueSet In oBalloon.BalloonValueSets
        ' '' ''                If oBalloonValueSet.Value = 0 Then
        ' '' ''                    oBalloonValueSet.Value = i
        ' '' ''                    i = i + 1
        ' '' ''                End If
        ' '' ''            Next
        ' '' ''            '多加的1要减去
        ' '' ''            i = i - 1
        ' '' ''        Next
1392

leaky114's avatar
leaky114 已提交
1393
        '点击每个序号组
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        Try
            Dim oBalloon As Balloon
            Do
                oBalloon = ThisApplication.CommandManager.Pick(kDrawingBalloonFilter, "选择引出序号")

                For Each oBalloonValueSet As BalloonValueSet In oBalloon.BalloonValueSets
                    'If (oBalloonValueSet.Value >= FirstBalloonNumber) Then
                    If oBalloonValueSet.Value = 0 Then
                        oBalloonValueSet.Value = BalloonNumber
                        BalloonNumber = BalloonNumber + 1
                    End If
                Next
            Loop While True
        Catch ex As Exception
leaky114's avatar
leaky114 已提交
1408

1409 1410
            'esc 退出后,还原balloon style
            oActiveBalloonStyle.TextStyle = oDrawingDocument.StylesManager.TextStyles.Item(OldBalloonTextStyl)
leaky114's avatar
leaky114 已提交
1411

1412 1413
            Return True
        End Try
leaky114's avatar
leaky114 已提交
1414 1415 1416
    End Function

    '检查序号完整性
leaky114's avatar
leaky114 已提交
1417
    Public Function CheckSerialNumber(ByVal oDrawingDocument As DrawingDocument) As Boolean
leaky114's avatar
leaky114 已提交
1418 1419

        Dim oActiveSheet As Sheet
leaky114's avatar
leaky114 已提交
1420
        oActiveSheet = oDrawingDocument.ActiveSheet
leaky114's avatar
leaky114 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

        If oActiveSheet.Balloons.Count = 0 Then
            MsgBox("该工程图无序号,请添加 序号", MsgBoxStyle.Critical)
            Return False
            Exit Function
        End If

        If oActiveSheet.PartsLists.Count = 0 Then
            MsgBox("该工程图无明细表,请插入一个 明细表 ", MsgBoxStyle.Critical)
            Return False
            Exit Function
        End If

        Dim oPartsListRows As PartsListRows = oActiveSheet.PartsLists.Item(1).PartsListRows

        Dim strList As String = ""
        For Each oPartsListRow As PartsListRow In oPartsListRows
            If oPartsListRow.Ballooned = False Then
                strList = strList & oPartsListRow.Item(1).Value & " , "
            End If
        Next

        If Strings.Len(strList) > 1 Then
            MsgBox("明细表:" & strList & " 无序号", MsgBoxStyle.Information, "检查序号完整性")
            Return False
        Else
            Return True
        End If

    End Function

leaky114's avatar
leaky114 已提交
1452
    Public Function InsertSerialNumber(ByVal oDrawingDocument As DrawingDocument) As Boolean
leaky114's avatar
leaky114 已提交
1453
        Dim oActiveSheet As Sheet
leaky114's avatar
leaky114 已提交
1454
        oActiveSheet = oDrawingDocument.ActiveSheet
leaky114's avatar
leaky114 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463

        If oActiveSheet.PartsLists.Count = 0 Then
            MsgBox("该工程图无明细表", MsgBoxStyle.Critical)
            Return False
            Exit Function
        End If

        Dim FirstBalloonNumber As Short

leaky114's avatar
leaky114 已提交
1464
        FirstBalloonNumber = InputBox("输入要插入的序号,并点击该序号的标注标识", "插入序号")
leaky114's avatar
leaky114 已提交
1465 1466 1467 1468 1469

        If FirstBalloonNumber = "" Then
            Return False
        End If

leaky114's avatar
leaky114 已提交
1470
        '点击被插入的序号标识
leaky114's avatar
leaky114 已提交
1471
        Dim oBalloon As Balloon
leaky114's avatar
leaky114 已提交
1472
        oBalloon = ThisApplication.CommandManager.Pick(kDrawingBalloonFilter, "选择被插入的引出序号标识")
leaky114's avatar
leaky114 已提交
1473 1474

        '  设置序号+1
leaky114's avatar
leaky114 已提交
1475 1476 1477 1478

        For Each oPartsListRow As Inventor.PartsListRow In oActiveSheet.PartsLists.Item(1).PartsListRows
            If oPartsListRow.Item(1).Value >= FirstBalloonNumber Then
                oPartsListRow.Item(1).Value = oPartsListRow.Item(1).Value + 1
leaky114's avatar
leaky114 已提交
1479 1480 1481
            End If
        Next

leaky114's avatar
leaky114 已提交
1482
        '设置插入序号对应的标识
leaky114's avatar
leaky114 已提交
1483 1484 1485 1486
        For Each oBalloonValueSet As BalloonValueSet In oBalloon.BalloonValueSets
            If oBalloonValueSet.Value = 0 Then
                oBalloonValueSet.Value = FirstBalloonNumber
            Else
leaky114's avatar
leaky114 已提交
1487
                MsgBox("该标识数值不为0,请重新选择。", MsgBoxStyle.Information)
leaky114's avatar
leaky114 已提交
1488 1489 1490 1491 1492 1493 1494
            End If
        Next

        Return True
    End Function

    '设置当前部件下级为虚拟件
leaky114's avatar
leaky114 已提交
1495
    Public Function SetBOMStructuret(ByVal oAssemblyDocument As AssemblyDocument) As Boolean
leaky114's avatar
leaky114 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
        '设置结构类型
        Dim BOMStructureType As BOMStructureEnum

        Dim intBOMStructureType As String
        intBOMStructureType = InputBox("输入要设置的类型:" & vbCrLf & "1——普通件" & vbCrLf & vbCrLf & "2——虚拟件", "BOM结构类型", 2)

        Select Case intBOMStructureType
            Case ""
                Return True
            Case "1"  '普通件
                BOMStructureType = kNormalBOMStructure
            Case "2"  '虚拟件
                BOMStructureType = kPhantomBOMStructure
        End Select

        ' Set a reference to the BOM
        Dim oBOM As BOM
leaky114's avatar
leaky114 已提交
1513
        oBOM = oAssemblyDocument.ComponentDefinition.BOM
leaky114's avatar
leaky114 已提交
1514 1515 1516 1517 1518 1519 1520 1521

        ' Set the structured view to 'all levels'
        oBOM.StructuredViewFirstLevelOnly = False

        ' Make sure that the structured view is enabled.
        oBOM.StructuredViewEnabled = True

        ' Set a reference to the "Structured" BOMView
leaky114's avatar
leaky114 已提交
1522

leaky114's avatar
leaky114 已提交
1523
        '获取结构化的bom页面
leaky114's avatar
leaky114 已提交
1524
        For Each oBOMView As BOMView In oBOM.BOMViews
leaky114's avatar
leaky114 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
            If oBOMView.ViewType = BOMViewTypeEnum.kModelDataBOMViewType Then
                '遍历这个bom页面
                SetPhantomBOMStructuretSub(oBOMView.BOMRows, BOMStructureType)
            End If
        Next
        Return True
    End Function

    '设置当前部件下级为虚拟件,遍历子程序
    Public Sub SetPhantomBOMStructuretSub(ByVal oBOMRows As BOMRowsEnumerator, ByVal BOMStructureType As BOMStructureEnum)
        Dim i As Long

        For i = 1 To oBOMRows.Count
            Dim oRow As BOMRow
            oRow = oBOMRows.Item(i)

            Dim oCompDef As ComponentDefinition
            oCompDef = oRow.ComponentDefinitions.Item(1)

            Debug.Print(oCompDef.Document.FullFileName)

            '      遍历下一级
            If Not oRow.ChildRows Is Nothing Then
                Call SetPhantomBOMStructuretSub(oRow.ChildRows, BOMStructureType)
            End If

            '跳过参考件
            If oRow.BOMStructure <> kInseparableBOMStructure Then
                oRow.BOMStructure = BOMStructureType
            End If
        Next

    End Sub

    '在尺寸前添加φ
leaky114's avatar
leaky114 已提交
1560
    Public Function ADDFai(ByVal oInventorDocument As Inventor.Document) As Boolean
leaky114's avatar
leaky114 已提交
1561 1562 1563 1564 1565 1566
        Dim oLinearGeneralDimension As LinearGeneralDimension    '选择的部件或零件

        Dim strDimension As String
        Dim strFai As String

        ' 是否已经选择了尺寸
leaky114's avatar
leaky114 已提交
1567 1568
        If oInventorDocument.SelectSet.Count <> 0 Then
            For Each oSelect As Object In oInventorDocument.SelectSet
leaky114's avatar
leaky114 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
                If oSelect.Type = ObjectTypeEnum.kLinearGeneralDimensionObject Then

                    '添加Φ,内部代号n
                    strFai = "<StyleOverride Font='AIGDT'>n</StyleOverride>"
                    strDimension = strFai & "<DimensionValue/>"
                    oSelect.Text.FormattedText = strDimension
                End If
            Next
        Else
            oLinearGeneralDimension = ThisApplication.CommandManager.Pick(kDrawingDefaultFilter, "选择要添加Φ的尺寸")
            If oLinearGeneralDimension Is Nothing Then       '取消选择
                Return True
                Exit Function
            End If

            strFai = "<StyleOverride Font='AIGDT'>n</StyleOverride>"
            strDimension = strFai & "<DimensionValue/>"
            oLinearGeneralDimension.Text.FormattedText = strDimension

        End If
leaky114's avatar
leaky114 已提交
1589
        Return True
leaky114's avatar
leaky114 已提交
1590 1591 1592 1593

    End Function

    '检查模型是否有对应的工程图
leaky114's avatar
leaky114 已提交
1594
    Public Function CheckIsInvHaveIdw(ByVal oAssemblyDocument As AssemblyDocument, ByVal StrInName As String) As Boolean
leaky114's avatar
leaky114 已提交
1595 1596
        ' Set a reference to the BOM
        Dim oBOM As BOM
leaky114's avatar
leaky114 已提交
1597
        oBOM = oAssemblyDocument.ComponentDefinition.BOM
leaky114's avatar
leaky114 已提交
1598 1599 1600 1601 1602 1603 1604 1605

        ' Set the structured view to 'all levels'
        oBOM.StructuredViewFirstLevelOnly = False

        ' Make sure that the structured view is enabled.
        oBOM.StructuredViewEnabled = True

        ' Set a reference to the "Structured" BOMView
leaky114's avatar
leaky114 已提交
1606

leaky114's avatar
leaky114 已提交
1607
        '获取结构化的bom页面
leaky114's avatar
leaky114 已提交
1608
        For Each oBOMView As BOMView In oBOM.BOMViews
leaky114's avatar
leaky114 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
            If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
                '遍历这个bom页面
                CheckIsInvHaveIdwSub(oBOMView.BOMRows, StrInName)
            End If
        Next
        Return True

    End Function

    '检查模型是否有对应的工程图
    Public Sub CheckIsInvHaveIdwSub(ByVal oBOMRows As BOMRowsEnumerator, ByVal StrInName As String)
        Dim i As Long

        For i = 1 To oBOMRows.Count
            Dim oRow As BOMRow
            oRow = oBOMRows.Item(i)

            Dim oCompDef As ComponentDefinition
            oCompDef = oRow.ComponentDefinitions.Item(1)

            Debug.Print(oCompDef.Document.FullFileName)

            Dim InventorFullName As String   '模型文件
            Dim IdwFullFileName As String  '工程图全文件名

            InventorFullName = oCompDef.Document.FullFileName

            If IsFileExsts(InventorFullName) = False Then   '跳过不存在的文件
                GoTo 999
            End If

            If InStr(InventorFullName, ContentCenterFiles) > 0 Then    '跳过零件库文件
                GoTo 999
            End If

            '检查收否含有指定的字符串
            If InStr(Strings.LCase(InventorFullName), Strings.LCase(StrInName)) = 0 Then
                GoTo 999
            End If

            IdwFullFileName = Strings.Replace(InventorFullName, LCaseGetFileExtension(InventorFullName), ".idw")

            If IsFileExsts(IdwFullFileName) = False Then
                ThisApplication.Documents.Open(InventorFullName)
            End If

            '      遍历下一级
            If Not oRow.ChildRows Is Nothing Then
                Call CheckIsInvHaveIdwSub(oRow.ChildRows, StrInName)
            End If
999:
        Next

    End Sub

    '打开部件中所有子集对应的工程图 ,部件文件,指定的图号
leaky114's avatar
leaky114 已提交
1665
    Public Function OpenAllDrwInAsm(ByVal oAssemblyDocument As AssemblyDocument, ByVal StockNum As String) As Boolean
leaky114's avatar
leaky114 已提交
1666 1667
        ' Set a reference to the BOM
        Dim oBOM As BOM
leaky114's avatar
leaky114 已提交
1668
        oBOM = oAssemblyDocument.ComponentDefinition.BOM
leaky114's avatar
leaky114 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697

        ' Set the structured view to 'all levels'
        oBOM.StructuredViewFirstLevelOnly = False

        ' Make sure that the structured view is enabled.
        oBOM.StructuredViewEnabled = True

        ' Set a reference to the "Structured" BOMView
        Dim oBOMView As BOMView

        '获取结构化的bom页面
        For Each oBOMView In oBOM.BOMViews
            If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
                '遍历这个bom页面
                OpenAllDrwInAsmSub(oBOMView.BOMRows, StockNum)
            End If
        Next
        Return True

    End Function

    '打开部件中所有子集对应的工程图
    Public Sub OpenAllDrwInAsmSub(ByVal oBOMRows As BOMRowsEnumerator, ByVal StockNum As String)
        Dim i As Long

        For i = 1 To oBOMRows.Count
            Dim oRow As BOMRow
            oRow = oBOMRows.Item(i)

leaky114's avatar
leaky114 已提交
1698 1699
            Dim oComponentDefinition As ComponentDefinition
            oComponentDefinition = oRow.ComponentDefinitions.Item(1)
leaky114's avatar
leaky114 已提交
1700

leaky114's avatar
leaky114 已提交
1701
            Debug.Print(oComponentDefinition.Document.FullFileName)
leaky114's avatar
leaky114 已提交
1702 1703 1704 1705

            Dim InventorFullName As String   '模型文件
            Dim IdwFullFileName As String  '工程图全文件名

leaky114's avatar
leaky114 已提交
1706
            InventorFullName = oComponentDefinition.Document.FullFileName
leaky114's avatar
leaky114 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745

            If IsFileExsts(InventorFullName) = False Then   '跳过不存在的文件
                GoTo 999
            End If

            If InStr(InventorFullName, ContentCenterFiles) > 0 Then    '跳过零件库文件
                GoTo 999
            End If

            '获取对应工程图文件名
            IdwFullFileName = Strings.Replace(InventorFullName, LCaseGetFileExtension(InventorFullName), ".idw")

            Select Case StockNum
                Case ""     '打开全部
                    '存在对于工程图,打开它
                    If IsFileExsts(IdwFullFileName) = True Then
                        ThisApplication.Documents.Open(IdwFullFileName)
                    End If
                Case Else   '打开指定图号
                    If Strings.InStr(Strings.LCase(GetFileNameInfo(InventorFullName).ONlyName), Strings.LCase(StockNum)) = 0 Then
                        Exit Select
                    End If

                    If IsFileExsts(IdwFullFileName) = True Then
                        ThisApplication.Documents.Open(IdwFullFileName)
                    End If

            End Select

            '遍历下一级
            If Not oRow.ChildRows Is Nothing Then
                Call OpenAllDrwInAsmSub(oRow.ChildRows, StockNum)
            End If
999:
        Next

    End Sub

    '打开活动文件对应的工程图
leaky114's avatar
leaky114 已提交
1746
    Public Sub OpenDrawingDocument(ByVal InventorDocument As Inventor.Document)
leaky114's avatar
leaky114 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

        Try
            SetStatusBarText()

            'If IsInventorOpenDoc() = False Then
            '    Exit Sub
            'End If

            'If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
            '    MsgBox("该功能仅适用于部件或零件", MsgBoxStyle.Information)
            '    'Return False
            '    Exit Sub
            'End If

            Dim InventorFullName As String   '模型文件
            Dim IdwFullFileName As String  '工程图全文件名

leaky114's avatar
leaky114 已提交
1764
            InventorFullName = InventorDocument.FullDocumentName
leaky114's avatar
leaky114 已提交
1765 1766
            IdwFullFileName = Strings.Replace(InventorFullName, LCaseGetFileExtension(InventorFullName), ".idw")

leaky114's avatar
leaky114 已提交
1767 1768 1769 1770 1771 1772 1773
            '当前文件夹查询没有就 到父文件夹查询
            If IsFileExsts(IdwFullFileName) = False Then
                SearchDrawingDocumentInPresentFolder(InventorDocument, 3)

            End If

            '查询到工程图
leaky114's avatar
leaky114 已提交
1774 1775 1776
            If IsFileExsts(IdwFullFileName) Then
                ThisApplication.Documents.Open(IdwFullFileName)
            Else
leaky114's avatar
leaky114 已提交
1777 1778 1779
                If SearchDrawingDocumentInPresentFolder(InventorDocument, 3) = False Then
                    MsgBox(InventorFullName & "没有对应的工程图。", MsgBoxStyle.Information, "打开工程图")
                End If
leaky114's avatar
leaky114 已提交
1780 1781 1782 1783 1784 1785 1786
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

leaky114's avatar
leaky114 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
    '到父文件夹查询工程图  ,inventor 文档 ,向上查询的层级
    Public Function SearchDrawingDocumentInPresentFolder(ByVal InventorDocument As Inventor.Document, PresidentLevel As Integer) As Boolean
        Dim PrsentFolder As String   '父文件夹
        Dim InventorFullFileName As String   '模型文件全名
        Dim InventorFileName As String   '模型文件名
        Dim IdwFullFileName As String  '工程图全文件名
        Dim IdwFileName As String '工程图名
        Dim i As Integer

        InventorFullFileName = InventorDocument.FullDocumentName
        InventorFileName = GetFileNameInfo(InventorFullFileName).SigleName
        IdwFileName = GetFileNameInfo(InventorFullFileName).ONlyName + IDW

        i = 0

        PrsentFolder = System.IO.Directory.GetParent(InventorFullFileName).FullName
        Do
            If IsDirectoryExists(PrsentFolder) = True Then
                PrsentFolder = System.IO.Directory.GetParent(PrsentFolder).FullName
                i = i + 1
            Else
                Exit Do
            End If
        Loop While (i <> PresidentLevel)

        Dim Files As ReadOnlyCollection(Of String)
        Files = My.Computer.FileSystem.GetFiles(PrsentFolder, FileIO.SearchOption.SearchAllSubDirectories, IdwFileName)

        If files.Count <> 0 Then
            For Each IdwFullFileName In files
                ThisApplication.Documents.Open(IdwFullFileName)
            Next
            Return True
        Else
            Return False
        End If

    End Function



1828 1829 1830
    '-------------------------------------------------------------------------------------------------------
    '批量替换部件下子集的名字
    ' 组件,被替换的文件名,替换的文件名
leaky114's avatar
leaky114 已提交
1831 1832 1833 1834
    Public Function ReplaceNameInAsm(ByVal AsmDoc As Document, ByVal OldName As String, ByVal NewName As String, ByVal IsSaveAsOld As MsgBoxResult) As Boolean

        Dim TempFullFileName As String       '更改旧模型文件的名字存档

leaky114's avatar
leaky114 已提交
1835
        For Each InventorDocument As Inventor.Document In AsmDoc.ReferencedDocuments
leaky114's avatar
leaky114 已提交
1836 1837 1838 1839 1840 1841 1842 1843
            Dim OldFullFileName As String   '被替换的旧文件全名
            Dim OldFileName As String   '被替换的旧文件仅文件名

            Dim NewFullFileName As String   '新文件全名
            Dim NewFileName As String   '新文件名

            'InventorDoc = ThisApplication.Documents.ItemByName(OldFullFileName)

leaky114's avatar
leaky114 已提交
1844
            OldFullFileName = InventorDocument.FullDocumentName
leaky114's avatar
leaky114 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871

            If IsFileExsts(OldFullFileName) = False Then   '跳过不存在的文件
                GoTo 999
            End If

            If InStr(OldFullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
                GoTo 999
            End If

            OldFileName = GetFileNameInfo(OldFullFileName).SigleName

            '替换旧文件全名为新文件全名
            If InStr(OldFileName, OldName) Then
                NewFileName = Replace(OldFileName, OldName, NewName)
                NewFullFileName = GetFileNameInfo(OldFullFileName).Folder & "\" & NewFileName

                '打开旧文件,不显示
                Dim OldDoc As Inventor.Document
                OldDoc = ThisApplication.Documents.Open(OldFullFileName, False)

                '另存为新文件
                OldDoc.SaveAs(NewFullFileName, False)

                '关闭旧图
                OldDoc.Close()

                '后台打开文件,修改ipro
leaky114's avatar
leaky114 已提交
1872 1873 1874
                Dim NewInventorDocument As Inventor.Document
                NewInventorDocument = ThisApplication.Documents.Open(NewFullFileName, False)  '打开文件,不显示
                SetDocumentIpropertyFromFileName(NewInventorDocument, True) '设置Iproperty,打开文件后需关闭
leaky114's avatar
leaky114 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903

                Dim oCO As Inventor.ComponentOccurrences
                oCO = AsmDoc.ComponentDefinition.Occurrences

                '全部替换为新文件
                For Each ooCO As ComponentOccurrence In oCO
                    If ooCO.ReferencedDocumentDescriptor.FullDocumentName = OldFullFileName Then
                        ooCO.Replace(NewFullFileName, True)
                        Exit For
                    End If
                Next

                '是否有对应的工程图文件,同时复制后修改文件名和模型链接
                Dim OldIdwFullFileName As String
                OldIdwFullFileName = GetNewExtensionFileName(OldFullFileName, ".idw")   '旧工程图

                'Dim TempFullFileName As String       '更改旧模型文件的名字存档

                If IsFileExsts(OldIdwFullFileName) = True Then
                    Dim NewIdwFullFileName As String
                    NewIdwFullFileName = GetNewExtensionFileName(NewFullFileName, ".idw")   '新工程图
                    FileSystem.FileCopy(OldIdwFullFileName, NewIdwFullFileName)             '复制为新工程图

                    'MsgBox("找到有对应的旧工程图,生成新的工程图,将打开,请链接到文件:" & vbCrLf & NewFullFileName & vbCrLf & "该文件名已复制,粘贴到对话框即可。", MsgBoxStyle.Information)
                    'Windows.Forms.Clipboard.SetText(NewFullFileName)
                    'ThisApplication.Documents.Open(NewIdwFullFileName, False)      '打开新的工程图,使其手动链接零件或部件
                    'ThisApplication.Documents.ItemByName(NewIdwFullFileName).Save2() '保存链接并关闭工程图
                    'ThisApplication.Documents.ItemByName(NewIdwFullFileName).Close()

leaky114's avatar
leaky114 已提交
1904 1905 1906 1907
                    InventorDocument = ThisApplication.Documents.Open(NewIdwFullFileName, False)  '打开文件,不显示
                    InventorDocument.ReferencedDocumentDescriptors(1).ReferencedFileDescriptor.ReplaceReference(NewFullFileName)
                    InventorDocument.Save2()
                    InventorDocument.Close()
leaky114's avatar
leaky114 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921

                    If IsSaveAsOld = MsgBoxResult.Yes Then
                        TempFullFileName = OldIdwFullFileName & ".old"   '暂时更改旧工程图文件的名字存档
                        ReFileName(OldIdwFullFileName, TempFullFileName)
                        'ReFileName(TempFullFileName, OldFullFileName)   '恢复旧零件或部件文件名
                    End If
                End If

                If IsSaveAsOld = MsgBoxResult.Yes Then
                    TempFullFileName = OldFullFileName & ".old"   '暂时更改旧工程图文件的名字存档
                    ReFileName(OldFullFileName, TempFullFileName)
                End If

                '是部件的遍历新文件的子集
leaky114's avatar
leaky114 已提交
1922 1923 1924
                NewInventorDocument = ThisApplication.Documents.Open(NewFullFileName, False)
                If NewInventorDocument.DocumentType = kAssemblyDocumentObject Then
                    ReplaceNameInAsm(NewInventorDocument, OldName, NewName, IsSaveAsOld)
leaky114's avatar
leaky114 已提交
1925
                End If
leaky114's avatar
leaky114 已提交
1926
                NewInventorDocument.Close(True)
leaky114's avatar
leaky114 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936

            End If
999:
        Next

        Return True

    End Function

    '导出 bom 平面性
leaky114's avatar
leaky114 已提交
1937
    Public Function ExportBOMAsFlat(ByVal oAssemblyDocument As AssemblyDocument, ByVal ExcelFullFileName As String) As Boolean
leaky114's avatar
leaky114 已提交
1938 1939 1940 1941 1942 1943 1944
        Dim FirstLevelOnly As Boolean
        FirstLevelOnly = False

        '==============================================================================================
        '基于bom结构化数据,可跳过参考的文件
        ' Set a reference to the BOM
        Dim oBOM As BOM
leaky114's avatar
leaky114 已提交
1945
        oBOM = oAssemblyDocument.ComponentDefinition.BOM
leaky114's avatar
leaky114 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
        oBOM.StructuredViewEnabled = True
        oBOM.StructuredViewFirstLevelOnly = False

        'Set a reference to the "Structured" BOMView
        Dim oBOMView As BOMView

        'Dim ColumnsTitle As String
        'ColumnsTitle = "库存编号|空格|零件代号|材料|质量|所属装配代号|数量|总数量|描述"

        Dim IOS2 As System.IO.StreamWriter
        If IsFileExsts(ExcelFullFileName) = False Then
            IOS2 = New IO.StreamWriter(ExcelFullFileName, False, System.Text.Encoding.Default)
        Else
            IOS2 = New IO.StreamWriter(ExcelFullFileName, True, System.Text.Encoding.Default)
        End If

        '写BOM表头
        Dim strColumnsTitle As String
        strColumnsTitle = "序号," & Strings.Replace(BOMTiTle, "|", ",")
        IOS2.WriteLine(strColumnsTitle)
        IOS2.Close()

        TotalItem = 1

        '获取结构化的bom页面
        For Each oBOMView In oBOM.BOMViews
            If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
                '遍历这个bom页面
                QueryBOMRowPropertieToExcel(ExcelFullFileName, oBOMView.BOMRows, FirstLevelOnly, BOMTiTle, "0", 1)
            End If
        Next

leaky114's avatar
leaky114 已提交
1978
        Return True
leaky114's avatar
leaky114 已提交
1979 1980 1981

    End Function

leaky114's avatar
21.12.3  
leaky114 已提交
1982
    '在 bom平面性导出,遍历bom 行文件ipro
leaky114's avatar
leaky114 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
    Private Sub QueryBOMRowPropertieToExcel(ByVal ExcelFullFileName As String, ByVal oBOMRows As BOMRowsEnumerator, ByVal FirstLevelOnly As Boolean, ByVal ColumnsTitle As String, _
                                            ByVal Level As String, ByVal PresentNumber As Integer)

        Dim i As Short
        Dim j As Short
        Dim iStepCount As Short
        iStepCount = oBOMRows.Count

        'Create a new ProgressBar object.
        'Dim oProgressBar As Inventor.ProgressBar

        'oProgressBar = ThisApplication.CreateProgressBar(False, iStepCount, "当前文件: ")

        '赋值数组
        Dim oBOMRowData(2000, 1) As String

        ReDim oBOMRowData(oBOMRows.Count - 1, 1)

        For i = 1 To oBOMRows.Count
            oBOMRowData(i - 1, 0) = oBOMRows.Item(i).ItemNumber
            oBOMRowData(i - 1, 1) = oBOMRows.Item(i).ReferencedFileDescriptor.FullFileName
        Next

        '冒泡排序()

        Dim Temp As String  '不定义变量类型,以自动适应数组Ar的类型
        Dim Flag As Boolean
        Dim n As Integer = oBOMRowData.Length / oBOMRowData.Rank - 1

        For i = 0 To n
            Flag = False
            '从第1个元素开始,比较每两个相邻元素的大小,让大元素下沉,小元素上浮
            '经过一轮循环,可使数组中最大元素下沉到数组最底部
            '进入下一轮循环,只对前 n - i 个元素进行相邻比较(已排到后面的不用比较)
            For j = 0 To n - i - 1
                If Val(oBOMRowData(j, 0)) > Val(oBOMRowData(j + 1, 0)) Then
                    Temp = oBOMRowData(j, 0)
                    oBOMRowData(j, 0) = oBOMRowData(j + 1, 0)
                    oBOMRowData(j + 1, 0) = Temp

                    Temp = oBOMRowData(j, 1)
                    oBOMRowData(j, 1) = oBOMRowData(j + 1, 1)
                    oBOMRowData(j + 1, 1) = Temp

                    Flag = True '如果有排序行为,则设为 True
                End If
            Next
            If Flag = False Then '如未排序,说明已完成整个排序过程,退出
                Exit For
            End If
        Next

        '循环每一行
        For i = 0 To n
            '文件指针
            Dim FilePointItemNumber As String

            FilePointItemNumber = oBOMRowData(i, 1)

            '寻找指针的行,开始提取数据

            For j = 1 To oBOMRows.Count
                Dim oRow As BOMRow
                oRow = oBOMRows.Item(j)
                Dim InventorDocFullFileName As String
                InventorDocFullFileName = oRow.ReferencedFileDescriptor.FullFileName

                If InventorDocFullFileName = FilePointItemNumber Then
                    ' Set the message for the progress bar
                    'oProgressBar.Message = InventorDocFullFileName
                    If IsFileExsts(InventorDocFullFileName) = False Then   '跳过不存在的文件
                        GoTo 999
                    End If

                    '数据操作
                    '========================================
                    '测试文件
                    'Debug.Print(ItemNumber & ":" & InventorDocFullFileName)

leaky114's avatar
leaky114 已提交
2062
                    Dim oInventorDocument As Inventor.Document
leaky114's avatar
leaky114 已提交
2063

leaky114's avatar
leaky114 已提交
2064
                    oInventorDocument = ThisApplication.Documents.Open(InventorDocFullFileName, False)
leaky114's avatar
leaky114 已提交
2065 2066 2067 2068 2069

                    SetStatusBarText(InventorDocFullFileName)

                    Dim oPropSets As PropertySets
                    Dim oPropSet As PropertySet
leaky114's avatar
leaky114 已提交
2070
                    oPropSets = oInventorDocument.PropertySets
leaky114's avatar
leaky114 已提交
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
                    oPropSet = oPropSets.Item(3)

                    Dim Array_ColumnsTitle() As String
                    Dim Array_ColumnsTitleValue() As String
                    Array_ColumnsTitle = Split(ColumnsTitle, "|")

                    ReDim Array_ColumnsTitleValue(Array_ColumnsTitle.Length)

                    Dim propitem As [Property]
                    For k = 0 To Array_ColumnsTitle.Length - 1 Step 1
                        Select Case Array_ColumnsTitle(k)
                            Case "空格"
                                Array_ColumnsTitleValue(k) = ""
                            Case Map_PartName
                                propitem = oPropSet.ItemByPropId(Inventor.PropertiesForDesignTrackingPropertiesEnum.kPartNumberDesignTrackingProperties)
                                Array_ColumnsTitleValue(k) = propitem.Value
                            Case Map_StochNum
                                propitem = oPropSet.ItemByPropId(Inventor.PropertiesForDesignTrackingPropertiesEnum.kStockNumberDesignTrackingProperties)
                                Array_ColumnsTitleValue(k) = propitem.Value
                            Case "描述"
                                propitem = oPropSet.ItemByPropId(Inventor.PropertiesForDesignTrackingPropertiesEnum.kDescriptionDesignTrackingProperties)
                                Array_ColumnsTitleValue(k) = propitem.Value
                            Case "材料"
                                Dim strMaterialName As String
leaky114's avatar
leaky114 已提交
2095
                                If oInventorDocument.DocumentType = kPartDocumentObject Then
leaky114's avatar
leaky114 已提交
2096
                                    Dim IptDoc As PartDocument
leaky114's avatar
leaky114 已提交
2097
                                    IptDoc = oInventorDocument
leaky114's avatar
leaky114 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
                                    strMaterialName = IptDoc.ComponentDefinition.Material.Name
                                Else
                                    strMaterialName = ""
                                End If
                                Array_ColumnsTitleValue(k) = strMaterialName

                            Case "成本中心"
                                propitem = oPropSet.ItemByPropId(Inventor.PropertiesForDesignTrackingPropertiesEnum.kCostCenterDesignTrackingProperties)
                                Array_ColumnsTitleValue(k) = propitem.Value

                            Case "质量"
                                Dim strMass As String
leaky114's avatar
leaky114 已提交
2110
                                strMass = GetMass(oInventorDocument)
leaky114's avatar
leaky114 已提交
2111 2112 2113
                                Array_ColumnsTitleValue(k) = strMass
                            Case "面积"
                                Dim strArea As String
leaky114's avatar
leaky114 已提交
2114
                                strArea = GetArea(oInventorDocument)
leaky114's avatar
leaky114 已提交
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
                                Array_ColumnsTitleValue(k) = strArea
                            Case "数量"
                                Array_ColumnsTitleValue(k) = oRow.ItemQuantity.ToString
                            Case "所属装配"
                                Dim StockNumPartName As StockNumPartName
                                StockNumPartName = GetStockNumPartName(oRow.ReferencedFileDescriptor.Parent.FullFileName)
                                Array_ColumnsTitleValue(k) = StockNumPartName.StockNum & StockNumPartName.PartName
                            Case "所属装配代号"
                                Dim StockNumPartName As StockNumPartName
                                StockNumPartName = GetStockNumPartName(oRow.ReferencedFileDescriptor.Parent.FullFileName)
                                Array_ColumnsTitleValue(k) = StockNumPartName.StockNum

                            Case "总数量"
                                Array_ColumnsTitleValue(k) = (oRow.ItemQuantity * PresentNumber).ToString

                            Case "缩略图"
                                'propitem = oPropSet.ItemByPropId(Inventor.PropertiesForDesignTrackingPropertiesEnum.kPartIconDesignTrackingProperties)
                                'Array_ColumnsTitleValue(k) = propitem.Value

                        End Select
                        Array_ColumnsTitleValue(k) = Strings.Replace(Array_ColumnsTitleValue(k), ",", ",")
                    Next k

leaky114's avatar
leaky114 已提交
2138
                    oInventorDocument.Close(False)
leaky114's avatar
leaky114 已提交
2139

leaky114's avatar
leaky114 已提交
2140
                    Select Case oInventorDocument.DocumentType
leaky114's avatar
leaky114 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
                        Case kAssemblyDocumentObject
                            Threading.Thread.Sleep(1000)
                        Case kPartDocumentObject
                            Threading.Thread.Sleep(200)
                    End Select

                    '集合数组数据
                    Dim ColumnsTitleValue As String
                    ColumnsTitleValue = TotalItem & "," & Join(Array_ColumnsTitleValue, ",")
                    TotalItem = TotalItem + 1

                    '测试数据
                    'Debug.Print(ColumnsTitleValue)

                    '写数据到文件
                    Dim IOS As System.IO.StreamWriter
                    If IsFileExsts(ExcelFullFileName) = False Then
                        IOS = New IO.StreamWriter(ExcelFullFileName, False, System.Text.Encoding.Default)
                    Else
                        IOS = New IO.StreamWriter(ExcelFullFileName, True, System.Text.Encoding.Default)
                    End If
                    IOS.WriteLine(ColumnsTitleValue)
                    IOS.Close()

                    '==========================================

999:
                    'oProgressBar.UpdateProgress()
                    Exit For
                End If

            Next j

        Next i

        'Debug.Print("==================================")
        '写数据到文件

        Dim IOS2 As System.IO.StreamWriter
        If IsFileExsts(ExcelFullFileName) = False Then
            IOS2 = New IO.StreamWriter(ExcelFullFileName, False, System.Text.Encoding.Default)
        Else
            IOS2 = New IO.StreamWriter(ExcelFullFileName, True, System.Text.Encoding.Default)
        End If
        '写空白行
        IOS2.WriteLine("")
        IOS2.Close()

        For i = 1 To oBOMRows.Count
            ' Get the current row.
            Dim PointItemNumber As String

            If Level = "0" Then
                PointItemNumber = i
            Else
                PointItemNumber = Level & "." & i
            End If

            For j = 1 To oBOMRows.Count
                Dim oRow As BOMRow
                oRow = oBOMRows.Item(j)
                Dim ItemNumber As String
                ItemNumber = oRow.ItemNumber
                Dim DocFullFileName As String
                DocFullFileName = oRow.ReferencedFileDescriptor.FullFileName

                If ItemNumber = PointItemNumber Then
                    '测试文件
                    'Debug.Print(ItemNumber & ":" & DocFullFileName)
                    ' Set the message for the progress bar
                    'oProgressBar.Message = DocFullFileName
                    If IsFileExsts(DocFullFileName) = False Then   '跳过不存在的文件
                        GoTo 99
                    End If

                    '数据操作
                    '========================================

                    '==========================================

                    '遍历下一级
                    If (Not oRow.ChildRows Is Nothing) And FirstLevelOnly = False Then
                        Call QueryBOMRowPropertieToExcel(ExcelFullFileName, oRow.ChildRows, FirstLevelOnly, ColumnsTitle, PointItemNumber, oRow.ItemQuantity)
                    End If
99:
                    'oProgressBar.UpdateProgress()
                    Exit For
                End If

            Next j

        Next i

88:

        'oProgressBar.Close()

    End Sub

    '保存文件时的事件
leaky114's avatar
leaky114 已提交
2241 2242 2243 2244
    'Public Sub ThisApplicationEvents_OnOnSaveDocument(ByVal DocumentObject As Inventor._Document, _
    '                                                ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
    '                                                 ByVal Context As Inventor.NameValueMap, _
    '                                                 ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles ThisApplicationEvents.OnSaveDocument
leaky114's avatar
leaky114 已提交
2245

leaky114's avatar
leaky114 已提交
2246
    'End Sub
leaky114's avatar
leaky114 已提交
2247 2248

    '打开文件时的事件
leaky114's avatar
leaky114 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
    'Public Sub ThisApplicationEvents_OnOpenDocument(ByVal oInventorDocument As Inventor.Document, _
    '                                ByVal FullDocumentName As String, _
    '                               ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
    '                              ByVal Context As Inventor.NameValueMap, _
    '                              ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles ThisApplicationEvents.OnOpenDocument
    '    '当打开文件为工程图
    '    If oInventorDocument.DocumentType = kDrawingDocumentObject Then
    '        '写入主视图比例
    '        'If IsSetDrawingScale = 1 Then
    '        SetDrawingScale(oInventorDocument)
    '        'End If

    '        '写入零部件质量
    '        'If IsSetMass = 1 Then
    '        SetMass(oInventorDocument)
    '        'End If
    '    End If
    'End Sub
leaky114's avatar
leaky114 已提交
2267 2268

    '激活一个文档时的事件
leaky114's avatar
leaky114 已提交
2269
    Public Sub ThisApplicationEvents_OnActivateDocument(ByVal oInventorDocument As Inventor.Document, _
leaky114's avatar
leaky114 已提交
2270 2271 2272 2273
                                                        ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
                                                        ByVal Context As Inventor.NameValueMap, _
                                                        ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles ThisApplicationEvents.OnActivateDocument
        '当打开文件为工程图
leaky114's avatar
leaky114 已提交
2274
        If oInventorDocument.DocumentType = kDrawingDocumentObject Then
leaky114's avatar
leaky114 已提交
2275
            '写入主视图比例
leaky114's avatar
leaky114 已提交
2276 2277 2278
            'If IsSetDrawingScale = 1 Then
            SetDrawingScale(oInventorDocument)
            'End If
leaky114's avatar
leaky114 已提交
2279

leaky114's avatar
leaky114 已提交
2280
            '写入零部件质量
leaky114's avatar
leaky114 已提交
2281 2282 2283
            'If IsSetMass = 1 Then
            SetMass(oInventorDocument)
            'End If
leaky114's avatar
leaky114 已提交
2284
        End If
leaky114's avatar
leaky114 已提交
2285

leaky114's avatar
leaky114 已提交
2286
    End Sub
leaky114's avatar
leaky114 已提交
2287

leaky114's avatar
21.12.3  
leaky114 已提交
2288
    '刷新引用
leaky114's avatar
leaky114 已提交
2289 2290 2291
    Public Function RefreshShowName(ByVal AsmDoc As Document) As Boolean

        ' 获取装配定义
leaky114's avatar
leaky114 已提交
2292 2293
        Dim oAssemblyComponentDefinition As AssemblyComponentDefinition
        oAssemblyComponentDefinition = AsmDoc.ComponentDefinition
leaky114's avatar
leaky114 已提交
2294 2295 2296 2297 2298

        Dim ShortName1 As String
        Dim ShortName2 As String
        Dim NumName As String
        Dim i As Integer
leaky114's avatar
leaky114 已提交
2299
        For Each oOcc In oAssemblyComponentDefinition.Occurrences
leaky114's avatar
leaky114 已提交
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

            If InStr(oOcc.ReferencedDocumentDescriptor.FullDocumentName, ContentCenterFiles) > 0 Then    '跳过零件库文件
                GoTo 999
            End If

            Debug.Print(oOcc.Name)
            Debug.Print(oOcc.ReferencedDocumentDescriptor.FullDocumentName)

            i = InStr(oOcc.Name, ":")
            ShortName1 = Strings.Left(oOcc.Name, i - 1)
            NumName = Strings.Right(oOcc.Name, Len(oOcc.Name) - i + 1)
            ShortName2 = GetFileNameInfo(oOcc.ReferencedDocumentDescriptor.FullDocumentName).ONlyName
            If ShortName1 <> ShortName2 Then
                oOcc.Name = ShortName2 & NumName
            End If
999:
        Next
        Return True
    End Function

    '对齐XYZ平面
    Public Function FlushXYZPlane() As Boolean

leaky114's avatar
leaky114 已提交
2323 2324
        Dim InventorDocument As Inventor.Document
        InventorDocument = ThisApplication.ActiveDocument
leaky114's avatar
leaky114 已提交
2325 2326

        Dim oAsmCompDef As AssemblyComponentDefinition
leaky114's avatar
leaky114 已提交
2327
        oAsmCompDef = InventorDocument.ComponentDefinition
leaky114's avatar
leaky114 已提交
2328 2329 2330

        ' Get references to the two occurrences to constrain.
        ' This arbitrarily gets the first and second occurrence.
leaky114's avatar
leaky114 已提交
2331 2332
        Dim oComponentOccurrence1 As ComponentOccurrence
        oComponentOccurrence1 = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "选择第一个部件或零件")
leaky114's avatar
leaky114 已提交
2333

leaky114's avatar
leaky114 已提交
2334
        If oComponentOccurrence1 Is Nothing Then       '取消选择
leaky114's avatar
leaky114 已提交
2335 2336 2337
            Exit Function
        End If

leaky114's avatar
leaky114 已提交
2338 2339
        Dim oComponentOccurrence2 As ComponentOccurrence
        oComponentOccurrence2 = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "选择第二个部件或零件")
leaky114's avatar
leaky114 已提交
2340

leaky114's avatar
leaky114 已提交
2341
        If oComponentOccurrence2 Is Nothing Then       '取消选择
leaky114's avatar
leaky114 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
            Exit Function
        End If

        ' Get the XY plane from each occurrence.  This goes to the
        ' component definition of the part to get this information.
        ' This is the same as accessing the part document directly.
        ' The work plane obtained is in the context of the part,
        ' not the assembly.

        For i = 1 To 3
            Dim oPartPlane1 As WorkPlane
leaky114's avatar
leaky114 已提交
2353
            oPartPlane1 = oComponentOccurrence1.Definition.WorkPlanes.Item(i)
leaky114's avatar
leaky114 已提交
2354 2355

            Dim oPartPlane2 As WorkPlane
leaky114's avatar
leaky114 已提交
2356
            oPartPlane2 = oComponentOccurrence2.Definition.WorkPlanes.Item(i)
leaky114's avatar
leaky114 已提交
2357 2358 2359 2360 2361

            ' Because we need the work plane in the context of the assembly
            ' we need to create proxies for the work planes.  The proxies
            ' represent the work planes in the context of the assembly.
            Dim oAsmPlane1 As WorkPlaneProxy = Nothing
leaky114's avatar
leaky114 已提交
2362
            oComponentOccurrence1.CreateGeometryProxy(oPartPlane1, oAsmPlane1)
leaky114's avatar
leaky114 已提交
2363 2364

            Dim oAsmPlane2 As WorkPlaneProxy = Nothing
leaky114's avatar
leaky114 已提交
2365
            oComponentOccurrence2.CreateGeometryProxy(oPartPlane2, oAsmPlane2)
leaky114's avatar
leaky114 已提交
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

            ' Create the constraint using the work plane proxies.
            Dim oMate As FlushConstraint

            oMate = oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane1, oAsmPlane2, 0)
        Next

        Return True
    End Function

    '获取未读取的文件所在部件并打开该部件   ( 部件文件对象 ; 文件是否需打开,打开的文件用后要关闭)
    Public Function GetUnkonwDocumentWithBOM(ByVal AsmDoc As AssemblyDocument, ByVal IsNeedClose As Boolean) As Boolean
leaky114's avatar
21.12.3  
leaky114 已提交
2378
        ' 获取所有引用文档
leaky114's avatar
leaky114 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
        Dim FirstLevelOnly As Boolean
        FirstLevelOnly = False

        '==============================================================================================
        '基于bom结构化数据,可跳过参考的文件
        ' Set a reference to the BOM
        Dim oBOM As BOM
        oBOM = AsmDoc.ComponentDefinition.BOM
        oBOM.StructuredViewEnabled = True

        'Set a reference to the "Structured" BOMView
        Dim oBOMView As BOMView

        '获取结构化的bom页面
        For Each oBOMView In oBOM.BOMViews
2394
            If oBOMView.ViewType = BOMViewTypeEnum.kModelDataBOMViewType Then
leaky114's avatar
leaky114 已提交
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
                '遍历这个bom页面
                QueryBOMRowToOpenUnkonwDocument(oBOMView.BOMRows, FirstLevelOnly)
            End If
        Next
        '==============================================================================================
        Return True
    End Function

    '遍历BOM结构,获取未读取的文件所在部件并打开该部件
    Public Sub QueryBOMRowToOpenUnkonwDocument(ByVal oBOMRows As BOMRowsEnumerator, ByVal FirstLevelOnly As Boolean)
        Dim i As Integer

        Dim iStepCount As Short

        'Dim oBOMRows As BOMRowsEnumerator
        'oBOMRows = oBOM.oBOMView.BOMRows

        iStepCount = oBOMRows.Count

        'Create a new ProgressBar object.
        'Dim oProgressBar As Inventor.ProgressBar

        'oProgressBar = ThisApplication.CreateProgressBar(False, iStepCount, "当前文件: ")

        For i = 1 To oBOMRows.Count
            ' Get the current row.
            Dim oRow As BOMRow
            oRow = oBOMRows.Item(i)

            Dim FullFileName As String

            FullFileName = oRow.ReferencedFileDescriptor.FullFileName

            '测试文件
            Debug.Print(FullFileName)

            ' Set the message for the progress bar
            'oProgressBar.Message = FullFileName
leaky114's avatar
leaky114 已提交
2433 2434 2435
            'If InStr(FullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
            '    GoTo 999
            'End If
leaky114's avatar
leaky114 已提交
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471

            '文件不存在,就打开父级文件
            If IsFileExsts(FullFileName) = False Then
                Dim InventorDoc As Inventor.Document
                '父级文件名
                FullFileName = oRow.ReferencedFileDescriptor.Parent.FullFileName
                InventorDoc = ThisApplication.Documents.Open(FullFileName, True)  '打开文件,显示

            End If

            '遍历下一级
            If (Not oRow.ChildRows Is Nothing) And FirstLevelOnly = False Then
                Call QueryBOMRowToOpenUnkonwDocument(oRow.ChildRows, FirstLevelOnly)
            End If
999:
            'oProgressBar.UpdateProgress()
        Next

        'oProgressBar.Close()

    End Sub

    '尺寸精度圆整
    Public Function SetDrawingDimPrecision() As Boolean
        Try
            SetStatusBarText()

            If IsInventorOpenDoc() = False Then
                Exit Function
            End If

            If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
                MsgBox("该功能仅适用于工程图", MsgBoxStyle.Information)
                Exit Function
            End If

leaky114's avatar
leaky114 已提交
2472 2473
            Dim oDrawingDocument As DrawingDocument
            oDrawingDocument = ThisApplication.ActiveDocument
leaky114's avatar
leaky114 已提交
2474 2475 2476 2477

            Dim oLinearGeneralDimension As LinearGeneralDimension    '选择的部件或零件

            ' 是否已经选择了尺寸
leaky114's avatar
leaky114 已提交
2478 2479
            If oDrawingDocument.SelectSet.Count <> 0 Then
                For Each oSelect As Object In oDrawingDocument.SelectSet
leaky114's avatar
leaky114 已提交
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
                    If oSelect.Type = ObjectTypeEnum.kLinearGeneralDimensionObject Then
                        oSelect.Precision = 0
                    End If
                Next
            Else
                oLinearGeneralDimension = ThisApplication.CommandManager.Pick(kDrawingDefaultFilter, "选择要添加Φ的尺寸")
                If oLinearGeneralDimension Is Nothing Then       '取消选择
                    Return True
                    Exit Function
                End If
                oLinearGeneralDimension.Precision = 0

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function

leaky114's avatar
21.12.3  
leaky114 已提交
2499
End Module