Having trouble Changing text font size in a drawing sketch

Having trouble Changing text font size in a drawing sketch

Chris.Truelove
Contributor Contributor
1,749 Views
10 Replies
Message 1 of 11

Having trouble Changing text font size in a drawing sketch

Chris.Truelove
Contributor
Contributor

Hi,

I am having trouble changing the font size within a sketch that is used as part of a border title.

I have tried a number of different ways to do this all of which usually fail with an error relating the the code that is trying to change the font size.

It should be noted that the code works as it should without the extra code that is trying to change the font size.

Below are some snippets of the code that was used to change the font size.

 

----------------------------------------------------------------------------------------------------

Public Sub Main SketchTextAdd()
' a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Create a new sketch on the active sheet.
Dim oSketch As Inventor.Sketch
oSketch = oDrawDoc.ActiveSheet.Sketches.Add

' Open the sketch for edit so the text boxes can be created.
' This is only required for drawing sketches, not part.
oSketch.Edit

'Identify border type
Dim oBorder = ActiveSheet.Border

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.

Dim sText As String

sText = InputBox("Enter new Title Text", "Secondary Title ", "")
iProperties.Value("Custom", "Second Title "& ActiveSheet.Name) = sText

Dim oTextBox As Inventor.TextBox
Dim oStyle As TextStyle
For Each oTextBox In oTextBoxes
oStyle = oTextBox.Style
oStyle.Font = "Arial"
oStyle.FontSize = 1
Next
If oBorder = "A3" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.7, 3.2),oTG.CreatePoint2d(35,2.4), sText)
Else If oBorder = "A4" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.39, 3.2),oTG.CreatePoint2d(22,2.4), sText)
Else If oBorder = "A4_P" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(11.15, 6.195), oTG.CreatePoint2d(20.25, 4), sText)
End If
oSketch.ExitEdit
End Sub

---------------------------------------------------------------------------------------------------

and I have also tried the following snippet of code to try and change the font size.

---------------------------------------------------------------------------------------------------

Dim oTextBox As Inventor.TextBox

If oBorder = "A3" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.7, 3.2),oTG.CreatePoint2d(35,2.4), sText)
Else If oBorder = "A4" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.39, 3.2),oTG.CreatePoint2d(22,2.4), sText)
Else If oBorder = "A4_P" Then
oTextBox.FormattedText = "<StyleOverride Fontsize = '0.5'>" & sText & "</StyleOverride>"
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(11.15, 6.195), oTG.CreatePoint2d(20.25, 4), sText)
End If
oSketch.ExitEdit
End Sub

----------------------------------------------------------------------------------------------------

I hope someone can help.

 

0 Likes
1,750 Views
10 Replies
Replies (10)
Message 2 of 11

JhoelForshav
Mentor
Mentor

@Chris.Truelove 

Try this 🙂

Sub Main' SketchTextAdd()
' a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Create a new sketch on the active sheet.
Dim oSketch As Inventor.Sketch
oSketch = oDrawDoc.ActiveSheet.Sketches.Add

' Open the sketch for edit so the text boxes can be created.
' This is only required for drawing sketches, not part.
oSketch.Edit

'Identify border type
Dim oBorder = ActiveSheet.Border

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.

Dim sText As String

sText = InputBox("Enter new Title Text", "Secondary Title ", "")
iProperties.Value("Custom", "Second Title "& ActiveSheet.Name) = sText

Dim oTextBox As Inventor.TextBox
If oBorder = "A3" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.7, 3.2),oTG.CreatePoint2d(35,2.4), sText)
Else If oBorder = "A4" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.39, 3.2),oTG.CreatePoint2d(22,2.4), sText)
Else If oBorder = "A4_P" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(11.15, 6.195), oTG.CreatePoint2d(20.25, 4), sText)
End If
oSketch.ExitEdit


