InvApp is a normal Marshall reference that works for everything else in my .dll.
Dim thisDoc As PartDocument = InvApp.ActiveDocument
Dim oPartCompDef As PartComponentDefinition = thisDoc.ComponentDefinition
Dim frontFace As Face = thisDoc.SelectSet.Item(1)
Const iLogicAddinGuid As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
Dim addin As ApplicationAddIn = InvApp.ApplicationAddIns.ItemById(iLogicAddinGuid)
Dim iLogicAuto As Object = addin.Automation
Dim namedEntities As Object = iLogicAuto.GetNamedEntities(oPartCompDef.Document)
namedEntities.SetName(frontFace, "FrontFace")
Intermittently, an exception will throw on .SetName (ArgumentException): Controls created on one thread cannot be parented to a control on a different thread error.
This exception only happens if there are no existing named faces in the part. The exception appears to be related to creating the "Geometry" tab under the iLogic tab.
This bug is INTERMITTENT and I have no idea what causes it to stop happening. I have tried restarting the computer, starting in brand new parts in inventor, etc. I was seeing the bug happen reliably, and then suddenly it stopped happening with the exact same code. There is no multithreading in my application, although I had
Imports System.Threading at the top of my code, it has been grayed out the whole time and as such is not being used for anything.
the code can run here,Is the problem with your Inventor App?Or your System setting?
I can reproduce the problem. It seems that it's not possible to create the first named entity. Not sure how this is supposed to work. But it's possible to create named entities without calling the iLogic addin. Something becomes a named entity as soon as you put the correct attribute on it and add it to the list. that list is an attribute of the document. Below you will find an iLogic rule that will do this. But there is 1 small problem. The geometry tab will not show or get updated until you reopen the part.
Sub Main() Dim InvApp = ThisApplication Dim thisDoc As PartDocument = InvApp.ActiveDocument Dim oPartCompDef As PartComponentDefinition = thisDoc.ComponentDefinition Dim frontFace As Face = thisDoc.SelectSet.Item(1) addNameToFace(thisDoc, frontFace, "FrontFace1") End Sub Private Sub addNameToFace(doc As PartDocument, face As Face, name As String) Dim attSetName As String = "iLogicEntityNameSet" Dim attName As String = "iLogicEntityName" Dim att As Attribute = getAttribute(face, attSetName, attName) att.Value = name Dim attSetNameDoc = "iLogicEntityNameSet" Dim attNameDoc = "iLogicEntityNamesOrdered" Dim attDoc As Attribute = getAttribute(doc, attSetNameDoc, attNameDoc) If (attDoc.Value.contains(name) = False) Then Dim atts As AttributesEnumerator = doc.AttributeManager.FindAttributes(attSetName, attName) Dim entities As String = "" For Each itemAtt As Attribute In atts entities = entities & itemAtt.Value & Constants.vbTab Next attDoc.Value = entities End If End Sub Public Function getAttribute(objectWithAttributes As Object, attSetName As String, attName As String) As Attribute Dim attSet As AttributeSet If (objectWithAttributes.AttributeSets.NameIsUsed(attSetName)) Then attSet = objectWithAttributes.AttributeSets.Item(attSetName) Else attSet = objectWithAttributes.AttributeSets.Add(attSetName) End If Dim att As Attribute If attSet.NameIsUsed(attName) Then att = attSet.Item(attName) Else att = attSet.Add(attName, ValueTypeEnum.kStringType, "") End If Return att End Function
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com
Your code can't run properly,
No error,No result. it seems no run it ever.
Did you reopen the document after you run the rule? Only then the change will be visible. I created a new rule that will do everything for you. (including closing and reopening the file. I also found a bug that, in my previous rule) Anyway, it works for me.
Sub Main() Dim doc As PartDocument = ThisDoc.Document Dim face As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face") Dim name As String = InputBox("Give a name", "Name", "Face ") addNameToFace(doc, face, name) Dim fileName = doc.FullFileName doc.Save() doc.Close() ThisApplication.Documents.Open(fileName) End Sub Private Sub addNameToFace(doc As PartDocument, face As Face, name As String) Dim attSetName As String = "iLogicEntityNameSet" Dim attName As String = "iLogicEntityName" Dim att As Attribute = getAttribute(face, attSetName, attName) att.Value = name Dim refKey() As Byte = New Byte() {} face.CreatedByFeature.GetReferenceKey(refKey) Dim refKeyString = doc.ReferenceKeyManager.KeyToString(refKey) Dim attFeatureName As String = "iLogicEntityNameFeatureName" Dim attFeature As Attribute = getAttribute(face, attSetName, attFeatureName) attFeature.Value = refKeyString Dim attSetNameDoc = "iLogicEntityNameSet" Dim attNameDoc = "iLogicEntityNamesOrdered" Dim attDoc As Attribute = getAttribute(doc, attSetNameDoc, attNameDoc) If (attDoc.Value.contains(name) = False) Then Dim atts As AttributesEnumerator = doc.AttributeManager.FindAttributes(attSetName, attName) Dim entities As String = "" For Each itemAtt As Attribute In atts entities = entities & itemAtt.Value & Constants.vbTab Next attDoc.Value = entities End If End Sub Public Function getAttribute(objectWithAttributes As Object, attSetName As String, attName As String) As Attribute Dim attSet As AttributeSet If (objectWithAttributes.AttributeSets.NameIsUsed(attSetName)) Then attSet = objectWithAttributes.AttributeSets.Item(attSetName) Else attSet = objectWithAttributes.AttributeSets.Add(attSetName) End If Dim att As Attribute If attSet.NameIsUsed(attName) Then att = attSet.Item(attName) Else att = attSet.Add(attName, ValueTypeEnum.kStringType, "") End If Return att End Function
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com