Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Trying to add text to an idw, unable to cast COM object of type System._ComObject

Rossco44
Explorer

Trying to add text to an idw, unable to cast COM object of type System._ComObject

Rossco44
Explorer
Explorer

Hey all,

I'm trying to add a text box to an idw file, but I'm getting the following error.

Rossco44_0-1669944236593.png

The weird part is that the code will open the file, and add the text box with the correct input, but THEN throws this error? It is happening within another subroutine that is mass-exporting PDFs from all passed idw files within the assembly that runs the rule.

 

Sub MakePDFFromDoc(oDocument As String, UserSelectedAction As Integer, folderName As String, jobNum As String, Qty As String)
 	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	
		
'	oMsg = MessageBox.Show(oDocument, "FolderName")
  
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	
	oFile = ThisApplication.Documents.Open(oDocument, True)
	
	'Create Job Number/Qty Text Box on the idw
	Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSketch As DrawingSketch = oDrawDoc.ActiveSheet.Sketches.Add
	oSketch.Edit
	
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	
	Dim JBText As String = "<" & jobNum & ">"
	Dim oTextBox As System.Windows.Forms.TextBox 
	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(2, 2), JBText)
	
	oSketch.ExitEdit

	oFullFileName = oFile.File.FullFileName
'	oMsg = MessageBox.Show(folderName, "oFullFileName")
	oPath = Left(oFullFileName, InStrRev(oFullFileName, "\") -1)
'	oMsg = MessageBox.Show(folderName, "oPath")
	oFileName = Right(oFullFileName, Len(oFullFileName) -InStrRev(oFullFileName, "\"))
'	oMsg = MessageBox.Show(folderName, "oFileName")
	oFilePart = Left(oFileName, InStrRev(oFileName, ".") -1)
'	oMsg = MessageBox.Show(folderName, "oFilePart")
 
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
 
	'get PDF target folder path
	oFolder = folderName
'	oMsg = MessageBox.Show(oFolder, "oFolder")
	oDirectoryName = System.IO.Path.GetDirectoryName(oFullFileName)
'	oMsg = MessageBox.Show(oDirectoryName & oFolder, "oDirectoryName")
 
	'Check for the PDF folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
 
	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & oFilePart & ".pdf"
	 
	'Publish document
	If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
		oPDFAddIn.SaveCopyAs(oFile, oContext, oOptions, oDataMedium)'For PDF's
	End If
 
	If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
		oFile.SaveAs(oFolder & "\" & oFilePart & ".dwg", True) 'For DWG's
	End If

	oFile.Close
 
'------end of iLogic-------
End Sub

The idw file's full address is passed in. oFile was originally running as an invisible open, but I had to change it as it was trying to add the textbox to the assembly file haha.

 

 

0 Likes
Reply
572 Views
9 Replies
Replies (9)

A.Acheson
Mentor
Mentor

Here was the error line. 

Dim oTextBox As System.Windows.Forms.TextBox

And the correct declaration of the object variable

Dim oTextBox As Inventor.TextBox 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Rossco44
Explorer
Explorer

Amazing! Thank you, I figured it would be something simple I just didn't know about ahaha.
.
Off the top of your head, do you know where I could look for finding the text parameter settings (size/colour etc.)? I know as is it just uses the default style of the open doc.

0 Likes

A.Acheson
Mentor
Mentor

 

Textstyle object

An API sample using text style

 

Dim oStyle As TextStyle = oSketch.TextBoxes.Item(1).Style
oStyle.FontSize
oStyle.Color

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Rossco44
Explorer
Explorer

thanks for that.

Unfortunately I'm getting an incorrect parameter error when it reaches

Dim oStyle As TextStyle = oSketch.TextBoxes.Item(1).Style

if I split the line into a Dim and an assignment line, the error moves to the assignment line.

Also how do i utilise the color object, would I do oStyle.Color = Red, or oStyle.Color(Red)?

Thanks for any help

0 Likes

A.Acheson
Mentor
Mentor

Here is the set color method. And if you look to a sample here you can see how to add that in. The snippet required is shown below. 

 

Dim oStyle As TextStyle = oSketch.TextBoxes.Item(1).Style
' Change the highlight color to green.
Dim oGreen As Color = ThisApplication.TransientObjects.CreateColor(0, 255, 0) oStyle.Color = oGreen

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Rossco44
Explorer
Explorer

Thanks for showing the color creation.

I may have mis-represented the error there. The error itself is for the declaration of the "oStyle" variable, it produces a "The parameter is incorrect" error.

 

I've attached the whole iLogic script as a .txt so you can browse the whole function to understand the purpose, the oStyle declaration is currently commented out on line 155.

Thank you

0 Likes

Rossco44
Explorer
Explorer

Okay, so

Turns out the issue was simply trying to declare oStyle before the textbox had been created.

I've currently changed it so that it's just doing
oTextBox.Style.FontSize = 1

oTextBox.Style.Color = oRed

 

But my issue now is, how do I change the text within the new textbox without affecting other textboxes that already exist on the drawing?

0 Likes

A.Acheson
Mentor
Mentor

So to make an in file edit without adjusting the styles you will need to override the style. You can do this with formatted text. I suggest to make a text box manually then adjust text /font as required then check out its formatted text.

 

The rule below will target the first text box on a sketch on a drawing. 


Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim
oSketch As DrawingSketch = oDrawDoc.ActiveSheet.Sketches(1)
Dim oTextBox As Inventor.TextBox = oSketch.TextBoxes(1)
sFormattedText = InputBox("","FormattedText",oTextBox.FormattedText)

You can also just put the text in as a general note and eliminate the need for a sketch which is a little unnecessary. See example here.

The same principle of formatted text will apply to that object also. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

Rossco44
Explorer
Explorer

Thanks for all the help!

I did eventually come across some tucked away forum thread, turns out all i had to do was remove ".Style" from the oTextBox.Style.Color ahaha.
So that section looks like this now:

oFile = ThisApplication.Documents.Open(oDocument, True)
	
	'Create Job Number/Qty Text Box on the idw
	Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSketch As DrawingSketch = oDrawDoc.ActiveSheet.Sketches.Add
	Dim oRed As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
	
	oSketch.Edit
	
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
'	Dim sFormattedJB As String
	
	Dim StartOv As String = "<StyleOverride FontSize = '0.6' > "
	Dim EndOv As String = "</StyleOverride>	"
	Dim JBText As String =  jobNum & vbCrLf
	Dim QtyText As String = "QTY: " & Qty
	Dim oTextBox As Inventor.TextBox
	
	
	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(10, 4), StartOv & JBText & QtyText & EndOv)
	oTextBox.Color = oRed
	
	
	oSketch.ExitEdit

 

We played around with the <StyleOverride> controls as well, but I imagine having just done oTextBox.FontSize = 0.8 would have worked too.
Thanks again.

0 Likes