[CONNECT UPDATE 15 C#] LineStyle Scale

Hello,

I have problem with setting scale for linestyle in c#. If anyone found working solution please share it.

So far I tried to use ElementPropertiesSetter SetLineStyle function because this function takes LineStyleParameters as param but i can't create LineStyleParameters because it does not have constructor.

I tried to get LineStyleParameters from ElementPropertiesGetter and I get some but I can't change Scale for that object, it stays unchanged after lineStyle.Scale = scale.

Regards.

Parents Reply Children
  • Hi Jan,

    I found some code in examples and I tried two ways to set LSScale for element but it doesn't work for me.

    In first attempt I tried DgnECInstanceEnabler but ObtainInscanceEnabler returns null for me.

    string schemaName = "BaseElementSchema";
    BNET.DgnEC.FindInstancesScope scope = BNET.DgnEC.FindInstancesScope.CreateScope(element, new BNET.DgnEC.FindInstancesScopeOption(DgnECHostType.Element, true));
    IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest);
    
    if (schema != null)
    {
        var class1 = schema.GetClass("LineStyleParametersStruct") as ECClass;
        DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(Session.Instance.GetActiveDgnFile(), class1);
        if (instanceEnabler != null)
        {
            StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;
            instance.MemoryBuffer.SetDoubleValue("LSScale", -1, Util.GetScale(dgnModel));
            instanceEnabler.CreateInstanceOnElement(element, instance, false);
        }
    }

    In second one I tried to use ECQuery to FindInstanced but it doesn't find any.

    List<IECClass> classes = new List<IECClass>();
    string schemaName = "BaseElementSchema";
    BNET.DgnEC.FindInstancesScope scope = BNET.DgnEC.FindInstancesScope.CreateScope(element, new BNET.DgnEC.FindInstancesScopeOption(DgnECHostType.Element, true));
    IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest);
    
    if (schema != null)
    {
        classes.Add(schema.GetClass("LineStyleParametersStruct"));
        ECQuery query = new ECQuery(classes);
        using (DgnECInstanceCollection ecInstances = DgnECManager.Manager.FindInstances(scope, query))
        {
            var instance = ecInstances.FirstOrDefault();
            if (instance != null)
            {
                instance.SetDouble("LSScale", Util.GetScale(dgnModel));
            }
        }
    }

    Any tip what I am doing wrong?

  • Any tip what I am doing wrong?

    I guess you are searching for wrong class name, but without some debugging, it's hard to say.

    But you choose, in my opinion, the most complicated way. To use ECQuery is powerful, but it requires to know exactly how ECQuery works (not very intuitive, because more graph than query) and also the structure of queried data. The problem is that hide complex logic, so when anything is wrong, it's usually not simple to find out what.

    Even when maybe less efficient, to use DgnECManager.GetElementProperties() is far simpler. Moreover, it allows easily understand, what EC classes and properties are available at specific element. Sometimes there is even no reason to rewrite code to ECQuery later, because it "just work".

    The only disadvantage of GetElementProperties (but it can be applied to all EC API) is a requirement to cast values to proper data type to access relevant methods. But it's common problem of OOP, when interfaces and inheritance is used. In the discussed case, everything is IECPropertyValue, but individual items can be containers like structures or arrays. But, fortunately, every IECPropertyValue knows what type it represents ;-)

    Regards,

      Jan

  • Using GetElementProperties microstation crash on "scaleParam.DoubleValue = scale" line.

    using (var ecInstances = DgnECManager.Manager.GetElementProperties(element, ECQueryProcessFlags.SearchAllClasses))
    {
        var instance = ecInstances.Where(x => x.ClassDefinition.Name == "LineElement").FirstOrDefault();
        if (instance != null)
        {
            var lsParams = instance.GetPropertyValue("LineStyleParams");
            if(lsParams != null)
            {
                var scale = Util.GetScale(dgnModel);
                var scaleParam = lsParams.ContainedValues.Where(x => x.Property.Name == "LSScale").FirstOrDefault();
                if(scaleParam != null)
                {
                    scaleParam.DoubleValue = scale;
                }
            }
        }
    }

  • Using GetElementProperties microstation crash on "scaleParam.DoubleValue = scale" line.

    And? You are developer and author of the code. What analysis you did? What exception is thrown?

    Maybe you should not overuse var keyword, that hides real types. Right now, I do not see any option how your code can be discussed, because it's not clear what LINQ returns.

    When it's something like IECPropertyValue, to set the scale should work.

    Regards,

      Jan

  • Another test, before anything is changed, should be to evaluate whether line style scale can be read and whether the value is correct (when compared with thew scale available in Element properties dialog.

    Regards,

      Jan