Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bug report when setting Face Name

5 REPLIES 5
Reply
Message 1 of 6
They_Call_Me_Jake
348 Views, 5 Replies

Bug report when setting Face Name

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.

5 REPLIES 5
Message 2 of 6
Stakin
in reply to: They_Call_Me_Jake

the code can run here,Is the problem with your Inventor App?Or your System setting?

Message 3 of 6

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.

JelteDeJong_0-1633620417177.png

 

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.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 6
Stakin
in reply to: JelteDeJong

Your code can't run properly,

No error,No result. it seems no run it ever.

Message 5 of 6
JelteDeJong
in reply to: Stakin

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.

EESignature


Blog: hjalte.nl - github.com

Message 6 of 6
Stakin
in reply to: JelteDeJong

wonderful,thank you very much

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report