Dim oStyle As TextStyle
For Each oTextBox In oSketch.TextBoxes
oStyle = oTextBox.Style
oStyle.Font = "Arial"
oStyle.FontSize = 1
Next
Message 3 of 11

Chris.Truelove
Contributor
Contributor

Hi,

Thanks for the pointer,

This changed all of the font sizes on all sheets.

I only wanted to change the font size on the A4_P sheet. Sorry I didn't explain that very well.

0 Likes
Message 4 of 11

JhoelForshav
Mentor
Mentor

@Chris.Truelove 

Like this then? 🙂

Sub Main' SketchTextAdd()
' a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Create a new sketch on the active sheet.
Dim oSketch As Inventor.Sketch
oSketch = oDrawDoc.ActiveSheet.Sketches.Add

' Open the sketch for edit so the text boxes can be created.
' This is only required for drawing sketches, not part.
oSketch.Edit

'Identify border type
Dim oBorder = ActiveSheet.Border

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.

Dim sText As String

sText = InputBox("Enter new Title Text", "Secondary Title ", "")
iProperties.Value("Custom", "Second Title "& ActiveSheet.Name) = sText

Dim oTextBox As Inventor.TextBox
If oBorder = "A3" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.7, 3.2),oTG.CreatePoint2d(35,2.4), sText)
Else If oBorder = "A4" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.39, 3.2),oTG.CreatePoint2d(22,2.4), sText)
Else If oBorder = "A4_P" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(11.15, 6.195), oTG.CreatePoint2d(20.25, 4), sText)
Dim oStyle As TextStyle = oTextBox.Style
oStyle.Font = "Arial"
oStyle.FontSize = 1
End If
oSketch.ExitEdit

End Sub
0 Likes
Message 5 of 11

Chris.Truelove
Contributor
Contributor

Thanks again.

 

Unfortunately that is still changing the font on all sheets!!....

0 Likes
Message 6 of 11

JhoelForshav
Mentor
Mentor

Hi @Chris.Truelove 

You are right... We are actually changing the actual style that the textbox is using. So every other textbox using this style will be updated aswell... I guess we'll have to create a new style. Try this 🙂

 

Sub Main' SketchTextAdd()
' a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Create a new sketch on the active sheet.
Dim oSketch As Inventor.Sketch
oSketch = oDrawDoc.ActiveSheet.Sketches.Add

' Open the sketch for edit so the text boxes can be created.
' This is only required for drawing sketches, not part.
oSketch.Edit


'Identify border type
Dim oBorder = ActiveSheet.Border

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.

Dim sText As String

sText = InputBox("Enter new Title Text", "Secondary Title ", "")
iProperties.Value("Custom", "Second Title " & ActiveSheet.Name) = sText

Dim oStyle As TextStyle = oDrawDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.SketchTextStyle

Dim oTextBox As Inventor.TextBox
If oBorder = "A3" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.7, 3.2), oTG.CreatePoint2d(35, 2.4), sText, oStyle)
Else If oBorder = "A4" Then
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.39, 3.2), oTG.CreatePoint2d(22, 2.4), sText, oStyle)
Else If oBorder = "A4_P" Then
Dim A4_P_Style As TextStyle
Try
A4_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A4_P_Style")
Catch
A4_P_Style = oStyle.Copy("A4_P_Style")
A4_P_Style.Font = "Arial"
A4_P_Style.FontSize = 1
End Try

oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(11.15, 6.195), oTG.CreatePoint2d(20.25, 4), sText, A4_P_Style)

End If
oSketch.ExitEdit

End Sub
0 Likes
Message 7 of 11

Chris.Truelove
Contributor
Contributor

Hi,

We are getting there,

The Font type and size for that particular border definition is now entering in a different size and font to the rest of the sheets in the drawing. Unfortunately I cannot control the size of the text or the font used. Editing the lines

Style.Font = "Times New Roman"
Style.FontSize = 1

 Makes no difference to the font size or style. The text is approx. 4 times bigger than the other text that is in the border/Title block.

