AutoCAD Map 3D Developer

AutoCAD Map 3D Developer

Reply
Member
vipwall
Posts: 3
Registered: ‎07-04-2011
Message 1 of 4 (662 Views)

FDO object properties through GetEntity

662 Views, 3 Replies
07-04-2011 05:28 AM

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 :smileysad: Please, help somebody.

 

P.S. Sorry for my English

ADN Support Specialist
Partha.Sarkar
Posts: 543
Registered: ‎06-09-2009
Message 2 of 4 (644 Views)

Re: FDO object properties through GetEntity

07-05-2011 10:08 AM in reply to: vipwall

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.

 


 



Partha Sarkar
Developer Technical Services
Autodesk Developer Network

Member
vipwall
Posts: 3
Registered: ‎07-04-2011
Message 3 of 4 (628 Views)

Re: FDO object properties through GetEntity

07-06-2011 11:13 PM in reply to: Partha.Sarkar

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.


ADN Support Specialist
Partha.Sarkar
Posts: 543
Registered: ‎06-09-2009
Message 4 of 4 (616 Views)

Re: FDO object properties through GetEntity

07-07-2011 09:21 PM in reply to: vipwall

I am glad to know that code snippet was useful to you :smileyhappy:

 

Cheers,



Partha Sarkar
Developer Technical Services
Autodesk Developer Network

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.