[ProStructures CE (local build from latest code from RE repo as of 12/27/2021), C#] Error attaching ECData to Element.

Hi, I am trying to attach data of an ECClass to an element and keep getting this exception:

Bentley.Exceptions.EnvironmentalException : CreateInstanceOnElement failed with error code: 6

This is NOT my first ECData class or schema. I have created 3 others (about 2 years ago) that work fine. 1 is ECXAttrubutes and it attached to the DgnFile, the other 2 are ECXData and are attached to Elements.

This new Schema  imports successfully, but throws the exception when I try to attach to an element as follows:

DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(ActiveDgnFile, baseDataClass);
if (instanceEnabler.SupportsCreateInstanceOnElement)
{
StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;
IDgnECInstance dgnInstance = instanceEnabler.CreateInstanceOnElement(callout, instance, false); // <<-- exception is thrown HERE
SetProperties(dgnInstance, bData);
dgnInstance.WriteChanges();
}

callout is a TextNodeElement

instance is the correct ECClass from my schema

I modeled this new Class and the import and attachment process after one of my existing ECXData classes and for whatever reason it is not working?

I have tried stripping my Class down to barebones and still I get this exception.

I feel like I'm missing something small but, unfortunately, "failed with error code: 6" is not very helpful?

Parents
  • Hi ,

    I am not certain if the OPM SDK provides this example however see if this code snip can help you start to get the desired results.

    MSCE SDK U16.2 Example: C:\Program Files\Bentley\MicroStationCONNECTSDK\examples\DgnEC\DgnECExample\WriteInstanceOnElementTool.cs+51

    /*---------------------------------------------------------------------------------**//**
    * Write instance on the element on first data button if it lies on some element.
    * @bsimethod                                                              Bentley Systems
    /*--------------+---------------+---------------+---------------+---------------+------*/
    protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
        //Locate an element.
        Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 1);
    
        //If an element is located write an instance on it.
        if(null != hitPath)
            {
            InstanceInfoToWrite instanceInfo = m_writeInstanceForm.GetSelectedInstanceInfoToWrite();
            FindInstancesScope scope = FindInstancesScope.CreateScope(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption());
            IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, instanceInfo.m_schemaName, 1, 0, SchemaMatchType.Exact);
    
            Bentley.DgnPlatformNET.Elements.Element element = hitPath.GetHeadElement();
    
            ECClass calss1 = schema.GetClass(instanceInfo.m_className) as ECClass;
            DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnFile(), calss1);
            StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;
            foreach(PropertyInfoToWrite pInfo in instanceInfo.m_properties)
            {
            SetInstancePropertyValue(ref instance, pInfo);
            }
            instanceEnabler.CreateInstanceOnElement(element, instance, false);
            }
    
        return true;
        }

    HTH,
    Bob



  • Uh, Nevermind. I found the problem... I knew it was something small/stupid I was overlooking!

    I am building the Schema programmatically and in my schema, I have a couple of "struct" classes defined like this:

    ECClass structClass1 = new ECClass("Struct1", true);  // true denotes that is is in fact a "IsStruct" class

    ECClass structClass2 = new ECClass("Struct2", true);

    then my Main Class is made up of the Struct classes (and some other stuff), kinda like this:

    ECClass mainClass = new ECClass(DataClassName, true);  // ROOT CAUSE of Error: This Class must NOT have the "true" IsStruct parameter
    mainClass.DisplayLabel = "Some Label Name";
    prop = new ECProperty("PropInfo1", structClass1);
    prop.DisplayLabel = "Struct1 Data";
    mainClass.Add(prop);
    prop = new ECProperty("PropInfo2", structClass2);
    prop.DisplayLabel = "Struct2 Data";
    mainClass
    .Add(prop);

    I had that IsStruct parameter in there, but should not have had it there for the Main Class. Darn, my bad! I wish these small/stupid mistakes wouldn't take me a day to figure out!

    Answer Verified By: Robert Hook 

Reply
  • Uh, Nevermind. I found the problem... I knew it was something small/stupid I was overlooking!

    I am building the Schema programmatically and in my schema, I have a couple of "struct" classes defined like this:

    ECClass structClass1 = new ECClass("Struct1", true);  // true denotes that is is in fact a "IsStruct" class

    ECClass structClass2 = new ECClass("Struct2", true);

    then my Main Class is made up of the Struct classes (and some other stuff), kinda like this:

    ECClass mainClass = new ECClass(DataClassName, true);  // ROOT CAUSE of Error: This Class must NOT have the "true" IsStruct parameter
    mainClass.DisplayLabel = "Some Label Name";
    prop = new ECProperty("PropInfo1", structClass1);
    prop.DisplayLabel = "Struct1 Data";
    mainClass.Add(prop);
    prop = new ECProperty("PropInfo2", structClass2);
    prop.DisplayLabel = "Struct2 Data";
    mainClass
    .Add(prop);

    I had that IsStruct parameter in there, but should not have had it there for the Main Class. Darn, my bad! I wish these small/stupid mistakes wouldn't take me a day to figure out!

    Answer Verified By: Robert Hook 

Children