Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: Chris.Truelove

Yes. It can definitely work with an existing DrawingSketch & existing TextBox.

Here's a modified version of my previous code.

(I edited the code in my last post a couple times, right after I posted it to to a misplaced variable name. That may have been what caused the error.)

See if this will work for you.

Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Return
End If

Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
Dim oBorder As Inventor.Border = oSheet.Border
Dim oSketch As DrawingSketch = oSheet.Sketches.Item(1) 'you can supply a name, if it has one
oSketch.Edit
Dim oTextBox As TextBox = oSketch.TextBoxes.Item(1) 'you can supply a name, if it has one

FitCheck :
If oTextBox.FittedTextWidth > oTextBox.Width Then
	oAns = MsgBox("The text is wider than the text box." & vbCrLf & _
	"Do you want to:  Reduce Font Size [Yes], Edit the contents [No], or Leave it that way [Cancel]?", vbYesNoCancel + vbQuestion, " ")
	If oAns = vbYes Then
		oTextBox.Style = GetTStyle(oDDoc)
		GoTo FitCheck
	ElseIf oAns = vbNo Then
		oTextBox.Text = InputBox("Edit contents.", " ", oTextBox.Text)
		GoTo FitCheck
	End If  'Don't really need to deal with what happens when they choose Cancel.
End If
oSketch.ExitEdit
End Sub

Function GetTStyle(ByRef oDoc As Document) As TextStyle
	Dim oTStyles As TextStylesEnumerator = oDoc.StylesManager.TextStyles
	Dim oTStyle As TextStyle
	Dim oTStylesNames As New List(Of String)
	For Each oTStyle In oTStyles
		oTStylesNames.Add(oTStyle.Name)
	Next
	Dim oTStyleName As String = InputListBox("Select a TextStyle for this TextBox.", oTStylesNames, " ", "Available TextStyles")
	oTStyle = oTStyles.Item(oTStyleName)
	Return oTStyle
End Function

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)