Creating a dynamic model annotation of a user parameter using XML tags

Creating a dynamic model annotation of a user parameter using XML tags

Erumb_Tony
Contributor Contributor
518 Views
12 Replies
Message 1 of 13

Creating a dynamic model annotation of a user parameter using XML tags

Erumb_Tony
Contributor
Contributor

I need to create a model general note that is dynamically linked to a user parameter that contains text. I have referred to the official documentation and tried various different combinations of xml tags, yet always get the "Incorrect parameter" error.

 

I also used the reverse method to get the logger to print the formatted text for me by using 'Thisapplication.Commandmanager.pick(kModelannotationFilter, "Pick your model annotation") and just gave that exact same output as input to my code - yet couldn't get it to work. I continue to get the "incorrect parameter" error.

 

Have a look at my code:

Dim Pdoc as Document = Thisapplication.activedocument
Dim MAS As ModelAnnotations = Pdoc.ComponentDefinition.ModelAnnotations
Dim MGNS As ModelGeneralNotes = MAS.ModelGeneralNotes
Dim AnnotationText As String = "<Parameter Resolved='True' ComponentIdentifier='C:\Workingfolder\Saddle.ipt' Name='BRS' 
Precision='3'></Parameter>"
'The above text is exactly what I got when using logger to print the formatted text
'BRS is the name of the user parameter and it is a multivalue with two values 'General'and 'custom'
Dim MGN As ModelGeneralNote 

Dim MGNDef As ModelGeneralNoteDefinition = MGNS.CreateDefinition(AnnotationText, True, kUpperLeftQuadrant)
Dim NewMGN As ModelGeneralNote = MGNS.Add(MGNDef)
NewMGN.Name = "Standard-Indicator"
NewMGN.Visible = True

 

0 Likes
519 Views
12 Replies
Replies (12)
Message 2 of 13

jjstr8
Collaborator
Collaborator

Try adding <StyleOverride/> to the beginning of your text.

Dim AnnotationText As String = "<StyleOverride/><Parameter Resolved='True' ComponentIdentifier='C:\Workingfolder\Saddle.ipt' Name='BRS' 
Precision='3'></Parameter>"

 

I'm noticing that this will trigger an info message if you later edit the note. It stops after closing and reopening the document, so I assume Inventor does some cleanup. Also, you probably already realized that it doesn't update if the parameter in the part changes.

 

jjstr8_2-1745872634897.png

 

@johnsonshiue : Is there a better syntax to get it to take the formatted text, or is this a bug?

 

Message 3 of 13

Erumb_Tony
Contributor
Contributor

I tried it, but I still get this error

 

Erumb_Tony_0-1745905756217.png

 

0 Likes
Message 4 of 13

jjstr8
Collaborator
Collaborator

You only shared 12 lines of code, so I don't know what's triggering the error. The only time it errors for me is if the file name is misspelled, or after running it a second time without deleting the first annotation. You can't have duplicate named annotations. Could you indicate which line of code is generating the error? It looks like this rule would apply only to the file it's in. I assume you already planned to use Pdoc.FullFileName instead of a hard-coded file name.

0 Likes
Message 5 of 13

Erumb_Tony
Contributor
Contributor

Sorry, I forgot I added a for each loop in between just to remove any existing annotation. So here is the updated code.

Dim Pdoc as document = ThisApplication.ActiveDocument
Dim MAS As ModelAnnotations = Pdoc.ComponentDefinition.ModelAnnotations
Dim MGNS As ModelGeneralNotes = MAS.ModelGeneralNotes

Dim AnnotationText As String = "<Parameter Resolved='True' ComponentIdentifier='C:\Workingfolder\Saddle.ipt' Name='BRS' Precision='3'</Parameter>"

For Each MGN As ModelGeneralNote In MGNS
	If MGN.Name = "Standard-Indicator"
		MGN.Delete
	End If
Next

