Code gives error when using modelstates

Code gives error when using modelstates

K.clement
Contributor Contributor
205 Views
2 Replies
Message 1 of 3

Code gives error when using modelstates

K.clement
Contributor
Contributor

Hello

 

Im using inventor 2022 and I have a bug in my code that I do not understeand

This code works great for normal parts, but when my part has multiple modelstates, and 

I run the code not 1 BUT 2x , the second time i get a error at line "Dim oParameter as....."

 

    Sub TestSub()
        Dim oDoc As Document = Nothing
        Dim oOcc As ComponentOccurrence = Nothing
        Dim oTG As TransientGeometry = App.TransientGeometry
        Dim oMatrix As Matrix = oTG.CreateMatrix
        Dim Path As String = "C:\...\example.ipt"
        oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0))
        'oMatrix.SetToRotation(Angle, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
        oOcc = App.ActiveDocument.ComponentDefinition.Occurrences.Add(Path, oMatrix)
        oDoc = oOcc.Definition.Document

        oDoc.Update2()
        Dim oParameter As Parameter = oDoc.ComponentDefinition.Parameters.Item("myParam")
        oParameter.Value = "10"

        oDoc.Update2()
        oDoc.Save2()
        oDoc.Close()
        App.ActiveDocument.Update2()

    End Sub

 

0 Likes
Accepted solutions (1)
206 Views
2 Replies
Replies (2)
Message 2 of 3

BM_Ashraf
Advocate
Advocate
Accepted solution

Hi,

Try this code 🙂

'Done by MechDS (zezo7790@gmail.com) 
'This ilogic Rule must be triggered while an Assembly Document is opend 
'It Adds a Part file to the assembly and changes a specific Parameter 

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oCDef As AssemblyComponentDefinition=  oDoc.ComponentDefinition
Dim oPartDoc As PartDocument = ThisApplication.Documents.Open("C:\Users\Public\TEST\2.ipt", False)
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Call oMatrix.SetTranslation(oTG.CreateVector(0, 0, 0), False)
Dim oOcc As ComponentOccurrence= oCDef.Occurrences.Add(oPartDoc.FullDocumentName, oMatrix) 
Dim oParameter As Parameter = oPartDoc.ComponentDefinition.Parameters.Item("myParam")
oParameter.Value = 10
oPartDoc.Update2
oPartDoc.Save2
oDoc.Update2

zezo15330_0-1663761433257.png

 

 

enjoy it !

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

0 Likes
Message 3 of 3

K.clement
Contributor
Contributor

Thx, this helped a lot, weird that 

Dim oDoc As Document = CType(App.Documents.Open(Path, False), Document)

instead of 

oDoc = oOcc.Definition.Document

fixed it 😮

I have no idea why, but ty