c# Microstation V8i "Operation not valid on complex element" error when trying to remove an element

Hi,

I've been all over to forums and the VBA Help File, but getting nowhere.

Working in a Microstation plugin, got a method to delete an element:

var element = GetLocalElementById(itemId); //<-- this works fine
if (element == null) return;

element.IsLocked = false;
ustn.ActiveModelReference.RemoveElement(element); // <-- this fails for a complex element

This works fine most of the time and has been working for years. A user has just sent over a .dgn file with a model where this Remove isn't working. The error is "Operation not valid on complex element" which seems to be because the element is a Cell. The element is Complex, a Cell and Droppable.

The VBA help file says to use .Drop() on the complex/droppable element and then try .RemoveElement but this isn't working either. I've tried every combination I can think of, including several from the help file and the forums and all of them blow up.

Any idea what's going on?

Parents
  • The itemId is fine, it breaks on the .RemoveElement line. 

    I figured it out. It's got nothing to do with the items it contains (being a complex element); rather it is the fact it has a parent. It needs to be removed from the list of elements in the parent, rather than deleted directly. With this change, it works.

  • How do you get itemId?
    The itemId is fine

    That's not the answer to my question.  When we ask a question in response to your post, we're seeking to elicit clues about non-performance.  Saying something 'is fine' when there's a problem neither helps nor encourages us to help further.

    the error message was completely unhelpful...

    The error message tells us that something is going on with a complex element.  But, since you didn't answer my question, we can't tell how you found an element and extracted its ID.  Next time, please answer our questions without ducking them, and consider attaching a DGN example of the kind of element that doesn't work with your code.

    I agree with Jan's response below: Elements ... can be members of complex elements.

     
    Regards, Jon Summers
    LA Solutions

  • What I was saying by 'fine' is that the itemId is a long encoded in a string and the Find function works fine for it and has for years. The exception, which I had never seen before, was in the .RemoveElement call.

    You seem to be saying the itemId is wrong because it's not the for the root element; in fact I need to delete the item whose itemId I'm given, but since in this case it isn't a root element, I need to remove it from the parent element.

    This confusion arises because we never make anything but root elements in our plugin - but a user had grouped some elements in Microstation itself, hence the problem.

  • Hi Michael,

    This confusion arises because we never make anything but root elements in our plugin - but a user had grouped some elements in Microstation itself, hence the problem.

    A margin note, just for a curiosity: When you will move to CONNECT Edition and you will stay with Interop (which allow simple migration, but is not good idea in long term perspective), be prepared for bigger challenges and surprises :-)

    More differences exist, some because of changes in VBA/COM API (so it exists in Interop too), some because of new MicroStation features:

    What was discussed several times (e.g. here), CE VBA API returns root element instead of the element itself. It's arguable whether it's enhancement or bug, but it require to check and test any code working with complex elements (but can be solved using Interop).

    Another change is a departure from concept of (potentially heavily nested) complex elements. So e.g. parametric solids, which is new CE element, that seems to be a complex element similar to SmartSolid or objects used in building products, is not complex, but structure in completely different way. I think there will be no impact on your application, but I think it's good to know new features may require different approach, different code and often also to migrate from limited VBA/COM API.

    With regards,

      Jan

  • itemId is a long

    An element ID is a MicroStation VBA DLong.  Bentley invented the DLong user defined type (UDL) to accommodate the 64-bit element ID because VBA in MicroStation V8i is not a 64-bit app.

    the itemId is a long encoded in a string

    Use method DLongToString to convert a DLong to a String.

    You seem to be saying the itemId is wrong

    Where do I write that the itemId is wrong?  I asked you to explain how you obtain it but you seem reluctant to answer that question.

     
    Regards, Jon Summers
    LA Solutions

Reply Children
No Data