Hi all! I have some trouble - now I developing some library for Autocad Civil 3D 2011: we are have connect to MS SQL Server base with some tables - it's our layers with geometry and some other columns. When user press button on our panel - we have opportunity select one FDO object on our map. After this our library will show layer name of selected object and names and values of other columns in table of selected object:
[CommandMethod("SHOWPROP")]
public static void ShowProperties()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
MgFeatureReader reader = null;
try
{
// Ask the user to select FDO object
PromptEntityOptions peo =
new PromptEntityOptions("\nSelect FDO object");
peo.SetRejectMessage("\nMust be a FDO object.");
peo.AllowNone = false;
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId srcId = per.ObjectId;
Transaction tr =
doc.TransactionManager.StartTransaction();
using (tr)
{
AcMapMap currentMap = AcMapMap.GetCurrentMap();
MgLayerCollection LayerCol = currentMap.GetLayers();
Autodesk.Gis.Map.Platform.AcMapLayer maplayer=null;
if (per.ObjectId != ObjectId.Null)
{
DBObject obj = tr.GetObject(per.ObjectId, OpenMode.ForRead);
if (obj != null)
{
ed.WriteMessage("\n" + "Type: " + obj.GetType().ToString());
ed.WriteMessage("\n" + "RXClassName: " + obj.GetRXClass().Name.ToString());
ed.WriteMessage("\n" + obj.Database.GeoDataObject.ToString());
Entity ent = (Entity)tr.GetObject(srcId, OpenMode.ForRead);
ed.WriteMessage("\n" + AcMapFeatureEntityService.GetLayer(per.ObjectId).ToString());
maplayer = AcMapFeatureEntityService.GetLayer(per.ObjectId);
ed.WriteMessage("\n" + "Layer name: " + maplayer.Name);
ed.WriteMessage("\n" + "Layer: " + maplayer.FeatureClassName.ToString());
//I don't know how initialize reader :(
//reader = ?????
MgClassDefinition classDef = maplayer.GetClassDefinition();
MgPropertyDefinitionCollection propertyDefs = classDef.GetProperties();
foreach (MgPropertyDefinition propertyDef in propertyDefs)
{
if (propertyDef.Name != ClassSystemProperties.SessionId)
{
switch (propertyDef.GetPropertyType())
{
case MgFeaturePropertyType.DataProperty:
MgDataPropertyDefinition dataProp = propertyDef as MgDataPropertyDefinition;
if (dataProp != null)
{
// Set default value.
string value = "(null)";
// Reads the value from featureReader according to DataType.
switch (dataProp.GetDataType())
{
case MgPropertyType.String:
//Next string not work :(
//value = reader.GetString(propertyDef.Name);
break;
case MgPropertyType.Double:
//Next string not work :(
//value = reader.GetDouble(propertyDef.Name).ToString();
break;
case MgPropertyType.Int16:
//Next string not work :(
//value = reader.GetInt16(propertyDef.Name).ToString();
break;
case MgPropertyType.Int32:
//Next string not work :(
//value = reader.GetInt32(propertyDef.Name).ToString();
break;
case MgPropertyType.Int64:
//Next string not work :(
//value = reader.GetInt64(propertyDef.Name).ToString();
break;
case MgPropertyType.Boolean:
//Next string not work :(
//value = reader.GetBoolean(propertyDef.Name).ToString();
break;
default:
break;
}
//Util.PrintLn(propertyDef.Name + " = " + value);
ed.WriteMessage("\n" + propertyDef.Name + " = " + value);
}
break;
case MgFeaturePropertyType.AssociationProperty:
break;
default:
break;
}
}
}
}
}
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\nException: {0}", ex.Message);
}
}
I know - somebody will say - "see Feature Inspector code in ObjectARX SDK!" but this example not suitable for this case. I do not have selected area on my map and old ideas for initialize FeatureReader not work 😞 Please, help somebody.
P.S. Sorry for my English
Hi vipwall,
I didn't scan through each line of your code snippet, but here is the general approach of FDO Feature selection using AutoCAD Map 3D Geospatial Platform API :
1. //Get Map selection set from AutoCAD selection set
// code Snippet below :
SelectionSet selSet = selResult.Value;
MgSelectionBase selectionBase = AcMapFeatureEntityService.GetSelection(selSet);
2. // Use the GetSelectedFeatures() to get the MgFeatureReader object
// Code Snippet below :
MgFeatureReader ftrRdr = selectionBase.GetSelectedFeatures(ParkPointLayer, ParkPointLayer.FeatureClassName, false);
try
{
while(ftrRdr.ReadNext())
{
string parkPointName = ftrRdr.GetString("STNAME"); // this this just a demo, change this according to your data field
ed.WriteMessage(
"\n Park Name :"+ parkPointName.ToString());
Int32 FeatureID = ftrRdr.GetInt32("FeatId"); // this this just a demo, change this according to your data field
ed.WriteMessage(
"\n Feature ID :"+ FeatureID.ToString());
}
}
Hope this helps.
P.S. - We have plenty of code snippet in ADN - Autodesk Developer Network (http://adn.autodesk.com/) site on the same. I am not sure if you are an ADN member and have access to ADN page. You might get some code snippet by Googling, from the Map 3D SDK samples and SDK docs.
Hi, Partha Sarkar!
Great thanks for you! I continued my research and the result was the following code. It works correctly:
[CommandMethod("SHOWPROP")] public static void ShowProperties() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; MgFeatureReader reader; PromptSelectionResult selectionRes; selectionRes = ed.SelectImplied(); PromptSelectionOptions selectionOpts = new PromptSelectionOptions(); selectionOpts.MessageForAdding = "Select single FDO object"; selectionOpts.SingleOnly = true; selectionRes = ed.GetSelection(selectionOpts); if ((selectionRes.Status == PromptStatus.Error)||(selectionRes.Status == PromptStatus.Cancel)) return; if (selectionRes.Value.Count!=1) { ed.WriteMessage("Need select only single object!"); acadApp.UpdateScreen(); return; } Transaction tr = doc.TransactionManager.StartTransaction(); try { ObjectId[] objIds = selectionRes.Value.GetObjectIds(); foreach (ObjectId objId in objIds) { DBObject obj = tr.GetObject(objId, OpenMode.ForRead); Entity ent = (Entity)obj; //If selected object is not FDO object if (ent.Layer!="*ACMAPDMGISENTITIES") { obj.Dispose(); tr.Abort(); return; } // This time access the properties directly ed.WriteMessage("\nType: " + ent.GetType().ToString()); Autodesk.Gis.Map.Platform.AcMapLayer maplayer = AcMapFeatureEntityService.GetLayer(objId); ed.WriteMessage("\n" + maplayer.ToString()); ed.WriteMessage("\n" + maplayer.FeatureClassName.ToString()); MgClassDefinition classDef = maplayer.GetClassDefinition(); MgPropertyDefinitionCollection propertyDefs = classDef.GetProperties(); MgSelectionBase selectionBase = AcMapFeatureEntityService.GetSelection(selectionRes.Value); maplayer = AcMapFeatureEntityService.GetLayer(objId); reader = selectionBase.GetSelectedFeatures(maplayer, maplayer.FeatureClassName, false); while (reader.ReadNext()) { foreach (MgPropertyDefinition propertyDef in propertyDefs) { if (propertyDef.Name != ClassSystemProperties.SessionId) { switch (propertyDef.GetPropertyType()) { case MgFeaturePropertyType.DataProperty: MgDataPropertyDefinition dataProp = propertyDef as MgDataPropertyDefinition; if (dataProp != null) { // Set default value. string value = "(null)"; // Reads the value from featureReader according to DataType. switch (dataProp.GetDataType()) { case MgPropertyType.String: value = reader.GetString(propertyDef.Name); break; case MgPropertyType.Double: value = reader.GetDouble(propertyDef.Name).ToString(); break; case MgPropertyType.Int16: value = reader.GetInt16(propertyDef.Name).ToString(); break; case MgPropertyType.Int32: value = reader.GetInt32(propertyDef.Name).ToString(); break; case MgPropertyType.Int64: value = reader.GetInt64(propertyDef.Name).ToString(); break; case MgPropertyType.Boolean: value = reader.GetBoolean(propertyDef.Name).ToString(); break; default: break; } ed.WriteMessage("\n" + propertyDef.Name + " = " + value); } break; case MgFeaturePropertyType.AssociationProperty: break; default: break; } } } } acadApp.UpdateScreen(); obj.Dispose(); } tr.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(ex.Message); tr.Abort(); } }
When we select some object on our map - program check - it is FDO object or not and show object properties. May be this code will help somebody.
@Anonymous and @Anonymous Hi all
Nice and cool code
I am not experienced Map3d
a question How do you set a value instead of extracting it for example is that possible
Change this
value=reader.GetString(propertyDef.Name)
With
case MgPropertyType.String:
value = reader.SetString(propertyDef.Name);
break;
Thanks all
Can't find what you're looking for? Ask the community or share your knowledge.