Well, I did not go through your code line by line, just a quick browsing. By the way, when posting code here, please use the "</>" button of the toolbar (above the message window while you are entering your message), so that the code is formatted read-friendly.
The other obvious error in your code is to add an existing OD record retrieved from source entity to another entity (target entity). You cannot do that. You need to create a NEW OD record, set its values with the values obtained from the source entity's record (note: values!, not record!).
Following are code samples to copy OD data from one entity to another. Note, this is an extension method.
public static void CopyObjectDataFrom(this ObjectId entId, ObjectId sourceEntId, ProjectModel mapProj = null)
{
var dicData = sourceEntId.RetrieveObjectDataFromEntity(mapProj);
if (dicData.Count == 0) return;
Autodesk.Gis.Map.ObjectData.Tables tables = null;
if (mapProj == null) mapProj = HostMapApplicationServices.Application.ActiveProject;
tables = mapProj.ODTables;
foreach (var item in dicData)
{
if (!mapProj.ODTables.IsTableDefined(item.Key)) continue;
using (var table = mapProj.ODTables[item.Key])
{
var fldDefs = table.FieldDefinitions;
using (var record = Record.Create())
{
table.InitRecord(record);
for (int i = 0; i < fldDefs.Count; i++)
{
string fldName = fldDefs[i].Name;
OdField odFld = FindOdField(item.Value, fldName);
if (odFld == null) continue;
MapValue mapVal = record[i];
switch (odFld.DataType)
{
case Map.Constants.DataType.Character:
mapVal.Assign(odFld.FieldValue.ToString());
break;
case Map.Constants.DataType.Integer:
mapVal.Assign(Convert.ToInt32(odFld.FieldValue));
break;
case Map.Constants.DataType.Real:
mapVal.Assign(Convert.ToDouble(odFld.FieldValue));
break;
case Map.Constants.DataType.Point:
mapVal.Assign((Point3d)odFld.FieldValue);
break;
}
}
table.AddRecord(record, entId);
}
}
}
//if (tables != null) tables.Dispose();
//if (mapProj != null) mapProj.Dispose();
}
This is the method RetrieveObjectDataFromEntity() used in above code:
public static Dictionary<string, IEnumerable<OdField>> RetrieveObjectDataFromEntity(this ObjectId entId, ProjectModel mapProj = null)
{
var dic = new Dictionary<string, IEnumerable<OdField>>();
if (mapProj == null) mapProj = HostMapApplicationServices.Application.GetProjectForDB(entId.Database);
var tables = mapProj.ODTables;
if (tables.TablesCount > 0)
{
using (Records records = tables.GetObjectRecords(0, entId, Map.Constants.OpenMode.OpenForRead, true))
{
if (records.Count > 0)
{
string tName = "";
try
{
foreach (Record record in records)
{
tName = record.TableName;
if (!tables.IsTableDefined(tName)) continue;
var table = tables[tName];
var fields = GetOdRecordData(record, table);
dic.Add(tName, fields);
}
}
catch (Map.MapException ex)
{
string err = ((Map.Constants.ErrorCode)ex.ErrorCode).ToString();
throw new ApplicationException(
"Cannot reach object data record!");
}
}
}
}
return dic;
}
Also, this is the class OdField used in above code:
public class OdField
{
public string FieldName { set; get; }
public object FieldValue { set; get; }
public Map.Constants.DataType DataType { set; get; }
public string StringValue
{
get
{
string ret = "";
if (FieldValue != null)
{
switch (DataType)
{
case Map.Constants.DataType.Character:
ret = FieldValue.ToString();
break;
case Map.Constants.DataType.Integer:
ret = ((int)FieldValue).ToString();
break;
case Map.Constants.DataType.Real:
ret = ((double)FieldValue).ToString();
break;
case Map.Constants.DataType.Point:
ret = ((Point3d)FieldValue).ToString();
break;
}
}
return ret;
}
}
}
HTH