Message 1 of 2
code to modify bend precision & symbols to bend-note & link part number with dynamic titles for flat or normal views finally completed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Have fun making Autodesk better with your tools!
I believe in sharing not selling.
I also believe this unlocks the ability to change hole precision now.
this was a lot of testing and testing as this API is not available for quick understanding.
Imports System
Imports Inventor
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
AddReference "System.Drawing.dll"
AddReference "System.Xml.dll"
'=============================================
' iLogic Rule: Auto‑Ref Flat‑Pattern View (Undoable) with Bend‑Note Precision Edit
'=============================================
Sub Main()
Dim oApp As Inventor.Application = ThisApplication
Dim dDoc As DrawingDocument = TryCast(oApp.ActiveDocument, DrawingDocument)
If dDoc Is Nothing Then
Logger.Debug("Not a Drawing Document")
Exit Sub
End If
' Start one undoable transaction
Dim trans As Transaction = oApp.TransactionManager.StartTransaction(dDoc, "Auto‑Ref Flat‑Pattern View")
Try
' Pick a view
Dim pickView As DrawingView = oApp.CommandManager.Pick( _
SelectionFilterEnum.kDrawingViewFilter, "Select a View" )
If pickView Is Nothing Then Exit Sub
If pickView.IsFlatPatternView Then
Dim sheet As Sheet = dDoc.ActiveSheet
' 1) Reference ALL dims in the picked view except zero‑zero ordinates
For Each oDim As DrawingDimension In sheet.DrawingDimensions
If Not oDim.Parent Is pickView Then Continue For
Dim txt As String = ""
Try : txt = oDim.Text.Text : Catch : End Try
If txt.Trim() = "0" _
OrElse Regex.IsMatch(txt, "(^|\D)\.[0]+(\D|$)") Then
Continue For
End If
Try : oDim.Tolerance.SetToReference() : Catch : End Try
Next
' 2) Reference & deep‑edit ALL bend notes in the picked view
For Each dNote As DrawingNote In sheet.DrawingNotes.BendNotes
Dim bNote As BendNote = CType(dNote, BendNote)
' a) Reference the angle callout
Try
CType(bNote.BendAngleDimension, AngularGeneralDimension) _
.Tolerance.SetToReference()
Catch : End Try
' b) Reference the radius callout
Try
CType(bNote.BendRadiusDimension, RadiusGeneralDimension) _
.Tolerance.SetToReference()
Catch : End Try
' c) Deep‑edit the bend‑note XML: strip tolerances, force precisions, add parentheses only once
Try
' — load the current formatted text
Dim xmlFrag As String = bNote.FormattedBendNote
Dim trimmedRaw As String = xmlFrag.Trim()
' — if it’s already wrapped in ( ), skip to next note
If trimmedRaw.StartsWith("(") AndAlso trimmedRaw.EndsWith(")") Then
Continue For
End If
' — load XML into DOM
Dim xmlDoc As Object = CreateObject("MSXML2.DOMDocument.6.0")
xmlDoc.async = False
xmlDoc.loadXML("<root>" & xmlFrag & "</root>")
Dim root As Object = xmlDoc.documentElement
' — strip out all tolerance attributes
Dim attrList() As String = {
"UpperTolerance", "LowerTolerance", _
"ToleranceType", "TolerancePrecision", _
"ToleranceAlternatePrecision"
}
For Each child As Object In root.ChildNodes
If child.nodeType = 1 Then
For Each attrName As String In attrList
Dim att As Object = child.Attributes.getNamedItem(attrName)
If Not att Is Nothing Then child.removeAttribute(attrName)
Next
End If
Next
' — force Precision on BendAngle → 0
Dim angleElem As Object = root.selectSingleNode("//BendAngle")
If Not angleElem Is Nothing Then
Dim attA As Object = angleElem.Attributes.getNamedItem("Precision")
If Not attA Is Nothing Then
attA.Text = "0"
Else
angleElem.setAttribute("Precision", "0")
End If
End If
' — force Precision on BendRadius → 3
Dim radiusElem As Object = root.selectSingleNode("//BendRadius")
If Not radiusElem Is Nothing Then
Dim attR As Object = radiusElem.Attributes.getNamedItem("Precision")
If Not attR Is Nothing Then
attR.Text = "3"
Else
radiusElem.setAttribute("Precision", "3")
End If
End If
' — rebuild XML: insert a single space before the Angle & Radius tags
Dim newXml As String = ""
For Each child As Object In root.ChildNodes
'If child.nodeName = "BendAngle" OrElse child.nodeName = "BendRadius" Then
If child.nodeName = "BendAngle" Then
newXml &= " " & child.xml
Else
newXml &= child.xml
End If
Next
' — only wrap in ( ) if it’s not already parenthesized
Dim frag As String = newXml.Trim()
If Not (frag.StartsWith("(") AndAlso frag.EndsWith(")")) Then
newXml = "(" & frag & ")"
Else
newXml = frag
End If
' — write it back
bNote.FormattedBendNote = newXml
Catch
' if anything fails, skip this note
End Try
Next
' 3) Override the view label for flat pattern
' pickView.Label.FormattedText = _
' "<Property Document='model' " & _
' "FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' " & _
' "PropertyID='5'>Part Number</Property>" & vbNewLine & _
' "<StyleOverride Underline='False'>FLAT PATTERN</StyleOverride>"
' pickView.Label.FormattedText = _
' "<StyleOverride Underline='False'>FLAT PATTERN</StyleOverride>" & vbNewLine & _
' "<StyleOverride FontSize='0.2032'>" & _
' "<Property Document='model' " & _
' "FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' " & _
' "PropertyID='5'>Part Number</Property>" & _
' "</StyleOverride>"
pickView.Label.FormattedText = _
"<StyleOverride Underline='False'>FLAT PATTERN</StyleOverride>" & vbNewLine & _
"<StyleOverride FontSize='0.2032'>" & _
"<Property Document='model' " & _
"FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' " & _
"PropertyID='5'>Part Number</Property>" & _
"</StyleOverride>"
Else
' —— Non‑flat pattern: original property‑driven label logic ——
Dim oPropSet As PropertySet = _
pickView.ReferencedDocumentDescriptor. _
ReferencedDocument.PropertySets(3)
pickView.Label.FormattedText = _
"<Property Document='model' " & _
"FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' " & _
"PropertyID='5'>Part Number</Property>"
End If
' Ensure the label is visible
pickView.ShowLabel = True
Finally
' End the transaction so all changes become one undo step
trans.End()
End Try
End Sub