Showing results for 
Search instead for 
Do you mean 
Reply

NX Open question GetPreviewImage()

hi -

I have builded a program (NXOpen API , VB.net) , that find and open drawing specifications from current workpart ( NX Manager - TC).
When there are more than one specification ,they will be displayed in a listbox ,and the user can pick one of them ,and then the drawingpart opens ,and switch to Drafting Application after OK button is pressed.

Everything Works fine - even if the drawingfile's not is loaded ,when the program is launched .

But I would like to enhance the program ,so it could display the drafting-parts previewimage, when the displayed Items is picked in the listbox.

I have tried to test the command GetPreviewImage() , but as far as I can figure it out ,this command only allows the parts "model" preview.

But does someone here on PLM Community know a command, so it should be possible to "catch" the preview from a "drawing" file , (the same image as when NX standard FileOpen is used ,or the image in the History Bar).

Another way way to catch the preview could be to "ask" for the drawingspecification's preview in Teamcenter - could this be a option?

lklo

3 REPLIES

Re: NX Open question GetPreviewImage()

Hello,

 

I don't know if it's better solution for you but you can explore the NX file directly by Windows without the NX software. The NX file is a compound file and you can extract differents informations: Attributes, all previews (2D, 3D)...

I created a VB.net dll for to extract all attributes with NX with openmcdf.dll (DLL read the compounds files http://sourceforge.net/projects/openmcdf/) but you can to do same thing for to extract all previews (it's Jpeg format)

Also you can explore directly a NX file with 7zip or StucturedStorageExplorer.

 

 

Verify that all previews are been save in the NX file via "Displayed Part Properties".

 

Good luck

Re: NX Open question GetPreviewImage()

I don't know if this solves the original problem, but it sounds very interesting, anyway.

 

Are you willing to share your code?

山田
yamada

Re: NX Open question GetPreviewImage()

hello

 

Below the code of the Class (I don't translated the comments French to English.) and the code also for use the Class.
Don't forget of download the file openmcdf.dll (http://sourceforge.net/projects/openmcdf/)

 

Code of Console

Imports AttributesExternFile.AttributesExternFile
Imports AttributesExternFile
Module Module1

    Sub Main()
'Test les fonctions
        Dim dfsdfsd As Xml.XmlDocument = AskXMLAttributes("C:\Coussinet.prt")
        Dim dd() As PartAttribute = AttributesExternFile.AttributesExternFile.AskAttributes("C:\Coussinet.prt")
        Dim ddd As PartAttribute = AskAttribute("C:\Coussinet.prt", "VOLeeUME")
        Dim dfddd As PartAttribute = AskAttribute("C:\Coussinet.prt", "VOLUME")
        Dim dddd As PartAttribute = AskAttribute("C:\Coussinet.prt", "voLume", xmlAttributeCase.Lower)
        Dim ddddd As PartAttribute = AskAttribute("C:\Coussinet.prt", "VOLUME", xmlAttributeCase.Upper)

    End Sub

End Module

 

Code of Class

Imports OpenMcdf
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Reflection

Public Class AttributesExternFile
    Shared xmldoc As New System.Xml.XmlDocument

    ''' <summary>
    ''' Permet de convertir la case du nom de l'attribut Xml 
    ''' </summary>
    ''' <remarks></remarks>
    Enum xmlAttributeCase
        Exact
        Upper
        Lower
    End Enum

    ''' <summary>Extrait du fichier pièce NX, un attribut par son titre.</summary>
    ''' <param name="FullPath">Chemin complet du fichier pièce NX.</param>
    ''' <param name="AttributeTitle">Titre de l'attribut de la pièce NX.</param>
    ''' <param name="myCase">Permet de convertir la case du nom de l'attribut Xml.</param>
    ''' <returns>Retourne Nothing si l'attribut n'existe pas ou si le paramètre optionnel de la case n'a pas été renseigné correctement.</returns>
    ''' <remarks></remarks>
    Shared Function AskAttribute(FullPath As String, AttributeTitle As String, Optional myCase As xmlAttributeCase = xmlAttributeCase.Exact) As PartAttribute


        GetAttrs(FullPath)

        Dim ff As XmlNode = xmldoc.SelectSingleNode("/UgAttributes/Attribute[@title='" & AttributeTitle & "']")
        Select Case myCase
            Case xmlAttributeCase.Lower
                ff = xmldoc.SelectSingleNode("/UgAttributes/Attribute[translate(@title,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & AttributeTitle & "']")
            Case xmlAttributeCase.Upper
                ff = xmldoc.SelectSingleNode("/UgAttributes/Attribute[translate(@title,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" & AttributeTitle & "']")
        End Select

        Return ConvertXmlNodeToPartAttribute(ff)

    End Function

    ''' <summary>Extrait tous les attributs du fichier pièce NX.</summary>
    ''' <param name="FullPath">Chemin complet du fichier pièce NX.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Shared Function AskAttributes(FullPath As String) As PartAttribute()

        GetAttrs(FullPath)
        Dim ff As XmlNodeList = xmldoc.SelectNodes("/UgAttributes/Attribute")

        Dim myTab(ff.Count - 1) As PartAttribute

        For i As Integer = 0 To ff.Count - 1
            myTab(i) = ConvertXmlNodeToPartAttribute(ff.Item(i))
        Next

        Return myTab

    End Function


    Shared Function ConvertXmlNodeToPartAttribute(myXmlNode As XmlNode) As PartAttribute

        If myXmlNode Is Nothing Then Return Nothing
        Dim gg As PartAttribute = New PartAttribute

        gg.Owner = myXmlNode.Attributes("owner").Value
        gg.PdmBased = myXmlNode.Attributes("pdmBased").Value
        gg.Title = myXmlNode.Attributes("title").Value
        gg.Utf8title = myXmlNode.Attributes("utf8title").Value
        gg.Utf8value = myXmlNode.Attributes("utf8value").Value
        gg.Value = myXmlNode.Attributes("value").Value
        gg.Version = myXmlNode.Attributes("version").Value
        gg.XsiType = myXmlNode.Attributes("xsi:type").Value

        Return gg

    End Function

    ''' <summary>Retourne un document XML contenant les attributes de la pièce (Fichier extrait de la pièce ''attrs'')</summary>
    ''' <param name="FullPath">Chemin complet du fichier NX</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Shared Function AskXMLAttributes(FullPath As String) As Xml.XmlDocument

        GetAttrs(FullPath)
        Return xmldoc

    End Function



    Shared Sub GetAttrs(FullPath As String)
        Try
            Dim fs As FileStream = New FileStream(FullPath, FileMode.Open, FileAccess.Read)
            Dim cf As CompoundFile = New CompoundFile(fs)
            Explore(cf.RootStorage)
        Catch exception As Exception
            MsgBox("Internal error: " & exception.Message, MsgBoxStyle.Critical, "ERROR")
        End Try
    End Sub

    Shared Function Explore(ByVal cfs As CFStorage)
        Dim action As VisitedEntryAction = Function(target As CFItem)

                                               If target.IsStream Then
                                                   If target.Name = "attrs" Then

                                                       Dim tag As CFStream = DirectCast(target, CFStream)
                                                       Try

                                                           xmldoc.LoadXml(Encoding.ASCII.GetString(tag.GetData))

                                                       Catch exception As Exception
                                                           MsgBox("Internal error: " & exception.Message, MsgBoxStyle.Critical, "ERROR")
                                                       Finally
                                                           'If stream2 IsNot Nothing Then
                                                           '    stream2.Flush()
                                                           '    stream2.Close()
                                                           '    stream2 = Nothing
                                                           'End If
                                                       End Try

                                                   End If

                                               Else

                                                   Explore(DirectCast(target, CFStorage))
                                               End If


                                           End Function
        cfs.VisitEntries(action, False)

    End Function
End Class

Public Class PartAttribute

    Private _owner As String = ""
    Property Owner As String
        Get
            Return _owner
        End Get
        Set(value As String)
            _owner = value
        End Set
    End Property

    Private _title As String = ""
    Property Title As String
        Get
            Return _title
        End Get
        Set(value As String)
            _title = value
        End Set
    End Property

    Private _value As String = ""
    Property Value As String
        Get
            Return _value
        End Get
        Set(value2 As String)
            _value = value2
        End Set
    End Property

    Private _version As String = ""
    Property Version As String
        Get
            Return _version
        End Get
        Set(value As String)
            _version = value
        End Set
    End Property

    Private _pdmBased As String = ""
    Property PdmBased As String
        Get
            Return _pdmBased
        End Get
        Set(value As String)
            _pdmBased = value
        End Set
    End Property

    Private _xsiType As String = ""
    Property XsiType As String
        Get
            Return _xsiType
        End Get
        Set(value As String)
            _xsiType = value
        End Set
    End Property

    Private _utf8title As String = ""
    Property Utf8title As String
        Get
            Return _utf8title
        End Get
        Set(value As String)
            _utf8title = value
        End Set
    End Property

    Private _utf8value As String = ""
    Property Utf8value As String
        Get
            Return _utf8value
        End Get
        Set(value As String)
            _utf8value = value
        End Set
    End Property
End Class