Importing an NX assembly and parts

Importing an NX assembly and parts

Anonymous
Not applicable
2,530 Views
4 Replies
Message 1 of 5

Importing an NX assembly and parts

Anonymous
Not applicable

Hi,

   I am trying to programatically import NX files. I took the sample code provided in the Inventor customization Sample Programs-->Translators-->Imports--Open an NX file suing the NX Translator Sample API Sample 

   But it is not working. There is no error. I have attached a sample NX assembly AssyCrane.prt (as a zip file). I have attached Visual Studio 2012 sample code.

   The attached inventor code, adds a button in Inventor Assembly mode. By clicking this button, the Inventor Customization (InventorSampleProg) gets launched.

   What am I doing wrong?

Thanks

Regards

PV Subramanian

0 Likes
Accepted solutions (1)
2,531 Views
4 Replies
Replies (4)
Message 2 of 5

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

Could you test in VBA? The following worked fine for me - I did not modify it apart from adding "Set" statements etc

Sub ImportNXFile()
    ' Set NX translator's CLSID and the NX file name.
    ' Dim strCLSID As String
    Dim strFileName As String
    ' The following statement is throwing error
    ' 'strCLSld' is not declared. It may be inaccessible due to its protection level.
    ' So allowed Visual Studio to add the declaration in line 8 and commented line 33
    strCLSld = "{93D506C4-8355-4E28-9C4E-C2B5F1EDC6AE}"

    ' Please set the full file name here, such as "C:\Part1.prt"
    strFileName = "C:\Temp\AssyCrane.prt"

    'If (Not (System.IO.file.Exists(strFileName))) Then
    '    Call MsgBox("Component File >" + strFileName + "< does not exist")
    '    Return
    'End If

    Dim oAddIns As ApplicationAddIns
    Set oAddIns = ThisApplication.ApplicationAddIns

    ' Find the NX translator, get the CLSID and activate it.
    Dim oTransAddIn As TranslatorAddIn
    Set oTransAddIn = oAddIns.ItemById(strCLSld)
    Call oTransAddIn.Activate

    ' Get the transient object and take it as a factory to produce other objects.
    Dim transientObj As TransientObjects
    Set transientObj = ThisApplication.TransientObjects

    ' Prepare the first parameter for Open(), the file name.
    Call MsgBox("NX File Import --- First Parameter")
    Dim file As DataMedium
    Set file = transientObj.CreateDataMedium
    file.FileName = strFileName

    ' Prepare the second parameter for Open(), the open type.
    Call MsgBox("NX File Import --- Second Parameter")
    Dim context As TranslationContext
    Set context = transientObj.CreateTranslationContext
    context.Type = kDataDropIOMechanism

    ' Prepare the third parameter for Open(), the options.
    Call MsgBox("NX File Import --- Third Parameter")
    Dim options As NameValueMap
    Set options = transientObj.CreateNameValueMap
    options.value("SaveComponentDuringLoad") = False
    options.value("SaveLocationIndex") = 0
    options.value("ComponentDestFolder") = ""
    options.value("SaveAssemSeperateFolder") = False
    options.value("AssemDestFolder") = ""
    options.value("ImportSolid") = True
    options.value("ImportSurface") = True
    options.value("ImportWire") = True
    options.value("ImportWorkPlane") = True
    options.value("ImportWorkAxe") = True
    options.value("ImportWorkPoint") = True
    options.value("ImportPoint") = True
    options.value("ImportAASP") = False
    options.value("ImportAASPIndex") = 0
    options.value("CreateSurfIndex") = 1
    options.value("GroupNameIndex") = 0
    options.value("GroupName") = ""
    options.value("ImportUnit") = 0
    options.value("CheckDuringLoad") = False
    options.value("AutoStitchAndPromote") = True
    options.value("AdvanceHealing") = False

    options.value("CHKSearchFolder") = True

    '100 is the search folder maximum that you can specify.
    'Assign separate search folder one by one.

    Dim searchFolder(3) As String
    searchFolder(0) = "C:\Temp\"
    'searchFolder(1) = "C:\Folder2\"
    options.value("SearchFolder") = searchFolder

    'Prepare the fourth parameter for Open(), the final document
    Call MsgBox("NX File Import --- Fourth Parameter")
    Dim sourceObj As Object

    'Open the NX file.
    Call oTransAddIn.Open(file, context, options, sourceObj)
    Call MsgBox("NX File Import --- Successfully Imported")
End Sub

What exact error do you get when you run your code?

Does import work fine for you when doing it through the UI?

Which Inventor version are you using?

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 3 of 5

Anonymous
Not applicable

Hi,

   I tried to copy your code into my code in Visual Studio Professional 2012.

   The "Set " keyword gets removed automatically. When I tried to add Set maually the editor does not accept it.

   I opened my code in Notepad. Replaced my code with your code. Then opened the solution in Visual Studio 2012; the Set keywords magically disappeared. I simultaneously opened the code in Notepad and the Set keyword is still there. Looks like something in Visual Studio is causing to remove the Set keyword automatically.    

 

What exact error do you get when you run your code?

    There is no error message. The message box pops up "NX File Import --- Fourth Parameter". After that there is no message.

Does import work fine for you when doing it through the UI?

    Yes.

Which Inventor version are you using?

    2014 Professional Build 170

Thanks

PV Subramanian

0 Likes
Message 4 of 5

Anonymous
Not applicable

