Inventor iLogic|更换图纸格式
- 2026-11-08 13:53:00
Inventor iLogic|更换图纸格式
想要一键更换图纸格式,于是就写了一个。
功能简介
解析图纸资源内置的图纸格式,通过列表,可以一键更换。
代码
Option Explicit OnImports System' ============================================================' 更换图框' 手动运行:用 InputListBox 弹出列表,选择目标图纸格式' ============================================================Sub Main()If Not TypeOf ThisDoc.Document Is DrawingDocument ThenMessageBox.Show("请在工程图文档中运行。", "提示")ReturnEnd IfDim doc As DrawingDocument = ThisDoc.Document' ---- 收集所有图纸格式名称 ----Dim formatNames As New List(Of String)For Each fmt As SheetFormat In doc.SheetFormatsformatNames.Add(fmt.Name)NextIf formatNames.Count = 0 ThenMessageBox.Show("当前工程图中没有任何图纸格式。", "提示")ReturnEnd IfformatNames.Sort()' ---- 弹出内置列表让用户选择 ----Dim selectedName As String = ""TryselectedName = CStr(InputListBox("请选择要切换的图纸格式:", _formatNames, _formatNames(0), _"更换图框", _"确定"))CatchEnd TryIf String.IsNullOrEmpty(selectedName) Then Return' ---- 定位 SheetFormat ----Dim selectedFormat As SheetFormat = NothingFor Each fmt As SheetFormat In doc.SheetFormatsIf fmt.Name = selectedName ThenselectedFormat = fmtExit ForEnd IfNextIf selectedFormat Is Nothing Then Return' ---- 事务中执行,Ctrl+Z 可一键撤销 ----Dim errs As New System.Text.StringBuilder()Dim tm As TransactionManager = ThisApplication.TransactionManagerDim mainTrans As Transaction = tm.StartTransaction(doc, "更换图框")TryExecuteChangeCore(doc, selectedFormat, errs)doc.Update()mainTrans.End()If errs.Length > 0 ThenMessageBox.Show("操作完成,但有以下步骤失败:" & vbCrLf & vbCrLf & errs.ToString() & vbCrLf & vbCrLf & _"按 Ctrl+Z 可一键撤销。", "部分失败", MessageBoxButtons.OK, MessageBoxIcon.Warning)ElseMessageBox.Show("操作完成!" & vbCrLf & vbCrLf & "按 Ctrl+Z 可一键撤销。", _"完成", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfCatch ex As ExceptionTrymainTrans.Abort()CatchEnd TryMessageBox.Show("操作失败,已自动回滚:" & vbCrLf & ex.GetType().Name & vbCrLf & ex.Message, _"错误", MessageBoxButtons.OK, MessageBoxIcon.Error)End TryEnd Sub' ============================================================' 核心执行:删旧 → 切方向 → 改尺寸 → 加图框 → 加标题栏' ============================================================Sub ExecuteChangeCore(doc As DrawingDocument, selectedFormat As SheetFormat, _ByRef errs As System.Text.StringBuilder)' ---- 从目标格式中提取各项参数 ----Dim targetSize As String = selectedFormat.Size.ToString().Replace("k", "").Replace("DrawingSheetSize", "")Dim targetBorder As String = ""If selectedFormat.ReferencedBorderDefinition IsNot Nothing ThentargetBorder = selectedFormat.ReferencedBorderDefinition.NameEnd IfDim targetTb As String = ""If selectedFormat.ReferencedTitleBlockDefinition IsNot Nothing ThentargetTb = selectedFormat.ReferencedTitleBlockDefinition.NameEnd IfDim targetOrient As Integer = CInt(selectedFormat.Orientation)' ---- 1. 删图框 ----Dim curBorder As String = ""If doc.ActiveSheet.Border IsNot Nothing ThencurBorder = doc.ActiveSheet.Border.Definition.NameEnd IfIf Not String.Equals(curBorder, targetBorder, StringComparison.OrdinalIgnoreCase) ThenIf doc.ActiveSheet.Border IsNot Nothing ThenTrydoc.ActiveSheet.Border.Delete()Catch ex As Exceptionerrs.AppendLine("【删图框】" & ex.GetType().Name & ": " & ex.Message)End TryEnd IfEnd If' ---- 2. 删标题栏 ----Dim curTb As String = ""If doc.ActiveSheet.TitleBlock IsNot Nothing ThencurTb = doc.ActiveSheet.TitleBlock.Definition.NameEnd IfIf Not String.Equals(curTb, targetTb, StringComparison.OrdinalIgnoreCase) ThenIf doc.ActiveSheet.TitleBlock IsNot Nothing ThenTrydoc.ActiveSheet.TitleBlock.Delete()Catch ex As Exceptionerrs.AppendLine("【删标题栏】" & ex.GetType().Name & ": " & ex.Message)End TryEnd IfEnd If' ---- 3. 切方向 ----TryDim targetOrientEnum As PageOrientationTypeEnum = CType(targetOrient, PageOrientationTypeEnum)If doc.ActiveSheet.Orientation <> targetOrientEnum Thendoc.ActiveSheet.Orientation = targetOrientEnumEnd IfCatch ex As Exceptionerrs.AppendLine("【方向】" & ex.GetType().Name & ": " & ex.Message)End Try' ---- 4. 改尺寸 ----Dim curSize As String = doc.ActiveSheet.Size.ToString().Replace("k", "").Replace("DrawingSheetSize", "")If Not String.Equals(curSize, targetSize, StringComparison.OrdinalIgnoreCase) ThenTrydoc.ActiveSheet.Size = selectedFormat.SizeCatch ex As Exceptionerrs.AppendLine("【尺寸】" & ex.GetType().Name & ": " & ex.Message)End TryEnd If' ---- 5. 加图框 ----Dim curBorder2 As String = ""If doc.ActiveSheet.Border IsNot Nothing ThencurBorder2 = doc.ActiveSheet.Border.Definition.NameEnd IfIf Not String.Equals(curBorder2, targetBorder, StringComparison.OrdinalIgnoreCase) ThenTryDim bd As BorderDefinition = selectedFormat.ReferencedBorderDefinitionIf bd IsNot Nothing Then doc.ActiveSheet.AddBorder(bd)Catch ex As Exceptionerrs.AppendLine("【图框】目标=""" & targetBorder & """ " & ex.GetType().Name & ": " & ex.Message)End TryEnd If' ---- 6. 加标题栏 ----Dim curTb2 As String = ""If doc.ActiveSheet.TitleBlock IsNot Nothing ThencurTb2 = doc.ActiveSheet.TitleBlock.Definition.NameEnd IfIf Not String.Equals(curTb2, targetTb, StringComparison.OrdinalIgnoreCase) ThenTryDim td As TitleBlockDefinition = selectedFormat.ReferencedTitleBlockDefinitionIf td IsNot Nothing Then doc.ActiveSheet.AddTitleBlock(td)Catch ex As Exceptionerrs.AppendLine("【标题栏】目标=""" & targetTb & """ " & ex.GetType().Name & ": " & ex.Message)End TryEnd IfEnd Sub
演示
关注
重播 分享 赞
代码由AI辅助生成,仅供参考。
本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。