set layerdefinition to layer

set layerdefinition to layer

Anonymous
Not applicable
476 Views
1 Reply
Message 1 of 2

set layerdefinition to layer

Anonymous
Not applicable

Greetings! I faced to issue I can't set cutstom LayerDefinition to a layer. I have a code:

 Public Shared Sub AddLegendStyleOnLayer(ByVal legendpath As String, ByVal fileName As String)
        Using locked As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()
            Dim xmlFile As XmlDocument = New XmlDocument()
            xmlFile.Load("C:\Users\dea\AppData\Roaming\MgLocal\Repositories\Library\Content\Layers\DEA~DEA_GEOM_SPATIAL~GEOM.LayerDefinition")
            Dim unicodeBytes As Byte() = Encoding.Unicode.GetBytes(xmlFile.OuterXml)
            Dim utf8Bytes As Byte() = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, unicodeBytes)
            Dim layerByteSource As MgByteSource = New MgByteSource(utf8Bytes, utf8Bytes.Length)
            Dim currentMap As AcMapMap = AcMapMap.GetCurrentMap()
            Dim rIDlayer As New MgResourceIdentifier("Library://Layers/" & Path.GetFileNameWithoutExtension(legendpath) & ".LayerDefinition")
            Dim xmlDirectory As String = "C:\Users\dea\AppData\Roaming\MgLocal\Repositories\Library\Content\Data\"
            rs.SetResource(rIDlayer, layerBytesource.GetReader(), Nothing)

            Dim rIdFeatureSource As New MgResourceIdentifier("Library://Data/" & fileName & ".FeatureSource")
            Dim featureSourceByteSource As New MgByteSource(xmlDirectory & "" & fileName & ".FeatureSource")
            featureSourceByteSource.SetMimeType("text/xml")
            rs.SetResource(rIdFeatureSource, featureSourceByteSource.GetReader(), Nothing)

          
            Dim layer1 As AcMapLayer
            layer1 = GetLayerByName("FeatureClass1")
            layer1.SetLayerDefinition(rIDlayer, rs)
            Dim updatedLayerByteSource As MgByteSource = UpdateByteSource(legendpath)
            rs.SetResource(layer1.LayerDefinition, updatedLayerByteSource.GetReader(), Nothing)
            layer1.ForceRefresh()   'exception on this string
            MsgBox("Everything is fine!")
End Sub

And when I connect to data source (i.e SDF data source) and get layer (FeatureClass1) from LayerCollection by using GetLayerByName I get an exception: Null reference. The GWS layer source is invalid. 

Any suggestions?

 

0 Likes
477 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

//try

//C# code
MgLayerCollection layers = AcMapMap.GetCurrentMap().GetLayers();

MgLayerBase layer = layers.GetItem("FeatureClass1")

 

 

0 Likes