Skip to end of metadata
Go to start of metadata

An example for:

  • Create the connector object (→ CreatePlmConnector())
  • ping SAP ECTR
  • create a document (→ CreateDocuments())
  • get the document information (→ ProvideInfo())
  • checkout a document (→ CheckoutEdit())
  • save a document (→ SaveDocument())
  • call a customer specific process (→ CallPlmProcess())


VB.NET sample code
'''
''' Example How to use PlmConnector dll added to a project as assembly in VB.NET 5.0
'''
''' Used MS VS2019
''' Based on ECTR5.2.3.0
'''

Imports System.Xml
Imports System.IO
Imports Com.Dscsag.Plm.Comm

Module Program

    Sub Main(args As String())
        Dim connFac As _PlmConnectorFactory = New PlmConnectorFactory
        Dim connConfig = New PlmConnectorConfig

        connConfig.ApplicationKey = "UGS"
        connConfig.ApiLevel = 2
        Dim conn As IPlmConnector = connFac.CreatePlmConnector(connConfig)

        Console.WriteLine("ECTR Frontend Version:  " & conn.VersionInfo.ECTRClientVersion)
        Console.WriteLine("ECTR Backend Version:   " & conn.VersionInfo.ECTRServerVersion)
        Console.WriteLine("ECTR Connector Version: " & conn.VersionInfo.PLMConnectorVersion)

        Ping(conn)
		
        'the document main original file 
        Dim fn = "D:\temp\123456789999.prt"

        'Create Document
        Dim fnNew = CreateDocument(conn, fn)

        'now saveAs or copyTo Document to new location
        FileCopy(fn, fnNew)

        SaveDocument(conn, fnNew)

        'Provide Info
        If ProvideInfo(conn, fnNew) Then

            'Checkout
            CheckOutDocument(conn, fnNew)

            'Modify the File
            ModifyTheFile(fnNew)

            'CheckIn
            SaveDocument(conn, fnNew)

        End If

        'CustomerSpecificProc(conn)
        'Console.ReadLine()
    End Sub



    Sub ModifyTheFile(fn As String)

        Dim fileStream As IO.FileStream =
        New IO.FileStream(fn, IO.FileMode.Open)
        Try
            ' Set the stream position to the desired location of the stream.
            fileStream.Seek(98, IO.SeekOrigin.Begin)

            ' Generate random value between 65 and 89.
            Dim value As Integer = Int((24 * Rnd()) + 65)

            fileStream.WriteByte(value)
        Finally
            fileStream.Close()
        End Try

    End Sub


    Function CheckoutDocument(conn As IPlmConnector, fn As String) As Boolean

        Dim done As Boolean = False

        Console.WriteLine("Checkout Edit: " & fn)
        Dim checkoutParts = New Utils.Collections.PlmRequestPartList()
        Dim reqPart = New PlmRequestPartImpl(fn)
        checkoutParts.Add(reqPart)

        Dim res = conn.CheckoutEdit(Nothing, checkoutParts)

        If res IsNot Nothing And Not res.Cancelled Then

            For Each resPart As IPlmResponsePart In res.Parts

                If resPart.Filename = fn Then

                    If resPart.Action = Com.Dscsag.Plm.Comm.PartAction.SET_RW Then
                        Console.WriteLine("Document is checked out...")
                        'Attribute is automatically set but we may need to refresh status in our Integration the part is loaded
                        done = True
                    ElseIf resPart.Action = Com.Dscsag.Plm.Comm.PartAction.SET_RW_BREAK_HARDLINK Then
                        Console.WriteLine("We have to break link in session due saveAs...")
                        'No answer to Ectr neccessary - if there is any problem to do it, its better to cancelCheckout document in ECTR
                        'Appears only when part is metioned as load and if "hardlink_in_session" dtype setting is swichted to "yes"
                        'if there is no saveAs in Integration for loaded files, you may need to close the files and you can use file copy instead of saveAs
                        'TODO SaveAs in another folder
                        'TODO Delete Original filename (link) in session folder
                        'TODO SaveAs back real file into session folder
                    ElseIf resPart.Action = Com.Dscsag.Plm.Comm.PartAction.UPDATE_ATTRIBUTES Then
                        Console.WriteLine("We have to update the document attribute in loaded file... - NOT IMPLEMENTED COMPLETELY")

                        ReadAttributes(conn, fn)

                    End If

                End If

            Next

        End If

        Return done

    End Function


    Public Function ReadAttributes(conn As IPlmConnector, fn As String) As Boolean

        Dim done As Boolean = False

        Dim meta = conn.GetMetaInfoSap(fn)

        If meta IsNot Nothing And meta.Attributes IsNot Nothing Then

            For Each attrName As String In meta.Attributes.Keys
                Dim attrValue = meta.Attributes.GetValueOrDefault(attrName)
                Console.WriteLine("Set Attribute: " & attrName & " to Value: " & attrValue)
            Next

            done = True

        End If

        Return done

    End Function


    Function SaveDocument(conn As IPlmConnector, fn As String) As Boolean

        Dim done As Boolean = False

        Console.WriteLine("Save Checkin: " & fn & " to SAP")
        Dim saveParts = New Utils.Collections.PlmRequestPartList()
        Dim reqPart = New PlmRequestPartImpl(fn)
        saveParts.Add(reqPart)

        Dim res = conn.Save(Nothing, saveParts, SaveOption.SAVECHECKIN)
        If (res IsNot Nothing And Not res.Cancelled) Then

            For Each resPart As IPlmResponsePart In res.Parts

                If resPart.Filename = fn Then

                    If resPart.Action = Com.Dscsag.Plm.Comm.PartAction.SET_RO Then
                        Console.WriteLine("Document is checked in...")
                        'Attribute is automatically set but we may need to refresh status in our Integration the part is loaded
                        done = True
                        Exit For
                    End If

                End If

            Next

        End If

        Return done

    End Function


    Function ProvideInfo(conn As IPlmConnector, fn As String) As Boolean

        Dim documentFound As Boolean = False
        Dim parts = New Utils.Collections.PlmRequestPartList()
        Dim part = New PlmRequestPartImpl(fn)
        parts.Add(part)
        Dim res = conn.ProvideInfo(parts, True)

        Dim partInfo As PartInfo = Nothing
        If (res IsNot Nothing And Not res.Cancelled) Then
            For Each resPart As IPlmResponsePart In res.Parts
                If (resPart.Filename = fn) Then
                    partInfo = resPart.PartInfo
                    documentFound = True
                    Exit For
                End If
            Next
        End If

        If (partInfo IsNot Nothing) Then
            Console.WriteLine("Found Create Document...")
            Console.WriteLine("DoumentKey   : " & partInfo.DocumentKey.ToString())
            Console.WriteLine("OriginalExist: " & partInfo.MasterOriginalExists.ToString())
            Console.WriteLine("OK           : " & partInfo.Ok.ToString())
            Console.WriteLine("CanCheckout  : " & partInfo.CanCheckout.ToString())
        Else
            Console.WriteLine("Created Document not found...")
        End If

        Return documentFound

    End Function


    Function CreateDocument(conn As IPlmConnector, fn As String) As String

        Dim req = New PlmCreateDocumentsRequest()
        Dim reqPart = New PlmRequestPartImpl(fn)

        Dim reqMeta = New PlmMetaInfoCadImpl()
        Dim reqPartAttr = New Attributes.PlmAttributeContainer

        reqPartAttr.AddAttribute(Attributes.PlmAttributeNames.DOCUMENT_DESCR, "description from call")
        reqPartAttr.AddAttribute(Attributes.PlmAttributeNames.DOCUMENT_DTYPE, "PINT")
        reqPartAttr.AddAttribute(Attributes.PlmAttributeNames.DOCUMENT_TYPE, "UGM")
        'reqPartAttr.AddAttribute(Attributes.PlmAttributeNames.AUTHORIZATION_GROUP, "svfsd")

        reqMeta.AddAttributes(reqPartAttr)
        reqPart.MetaInfoCad = reqMeta
        req.AddRequestPart(reqPart)
        req.ShowDialogOption = ShowDialogOption.SHOW_NEVER

        Dim plmRes = conn.CreateDocuments(req)
        Dim fnNew As String = Nothing
        If (plmRes IsNot Nothing And Not plmRes.RequestCancelled And Not plmRes.RequestFailed And req.CreateDocumentEntries.Count > 0) Then
            Dim createEntry = req.CreateDocumentEntries.ElementAt(0) 'We get only one if we ask only for one...
            If createEntry.State = ProcessEntryState.STATE_DONE Then
                Console.WriteLine("Document created - new name: " & createEntry.NewFilename)
                fnNew = createEntry.NewFilename
                If createEntry.HasMetaInfoFromSap Then

                    For Each attrName As String In createEntry.MetaInfoSap.Attributes.Keys
                        Dim attrValue = createEntry.MetaInfoSap.Attributes.GetValueOrDefault(attrName)
                        Console.WriteLine("Set Attribute After Create Document: " & attrName & " to Value: " & attrValue)
                    Next

                End If

            Else
                Console.WriteLine("Document not created - state: " & createEntry.State.ToString())
                If createEntry.HasMessage Then
                    Console.WriteLine("Document Message: " & createEntry.Message.Message)
                End If
            End If
        End If

        Return fnNew

    End Function


    Sub Ping(conn As IPlmConnector)
        Try
            conn.Ping()
        Catch ex As PlmConnectorException
            Console.WriteLine("PlmConnector Exception: " & ex.Message)
        Catch ex As Exception
            Console.WriteLine("Std Exception: " & ex.Message)
        End Try
    End Sub


    Sub CustomerSpecificProc(conn As IPlmConnector)

        Dim xmlstring = "<FUNCTION NAME=""customer.proc.gen.PlmProcessGenericDocumentCreate"" PASSTHROUGHXML=""True"">" &
                        "<SESSION APPLICATION=""UGS"" />" &
                        "<PARAMETERS>" &
                        "<PARAMETER TYPE=""SAPOBJECTS"">" &
                        "<SAPDOCUMENTS>" &
                        "<SAPDOCUMENT DTYPE=""PINT"" TYPE=""UGM"" PART=""000"">" &
                        "<SEEDPART PATH=""""/>" &
                        "<BASICDATA DESCRIPTION=""That is the Description"" AUTHGRP=""4D"">" &
                        "</BASICDATA>" &
                        "<!--ADDFILES>" &
                        "</ADDFILES-->" &
                        "<MASTERS>" &
                        "<MASTER ISMAINMASTER=""True"" ISWAVELINKED = ""True"" ISREFERENCED = ""True"">" &
                        "<SAPDOCUMENT TYPE=""UGD"" NUMBER=""10001241"" PART=""000"" VERSION=""00"" />" &
                        "</MASTER>" &
                        "</MASTERS>" &
                        "</SAPDOCUMENT>" &
                        "</SAPDOCUMENTS>" &
                       "</PARAMETER>" &
                        "</PARAMETERS>" &
                        "</FUNCTION>"

        Dim resPayLoad = CallEmbeddedProcess(conn, "customer.proc.Wrapper", xmlstring) 'need to take over customer specific

        'Now we can map it to an XmlElement
        Dim xmldoc As New XmlDataDocument()

        xmldoc.LoadXml(resPayLoad)
        '...and now parse Response to evaluate

    End Sub


    Function CallEmbeddedProcess(conn As IPlmConnector, WrapperProc As String, XmlInput As String) As String

        Dim request = New PlmRequest(WrapperProc)
        request.Parameters.AddOption("PAYLOAD", "<![CDATA[" & XmlInput & "]]>")
        Dim iRes As IPlmResponse = conn.CallPlmProcess(request)

        Dim resPayLoad As String = ""
        If (iRes IsNot Nothing And Not iRes.Cancelled) Then
            If (iRes.Materials.Count > 0) Then
                Dim mat = iRes.Materials.ElementAt(0)
                Dim matData = mat.Data
                If (matData.Count > 0) Then
                    matData.TryGetValue("PAYLOAD", resPayLoad)
                End If
            End If
        End If

        If (resPayLoad.Length > 0) Then
            resPayLoad.Remove("<![CDATA[")
            resPayLoad.Remove("]]>")
        End If

        Return resPayLoad

    End Function

End Module


  • No labels