Dim MGNDef As ModelGeneralNoteDefinition = MGNS.CreateDefinition(AnnotationText,True,kUpperLeftQuadrant)
Dim NewMGN As ModelGeneralNote = MGNS.Add(MGNDef)
NewMGN.Name = "Standard-Indicator"
NewMGN.Visible = True

The error is on this line:

Dim NewMGN As ModelGeneralNote = MGNS.Add(MGNDef)

 And yes, I did plan to use Pdoc.FullFileName - but that is another concern for me - I don't know how the HTML tag interpret Pdoc.FullFileName ? Should it be something like this ?

 

Dim AnnotationText as string = "<Parameter Resolved='True' ComponentIdentifier=' " & Pdoc.fullfilename & " ' Name = 'BRS' Precision='3'</Parameter>"

Thanks 🙂

Message 6 of 13

jjstr8
Collaborator
Collaborator

Yes, that works for the file name. Honestly, I'm having trouble re-creating your original error. I guess it happened after running the rule a second time and I didn't pay attention to the fact that you were renaming it and that it would be a duplicate. That being said, the <StyleOverride/> is certainly not needed, but the info popup still comes up though.

0 Likes
Message 7 of 13

Erumb_Tony
Contributor
Contributor

@jjstr8, sorry for the delay. I do get the info popup, but instead of getting the actual value of the parameter, I get the entire text - everything below on the annotation

"<Parameter Resolved='True' ComponentIdentifier='C:\Workingfolder\Saddle.ipt' Name='BRS' Precision='3'</Parameter>"

It does not work even on the first try. But does it work on your computer ?

 

0 Likes
Message 8 of 13

Stakin
Collaborator
Collaborator
"<Parameter Resolved='True' ComponentIdentifier='C:\Workingfolder\Saddle.ipt' Name='BRS' Precision='3'></Parameter>"

missing a ">" 

Message 9 of 13

jjstr8
Collaborator
Collaborator

Nice catch!

0 Likes
Message 10 of 13

Erumb_Tony
Contributor
Contributor

Thanks, I added the '>'. But for some reason it still won't work. I get an error on the same line. Same error as before.😐

Dim NewMGN As ModelGeneralNote = MGNS.Add(MGNDef)

 

0 Likes
Message 11 of 13

Stakin
Collaborator
Collaborator

I tried the code, if the file path or the parameter name are not exist,it will break at this line.

you can change the formattedtext to "Text test",the code will run over.

 

maybe you should check file path and the parameter name.

'C:\Workingfolder\Saddle.ipt' Name='BRS'

 

And i tried it in another file ,it will break at this line.

 

0 Likes
Message 12 of 13

Stakin
Collaborator
Collaborator

In another file,make a linkparameter from source file

I referred to the post " https://forums.autodesk.com/t5/inventor-programming-ilogic/create-a-user-parameter-link-between-2-pa...".

This version can run in any inventor document to add modelgeneralnote from a resource document parameter. 

