Exception on attempt to use API-created TextStyle in TextBox within Part PlanarSketch

Exception on attempt to use API-created TextStyle in TextBox within Part PlanarSketch

Maxim-CADman77
Advisor Advisor
273 Views
4 Replies
Message 1 of 5

Exception on attempt to use API-created TextStyle in TextBox within Part PlanarSketch

Maxim-CADman77
Advisor
Advisor

I get the Unspecified error (0x80004005 (E_FAIL)) when try to set local API-created TextStyle for TextBox within Part PlanarSketch (it is present in Style Editor but not in 'Style' drop-down list within 'Format text' window)

My sample model is attached, my iLogic code is:

 

 

Option Explicit On

Dim tgtTxtStyleName As String = "My-Sketch-Text-Style-COPY-" & Now.ToString("yyyyMMddHHmmss")
Dim ptDoc As PartDocument = ThisDoc.Document
Dim ptDef As ComponentDefinition = ptDoc.ComponentDefinition

If ptDoc.ActiveAnnotationsStandard <> "ISO" Then ptDoc.ActiveAnnotationsStandard = "ISO"

Dim srcTxtStyle, tgtTxtStyle As TextStyle
Try
	tgtTxtStyle = ptDoc.TextStyles(tgtTxtStyleName)
Catch
	srcTxtStyle = (From tStyle As TextStyle In ptDoc.TextStyles Where tStyle.InternalName.StartsWith("1:Sketch Text (ISO")).FirstOrDefault
	tgtTxtStyle = srcTxtStyle.Copy(tgtTxtStyleName)
End Try

Logger.Info("tgtTxtStyle: " & If(tgtTxtStyle Is Nothing, "-", "+"))

Dim sk As PlanarSketch = ptDef.Sketches(ptDef.Sketches.Count)
Dim tBox As TextBox = sk.TextBoxes(1)
Logger.Info("tBox: " & If(tBox Is Nothing, "-", "+"))

tBox.Style = tgtTxtStyle ' Unspecified error (0x80004005 (E_FAIL))

 

 

 

What I'm missing?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
274 Views
4 Replies
Replies (4)
Message 2 of 5

J-Camper
Advisor
Advisor

If the "TextStyle.StyleLocation" is not local/both then my first thought would be that you need to use the "TextStyle.ConvertToLocal" method before trying to use/Copy the style in a part file.  It sounds like it is only in the library if you don't see it in a drop down, but see it in the styles manger [unless styles manger is only showing local styles when you say it is visible there]. 

0 Likes
Message 3 of 5

Maxim-CADman77
Advisor
Advisor

I've initially wrote that style is Local.

Here is the direct proof with line

MsgBox(tgtTxtStyle.StyleLocation, ,"tgtTxtStyle.StyleLocation:")

MaximCADman77_0-1741501447868.png

MaximCADman77_1-1741501556663.png

 

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 4 of 5

Ivan_Sinicyn
Advocate
Advocate

 

It looks like there are restrictions on style reassignments for created text and it has to be recreated

 

 

 

Option Explicit On

' Define unique text style name with timestamp
Dim tgtTxtStyleName As String = "My-Sketch-Text-Style-" & Now.ToString("yyyyMMddHHmmss")
Dim ptDoc As PartDocument = ThisDoc.Document ' Get current part document
Dim ptDef As ComponentDefinition = ptDoc.ComponentDefinition ' Get component definition

' Set ISO annotation standard if not already set
If ptDoc.ActiveAnnotationsStandard <> "ISO" Then ptDoc.ActiveAnnotationsStandard = "ISO"

Dim srcTxtStyle, tgtTxtStyle As TextStyle ' Declare source and target text style variables
Try
    ' Try to get existing style by name
    tgtTxtStyle = ptDoc.TextStyles(tgtTxtStyleName)
Catch
    ' Find source style starting with "1:Sketch Text (ISO)"
    srcTxtStyle = (From tStyle As TextStyle In ptDoc.TextStyles Where tStyle.InternalName.StartsWith("1:Sketch Text (ISO")).FirstOrDefault
    ' Create a copy of source style with new name
    tgtTxtStyle = srcTxtStyle.Copy(tgtTxtStyleName)
    ' Copy font and font size from source to target
    tgtTxtStyle.Font = srcTxtStyle.Font
    tgtTxtStyle.FontSize = srcTxtStyle.FontSize
End Try

' Log whether target style was created
Logger.Info("tgtTxtStyle created: " & If(tgtTxtStyle Is Nothing, "No", "Yes"))
' Log target style details
Logger.Info("tgtTxtStyle Name: " & tgtTxtStyle.Name)
Logger.Info("tgtTxtStyle Font: " & tgtTxtStyle.Font)
Logger.Info("tgtTxtStyle FontSize: " & tgtTxtStyle.FontSize)

' Get the last planar sketch in the part
Dim sk As PlanarSketch = ptDef.Sketches(ptDef.Sketches.Count)
' Get the first text box in the sketch
Dim tBox As TextBox = sk.TextBoxes(1)
' Log whether text box exists and its details
Logger.Info("tBox exists: " & If(tBox Is Nothing, "No", "Yes"))
Logger.Info("tBox Current Text: " & tBox.Text)
Logger.Info("tBox Current Style: " & tBox.Style.Name)

' Recreate the text box with new style
Dim currentText As String = tBox.Text ' Save current text
Dim rangeBox As Box2d = tBox.RangeBox ' Get bounding box of text
' Calculate text height from RangeBox
Dim textHeight As Double = rangeBox.MaxPoint.Y - rangeBox.MinPoint.Y
' Log found coordinates and height
Logger.Info("Found coordinates (MinPoint): X=" & rangeBox.MinPoint.X & ", Y=" & rangeBox.MinPoint.Y)
Logger.Info("Original text height: " & textHeight)
' Adjust Y coordinate to align top-left
Dim position As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(rangeBox.MinPoint.X, rangeBox.MinPoint.Y + textHeight)
' Log inserted coordinates
Logger.Info("Inserted coordinates (Top-Left): X=" & position.X & ", Y=" & position.Y)
tBox.Delete() ' Delete original text box
' Create new text box with saved text and new style
tBox = sk.TextBoxes.AddFitted(position, currentText, tgtTxtStyle)
' Log new style of recreated text box
Logger.Info("tBox recreated with new style: " & tBox.Style.Name)

' Update the document to reflect changes
ptDoc.Update()
Logger.Info("Document updated")
INV 2025.3
Message 5 of 5

Maxim-CADman77
Advisor
Advisor

Thank you - it slipped my attention that Text Style could be directly set during TextBox creation by means of API (which is beneficial in comparison to manual process).

 

Dear @johnsonshiue could you clarify why the Text Style created with API (and available through API) is not available within manual TextBox creation (I mean in Style drop-down list)?

MaximCADman77_0-1741551229251.png

Isn't it a defect?

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes