variables to parameters of an assembly

variables to parameters of an assembly

18297BQ37M
Explorer Explorer
437 Views
4 Replies
Message 1 of 5

variables to parameters of an assembly

18297BQ37M
Explorer
Explorer

Hello, I am new to inventor and I would like to pass my text type variables to parameters of an assembly, but when I execute it, it does not pass the variables to the inventor.
If someone could help me or pass a tutorial to give me a reference, it would be very helpful.
I attach my code and my form next to where I want it to be displayed in inventor.

Imports System
Imports System.Type
Imports System.Activator
Imports System.Runtime.InteropServices
Imports Inventor
Public Class Form1
Dim _invApp As Inventor.Application 'Variable que hace referencia a inventor la cual se utilizará para proporcionar un acceso rápido al objeto de aplicación de Inventor
Dim _started As Boolean = False
Dim Panel_PanelRef As String
Dim Panel_PanelWidth As String
Dim Panel_PanelHeight As String
Dim Panel_PanelThickness As String
Dim oPartDoc As PartDocument

Public Sub New()

' Esta llamada es requerida por el diseñador.
InitializeComponent()

'Añadir cualquier inicialización después de la llamada InitializeComponent()
Try
_invApp = Marshal.GetActiveObject("Inventor.Application") 'Esta variable contiene un objeto de tipo inventor
Catch ex As Exception
Try
Dim invAppType As Type =
GetTypeFromProgID("Inventor.Application")

_invApp = CreateInstance(invAppType)
_invApp.Visible = True

_started = True

Catch ex2 As Exception
MsgBox(ex2.ToString())
MsgBox("Unable to get or start Inventor")
End Try
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Panel_PanelRef = TextBox1.Text
Panel_PanelWidth = TextBox2.Text
APanel_PanelHeight = TextBox3.Text
Panel_PanelThickness = TextBox4.Text



Try
_invApp = Marshal.GetActiveObject("Inventor.Application")


Catch ex As Exception
MessageBox.Show("Cannot connect to Inventor")
End Try



oPartDoc = _invApp.ActiveDocument

oPartDoc.ComponentDefinition.Parameters.Item("API_Panel_PanelRef").Expression = Panel_PanelRef
oPartDoc.ComponentDefinition.Parameters.Item("API_Panel_PanelWidth").Expression = Panel_PanelWidth
oPartDoc.ComponentDefinition.Parameters.Item("API_APanel_PanelHeight").Expression = APanel_PanelHeight
oPartDoc.ComponentDefinition.Parameters.Item("API_Panel_PanelThickness").Expression = Panel_PanelThickness

oPartDoc.Update()

End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
' Esto cerrará la sesión de Inventor iniciada cuando se inició el formulario
If _started Then
_invApp.Quit()
End If
_invApp = Nothing
'Aquí se sale de la sesión de Inventor si _started está establecido en True (es decir, fue el código el que inició la sesión, ya que uno aún no se estaba ejecutando).
End Sub

End Class

 

18297BQ37M_0-1662674531802.png

18297BQ37M_1-1662674712566.png

 

18297BQ37M_2-1662674776159.png

 

0 Likes
438 Views
4 Replies
Replies (4)
Message 2 of 5

A.Acheson
Mentor
Mentor

Is it possible to edit your original post and place the code into the code editor and select vb code. When pasted directly in the post the formatting is all messed up and difficult to read. The button for code editor looks like </>.

 

Do you know  what errors your code has? Is there errors in the debug section of visual studio? Are you getting string values from the form to a message box? Is inventor opened allready to a document? If not there is no might not be an active document. You could use Documents.Open to open a specific document. If that works your problem is further down.

 