Sub Main()
	Dim Pdoc As Document = ThisApplication.ActiveDocument
	Pdoc.Update
	Dim MAS As ModelAnnotations = Pdoc.ComponentDefinition.ModelAnnotations
	Dim MGNS As ModelGeneralNotes = MAS.ModelGeneralNotes
	Dim oSourceFile As String ="C:\Workingfolder\Saddle.ipt"'
	Dim oSourceParaName As String = "BRS"
        Dim oNoteName As string="Standard-Indicator"
	Dim oDerivedParamTable As Inventor.DerivedParameterTable
	Dim oNeedsDerivedParamTable As Inventor.DerivedParameterTable
	Dim oSourceParameter As Inventor.Parameter
	Dim orefFiles As List(Of String) = Pdoc.AllReferencedDocuments.Cast(Of Document).Select(Function(x) x.FullDocumentName).ToList()
	If oSourceFile <> Pdoc.FullFileName And orefFiles.Contains(oSourceFile) And Pdoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
		For Each oDerivedParamTable In Pdoc.ComponentDefinition.Parameters.DerivedParameterTables
			Dim oRefDocDesc As DocumentDescriptor
			oRefDocDesc = oDerivedParamTable.ReferencedDocumentDescriptor
			If Strings.InStr(1, oRefDocDesc.FullDocumentName, sSourceFile, vbBinaryCompare) > 0 Then
				oNeedsDerivedParamTable = oDerivedParamTable
				oSourceFile = Pdoc.FullFileName
				For Each oPara As Inventor.Parameter In oNeedsDerivedParamTable.LinkedParameters
					If oPara.Name.Contains(oSourceParaName) Then
						oSourceParameter = oPara
						oSourceParaName = oPara.Name
						Exit For
					End If
				Next
				Exit For
			End If
		Next
	End If
	If oSourceFile <> Pdoc.FullFileName And Not orefFiles.Contains(oSourceFile) Then
		Dim tmDoc As Document
		If System.IO.File.Exists(oSourceFile) Then
			tmDoc = ThisApplication.Documents.Open(oSourceFile, False)
		Else
			MsgBox($"The file ‘{oSourceFile}’ don't Exist!“)
			Exit Sub
		End If
		Dim oParaMeter As Inventor.Parameter
		Try
			oParaMeter = tmDoc.ComponentDefinition.Parameters.Item(oSourceParaName)
		Catch
			MsgBox($"The parameter ‘{oSourceParaName}’ don't Exist!“)
			Exit Sub
		End Try
		Dim oParamsToLink As ObjectCollection
		oParamsToLink = ThisApplication.TransientObjects.CreateObjectCollection
		oParamsToLink.Add(oParaMeter)
		If oNeedsDerivedParamTable Is Nothing Then
			oNeedsDerivedParamTable = Pdoc.ComponentDefinition.Parameters.DerivedParameterTables.Add2(oSourceFile, oParamsToLink)
		Else
			oNeedsDerivedParamTable.LinkedParameters = oParamsToLink
		End If
		If oSourceParameter Is Nothing Then
			For Each oPara As Inventor.Parameter In oNeedsDerivedParamTable.LinkedParameters
				If oPara.Name.Contains(oSourceParaName) Then
					oSourceParaName = oPara.Name
					Exit For
				End If
			Next
		End If
		tmDoc.Close
		oSourceFile = Pdoc.FullFileName
	End If
	Pdoc.Update
	Dim AnnotationText As String = "<Parameter Resolved='True' ComponentIdentifier='" & oSourceFile & "' Name='" & oSourceParaName & "' Precision='3'></Parameter>"
	For Each MGN As ModelGeneralNote In MGNS
		If MGN.Name = oNoteName
			MGN.Delete
		End If
	Next
	Dim MGNDef As ModelGeneralNoteDefinition = MGNS.CreateDefinition(AnnotationText, True, kUpperLeftQuadrant)
	Dim NewMGN As ModelGeneralNote = MGNS.Add(MGNDef)
	NewMGN.Name = oNoteName
	NewMGN.Visible = True
End Sub

“ 

0 Likes
Message 13 of 13

marcin_otręba
Advisor
Advisor

hi, i tried the code and only situation when i got an error in this line is when file name is not the pdoc filename so i suggest to change the code to:

 

Dim Pdoc As Document = ThisApplication.ActiveDocument
Dim MAS As ModelAnnotations = Pdoc.ComponentDefinition.ModelAnnotations
Dim MGNS As ModelGeneralNotes = MAS.ModelGeneralNotes

Dim AnnotationText As String = "<Parameter Resolved='True' ComponentIdentifier='" & Pdoc.FullFileName & "' Name='BRS' Precision='3' ></Parameter>"

For Each MGN As ModelGeneralNote In MGNS
	If MGN.Name = "Standard-Indicator"
		MGN.Delete
	End If
Next

Dim MGNDef As ModelGeneralNoteDefinition = MGNS.CreateDefinition(AnnotationText, True, kUpperLeftQuadrant)

Dim NewMGN As ModelGeneralNote = MGNS.Add(MGNDef)
NewMGN.Name = "Standard-Indicator"
NewMGN.Visible = True

it works without issue. 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes