[Microstation V8i] (08.11.07.443) error when calculating an area of a shape with a small area.

I am calculating the area of a grouped hole with the help of this command ComputeGroupedHoleArea (at the bottom of the page).

It works very well but now I came across an grouped hole with an element in it which has a very small area.

area = area - oEnumerator.Current.AsClosedElement.Area

Throws an error in the Do While block)

. Well...

oEnumerator.Current.AsClosedElement.Area

does throw an error.

The .Area is where it goes wrong so the code is fine but the .Area itself is not.

After some investigation it turns out the area of the element was very small. The element was a triangle with the second point exactly between the other two.

When I used Microstation itself to calculate the area it said it could calculate it. 

When I used Microstation to calculate the area of the Grouped Hole it worked fine.

A way to let Microstation calculate the area outside VBA and return the value would be a solution?

Now I would like it very much if it would just return 0.

Try and Catch looked promising but that does not exist (yet) in VBA.

I read something about using On Error but It is a little bit out of my league (yet).

Does anyone has a solution for catching such an error and replace it with another value (0)?

A dgn with the grouped hole is in the attachment.

errorArea.dgn

Parents
  • it turns out the area of the element was very small

    I used the Analyze Element tool to examine one of your grouped holes.  That one contains a complex shape comprising four line strings having three coincident points, so they don't make a measurable shape.

    I would like it very much if it would just return 0.

    Try and Catch looked promising but that does not exist (yet) in VBA

    Remove 'yet' from your statement.  It's very unlikely that Microsoft will continue to develop VBA more than necessary to keep it viable.  Microsoft's direction is clear: use .NET.  Which is a pity, because VBA remains a great Rapid Application Development (RAD) tool

    I read something about using On Error but It is a little bit out of my league

    Something like this...

    ' ---------------------------------------------------------------------
    '   MeasureArea
    '   Measure a closed element's area, including an error trap for zero-
    '   area elements.
    '   Returns: Closed element area or zero on error.
    ' ---------------------------------------------------------------------
    Function MeasureArea(ByVal oElement As Element) As Double
      MeasureArea = 0#
      On Error GoTo err_MeasureArea
      If oElement.IsClosedElement Then
        MeasureArea = oElement.AsClosedElement.Area
      End If
      Exit Function
      
    err_MeasureArea:
      Debug.Print "Error " & Err.Description
    End Function

     
    Regards, Jon Summers
    LA Solutions

  • That is just catching an error. What I mean is catching it and continue to run the program and stop if an error occurs which i did not reconed with. What I read about try catch in VBA is saying to me it is very tricky programming and you have to know what you are doing: Do it as a professional programmer.

    If VBA stops I think I will make programs in open source packages instead. I would say I am doing RAD.

    What I would first try is to do with an error routine:

    On Error Resume Next
    If oElement.IsClosedElement Then
        MeasureArea = oElement.AsClosedElement.Area
        MeasureArea = MeasureArea
        On Error GoTo err_MeasureArea
    End If

    I do not know if the error-catching process is set right.

Reply
  • That is just catching an error. What I mean is catching it and continue to run the program and stop if an error occurs which i did not reconed with. What I read about try catch in VBA is saying to me it is very tricky programming and you have to know what you are doing: Do it as a professional programmer.

    If VBA stops I think I will make programs in open source packages instead. I would say I am doing RAD.

    What I would first try is to do with an error routine:

    On Error Resume Next
    If oElement.IsClosedElement Then
        MeasureArea = oElement.AsClosedElement.Area
        MeasureArea = MeasureArea
        On Error GoTo err_MeasureArea
    End If

    I do not know if the error-catching process is set right.

Children
  • Do it as a professional programmer

    A professional programmer wouldn't use VBA.  You have to work with what's available.

    Is there a way to let Microstation itself calculate it and return the value to VBA?

    The MicroStation measure area command calls the MDL function that calculates area.  VBA wraps the same MDL function. 

    The MicroStation command probably has some extra tests that take corrective action if they find a degenerate shape.  Besides your case of a zero-area triangle, for example, non-planar shapes are not tolerated.

    You could write VBA code to catch degenerate and non-planar shapes, but it's not the ideal language for that kind of analysis.  The error trap is simpler.

     
    Regards, Jon Summers
    LA Solutions

  • Hi Pieter,

    Do it as a professional programmer.

    As Jon wrote: Professional programmer will not use VBA for nothing else than simple evaluation of concepts using code snippets.

    What I read about try catch in VBA is saying to me it is very tricky programming

    It's not tricky at all, but that's the difference between real programmer and a user creating macro: The programmer knows exactly what he is doing, no matter what API he uses.

    What is the linked discussion about, is that error handling and exceptions catching is very basic and limiting in VBA, leading to more code than is necessarry ... but not tricky.

    What I mean is catching it and continue to run the program and stop if an error occurs which i did not reconed with.

    It's misunderstanding of some core concepts in my opinion. What you are talking about is robust code, when the priority is to survive unexpected situations (that can be caused by even the program itself, because it's e.g. buggy). But it's not how VBA (and moreover, MicroStation VBA API) is designed, because exceptions can happen somewhere inside, and VBA code is informed about this fact only.

    If VBA stops I think I will make programs in open source packages instead

    It sounds like a threatening ;-)

    When you find any tool that will server your need better, use it. It's core software development rule, even when ignored often (and many excuses used instead of it): Use the best tool you have to solve the problem.

    But, in my opinion, the whole discussion is useless, because:

    • As Jon mentioned, your elements are incorrect. It has been discussed many times, that MicroStation can "survive" not correct data often (but be aware MicroStation CE seems to be more strict), but not always, not all functions and (especially) not all APIs (VBA is the worse one).
    • When your VBA code fails (because of wrong element), but MicroStation evidently is fine and is able to calculate the area, it makes no sense to discuss what to do in VBA, but it's wise to move a responsibility for calculation to MicroStation. Which means to call MDL functions, the same as MicroStation itself (and all MicroStation tools) uses.
    Is there a way to let Microstation itself calculate it and return the value to VBA?

    Yes, call mdlMeasure_elmDscrArea C function, because it works fine.

    I found this discussion how to do it in MicroStation CE and it works also with your element. In V8i, the call is slightly different, so it's about to adapt the code. At the end, the code is much simpler, because the complete are calculation is just one function call.

    With regards,

      Jan