_invApp.Documents.Open("C\User\TempPart.ipt,True)

 

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

A.Acheson
Mentor
Mentor

A few observations.

  1. There is a call to sub routine. Make sure there is a sub routine otherwise remove. 
InitializeComponent()

      2. Declaration of String is this "Panel_PanelHeight

Dim Panel_PanelHeight As String

Written later incorrectly as 

Dim APanel_PanelHeight As String

 

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

18297BQ37M
Explorer
Explorer

tells me that the parameter is incorrect

18297BQ37M_0-1662738345875.png

Imports System
Imports System.Type
Imports System.Activator
Imports System.Runtime.InteropServices
Imports Inventor
Public Class Form1
    Dim _invApp As Inventor.Application 'Variable que hace referencia a inventor la cual se utilizará para proporcionar un acceso rápido al objeto de aplicación de Inventor
    Dim _started As Boolean = False

    Dim oPartDoc As PartDocument
    Dim assemblies As AssemblyDocument

    Public Sub New()

        ' Esta llamada es requerida por el diseñador.
        InitializeComponent()



        'Añadir cualquier inicialización después de la llamada InitializeComponent()
        Try
            _invApp = Marshal.GetActiveObject("Inventor.Application") 'Esta variable contiene un objeto de tipo inventor 

        Catch ex As Exception
            Try
                Dim invAppType As Type =
                    GetTypeFromProgID("Inventor.Application")

                _invApp = CreateInstance(invAppType)
                _invApp.Visible = True

                _started = True

            Catch ex2 As Exception
                MsgBox(ex2.ToString())
                MsgBox("Unable to get or start Inventor")
            End Try
        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Panel_PanelRef As String
        Dim Panel_PanelWidth As String
        Dim Panel_PanelHeight As String
        Dim Panel_PanelThickness As String

        Panel_PanelRef = TextBox1.Text
        Panel_PanelWidth = TextBox2.Text
        Panel_PanelHeight = TextBox3.Text
        Panel_PanelThickness = TextBox4.Text



        Try
            _invApp = Marshal.GetActiveObject("Inventor.Application")


        Catch ex As Exception
            MessageBox.Show("Cannot connect to Inventor")
        End Try


        assemblies = _invApp.ActiveDocument

        'oPartDoc = _invApp.ActiveDocument

        assemblies.ComponentDefinition.Parameters.Item("API_Panel_PanelRef").Expression = Panel_PanelRef
        assemblies.ComponentDefinition.Parameters.Item("API_Panel_PanelWidth").Expression = Panel_PanelWidth
        assemblies.ComponentDefinition.Parameters.Item("API_APanel_PanelHeight").Expression = Panel_PanelHeight
        assemblies.ComponentDefinition.Parameters.Item("API_Panel_PanelThickness").Expression = Panel_PanelThickness

        assemblies.Update()

    End Sub
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        ' Esto cerrará la sesión de Inventor iniciada cuando se inició el formulario
        If _started Then
            _invApp.Quit()
        End If
        _invApp = Nothing
        'Aquí se sale de la sesión de Inventor si _started está establecido en True (es decir, fue el código el que inició la sesión, ya que uno aún no se estaba ejecutando).
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        _invApp.Documents.Open("C:\Users\Practicante1\Downloads\DoorAndFrame-20220908-1155\DoorAndFrame\DoorAndFrameAssemblyMaster.iam", True)
    End Sub
End Class
0 Likes
Message 5 of 5

A.Acheson
Mentor
Mentor

I think the issue here is "Parameter.Expression" is used for changing the expression of a formula and is failing for a text parameter. 

Use "Parameter.Value" Instead for modifying values not requiring an expression. 

 

'Option 1

' Declare as Inventor parameter. Generic and can be any parameter
Dim TempParameter As Inventor.Parameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("Temp")
TempParameter.Value = "Hello"

'Option 2
' Declare as specifically as Inventor parameter. 
Dim TempUserParameter As Inventor.UserParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Userparameters.Item("Temp")
TempUserParameter.Value = "Hello"

 

AAcheson_0-1662742497394.png

 

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