Hi Dave,
Thanks getting back to me again, I reworked the code to do as you advised and I have still not had any luck getting the writer to work. I also tried generating a writer connection string using the FMEPCConfig.exe and updated the overwrite parameters.
At this stage we are looking a moving away from using an SDF database to something more universally accessible, so it will not be to much of a problem if I cannot get the writer to work. I have included my updated code and the writer connection string if you wanted to have a look through it. If not, no worries and thanks for your help so far
.
Writer Connection String:
SOURCE_FORMAT,SDF3,SOURCE_DATASET,"E:\Current Jobs\00 Not Prejects\Dynamo and Navis Training\Projects\Automation - Solution Architecture Document\DSSTest - Copy.sdf",RUNTIME_MACROS,"OVERWRITE_FILE,Yes,DEFAULT_SCHEMA_NAME,Default,TABLE_DEFAULTS,,OverwriteTable,YES,WRITER_MODE,INSERT,IdentityColumnName,FeatId,GeometryColumnName,Geometry,ADVANCED,,XY_TOLERANCE,0.0,Z_TOLERANCE,0.0,DESTINATION_DATASETTYPE_VALIDATION,Yes",META_MACROS,"DestOVERWRITE_FILE,Yes,DestDEFAULT_SCHEMA_NAME,Default,DestTABLE_DEFAULTS,,DestOverwriteTable,YES,DestWRITER_MODE,OVERWRITE,DestIdentityColumnName,FeatId,DestGeometryColumnName,Geometry,DestADVANCED,,DestXY_TOLERANCE,0.0,DestZ_TOLERANCE,0.0,DestDESTINATION_DATASETTYPE_VALIDATION,Yes",METAFILE,SDF3,COORDSYS,
Script:
(I tried this with and without creating a sepperate session for writing)
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from System.Collections.Specialized import *
from System import Array
#The inputs to this node will be stored as a list in the IN variables.
fmeHome = IN[0]
connectionString = IN[1]
writeConnectionString = IN[3]
# import FMEObjects .NET API from FME Home
sys.path.append(fmeHome)
clr.AddReferenceToFile('FMEObjectsDotNet4.dll')
from Safe.FMEObjects import *
# replace outer commas in connection string with semi-colons to simplify parsing for reader
qflag = 0
parsedString = ''
for c in connectionString:
if c == '"':
qflag = abs(qflag - 1)
else:
if c == ',' and qflag == 0:
c = ';'
parsedString = parsedString + c
# parse connection string to get parameters for reader
connectionList = parsedString.Split(';')
sourceFormat = connectionList[1]
sourceDataset = connectionList[3]
# replace outer commas in connection string with semi-colons to simplify parsing for writer
qflag = 0
parsedString = ''
for c in writeConnectionString:
if c == '"':
qflag = abs(qflag - 1)
else:
if c == ',' and qflag == 0:
c = ';'
parsedString = parsedString + c
# parse connection string to get parameters for writer
writeConnectionList = parsedString.Split(';')
writeSourceFormat = writeConnectionList[1]
writeSourceDataset = writeConnectionList[3]
# convert connection list to Stringcollection for reader
rparams = StringCollection()
for param in connectionList:
rparams.Add(param)
# convert connection list to Stringcollection for writer
writeParams = StringCollection()
for param in writeConnectionList:
writeParams.Add(param)
# create FMEObjects session
nullParams = StringCollection()
session = FMEObjects.CreateSession()
session.Init(nullParams)
writeSession = FMEObjects.CreateSession()
writeSession.Init(None)
# create blank output list
outlist = []
# create log file for debugging
if IN[2] != None:
log = session.LogFile()
log.SetFileName(IN[2], False)
if IN[2] != None:
logWrite = writeSession.LogFile()
logWrite.SetFileName(IN[4], False)
# open new reader and writer using connection string
reader = session.CreateReader(sourceFormat, False, rparams)
reader.Open(sourceDataset, nullParams)
writer = writeSession.CreateWriter(writeSourceFormat, None)
writer.Open(writeSourceDataset, writeParams)
# read first table
feature = session.CreateFeature()
moreSchemas = reader.ReadSchema(feature)
tableName = feature.FeatureType
# creates feature vector and adds the datasets first feature
featureVector = session.CreateFeatureVector()
# featureVector.Append(feature)
# get names of attributes in table
attrs = StringCollection()
feature.GetAllAttributeNames(attrs)
# convert collection of attribute names to list and keep only user attributes
attrList = []
titleList = []
for i in range(attrs.Count):
attr = attrs.Item[i]
if attr.startswith('*') or attr.startswith('fme_'):
pass
else:
attrList.append(attr)
titleList.append(attr)
titleList.append('Geometry')
# set attribute names as first item in table list
outlist.append(titleList)
# loop through features in table
while reader.Read(feature):
featureList = []
# adds features to feature vector
featureVector.Append(feature)
# get all attribute values from feature
for attr in attrList:
value = feature.GetStringAttribute(attr)
featureList.append(value)
featureList.append(feature.ExportGeometryToOGCWKT())
# append feature attribute values to table list
outlist.append(featureList)
feature = session.CreateFeature()
# sets writer schema
writer.GetSchemaFeatures(featureVector)
feature = writeSession.CreateFeature()
writeList = [] #test to see what is being updated
featureList = [] # test to see what features are in feature vector
# loops through feature in feature vector updating a value and writing it to the database
# This is an alternate loop to what is used below
moreFeatures = True
while moreFeatures == True:
try:
feature = featureVector.RemoveLast()
featureList.append(feature) # testing what feature is being updated
writeList.append(feature.GetStringAttribute("Number of Storeys")) # testing value of feature being updated
feature.SetStringAttribute("Number of Storeys", "15")
writer.StartTransaction()
logWrite.LogFeature(feature, 0, -1)
writer.Write(feature)
writer.CommitTransaction()
#feature = session.CreateFeature()
except:
moreFeatures = False
"""# rereading source features from start of list
reader = session.CreateReader(sourceFormat, False, rparams)
reader.Open(sourceDataset, nullParams)
# loops through features writing updated features to database
while reader.Read(feature):
feature.SetStringAttribute("Number of Storeys", "15")
writer.StartTransaction()
writeList.append(feature.FeatureType) # test to see what table is being writen too
logWrite.LogFeature(feature, 0, -1)
writer.Write(feature)
writer.CommitTransaction()
feature = writeSession.CreateFeature()"""
#Assign your output to the OUT variable.
OUT = outlist
Thanks again for your help.
Cheers,
Harry