Dear Better Experienced Collegues,
I am having a serious issue attaching object data to an object. I am providing the tableName from another part of the program as a string, the name of the field which is being converted from a hashtable into the position of the field in the record [will change this later], and the value is coming from a combo box in a winform. Please note that I have locked the drawing early in this procedure, based on the information in a post I felt may have been related. I am not handling the exception as I am stepping through the code with VS2010 Expression for C#, but will handle any exceptions later. Just to be clear, I am using Autodesk Map 2011. Here is my c# (c sharp) code:
public static bool CommandAddData(string tableName, string updateField, string cmbValue)
{
Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
using (DocumentLock lk = dwg.LockDocument())
{
Tables tables = HostMapApplicationServices.Application.ActiveProject.ODTables;
ObjectId[] selObjs = SLiiCGIS.GISFunctions.SelectionFunc.selectedObjects.Value.GetObjectIds();
// Get Application Level Table Definition for object data
Hashtable masterTableDef = SLiiCGIS.ApplicationVariables.tableDefinition;
if (selObjs.Length > 0)
{
foreach (ObjectId sel in selObjs)
{
Autodesk.Gis.Map.ObjectData.Table table = tables[tableName];
using (Records rcds = tables.GetObjectRecords(0, sel, Autodesk.Gis.Map.Constants.OpenMode.OpenForWrite, false))
if (rcds.Count == 0)
{
try
{
// Create and initialize an record
Autodesk.Gis.Map.ObjectData.Record tblRcd = Autodesk.Gis.Map.ObjectData.Record.Create();
table.InitRecord(tblRcd);
int tblPos = Convert.ToInt32(masterTableDef[updateField]);
MapValue val = tblRcd[tblPos]; // String type
val.Assign(cmbValue);
table.AddRecord(tblRcd, sel);
return true;
}
catch (MapException)
{
return false;
}
}
else
{
try
{
foreach (Autodesk.Gis.Map.ObjectData.Record tblRcd in rcds)
{
// Create and initialize an record
int tblPos = (int)masterTableDef[updateField];
MapValue val = tblRcd[tblPos]; // String type
val.Assign(cmbValue);
return true;
}
}
catch (MapException)
{
return false;
}
}
}
return true;
}
else
{
return false;
}
}
}
Almost directly out of the book (AutoCAD Map 3D SDK | ObjectARX .NET Developer's Guide | Object Data | Attaching Object Data). Any assistance would be greatly appreciated.
Solved! Go to Solution.
Please try this code...
#region Add Or Update Object Data Record public static bool AddOrUpdOD(string TableName, ObjectId objectID, string columnName, string value) { Autodesk.Gis.Map.ObjectData.Tables tables = Autodesk.Gis.Map.HostMapApplicationServices.Application.ActiveProject.ODTables; Autodesk.Gis.Map.ObjectData.Table table = tables[TableName]; Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; int recCount = 0; try { using(DocumentLock lk = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()) { using(Records rcs = table.GetObjectTableRecords(0, objectID, Autodesk.Gis.Map.Constants.OpenMode.OpenForWrite, false)) { recCount = rcs.Count; } if(recCount == 0) { Record Rec = Record.Create(); table.InitRecord(Rec); table.AddRecord(Rec,objectID); } using(Records records = table.GetObjectTableRecords(0, objectID, Autodesk.Gis.Map.Constants.OpenMode.OpenForWrite, false)) { if(records.Count == 0) { ed.WriteMessage("\nObject Data attachment failed."); records.Dispose(); return false; } foreach(Record Recd in records) { for(int i = 0; i < Recd.Count; i++) { FieldDefinitions tblDefs = table.FieldDefinitions; FieldDefinition column = null; column = tblDefs[i]; if(column.Name.CompareTo(columnName) == 0) { Recd[i].Assign(value); records.UpdateRecord(Recd); break; } } } } } return true; } catch(Autodesk.Gis.Map.MapException Exp) { System.Windows.Forms.MessageBox.Show("ERROR:" + Environment.NewLine + Exp.Message + Environment.NewLine + Exp.StackTrace); return false; } } #endregion
this code works perfectly for me....
Thanks...
Chandan.
Hello Chandan,
Thank you very much, for saving my life. I will be analyzing the superiority of your code for at least 30 minutes 🙂
Hi,
Could you please tell me how you are assigning the value to the ObjecID
I am trying to attach a record to the Acadline using
odTable.AddRecord(oRecord, OBjID)
Thanks,
You may want to select the entity and get the object id using following code snippet:
Editor AcadEditor = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionOptions options = new PromptSelectionOptions();
// Single select mode
options.SingleOnly = true;
options.SinglePickInSpace = true;
options.MessageForAdding = "Select an entity:";
AcadEditor.WriteMessage("\n Please select an entity to add a record: ");
PromptSelectionResult result = AcadEditor.GetSelection(options);
if (result.Status != PromptStatus.OK)
{
AcadEditor.WriteMessage("\n User cancelled! ");
return;
}
ObjectId[] ids = result.Value.GetObjectIds();
if (ids.Length != 1)
{
AcadEditor.WriteMessage("\n Invalid selection! ");
return;
}
//you code goes below...
OBjID = ids[0];
.....
Can't find what you're looking for? Ask the community or share your knowledge.