Import DWG Component to Sketch in Inventor API VB.NET

Import DWG Component to Sketch in Inventor API VB.NET

florian_wenzel
Advocate Advocate
480 Views
2 Replies
Message 1 of 3

Import DWG Component to Sketch in Inventor API VB.NET

florian_wenzel
Advocate
Advocate

Hi,

 

Inventor 2022

API Visual Studio

 

I try to use the Sample Code to import a DWG Sketch into  Inventor Sketch.

This Code works with Inventor API (with VBA), but when i want to use it in Visual Studio with VB.NET, then i got some Problem.

 

This is My CODE:

Imports System.Runtime.InteropServices
Imports Inventor
Imports Microsoft.Win32

Module CommandFunctionButton_04
    Public Sub CommandFunctionfweButton_04()

        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry
        Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents

        Dim oImportedCompDef As ImportedComponentDefinition
        oImportedCompDef = oRefComponents.ImportedComponents.CreateDefinition("C:\AutoCAD\ACADDWG.dwg")
        Dim oImportedDWGDef As ImportedDWGComponentDefinition

        If oImportedCompDef.Type = ObjectTypeEnum.kImportedDWGComponentDefinitionObject Then
            oImportedDWGDef = oImportedCompDef

        End If

        Dim oMatrix As Matrix
        oMatrix = oTG.CreateMatrix
        oMatrix.SetTranslation(g_inventorApplication.oTG.CreateVector(0, 0, 1))
        oImportedDWGDef.Transformation = oMatrix

        Dim oImportedComponent As ImportedComponent
        oImportedComponent = oRefComponents.ImportedComponents.Add(oImportedDWGDef)

        Dim oImportedDWGComponent As ImportedDWGComponent

        If oImportedComponent.Type = ObjectTypeEnum.kImportedDWGComponentObject Then
            oImportedDWGComponent = oImportedComponent

            Dim oSk As PlanarSketch
            oSk = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

            Dim oDWGModelSpaceDef As DWGBlockDefinition
            oDWGModelSpaceDef = oImportedDWGComponent.ModelSpaceDefinition

            Dim oDWGEntity As DWGEntity
            For Each oDWGEntity In oDWGModelSpaceDef.Entities

                Call oSk.AddByProjectingEntity(oDWGEntity)
            Next
        End If

    End Sub
End Module

 

I got a Problem with :

oImportedDWGDef.Transformation = oMatrix

 

Error:

Using Variable, befor give any Value, Value null

 

florianwenzelEJNZZ_0-1651173437931.png

 

Other Problem:

The Public Member oTG for type Aplication was not Found.

 

florianwenzelEJNZZ_1-1651173574971.png

 

I dont know, where is the Problme.

 

Thanks for any Sugestion

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

JelteDeJong
Mentor
Mentor
Accepted solution

The first "Variable 'oImportedDWGDef' is used before it has been assigned a value. A null reference exception could result at runtime." is just a warning. You can solve it by giving it a value. (that value can be nothing 😉

 

The second problem is a real problem. g_inventorApplication does not have a property oTg. But oTg is a variable in the function. So you don't need to put  g_inventorApplication in front of it.

 

Try it like this:

Module CommandFunctionButton_04
    Private g_inventorApplication As Inventor.Application

    Public Sub CommandFunctionfweButton_04()

        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry
        Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents

        Dim oImportedCompDef As ImportedComponentDefinition
        oImportedCompDef = oRefComponents.ImportedComponents.CreateDefinition("C:\AutoCAD\ACADDWG.dwg")
        Dim oImportedDWGDef As ImportedDWGComponentDefinition '  = Nothing

        If oImportedCompDef.Type = ObjectTypeEnum.kImportedDWGComponentDefinitionObject Then
            oImportedDWGDef = oImportedCompDef

        End If

        Dim oMatrix As Matrix
        oMatrix = oTG.CreateMatrix()
        oMatrix.SetTranslation(oTG.CreateVector(0, 0, 1))
        oImportedDWGDef.Transformation = oMatrix

        Dim oImportedComponent As ImportedComponent
        oImportedComponent = oRefComponents.ImportedComponents.Add(oImportedDWGDef)

        Dim oImportedDWGComponent As ImportedDWGComponent

        If oImportedComponent.Type = ObjectTypeEnum.kImportedDWGComponentObject Then
            oImportedDWGComponent = oImportedComponent

            Dim oSk As PlanarSketch
            oSk = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

            Dim oDWGModelSpaceDef As DWGBlockDefinition
            oDWGModelSpaceDef = oImportedDWGComponent.ModelSpaceDefinition

            Dim oDWGEntity As DWGEntity
            For Each oDWGEntity In oDWGModelSpaceDef.Entities

                Call oSk.AddByProjectingEntity(oDWGEntity)
            Next
        End If

    End Sub
End Module

 

 

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 3 of 3

florian_wenzel
Advocate
Advocate

Hi,

Thanks Very Much

0 Likes