AutoCAD Map 3D Developer

AutoCAD Map 3D Developer

21 Posts
0 Kudos
Registered: ‎05-16-2006
Post 1 of 4

Set FDO layer filter

429 Views, 3 Replies
07-13-2012 07:51 AM

Hi all,

   I need to set a fillter for all fdo layer in a map (as Query to filter data command).

To do this I wrote this code but the filter is not set. If I manualy run the "Query to filter data" command
If you launch the "Query to filter data" command manually  on a layer i find the filter set, and is applied only if I press the Ok or Apply button.I think that in my code is missing an instruction to apply the filter set.
Can you help me?

Dim objLayers As MgLayerCollection = objMap.GetLayers()
        For Each objLayer As MgLayerBase In objLayers
            Dim objLayerDefId As MgResourceIdentifier = objLayer.GetLayerDefinition()
            Dim objByteReader As MgByteReader = objResourceServ.GetResourceContent(objLayerDefId)
            Dim objXML As XmlDocument = New XmlDocument()
            Dim objSerializer As New XmlSerializer(GetType(LayerDefinition))
            Dim objLayerV As LayerDefinition = TryCast(objSerializer.Deserialize(New StringReader(objByteReader.ToString)), LayerDefinition)
            Dim objVectorLayerDef As VectorLayerDefinitionType = TryCast(objLayerV.Item, VectorLayerDefinitionType)
            objVectorLayerDef.Filter = strFilter
            Dim objXs As New XmlSerializer(objLayerV.[GetType]())
            Dim objWriter As New StringWriter()
            objXs.Serialize(objWriter, objLayerV)
            Dim objBuffer As Byte() = StringToBytes(objWriter.ToString())
            Dim objSource As New MgByteSource(objBuffer, objBuffer.Length)

            ' Update the resource in Map
            objResourceServ.SetResource(objLayer.LayerDefinition, objSource.GetReader(), Nothing)





Matteo Conte

ADN Support Specialist
294 Posts
17 Kudos
Registered: ‎12-23-2008
Post 2 of 4

Re: Set FDO layer filter

08-05-2012 08:15 PM in reply to: matteo.conte

Have you ever refered to the QueryAndLocate  and FeatureInspector sample in Map 3D SDK? 

Daniel Du
Developer Technical Services
Autodesk Developer Network

ADN Support Specialist
543 Posts
26 Kudos
Registered: ‎06-09-2009
Post 3 of 4

Re: Set FDO layer filter

08-05-2012 09:39 PM in reply to: matteo.conte

Matteo had contacted DevTech / ADN team on this issue and I worked with him to resolve this; It's now resolved.


Once you update the XML LayerDefinition, you need to update the resource in Map and then call the layer.ForceRefresh(). Here is the relevant code snippet :



// Update the resource in Map

ResourceService.SetResource(layer.LayerDefinition, source.GetReader(), null);


// layer.ForceRefresh();





Partha Sarkar
Developer Technical Services
Autodesk Developer Network

Active Contributor
41 Posts
0 Kudos
Registered: ‎12-08-2010
Post 4 of 4

Re: Set FDO layer filter

10-25-2012 06:13 AM in reply to: Partha.Sarkar

i am having same issue. but your solution is not working for me. could you please review my below code


//Select Entity on Map

Editor acEditor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;


SelectionSetss = GetAcSelectionSetSingleGeom(acEditor);


ObjectIdCollection objIdColle = newObjectIdCollection(ss.GetObjectIds());


MgSelectionBase mgSe = AcMapFeatureEntityService.GetSelection(ss);


AcMapMap acMap = AcMapMap.GetCurrentMap();


MgLayerCollectionmgLayColl = acMap.GetLayers();


MgLayerBase parcelsLayer1 = mgLayColl.GetItem("EL_ADMIN_AREA");


stringfcName1 = parcelsLayer1.GetFeatureClassName();


intII = mgSe.GetSelectedFeaturesCount(parcelsLayer1, fcName1);


MgFeatureReader featureReader = mgSe.GetSelectedFeatures(parcelsLayer1, parcelsLayer1.FeatureClassName, true);


MgFeatureService fs = AcMapServiceFactory.GetService(MgServiceType.FeatureService) asMgFeatureService;




stringgeometryName = parcelsLayer1.GetFeatureGeometryName();


MgByteReadergeometryData = featureReader.GetGeometry(geometryName);


MgAgfReaderWriter agfReaderWriter = newMgAgfReaderWriter();


MgGeometrygeometry = agfReaderWriter.Read(geometryData);


MgPolygon mgPoly = geometry asMgPolygon;


MgWktReaderWriter wktReaderWriter = newMgWktReaderWriter();


stringwkt = wktReaderWriter.Write(mgPoly);

wkt = wkt.Replace(


wkt = wkt.Replace(

",", " 0,");

wkt = wkt.Replace(

"))", " 0))");


string filter = "GEOM INSIDE GeomFromText('" + wkt + "')";


AcMapLayer layer1 = mgLayColl.GetItem("EL_CONDUCTOR") asAcMapLayer;


MgResourceIdentifierlayerDefId = layer1.GetLayerDefinition();


MgResourceService rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService) asMgResourceService;


MgByteReaderlayerReader = rs.GetResourceContent(layerDefId);


XmlDocument objXML = newXmlDocument();


XmlSerializer objSerializer = newXmlSerializer(typeof(LayerDefinition));


LayerDefinition objLayerDef = objSerializer.Deserialize(newStringReader(layerReader.ToString())) asLayerDefinition;


VectorLayerDefinitionType objVectorLayerDef = objLayerDef.Item asVectorLayerDefinitionType;

objVectorLayerDef.Filter = filter;

using (StringWriter objWriter = newStringWriter())


XmlSerializer objXs = newXmlSerializer(typeof(LayerDefinition));

objXs.Serialize(objWriter, objLayerDef);

byte[] unicodeBytes = Encoding.Unicode.GetBytes(objWriter.ToString());


byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, unicodeBytes);


MgByteSource objSource = newMgByteSource(utf8Bytes, utf8Bytes.Length);

rs.SetResource(layer1.LayerDefinition, objSource.GetReader(),








if (featureReader != null)



Post to the Community

Have questions about Autodesk products? Ask the community.

New Post