学习《Building Applications with FME Objects》 之六 转换要素

时间:2021-04-19 09:46:46

这个API可以非常容易的在格式之间做转换,首先打开reader和writer(具体操作前两篇已经有介绍),可以简单的使用FMEODialog的xlatePrompt方法显示窗口,同时允许用户输入reader和writer的参数:

学习《Building Applications with FME Objects》 之六 转换要素

注意:FMEODialog对象仅能用于WINDOWS平台

 

下面的代码演示了该方法返回的参数并传递给reader和writer:

 

Public Sub TranslationPrompt(sSourceDir As String, _
                                sDestDir As String)
    Dim sDestDataset As String
    Dim sDestFormat As String
    Dim sSrcDataset As String
    Dim sSrcFormat As String
    Dim bCompleted As Boolean
    Dim bEnd As Boolean
    Dim fmeFeature As FMEOFeature
    Dim fmeDialog As FMEODialog
    Dim fmeSrcDirectives As FMEOStringArray
    Dim fmeDestDirectives As FMEOStringArray
    Dim fmeKeywords As FMEOStringArray
    Set fmeKeywords = m_fmeSession.createStringArray
    Set fmeDialog = m_fmeSession.createDialog
    Set fmeSrcDirectives = m_fmeSession.createStringArray
    Set fmeDestDirectives = m_fmeSession.createStringArray
    bCompleted = fmeDialog.xlatePrompt("MIF", sSourceDir, _
                 "MIF", sDestDir, _
                 sSrcFormat, sSrcDataset, fmeSrcDirectives, _
                 sDestFormat, sDestDataset, fmeDestDirectives)
    If bCompleted = True Then
        Set m_fmeReader = m_fmeSession.createReader( _
                                    sSrcFormat, _
                                    True, fmeSrcDirectives)
        Call m_fmeReader.open(sSrcDataset, fmeKeywords)
        Set m_fmeWriter = m_fmeSession.createWriter( _
                                            sDestFormat, _
                                            fmeDestDirectives)
        Call m_fmeWriter.open(sDestDataset, fmeKeywords)
End Sub

xlatePrompt方法支持的参数:

学习《Building Applications with FME Objects》 之六 转换要素

 

如何源和目标的参数中都提供了TITLE值,那么优先采用目标参数中设定的TITLE值。

 

一旦reader和writer被打开,就可以简单的做要素转换,从reader写到writer,代码如下:

bEnd = False
Do While bEnd = False
    bEnd = m_fmeReader.readSchema(fmeFeature)
    If bEnd = False Then
        Call m_fmeWriter.addSchema(fmeFeature)
    End If
Loop
bEnd = False
Do While bEnd = False
    bEnd = m_fmeReader.read(fmeFeature)
    If bEnd = False Then
        Call m_fmeWriter.write(fmeFeature)
    End If
Loop

 

提示:FMEOWriter的addSchema和write方法会清空传递给它的要素,如果你还打算在写入后继续使用该要素,那么在写入前先对该要素进行clone(克隆)。

 

一旦所有的要素被转换完毕,就需要关闭reader和writer,如下:

Call m_fmeReader.Close

Call m_fmeWriter.Close

 

 

 

 

参考资料:

《Building Applications with FME Objects》February 2005

转载请注明文章来源 http://www.cnblogs.com/booolee