Hi Adam,

   I tried your code in VBA and it works properly. The UG NX part CraneAssy is converted to Inventor.

   But when I tried to add it to the VB .NET, compile it and run it - there is error. I added Try ..... Catch ..... End Try for the Sub and the error is thrown at

        oTransAddIn.Open(file, context, options, sourceObj)

    I hope this gives you some clues.

    I have attached the exception thrown, which I wrote to a file.

Thanks

PV Subramanian

0 Likes
Message 5 of 5

wayne.brill
Collaborator
Collaborator
Accepted solution

Hi PV,

 

I was able to recreate the error with the VB project. (provided privately)

 

The solution I found was to change the code so that context.Type was set correctly.  With these changes the error went away ant the PRT file was opened in Inventor.

 

Also one of the main differences between VBA and VB is the use of Set statements to instantiate an object. (VB.NET does not use Set statements)

 
Friend Class ButtonOneCmd
    Inherits Command

    Dim strCLSld As String
    'WB commented
    ' Private Property kDataDropIOMechanism As IOMechanismEnum

    Public Overrides Sub StartCommand()

        ' call base command button's StartCommand (to also start interaction)
        ' MyBase.StartCommand()
        'MsgBox("Inventor Sample Prog. This is for NX Import Issue")
        'MessageBox.Show("Starting NX File Import")
        ImportNXFile()
    End Sub

    Public Overrides Sub SelectEvents_OnSelect(ByVal JustSelectedEntities As ObjectsEnumerator, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As Inventor.View)

        MsgBox("Please select an object.")

    End Sub
	

    Sub ImportNXFile()
        Try
            ' Set NX translator's CLSID and the NX file name.
            ' Dim strCLSID As String
            Dim strFileName As String
            ' The following statement is throwing error 
            ' 'strCLSld' is not declared. It may be inaccessible due to its protection level.
            ' So allowed Visual Studio to add the declaration in line 8 and commented line 33
            strCLSld = "{93D506C4-8355-4E28-9C4E-C2B5F1EDC6AE}"

            ' Please set the full file name here, such as "C:\Part1.prt"
            strFileName = "C:\Temp\AssyCrane.prt"

            If (Not (System.IO.File.Exists(strFileName))) Then
                MessageBox.Show("Component File >" + strFileName + "< does not exist")
                Return
            End If

            Dim oAddIns As ApplicationAddIns
            oAddIns = InventorApplication.ApplicationAddIns

            ' Find the NX translator, get the CLSID and activate it.
            Dim oTransAddIn As TranslatorAddIn
            oTransAddIn = oAddIns.ItemById(strCLSld)
            oTransAddIn.Activate()

            ' Get the transient object and take it as a factory to produce other objects.
            Dim transientObj As TransientObjects
            transientObj = InventorApplication.TransientObjects

            ' Prepare the first parameter for Open(), the file name.
            ' MessageBox.Show("NX File Import --- First Parameter")
            Dim file As DataMedium
            file = transientObj.CreateDataMedium
            file.FileName = strFileName

            ' Prepare the second parameter for Open(), the open type.
            ' MessageBox.Show("NX File Import --- Second Parameter")
            Dim context As TranslationContext
            context = transientObj.CreateTranslationContext
            'context.Type = kDataDropIOMechanism
            'WB added
            context.Type = IOMechanismEnum.kDataDropIOMechanism

            ' Prepare the third parameter for Open(), the options.
            '  MessageBox.Show("NX File Import --- Third Parameter")
            Dim options As NameValueMap
            options = transientObj.CreateNameValueMap
            options.Value("SaveComponentDuringLoad") = False
            options.Value("SaveLocationIndex") = 0
            options.Value("ComponentDestFolder") = ""
            options.Value("SaveAssemSeperateFolder") = False
            options.Value("AssemDestFolder") = ""
            options.Value("ImportSolid") = True
            options.Value("ImportSurface") = True
            options.Value("ImportWire") = True
            options.Value("ImportWorkPlane") = True
            options.Value("ImportWorkAxe") = True
            options.Value("ImportWorkPoint") = True
            options.Value("ImportPoint") = True
            options.Value("ImportAASP") = False
            options.Value("ImportAASPIndex") = 0
            options.Value("CreateSurfIndex") = 1
            options.Value("GroupNameIndex") = 0
            options.Value("GroupName") = ""
            options.Value("ImportUnit") = 0
            options.Value("CheckDuringLoad") = False
            options.Value("AutoStitchAndPromote") = True
            options.Value("AdvanceHealing") = False

            options.Value("CHKSearchFolder") = True

            '100 is the search folder maximum that you can specify.
            'Assign separate search folder one by one.

            Dim searchFolder(3) As String
            searchFolder(0) = "C:\Temp\"
            'searchFolder(1) = "C:\Folder2\"
            options.Value("SearchFolder") = searchFolder

            'Prepare the fourth parameter for Open(), the final document
            MessageBox.Show("NX File Import --- Fourth Parameter")
            Dim sourceObj As Object

            'Open the NX file.
            oTransAddIn.Open(file, context, options, sourceObj)
            MessageBox.Show("NX File Import --- Successfully Imported")
        Catch ex As Exception
            MessageBox.Show("Exception StackTrace To String" + ex.StackTrace.ToString)
            MessageBox.Show("Exception Current Exception " + ex.ToString)
            MessageBox.Show("Exception Message " + ex.Message)
            MessageBox.Show("Exception Source " + ex.Source)
            MessageBox.Show("Exception StackTrace " + ex.StackTrace)
            MessageBox.Show("Exception Targetsite " + ex.TargetSite.ToString)
            MessageBox.Show("Exception Targetsite Name " + ex.TargetSite.Name)
        End Try
    End Sub

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network