For reference the font size that is used in the title block is 2.5!!!!.....

 

Thanks for your help so far.

0 Likes
Message 8 of 11

WCrihfield
Mentor
Mentor

Sorry I didn't get back with you sooner on this matter in the last post.  I got caught away at work.

Here's what I was working on yesterday for you.

There are several lines commented out, just to show some optional possibilities.

I was actually thinking specifying the font and its size by specifying the TextStyle you wanted to use, instead of the more difficult route of specifying the individual Font Name, and Font Size, etc.

Then putting a block of code to check the resulting text size compared to the text box size, then offering some options to help correct any issues, in a few different ways like in the following code.

This code I am testing with an A-Size sheet (8.5 in x 11 in), and have a simple rectangle border that is .25 in from all sides of the sheet.  And the rectangle for my text box is being translated to Inches by multiplying by 2.54.

It places the TextBox in the lower left corner of this border (just as an example).

 

 

 

 

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 oSheet As Inventor.Sheet = oDDoc.ActiveSheet
Dim oBorder As Inventor.Border = oSheet.Border

Dim oTStyles As TextStylesEnumerator = oDDoc.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)

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oPoint1 As Point2d = oBorder.RangeBox.MinPoint
Dim oPoint2 As Point2d = oTG.CreatePoint2d(oPoint1.X + (3*2.54), oPoint1.Y + (.75*2.54))

Dim oSketch As DrawingSketch = oSheet.Sketches.Add
oSketch.Edit

Dim oText As String = InputBox("Enter new Title Text", " ")

'Dim oFText As String = "<StyleOverride Font='Simplex' FontSize='18' Bold-'True' Italic='False' Underline='False'>" & oText & "</StyleOverride>"
'Dim oTextBox As Inventor.TextBox = oSketch.TextBoxes.AddByRectangle(oPoint1, oPoint2, oFText)
Dim oTextBox As Inventor.TextBox = oSketch.TextBoxes.AddByRectangle(oPoint1, oPoint2, oText, oTStyle)
'oTextBox.ShowBoundaries = True
'iLogicVb.DocumentUpdate
'MsgBox("There are " & oTextBox.BoundaryGeometry.Count & " entities within oTextBox.BoundaryGeometry.")
'For Each oSE As SketchEntity In oTextBox.BoundaryGeometry
'	oSE.SketchOnly = False
'	oSE.Construction = False
'	oSketch.GeometricConstraints.AddGround(oSE)
'Next
'oTextBox.ShowBoundaries = True
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
		oNewTStyleName = InputListBox("Select a TextStyle for this TextBox.",oTStylesNames," ","Available TextStyles")
		oTextBox.Style = oTStyles.Item(oNewTStyleName )
		'Then GoTo FitCheck (the beginning Of this Size Check Process)
		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

 

 

 

Let me know how this works for you.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 11

Chris.Truelove
Contributor
Contributor

Hi,

Thanks for this,

I'm not sure why but when I click Yes if the text does not fit the box. The option to choose another text style pops up as it is supposed to but when that style is selected the code errors. I do like the way that it uses the styles library and I could easily create a coupe of styles with different font sizes. I think it would be better if it just defaulted to one style for all of the borders except the A4_P border and then used a style that was specific to that border.

I also like the catch that allows the user to edit the text again if they want to. Could this be modified to allow the user to edit that same text  if they have already exited this operation. i.e. if a sketch with a text box already exists, can that be edited instead of creating a new sketch and text box.???........

If this is going to take up too much of your time please do not feel obligated to take it further.

 

Thank you for your help.

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor

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)

0 Likes
Message 11 of 11

Chris.Truelove
Contributor
Contributor

That was quick!!!!.......

Unfortunately it errors straight away.

 

Error Message;

Error in rule: Test 2, in document: Std_Inventor.dwg

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

More Info;

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DrawingSketches.get_Item(Object Index)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

 

0 Likes